Repository navigation
Commit 3b2da5c
authored
Fixes from a standalone Lagerverk monorepo: migration race, required columns, dev migrations, wrangler reconciliation (#22)
* CLI and Vite plugin: early wrangler patch, unsafe-migration checks, per-database reset, rename answers, transform prefilter
- The plugin patches wrangler.jsonc in its `config` hook (patchWranglerConfig:
true), ahead of @cloudflare/vite-plugin's, which builds the emitted
wrangler.json from the config it reads there and restarts a dev server on a
later write. One build now ships the bindings it patched; the first dev start
no longer restarts mid-start. The change is logged (`durable-db updated
wrangler.jsonc (…)`); nothing is written while a database file does not
parse. syncWranglerConfig() is exported from durable-db/vite.
- transform skips modules that neither mention defineDatabase/destroyDatabase
nor import an `action` factory, so a syntax error elsewhere is not reported
as [plugin:durable-db].
- Dev epochs are per database: `db reset --database x` sets
state.json databases[x].epoch and rotates x alone; stubs call
applyDevEpoch(key, dbName). getTenantId() inside a global database's DO no
longer yields 'global' in dev.
- --database on push/generate/status/reset/validate; an unknown name throws
UnknownDatabaseError listing the valid ones (exit 1).
- Rename questions: --rename <table>.<old>=<new> / <old>=<new> and
--no-renames answer drizzle-kit's "created or renamed?" before it diffs
(renames applied to the previous snapshot, create + drop as two diffs);
without a TTY an unanswered one throws SchemaRenameRequiredError naming the
command and both answers. status reports "needs rename resolution".
- generate refuses a column added NOT NULL without a default
(UnsafeMigrationError, --force overrides) and warns about drops and columns
made NOT NULL; push/status show the warnings, validate reports them for the
not-yet-generated change. Every diff runs before any file is written.
- The plugin records the running dev server (dev-server.json, pid + token);
`reset --purge-local-storage` refuses while it is alive (--force).
* docs: wrangler sync timing and reconciliation, per-database reset, renames, unsafe migrations, dev/prod migration hand-off, testing actions
* Migrations: one run for concurrent first calls; dev migrations hand off to the generated production migration
ensureMigrations() memoises the run in flight instead of guarding it with a
boolean. runMigrations() reads the pending list and then awaits the PITR probe
and bookmark, so concurrent first calls on a fresh Durable Object (a new
tenant's first page, StrictMode's double effect) applied the same CREATE
TABLEs twice and one failed with MigrationSchemaConflictError. A failed run is
retried by the next call; resetMigrationState() drops a run in flight.
In dev, the statements applied by dev migrations (_dev_…) are journaled in
__dev_migration_statements. A pending production migration whose statements
those dev migrations already ran (db push, then db generate) is recorded as
applied without being executed, instead of failing with "duplicate column
name" on every request until db reset. Production storage never has the
journal. "duplicate column name" now maps to MigrationSchemaConflictError like
"already exists", naming the dev migrations involved and the
`db reset --database <name>` to run.
* Quote identifiers with backticks so a missing column fails instead of reading as a string
SQLite (workerd's build included) reads a double-quoted identifier that names
no column as a string literal: a column not yet migrated selected its own name
and a WHERE on it compared two constants. createKyselyFromSql compiles with a
SqliteQueryCompiler that quotes identifiers in backticks, so such a query
fails with "no such column". The parameter inliner skips backtick-quoted
identifiers too.
* Actions: optional args accept undefined; direct calls run against the test database in tests
An optional key (`'photoKey?': 'string'`) type-checks with `photoKey:
undefined` but validation rejected it. Action args are compiled with a `type`
whose validators drop undefined-valued keys of a plain-object args value
first; it is the `type` durable-db/registry exports to the action transform,
so the RPC stub and the Durable Object both use it, whether args is an object
or a type(...). The untransformed action() and the test harness use it too.
An action called directly outside the build (code under test) now runs against
the newest live test database createTestDatabase() created with its
database's tables; destroyDatabase() empties it. Without one, the error says
how to set it up and no longer points at a Vite config or lists ArkType
internals as the action's args.
* Admit Vite 8 in the vite peer range
The plugin is built and tested with Vite 8; installs on it reported an unmet peer dependency.
* Wrangler config: reconcile durable-db's Durable Objects and edit wrangler.jsonc in place
patchWranglerConfig() was add-only. It now also removes durable-db's own
bindings (<X>_DATABASE_DO -> <X>DatabaseDO) whose class is no longer a
discovered database (workerd refuses to start with a binding to a class the
worker does not export), writes a renamed_classes migration when a database's
class name changed unambiguously (an extension's mount changed), and never
deletes a removed database's class: it warns with the exact deleted_classes
migration to add. Bindings of other names or to other workers' classes are
left alone. checkWranglerConfig() reports stale bindings and classes.
PatchResult keeps its fields and adds addedBindings, removedBindings,
newClasses, renamedClasses and deletedClasses.
Edits are made in place on the JSONC text (parsed with jsonc-parser, a new
dependency): comments, layout, line endings and the trailing newline are kept,
one-line arrays stay on one line, and no wrangler.jsonc.backup is written.
* Changeset: runtime and wrangler config fixes
* Vite plugin: the transform pre-check admits every import the parser takes an action factory from
- ACTION_IMPORT required `{` right after `import`, so an action file that
also took the database module's default export
(`import main, { action } from '../databases/main'`) was never parsed:
its actions stayed unregistered and the worker ran the untransformed
action(), which throws. The gap between `import` and `{` now admits any
clause text (a default binding, `type`, comments, line breaks) and
excludes only what cannot occur in it (a quote, `;`, `{`, `}`).
Tests: the default-binding, no-space, multi-line, comment and string
specifier forms reach the parser; the default-binding action file is
transformed into stubs.
- syncWranglerConfig's unit tests move to tests/vite/modules/syncWrangler.test.ts
(tests mirror src/), plus prefixed sources and discoverDatabaseInfos;
durableDb.test.ts keeps the config-hook tests and checks the re-export.
- The hot-channel watcher test rewrites state.json until a reload is seen:
macOS starts fs.watch's FSEvents stream on another thread, so a write
right after configureServer could precede it under a loaded test run.
* Wrangler config: a renamed database's migrations move with its data; renames are no longer guessed
A renamed_classes migration hands the new class the old storage, with its
__migrations journal, while the build embedded the migrations of the new
name's directory: after a mount change, `db generate` wrote a fresh init there
and the deploy failed every request with "table already exists". The rename
now moves the old database's migrations directory (the sibling whose name
gives the stale class, exactly) to the new name. When it cannot - the new
directory already has migrations, or several directories belong to the old
class - neither a rename nor a new class is written, PatchResult.conflicts
lists it, a warning gives the two ways out (rm -r + mv to keep the data, or
the deleted_classes migration), and the plugin throws until one is done.
Migrations are loaded after the config is reconciled, so the moved ones are
embedded. The log no longer claims dev data is kept: miniflare keys local
storage by class name.
Pairing is limited to a database of a mounted source whose file stem the
stale class ends with (a mount change, or a database moved into a mount).
catalog.ts -> product-catalog.ts, and a database moved out of a mount (which
reads the same as an app replacing product-catalog.ts with catalog.ts), are
never paired.
checkWranglerConfig() no longer reports a rename target as a missing class
too, suggests one migration with the next free tag, gives the mv for a
rename's migrations, and reports renamedClasses, migrationsMoves and
conflicts. A failed production build names what is missing and what is stale
(describeWranglerCheck()).
* Action args: optional keys accept undefined through ArkType's own option
Replace the pipe that dropped undefined-valued keys before validation with an
ArkType scope that sets exactOptionalPropertyTypes: false (jitless, like the
shared one). A required key whose type admits undefined ('string | undefined')
stays valid when passed as undefined, as it was in 0.3.0; optional keys of
nested objects accept undefined too; and the args validator is a plain object
Type again, so .props and .toJsonSchema() work (undefined becomes { not: {} }
in JSON Schema). The type exported from durable-db is unchanged.
* Migrations: a second db push no longer breaks the dev database; hints name the database
db push diffs from the production snapshot, so after add a column, restart,
add another, the new squashed dev migration repeats the first one's ALTERs
and failed with "duplicate column name" until db reset. A pending dev
migration now skips the statements of an earlier dev migration (from the dev
statement journal) that it contains whole. One that redoes an earlier change
in another form still runs, and its MigrationSchemaConflictError says so.
The generated Durable Object sets databaseName, so the db reset hint names the
database the db commands know (orderItems, not order-items).
* Docs and changeset: migrations move with a rename, args scope, repeated dev pushes1 parent 7f8b595 commit 3b2da5c
55 files changed
Lines changed: 6451 additions & 430 deletions
File tree
- .changeset
- src
- cli
- context
- db
- migrations
- testing
- vite
- modules
- tests
- cli
- context
- db
- migrations
- testing
- vite
- modules
- workers
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
0 commit comments