@lunora/cli 1.0.0-alpha.38 → 1.0.0-alpha.381
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +33 -0
- package/README.md +1 -1
- package/dist/bin.mjs +2 -10
- package/dist/index.d.mts +1011 -374
- package/dist/index.d.ts +1011 -374
- package/dist/index.mjs +1 -19
- package/dist/packem_chunks/dispatch.mjs +1 -0
- package/dist/packem_chunks/handler.mjs +1 -160
- package/dist/packem_chunks/handler10.mjs +1 -16
- package/dist/packem_chunks/handler11.mjs +1 -22
- package/dist/packem_chunks/handler12.mjs +1 -192
- package/dist/packem_chunks/handler13.mjs +3 -131
- package/dist/packem_chunks/handler14.mjs +1 -65
- package/dist/packem_chunks/handler15.mjs +1 -58
- package/dist/packem_chunks/handler16.mjs +1 -80
- package/dist/packem_chunks/handler17.mjs +7 -43
- package/dist/packem_chunks/handler18.mjs +1 -105
- package/dist/packem_chunks/handler19.mjs +3 -170
- package/dist/packem_chunks/handler2.mjs +1 -114
- package/dist/packem_chunks/handler20.mjs +3 -94
- package/dist/packem_chunks/handler21.mjs +1 -94
- package/dist/packem_chunks/handler22.mjs +2 -0
- package/dist/packem_chunks/handler23.mjs +1 -0
- package/dist/packem_chunks/handler24.mjs +99 -0
- package/dist/packem_chunks/handler25.mjs +10 -0
- package/dist/packem_chunks/handler26.mjs +1 -0
- package/dist/packem_chunks/handler3.mjs +1 -204
- package/dist/packem_chunks/handler4.mjs +3 -33
- package/dist/packem_chunks/handler5.mjs +1 -49
- package/dist/packem_chunks/handler6.mjs +1 -91
- package/dist/packem_chunks/handler7.mjs +1 -42
- package/dist/packem_chunks/handler8.mjs +2 -174
- package/dist/packem_chunks/handler9.mjs +1 -315
- package/dist/packem_chunks/runCodegenCommand.mjs +4 -52
- package/dist/packem_chunks/runDeployCommand.mjs +11 -594
- package/dist/packem_chunks/runDevCommand.mjs +2 -0
- package/dist/packem_chunks/runInitCommand.mjs +164 -1432
- package/dist/packem_chunks/runResetCommand.mjs +1 -41
- package/dist/packem_chunks/runRpcCommand.mjs +1 -68
- package/dist/packem_shared/COMMANDS-Ba7ybcYb.mjs +1 -0
- package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-CyIsI3S1.mjs +1 -0
- package/dist/packem_shared/EXIT_CODE-D_XCixv8.mjs +1 -0
- package/dist/packem_shared/admin-token-VdUnvnKW.mjs +1 -0
- package/dist/packem_shared/admin-url-DJB8P6Jh.mjs +1 -0
- package/dist/packem_shared/binding-manifest-file-CQ1eQYy1.mjs +2 -0
- package/dist/packem_shared/buildRegistryIndex-Dc8D8AR6.mjs +1 -0
- package/dist/packem_shared/catalog-CahzmDLV.mjs +1 -0
- package/dist/packem_shared/cli-B5fWPcrA.mjs +2 -0
- package/dist/packem_shared/cli-manifest-CRet0egA.mjs +1 -0
- package/dist/packem_shared/cloudflare-credentials-BVmZnDZk.mjs +1 -0
- package/dist/packem_shared/codegen-error-AmH54ofi.mjs +3 -0
- package/dist/packem_shared/command-CAXl2A1S.mjs +1 -0
- package/dist/packem_shared/commands-DgUDxkA8.mjs +13 -0
- package/dist/packem_shared/createLogger-Cl18I8AX.mjs +2 -0
- package/dist/packem_shared/createRecordingSpawner-Ch7zuQQE.mjs +1 -0
- package/dist/packem_shared/deploy-target-DCWwiuAe.mjs +1 -0
- package/dist/packem_shared/diffSnapshots-WWwx-KvZ.mjs +5 -0
- package/dist/packem_shared/docker-AqLAkOyY.mjs +1 -0
- package/dist/packem_shared/health-probe-Bw9JFfIy.mjs +1 -0
- package/dist/packem_shared/import-BRM1hs7j.mjs +12 -0
- package/dist/packem_shared/index-oHZBGBwB.mjs +1 -0
- package/dist/packem_shared/insertSchemaExtension-DuiV6cba.mjs +8 -0
- package/dist/packem_shared/lint-ignore-report-DKZagpqk.mjs +2 -0
- package/dist/packem_shared/open-url-EnKy--w-.mjs +1 -0
- package/dist/packem_shared/output-format-B4eQHi5y.mjs +2 -0
- package/dist/packem_shared/parseManifest-CwPTKdtS.mjs +1 -0
- package/dist/packem_shared/path-containment-CgxYZggb.mjs +1 -0
- package/dist/packem_shared/plan-Dl57T11P.mjs +5 -0
- package/dist/packem_shared/planDevCommand-D8sm56hO.mjs +1 -0
- package/dist/packem_shared/platform-diagnostics-Cn2g6Jh-.mjs +4 -0
- package/dist/packem_shared/prompt-cancelled-BvsNxg_Q.mjs +1 -0
- package/dist/packem_shared/render-lunora-error-C3OJY5wk.mjs +2 -0
- package/dist/packem_shared/resolve-BrsV4Qyc.mjs +5 -0
- package/dist/packem_shared/resolve-target-C_ZloTvd.mjs +1 -0
- package/dist/packem_shared/runAddCommand-DTvfp4h2.mjs +1 -0
- package/dist/packem_shared/runExportCommand-slAq5BCF.mjs +5 -0
- package/dist/packem_shared/runMigrateGenerateCommand-Ii_WMZEA.mjs +11 -0
- package/dist/packem_shared/schemaIrToSnapshot-BjK-0IRo.mjs +1 -0
- package/dist/packem_shared/secret-key-6bCftsxj.mjs +1 -0
- package/dist/packem_shared/shared-D-zCOmgY.mjs +1 -0
- package/dist/packem_shared/storage-DWhTX8eS.mjs +1 -0
- package/dist/packem_shared/tui-prompts-B3YwUhGw.mjs +4 -0
- package/dist/packem_shared/variables-CPYyJtsL.mjs +1 -0
- package/dist/packem_shared/wrangler-name-Dsk5K1f-.mjs +1 -0
- package/dist/packem_shared/wrangler-secrets-3chIVv3H.mjs +1 -0
- package/package.json +39 -19
- package/skills/README.md +35 -17
- package/skills/lunora/SKILL.md +154 -64
- package/skills/lunora-create-package/SKILL.md +97 -84
- package/skills/lunora-deploy/SKILL.md +175 -105
- package/skills/lunora-functions/SKILL.md +362 -56
- package/skills/lunora-migration-helper/SKILL.md +162 -137
- package/skills/lunora-performance-audit/SKILL.md +137 -95
- package/skills/lunora-quickstart/SKILL.md +137 -136
- package/skills/lunora-realtime/SKILL.md +140 -99
- package/skills/lunora-setup-auth/SKILL.md +162 -102
- package/skills/lunora-setup-hyperdrive/SKILL.md +114 -97
- package/skills/lunora-setup-hyperdrive-global/SKILL.md +93 -90
- package/skills/lunora-setup-mail/SKILL.md +85 -113
- package/skills/lunora-setup-scheduler/SKILL.md +132 -96
- package/skills/lunora-setup-storage/SKILL.md +189 -95
- package/dist/packem_chunks/planDevCommand.mjs +0 -541
- package/dist/packem_chunks/runMigrateGenerateCommand.mjs +0 -397
- package/dist/packem_shared/COMMANDS-B0ftFD_3.mjs +0 -948
- package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +0 -244
- package/dist/packem_shared/admin-url-4UzT-CI4.mjs +0 -19
- package/dist/packem_shared/api-spec-CtA6ilu4.mjs +0 -13
- package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +0 -38
- package/dist/packem_shared/codegen-error-DJG-ghs_.mjs +0 -31
- package/dist/packem_shared/command-D3lB_4Az.mjs +0 -19
- package/dist/packem_shared/commands-B-gR09Z_.mjs +0 -845
- package/dist/packem_shared/createLogger-B40gPzQo.mjs +0 -78
- package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +0 -43
- package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +0 -61
- package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +0 -161
- package/dist/packem_shared/docker-hMQ97KSQ.mjs +0 -21
- package/dist/packem_shared/insertSchemaExtension-DAqbfr9Z.mjs +0 -64
- package/dist/packem_shared/open-url-Dfq6fAyT.mjs +0 -41
- package/dist/packem_shared/output-format-wUvAN6AL.mjs +0 -17
- package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +0 -56
- package/dist/packem_shared/parseManifest--vZf2FY1.mjs +0 -94
- package/dist/packem_shared/prompt-cancelled-APzX1Im-.mjs +0 -9
- package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +0 -16
- package/dist/packem_shared/runAddCommand-bnY6-HKb.mjs +0 -4
- package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +0 -79
- package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +0 -43
- package/dist/packem_shared/storage-BIsph-Vk.mjs +0 -84
- package/dist/packem_shared/tui-prompts-BjEN8XgP.mjs +0 -658
- package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +0 -12
- package/dist/packem_shared/wrangler-secrets-P2_ZUR-k.mjs +0 -47
|
@@ -1,194 +1,219 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: lunora-migration-helper
|
|
3
|
-
description: Plans Lunora schema and data migrations with widen-migrate-
|
|
4
|
-
breaking schema changes, backfills, table reshaping, online data migrations
|
|
5
|
-
(`defineMigration` + `lunora migrate up`), the `.global()` D1 SQL flow, and the
|
|
6
|
-
pre-deploy schema-drift gate.
|
|
3
|
+
description: Plans and runs Lunora schema and data migrations with the widen → migrate → narrow pattern. Covers online data migrations (`defineMigration` in `lunora/migrations.ts`, `lunora migrate create | up | down | status`), what the pre-deploy schema-drift gate treats as breaking and how a migration covers it, `.global()` table DDL on D1 (`lunora migrate generate`) and Hyperdrive, moving a dataset with `lunora migrate d1-to-hyperdrive`, and re-keying a `.shardBy()` table. Use when the user wants to make a field required, rename or drop a field or table, change a field's type, add a unique index to a populated table, backfill data, switch a table between root / `.shardBy()` / `.global()`, or when `lunora deploy` reports "schema drift gate blocked".
|
|
7
4
|
---
|
|
8
5
|
|
|
9
6
|
# Lunora Migration Helper
|
|
10
7
|
|
|
11
|
-
|
|
8
|
+
Change a Lunora schema that already has data in it without breaking the deploy
|
|
9
|
+
or losing rows.
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
Not needed for a greenfield schema with no data, a new optional field, a new
|
|
12
|
+
table, or a new non-unique index. Those are additive and deploy as-is.
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
- Changing field types or structure.
|
|
17
|
-
- Splitting/merging tables, renaming/removing fields.
|
|
18
|
-
- Reshaping `.global()` (D1-backed) tables.
|
|
14
|
+
## Which storage layer are you migrating?
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
Check the table's modifier in `lunora/schema.ts` first; the tools differ.
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
-
|
|
18
|
+
| Table | Where rows live | Structural DDL | Data backfill |
|
|
19
|
+
| ------------------------------------ | ------------------------------------ | -------------------------------------------------------------- | -------------------------------------- |
|
|
20
|
+
| default (root) or `.shardBy(key)` | SQLite in the root / per-key ShardDO | Applied by the runtime from the schema | `defineMigration` + `lunora migrate` |
|
|
21
|
+
| `.global()` | D1 (`DB` binding) | Additive DDL auto-provisioned; destructive SQL applied by hand | SQL `UPDATE` via `wrangler d1 execute` |
|
|
22
|
+
| `.global({ backend: "hyperdrive" })` | Postgres/MySQL via Hyperdrive | Additive DDL auto-provisioned; no `migrate generate` support | SQL `UPDATE` against the database |
|
|
25
23
|
|
|
26
|
-
##
|
|
24
|
+
## The pattern: widen, migrate, narrow
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
The schema-drift gate blocks a breaking change; one that needs a backfill is
|
|
27
|
+
let through once a new migration on that table covers it (the rest are listed
|
|
28
|
+
below). Stage every such change across deploys:
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
1. **Widen.** Make the schema accept both shapes: add the new field as
|
|
31
|
+
`v.optional(...)`, keep the old one. Make reads handle both, and start
|
|
32
|
+
writing the new shape on every insert/patch so rows created during the
|
|
33
|
+
migration aren't missed. Deploy.
|
|
34
|
+
2. **Migrate.** Backfill existing rows with a `defineMigration`, run it with
|
|
35
|
+
`lunora migrate up`, and confirm with `lunora migrate status`. On a
|
|
36
|
+
`.global()` table, backfill with SQL instead (see below).
|
|
37
|
+
3. **Narrow.** Make the field required / drop the old one, remove the
|
|
38
|
+
both-shapes code. Deploy.
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
Prefer adding a new field to changing an existing field's type: it rolls back
|
|
41
|
+
cleanly. Keep a deprecated field as `v.optional` with a `// deprecated:` comment
|
|
42
|
+
until nothing reads it.
|
|
41
43
|
|
|
42
|
-
##
|
|
44
|
+
## What the drift gate considers breaking
|
|
43
45
|
|
|
44
|
-
The
|
|
45
|
-
|
|
46
|
+
The gate (run by `lunora deploy`, `prepare`, `build` and, read-only, `verify`)
|
|
47
|
+
diffs the schema against the committed `lunora/.lunora-schema.json`.
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
- **Safe:** new table, new optional field, new non-unique index or relation, a
|
|
50
|
+
required field made optional.
|
|
51
|
+
- **Breaking, fixable by a migration:** dropped field or table, changed field
|
|
52
|
+
type, optional → required, new required field on an existing table, a new
|
|
53
|
+
unique index or `.unique()` column (existing duplicates would make the shard
|
|
54
|
+
fail to open).
|
|
55
|
+
- **Breaking, not fixable by a migration:** changing a table's shard mode
|
|
56
|
+
(root ↔ `.shardBy()` ↔ `.global()`, or the `.global()` backend) moves rows to
|
|
57
|
+
other storage; use export → import. A dropped index or relation needs call
|
|
58
|
+
sites changed, then `--update-schema-baseline`.
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
A migration covers a change only if it is new since the baseline and its
|
|
61
|
+
`table` is a string literal naming the affected table. The baseline advances
|
|
62
|
+
only after a successful deploy.
|
|
58
63
|
|
|
59
|
-
|
|
60
|
-
one — safer transition, easier rollback.
|
|
64
|
+
## Online data migrations
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
A migration transforms one document at a time, runs inside each shard's Durable
|
|
67
|
+
Object in keyset batches, and is resumable: per-shard progress is kept in a
|
|
68
|
+
reserved `__lunora_migrations` table.
|
|
63
69
|
|
|
64
|
-
|
|
65
|
-
comment explaining why it existed. Delete only once you are sure nothing reads
|
|
66
|
-
it.
|
|
70
|
+
Scaffold it rather than writing the file from scratch:
|
|
67
71
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
// Adding an optional field — safe.
|
|
72
|
-
users: defineTable({
|
|
73
|
-
name: v.string(),
|
|
74
|
-
bio: v.optional(v.string()),
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Adding a new table — safe.
|
|
78
|
-
posts: defineTable({ userId: v.id("users"), title: v.string() }).index("by_user", ["userId"]);
|
|
79
|
-
|
|
80
|
-
// Adding an index — safe.
|
|
81
|
-
users: defineTable({ name: v.string(), email: v.string() }).index("by_email", ["email"]);
|
|
72
|
+
```bash
|
|
73
|
+
lunora migrate create backfill-display-name --table users
|
|
82
74
|
```
|
|
83
75
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
`@lunora/server`. It transforms one document at a time, runs **inside each
|
|
88
|
-
shard's** Durable Object in keyset batches, and is **resumable** — per-shard
|
|
89
|
-
progress is tracked in a reserved `__lunora_migrations` table, so an interrupted
|
|
90
|
-
run resumes where it stopped. Codegen discovers declarations and emits them into
|
|
91
|
-
the registry the DO and CLI look up by `id`.
|
|
76
|
+
This appends to `lunora/migrations.ts`. Codegen only discovers
|
|
77
|
+
`export const x = defineMigration({...})` with a literal `id`. An
|
|
78
|
+
`export default` is silently skipped, and `lunora migrate up` then can't find it.
|
|
92
79
|
|
|
93
80
|
```ts
|
|
94
|
-
// lunora/migrations
|
|
95
|
-
import { defineMigration } from "@lunora/server";
|
|
81
|
+
// lunora/migrations.ts
|
|
82
|
+
import { defineMigration } from "@lunora/server"; // or "lunorash/server"
|
|
96
83
|
|
|
97
|
-
export
|
|
84
|
+
export const backfillDisplayName = defineMigration({
|
|
98
85
|
id: "backfill-display-name",
|
|
99
86
|
table: "users",
|
|
100
|
-
batchSize: 200, // optional
|
|
87
|
+
batchSize: 200, // optional
|
|
101
88
|
up: (doc) => {
|
|
102
89
|
if (typeof doc.displayName === "string") {
|
|
103
|
-
return; //
|
|
90
|
+
return undefined; // skip: not rewritten, not counted as changed
|
|
104
91
|
}
|
|
92
|
+
|
|
105
93
|
return { ...doc, displayName: doc.name ?? "Anonymous" };
|
|
106
94
|
},
|
|
107
|
-
// optional
|
|
95
|
+
// optional; applied by `lunora migrate down`
|
|
108
96
|
down: (doc) => {
|
|
109
|
-
const { displayName, ...rest } = doc
|
|
97
|
+
const { displayName, ...rest } = doc;
|
|
98
|
+
|
|
110
99
|
return rest;
|
|
111
100
|
},
|
|
112
101
|
});
|
|
113
102
|
```
|
|
114
103
|
|
|
115
|
-
The
|
|
116
|
-
|
|
104
|
+
The runner keeps each row's original `_id` and `_creationTime`.
|
|
105
|
+
|
|
106
|
+
### Reading another table
|
|
107
|
+
|
|
108
|
+
The transform's second argument is a shard-scoped, read-only `ctx.db`
|
|
109
|
+
(`get`, `findFirst`, `findMany`, `count`), and the transform may be `async`.
|
|
110
|
+
That covers the common case of copying a field down from a parent:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
up: async (doc, ctx) => {
|
|
114
|
+
const thread = await ctx.db.get(String(doc.threadId), "threads");
|
|
115
|
+
|
|
116
|
+
return thread ? { ...doc, userId: thread.userId } : undefined;
|
|
117
|
+
},
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
To change rows in a second table, write a second migration over that table.
|
|
121
|
+
|
|
122
|
+
### A shard key can't be backfilled
|
|
123
|
+
|
|
124
|
+
If the field is the table's `.shardBy()` key, a migration can't help: a row with
|
|
125
|
+
no shard key belongs to no shard (so the runner never sees it), and setting the
|
|
126
|
+
key would mean moving the row to another Durable Object. Re-key with
|
|
127
|
+
`lunora export` → rewrite the NDJSON → `lunora import`. Ids are preserved, so
|
|
128
|
+
foreign keys survive. The same route applies to any shard-mode change.
|
|
117
129
|
|
|
118
130
|
### Run it
|
|
119
131
|
|
|
120
132
|
```bash
|
|
121
|
-
lunora migrate
|
|
122
|
-
lunora
|
|
123
|
-
lunora migrate
|
|
124
|
-
lunora migrate
|
|
125
|
-
lunora migrate status backfill-display-name # per-shard progress
|
|
126
|
-
lunora migrate down backfill-display-name # revert (if `down` defined)
|
|
133
|
+
lunora migrate up backfill-display-name --dry-run # preview, rewrites nothing
|
|
134
|
+
lunora migrate up backfill-display-name # against the dev worker (localhost:8787)
|
|
135
|
+
lunora migrate status backfill-display-name # per-shard progress
|
|
136
|
+
lunora migrate down backfill-display-name # only if `down` is defined
|
|
127
137
|
```
|
|
128
138
|
|
|
129
|
-
|
|
130
|
-
`--prod --url <worker> --yes` to target production (with `LUNORA_ADMIN_TOKEN`).
|
|
139
|
+
Options: `--batch-size <n>`, `--steps <n>` (cap batches per run), `--format json`.
|
|
131
140
|
|
|
132
|
-
|
|
141
|
+
Production runs rewrite live data, so they require an explicit confirmation:
|
|
133
142
|
|
|
134
143
|
```bash
|
|
135
|
-
|
|
136
|
-
|
|
144
|
+
LUNORA_ADMIN_TOKEN=… lunora migrate up backfill-display-name --prod --url https://app.example.com --yes
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`--url` can be omitted when the checkout is linked to production. Alternatively,
|
|
148
|
+
`lunora deploy --migrate --migrate-yes --migrate-url <url>` runs every declared
|
|
149
|
+
migration right after the deploy (`up` is idempotent, so finished ones no-op).
|
|
150
|
+
|
|
151
|
+
## `.global()` tables
|
|
137
152
|
|
|
138
|
-
|
|
139
|
-
|
|
153
|
+
The runtime provisions `.global()` tables itself on first use, on both D1 and
|
|
154
|
+
Hyperdrive: `CREATE TABLE IF NOT EXISTS`, additive `ADD COLUMN`, and declared
|
|
155
|
+
indexes. Additive changes therefore need no DDL step.
|
|
140
156
|
|
|
141
|
-
|
|
142
|
-
|
|
157
|
+
For **D1-backed** global tables, `lunora migrate generate` diffs the schema
|
|
158
|
+
against `lunora/migrations/.snapshot.json` and writes a reviewable
|
|
159
|
+
`lunora/migrations/<timestamp>_<name>.sql`:
|
|
143
160
|
|
|
144
|
-
|
|
145
|
-
lunora
|
|
161
|
+
```bash
|
|
162
|
+
lunora migrate generate --name add_user_status
|
|
146
163
|
```
|
|
147
164
|
|
|
148
|
-
`lunora
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
165
|
+
No command applies that file, `lunora deploy` included. Its value is the
|
|
166
|
+
statements the runtime never issues (`DROP TABLE`, `DROP INDEX`) plus a comment
|
|
167
|
+
block of changes it couldn't generate (renames, backfilling `UPDATE`s), which you
|
|
168
|
+
write yourself. Apply it deliberately, after review:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
wrangler d1 execute DB --remote --file=lunora/migrations/<file>.sql
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
The file is multi-statement, so it is not a `@lunora/d1` `Migration`. Commit
|
|
175
|
+
`.snapshot.json` with it, or the next diff won't see a dropped table.
|
|
176
|
+
|
|
177
|
+
Hyperdrive-backed tables are left out of `migrate generate` (it emits SQLite
|
|
178
|
+
DDL). Write destructive DDL for them by hand against your database. See
|
|
179
|
+
`lunora-setup-hyperdrive-global`.
|
|
180
|
+
|
|
181
|
+
`defineMigration` does not reach `.global()` rows on either backend: it runs
|
|
182
|
+
inside each shard's Durable Object against that shard's SQLite, never D1 or
|
|
183
|
+
Hyperdrive. Backfill a global table with a SQL `UPDATE` (D1: `wrangler d1
|
|
184
|
+
execute`; Hyperdrive: your Postgres/MySQL client). The drift gate still keys on
|
|
185
|
+
a migration naming the table, so once the SQL backfill is done, deploy the
|
|
186
|
+
narrowing change with `--allow-schema-drift` rather than a no-op migration.
|
|
187
|
+
|
|
188
|
+
### Moving a dataset from D1 to Hyperdrive
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
lunora migrate d1-to-hyperdrive --from-url https://old-worker --to-url https://new-worker
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
`--tables a,b` limits it to some tables, and `--out <path>` keeps the
|
|
195
|
+
intermediate NDJSON dump. This moves the data to another backend; any
|
|
196
|
+
reshaping still follows widen → migrate → narrow.
|
|
197
|
+
|
|
198
|
+
## Common pitfalls
|
|
199
|
+
|
|
200
|
+
1. **Making a field required before backfilling.** The gate blocks it unless a
|
|
201
|
+
new migration on that table is part of the same deploy. Widen first.
|
|
202
|
+
2. **Backfilling with a hand-written mutation.** A mutation that `.collect()`s
|
|
203
|
+
a large table runs into transaction limits and can't resume.
|
|
204
|
+
`defineMigration` batches per shard and resumes.
|
|
205
|
+
3. **Not dual-writing during the window.** Rows inserted mid-migration keep the
|
|
206
|
+
old shape. Write the new shape from the widen step on.
|
|
207
|
+
4. **A non-literal `table`.** `table: TABLE_NAME` covers nothing in the gate.
|
|
208
|
+
5. **Bypassing the gate.** `--allow-schema-drift` / `--update-schema-baseline`
|
|
209
|
+
ship the shape without touching data; use them only when existing rows are
|
|
210
|
+
already compatible.
|
|
182
211
|
|
|
183
212
|
## Checklist
|
|
184
213
|
|
|
185
|
-
- [ ] Identified the
|
|
186
|
-
- [ ] Widened the schema
|
|
187
|
-
- [ ]
|
|
188
|
-
- [ ]
|
|
189
|
-
- [ ]
|
|
190
|
-
- [ ]
|
|
191
|
-
structural changes.
|
|
192
|
-
- [ ] Verified completion with `lunora migrate status`.
|
|
193
|
-
- [ ] Narrowed the schema (required / drop old field); removed both-shapes code.
|
|
194
|
-
- [ ] Deployed the final schema; schema-drift gate passed.
|
|
214
|
+
- [ ] Identified the layer (root / `.shardBy()` / `.global()` D1 / Hyperdrive).
|
|
215
|
+
- [ ] Widened the schema; reads handle both shapes; writes use the new shape. Deployed.
|
|
216
|
+
- [ ] `lunora migrate create <id> --table <t>`; transform filled in; `--dry-run` looks right.
|
|
217
|
+
- [ ] `lunora migrate up <id>` (prod: `--prod --yes`); `lunora migrate status <id>` complete on every shard.
|
|
218
|
+
- [ ] D1 destructive DDL: `migrate generate` output reviewed and applied with `wrangler d1 execute`.
|
|
219
|
+
- [ ] Narrowed the schema and removed both-shapes code; drift gate green on deploy.
|