Migrating to Karni 0.6.0¶
Every change below is recorded in Karni's machine-readable changelog
(packages/karni/karni/changelog/0.6.0.toml). karni upgrade applies the two
codemods (the frontend's npm floor and app.recipes) and lists the rest with
these notes.
Upgrading¶
pipx upgrade karni-cli # the CLI carries the changelog
karni upgrade --dry-run # shows the diff and the notes below, writes nothing
karni upgrade # applies the codemods on a clean git tree
# then pin karni to 0.6.0 in pyproject.toml, reinstall, and run:
karni rules sync # rewrite the agent files
karni check
Changed in 0.6.0¶
The frontend installs with plain npm install¶
A new project's frontend/package-lock.json pins every package but
@karni/ui, so npm install works on npm 10.8 or later, the npm that Node
20.19 and Node 22 bundle, as well as on npm 11. frontend/package.json asks
for npm 10.8 in engines instead of npm 11. Without a lock, npm before 11.6
resolves Vite 8's optional peers and can crash with Cannot read properties
of null (reading 'edgesOut')
(why).
An earlier project asks for npm 11 in engines, and with engine-strict in
frontend/.npmrc npm 10 stops with EBADENGINE. karni upgrade changes
"npm": ">=11" to "npm": ">=10.8" in frontend/package.json. A project
that has frontend/package-lock.json then installs with plain npm install
on npm 10. A project without one runs its first install once with npm 11.6 or
later, (cd frontend && npx --yes npm@11 install), and commits the lock it
writes.
A written row must stay inside the caller's write scope¶
This is a security fix. A create or an update is now saved and then checked
against its own access predicate's rows, in one transaction. A row the
caller could not change afterwards is refused with a 403
(E134) and nothing is saved. Before 0.6.0,
write=Owned("seller__user") let anyone signed in post a listing into
someone else's shop, because Owned checks a traversal only on rows that
exist, and a lesson could go into another instructor's course the same way.
Nothing changes for a viewset whose write predicate admits every row of the
caller (HasRole, Authenticated, Public, an owner field Karni stamps).
What to do: run the project's tests. A test that wrote a row outside the
caller's scope on purpose now gets a 403; give that caller a role the
predicate admits, or override check_written_row() on the viewset where the
rule must differ (API: access control).
Karni stamps only an owner field that is one foreign key to the user. A model
whose owner_field is a to-many field (a workspace's members) or a path
through another model gets no stamp, so a create that does not put the caller
in it makes a row the caller cannot reach, and is now refused. Make the
creator part of the row in the viewset, in the same transaction as the check:
from django.db import transaction
class WorkspaceViewSet(BaseViewSet):
model = Workspace
access = Granted()
def perform_create(self, serializer):
with transaction.atomic():
workspace = serializer.save()
workspace.members.add(self.request.user)
self.check_written_row(workspace)
An update that takes the caller out of such a field is refused the same way.
An assert_crud test whose bodies list other users there sends the caller
too: self.assert_crud(WorkspaceViewSet, "member", payload={"members":
[str(self.user_as("member").pk)]}).
Rules a karni.yaml can say, and the recipes that use them¶
bounds, unique, targets, frozen, delete, moves and actions on
an entity, delete and assign in its access, and the read word
where:<field> are new (rules, experimental). A project
without them is unchanged.
The booking, CRM, LMS, marketplace and data-portal recipes declare their business rules with them now (each recipe page lists them under Rules). A project built from one of these recipes before 0.6.0 keeps its old rules until it takes the new ones:
- Copy the rule keys of the recipe's entities from the new
karni.yaml(karni recipes show <name>) into the project'skarni.yaml. The CRM addslost_reasonandclosed_atto deals anddone_atto activities, and the marketplace addspaid_atand the statevoidto payouts: copy those fields too. - Check the data first: a new
uniqueentry is a risky change inkarni plan, because rows that already share the values stop the migration. Find them (two live bookings of one member for one slot, two shops of one person, two indicators with one label), cancel or delete the extra ones, then runkarni build --allow-destructive. - The recipe's own files changed too (
<app>/viewsets.py,<app>/seeds.pyand the tests).karni buildwrites a recipe's files only over its own stubs, so compare yours withkarni recipes show <name> --filesand take the parts you want: pay that returns the open payment, cancel and refund that refund, the seeders that keep the rules. - Run the project's tests and
karni check.
karni upgrade lists both changes. Neither is applied by a codemod: who may
write and which rules a project keeps are the project's decisions.
A project lists its recipes in app.recipes¶
A project may hold several recipes now (several recipes in one
project), so karni.yaml lists them in app.recipes, a list.
app.recipe, one name, is still read as a list of one, with a
KarniDeprecationWarning, until 1.0. karni upgrade rewrites it:
The booking and CRM recipes follow the family rules. Their roles are
admin, customer and <prefix>-<role> (bookings-staff, crm-manager,
crm-rep), their money fields read app.currency, and their pages live
under the app's route (/bookings/resources, /crm/deals). A project made
from one before 0.6.0 keeps what it has: nothing changes until you copy the
new keys from karni recipes show <name>. To add another recipe to such a
project, first rename its roles the same way (in roles, in every
role:<slug> of access, and in the tests that sign in with one), then run
karni recipes add <name>.
The emails a business edits move to notifications (experimental)¶
The editor's table and /api/email-templates/ moved from karni-commerce to
notifications (karni.notify), so every part's emails are
edited in one place. The API keeps its shape, so @karni/ui's
EmailTemplateEditor works as before. For a project with commerce:
- Install the contract:
features: {notify: true}in karni.yaml (the shop'srequiresswitches it on), orINSTALLED_APPS += ["karni.notify"]. - Run
migrate. karni-commerce's migration 0008 copies each edited email tonotify.EmailTemplatewith its dates and authors, then drops commerce's table. With edits and withoutkarni.notifyit stops (E446) and drops nothing. - Give whoever edits the emails
notify.emailtemplate(the shop'sshop-managerhas it).karni_commerce.emailtemplategrants no longer name a model. - Remove
KARNI_MAIL_SOURCE = "karni_commerce.mail.MailSource"from the settings if you set it. The default,karni.notify.source.NotifySource, is the same source, andMailSourceis an alias of it. - Code that imported
karni_commerce.models.EmailTemplateorkarni_commerce.email_templatesimportskarni.notify.models.EmailTemplateandkarni.notify.editor.
While karni.notify is installed, karni.mail.send_templated(..., kind=...)
also honours each person's own switches and unsubscribes, and a move's email
(moves.<state>.notify) goes to the recipient's inbox too
(KARNI_NOTIFY_MOVE_CHANNELS = ["email"] keeps it to the email).
The test payment provider has a checkout page while DEBUG is on¶
With KARNI_PAYMENTS_PROVIDER = "test" and DEBUG on, a checkout now sends
the person to /payments/test-checkout/<session>/ on the request's host,
where they pay or decline, instead of the imaginary
https://checkout.test/<session> address. KARNI_PAYMENTS_TEST_CHECKOUT_PAGE
chooses (None, the default, follows DEBUG). With DEBUG off, as in
pytest, the address stays the imaginary one.
A browser test that stubbed https://checkout.test/** now lands on the page:
wait for its "Test payment" heading and read the session from the path, as
EventHub's e2e/eventhub.spec.ts does, then click Pay or post the signed
webhook as before. Or set KARNI_PAYMENTS_TEST_CHECKOUT_PAGE = False to keep
the imaginary address.
Seeded menus follow their seed¶
A karni-site menu that {% get_menu "header" seed="website/menus/header.html" %}
made now follows that template, as Everything the client edits
said it did. On the next render after a change to the seed's links (a new
link, a new text, a page that was renamed), each item the seed made and
nobody changed takes the new link, an item whose link left the seed goes,
and a new link becomes an item. Before, the seed made the menu once, so a
renamed view left an item that pointed nowhere (SITE007).
An item someone changed in the admin, added or deleted stays as it is. A menu seeded before 0.6.0 follows too: an item nobody saved since the seed made it follows, and the others are kept. A menu made in the admin, without a seed, never follows one.
What to do: run karni migrate (karni-site's migration 0002 adds
Menu.seeded and MenuItem.seed_key). To keep a seeded menu as it is,
whatever its seed says, set KARNI_SITE_MENU_FOLLOW_SEED = False in
config/settings.py.
A site's defaults are stored in the deploy, and leave no history¶
A visitor's request used to be the first render of a page, and it stored
each new block, a menu's seed and the site's details, each with an entry in
the change history (33 blocks on a page were 33 rows and 33 entries).
Storing a template's default is nobody's edit, so it now leaves no entry,
on a visit as in a deploy. An edit in the admin, the API or a shell is
recorded as before. manage.py karni_site_seed renders every page once,
so no visitor's request has to store anything
(the command).
What to do: a new project's deploy files run it. In an earlier project, run
karni make deploy compose --force (the release step then runs it), or add
python manage.py karni_site_seed after migrate and collectstatic in
your own deploy.
A closed site says noindex on every page¶
While search engines may not index a karni-site site
(KARNI_SITE_ALLOW_INDEXING = False, or the default outside
DJANGO_ENV=prod), only robots.txt said so, and a proxy can rewrite that
file (a CDN may put its own Allow: / in front). Every response Django
sends now carries X-Robots-Tag: noindex, nofollow, through the redirect
middleware the site already has, and {% karni_seo %} writes <meta
name="robots" content="noindex, nofollow">. A production site with
indexing allowed sends neither. karni check --seo checks the pages as an
open site serves them.
What to do: nothing for a site in production. To keep robots.txt the only
signal, set KARNI_SITE_ROBOTS_TAG = "". A test that compares a whole page
in development now sees the meta tag: set KARNI_SITE_ALLOW_INDEXING = True
in the test settings, or compare less.
Static files under fingerprinted names¶
KARNI_STATIC_STORAGE chooses the static files' storage. plain, the
default, keeps the names as written: an earlier project is unchanged.
manifest gives each file a name that carries a hash of its content, which
{% static %} names and a browser may keep for a year. A new site
project's .env.prod.example sets it, and the Caddyfile karni make deploy
compose writes caches those names for a year.
What to do, to opt in: set KARNI_STATIC_STORAGE=manifest in .env.prod,
run karni make deploy compose --force for the Caddyfile's cache rule (or
add it to your own), and run karni check --seo, which reports a page
linking a static file that does not exist (SITE017). Such a file gets its
plain name and a warning in the log, and a stylesheet's url(...) to a
missing file is kept as written. Leave it plain when a template builds a
static file's address without {% static %} and relies on the browser
fetching the new version after a release.