@lunora/cli 1.0.0-alpha.362 → 1.0.0-alpha.364
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/dist/packem_chunks/handler6.mjs +2 -2
- package/dist/packem_chunks/runDeployCommand.mjs +1 -1
- package/dist/packem_chunks/runDevCommand.mjs +2 -2
- package/package.json +11 -11
- package/skills/lunora/SKILL.md +140 -164
- package/skills/lunora-create-package/SKILL.md +97 -85
- package/skills/lunora-deploy/SKILL.md +175 -144
- package/skills/lunora-functions/SKILL.md +186 -85
- package/skills/lunora-migration-helper/SKILL.md +146 -174
- package/skills/lunora-performance-audit/SKILL.md +135 -162
- package/skills/lunora-quickstart/SKILL.md +128 -195
- package/skills/lunora-realtime/SKILL.md +137 -130
- package/skills/lunora-setup-auth/SKILL.md +157 -124
- package/skills/lunora-setup-hyperdrive/SKILL.md +101 -104
- package/skills/lunora-setup-hyperdrive-global/SKILL.md +92 -94
- package/skills/lunora-setup-mail/SKILL.md +83 -117
- package/skills/lunora-setup-scheduler/SKILL.md +130 -99
- package/skills/lunora-setup-storage/SKILL.md +83 -149
|
@@ -1,140 +1,113 @@
|
|
|
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 / Hyperdrive
|
|
6
|
-
structural flows, and the
|
|
7
|
-
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".
|
|
8
4
|
---
|
|
9
5
|
|
|
10
6
|
# Lunora Migration Helper
|
|
11
7
|
|
|
12
|
-
|
|
8
|
+
Change a Lunora schema that already has data in it without breaking the deploy
|
|
9
|
+
or losing rows.
|
|
13
10
|
|
|
14
|
-
|
|
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.
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
- Changing field types or structure.
|
|
18
|
-
- Splitting/merging tables, renaming/removing fields.
|
|
19
|
-
- Reshaping `.global()` tables (D1- or Hyperdrive-backed).
|
|
14
|
+
## Which storage layer are you migrating?
|
|
20
15
|
|
|
21
|
-
|
|
16
|
+
Check the table's modifier in `lunora/schema.ts` first; the tools differ.
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
-
|
|
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 |
|
|
26
23
|
|
|
27
|
-
##
|
|
24
|
+
## The pattern: widen, migrate, narrow
|
|
28
25
|
|
|
29
|
-
|
|
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:
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
differently here: tables **auto-provision on first use** — the runtime applies
|
|
41
|
-
the DDL through the dialect — so there is no `lunora migrate generate` step
|
|
42
|
-
and no versioned SQL file to commit. See `lunora-setup-hyperdrive-global`.
|
|
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.
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
follow the same **widen → migrate → narrow** discipline — check the table's
|
|
48
|
-
backend before assuming which structural path applies.
|
|
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.
|
|
49
43
|
|
|
50
|
-
|
|
51
|
-
> dataset from D1 onto Hyperdrive, use
|
|
52
|
-
> `lunora migrate d1-to-hyperdrive --from-url <d1-worker> --to-url <hd-worker>`
|
|
53
|
-
> (`--tables` scopes it; `--out` keeps the intermediate NDJSON dump). This is a
|
|
54
|
-
> backend move, not a schema change — the widen → migrate → narrow discipline
|
|
55
|
-
> below still governs any reshaping you do on either side of it.
|
|
44
|
+
## What the drift gate considers breaking
|
|
56
45
|
|
|
57
|
-
|
|
46
|
+
The gate (run by `lunora deploy`, `prepare`, `build` and, read-only, `verify`)
|
|
47
|
+
diffs the schema against the committed `lunora/.lunora-schema.json`.
|
|
58
48
|
|
|
59
|
-
|
|
60
|
-
|
|
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`.
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
2. **Migrate** — backfill existing rows to the new shape (an online
|
|
66
|
-
`defineMigration` run with `lunora migrate up`; plus `lunora migrate generate`
|
|
67
|
-
for `.global()` structural DDL). Verify completeness with `lunora migrate
|
|
68
|
-
status`.
|
|
69
|
-
3. **Narrow** — make the field required / drop the old field, remove the
|
|
70
|
-
both-shapes read code. Deploy.
|
|
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.
|
|
71
63
|
|
|
72
|
-
|
|
64
|
+
## Online data migrations
|
|
73
65
|
|
|
74
|
-
|
|
75
|
-
|
|
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.
|
|
76
69
|
|
|
77
|
-
|
|
70
|
+
Scaffold it rather than writing the file from scratch:
|
|
78
71
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
it.
|
|
82
|
-
|
|
83
|
-
## Safe Changes (No Migration Needed)
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
// Adding an optional field — safe.
|
|
87
|
-
users: defineTable({
|
|
88
|
-
name: v.string(),
|
|
89
|
-
bio: v.optional(v.string()),
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// Adding a new table — safe.
|
|
93
|
-
posts: defineTable({ userId: v.id("users"), title: v.string() }).index("by_user", ["userId"]);
|
|
94
|
-
|
|
95
|
-
// Adding an index — safe.
|
|
96
|
-
users: defineTable({ name: v.string(), email: v.string() }).index("by_email", ["email"]);
|
|
72
|
+
```bash
|
|
73
|
+
lunora migrate create backfill-display-name --table users
|
|
97
74
|
```
|
|
98
75
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
`@lunora/server`. It transforms one document at a time, runs **inside each
|
|
103
|
-
shard's** Durable Object in keyset batches, and is **resumable** — per-shard
|
|
104
|
-
progress is tracked in a reserved `__lunora_migrations` table, so an interrupted
|
|
105
|
-
run resumes where it stopped. Codegen discovers declarations and emits them into
|
|
106
|
-
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.
|
|
107
79
|
|
|
108
80
|
```ts
|
|
109
|
-
// lunora/migrations
|
|
110
|
-
import { defineMigration } from "@lunora/server";
|
|
81
|
+
// lunora/migrations.ts
|
|
82
|
+
import { defineMigration } from "@lunora/server"; // or "lunorash/server"
|
|
111
83
|
|
|
112
|
-
export
|
|
84
|
+
export const backfillDisplayName = defineMigration({
|
|
113
85
|
id: "backfill-display-name",
|
|
114
86
|
table: "users",
|
|
115
|
-
batchSize: 200, // optional
|
|
87
|
+
batchSize: 200, // optional
|
|
116
88
|
up: (doc) => {
|
|
117
89
|
if (typeof doc.displayName === "string") {
|
|
118
|
-
return; //
|
|
90
|
+
return undefined; // skip: not rewritten, not counted as changed
|
|
119
91
|
}
|
|
92
|
+
|
|
120
93
|
return { ...doc, displayName: doc.name ?? "Anonymous" };
|
|
121
94
|
},
|
|
122
|
-
// optional
|
|
95
|
+
// optional; applied by `lunora migrate down`
|
|
123
96
|
down: (doc) => {
|
|
124
|
-
const { displayName, ...rest } = doc
|
|
97
|
+
const { displayName, ...rest } = doc;
|
|
98
|
+
|
|
125
99
|
return rest;
|
|
126
100
|
},
|
|
127
101
|
});
|
|
128
102
|
```
|
|
129
103
|
|
|
130
|
-
The
|
|
131
|
-
`_id` / `_creationTime`, so do not change them.
|
|
104
|
+
The runner keeps each row's original `_id` and `_creationTime`.
|
|
132
105
|
|
|
133
106
|
### Reading another table
|
|
134
107
|
|
|
135
|
-
The transform's second argument is a
|
|
136
|
-
`get
|
|
137
|
-
|
|
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:
|
|
138
111
|
|
|
139
112
|
```ts
|
|
140
113
|
up: async (doc, ctx) => {
|
|
@@ -144,104 +117,103 @@ up: async (doc, ctx) => {
|
|
|
144
117
|
},
|
|
145
118
|
```
|
|
146
119
|
|
|
147
|
-
|
|
148
|
-
row read, and a transform writing directly would make that count describe
|
|
149
|
-
something other than what happened. To touch a second table, run a second
|
|
150
|
-
migration over that table.
|
|
120
|
+
To change rows in a second table, write a second migration over that table.
|
|
151
121
|
|
|
152
|
-
### A shard key
|
|
122
|
+
### A shard key can't be backfilled
|
|
153
123
|
|
|
154
|
-
If the field
|
|
155
|
-
|
|
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.
|
|
156
129
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
130
|
+
### Run it
|
|
131
|
+
|
|
132
|
+
```bash
|
|
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
|
|
137
|
+
```
|
|
161
138
|
|
|
162
|
-
|
|
163
|
-
`lunora import` — ids are preserved, so foreign keys survive), not a migration.
|
|
164
|
-
So the rule elsewhere in this skill that a data backfill is always an online
|
|
165
|
-
`defineMigration` has this one exception.
|
|
139
|
+
Options: `--batch-size <n>`, `--steps <n>` (cap batches per run), `--format json`.
|
|
166
140
|
|
|
167
|
-
|
|
141
|
+
Production runs rewrite live data, so they require an explicit confirmation:
|
|
168
142
|
|
|
169
143
|
```bash
|
|
170
|
-
lunora migrate
|
|
171
|
-
lunora codegen # discover + register it
|
|
172
|
-
lunora migrate up backfill-display-name --dry-run # preview, no rows rewritten
|
|
173
|
-
lunora migrate up backfill-display-name # run across shards (keyset batches)
|
|
174
|
-
lunora migrate status backfill-display-name # per-shard progress
|
|
175
|
-
lunora migrate down backfill-display-name # revert (if `down` defined)
|
|
144
|
+
LUNORA_ADMIN_TOKEN=… lunora migrate up backfill-display-name --prod --url https://app.example.com --yes
|
|
176
145
|
```
|
|
177
146
|
|
|
178
|
-
|
|
179
|
-
|
|
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).
|
|
180
150
|
|
|
181
|
-
## `.global()`
|
|
151
|
+
## `.global()` tables
|
|
182
152
|
|
|
183
|
-
|
|
184
|
-
|
|
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.
|
|
156
|
+
|
|
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`:
|
|
185
160
|
|
|
186
161
|
```bash
|
|
187
|
-
|
|
188
|
-
|
|
162
|
+
lunora migrate generate --name add_user_status
|
|
163
|
+
```
|
|
189
164
|
|
|
190
|
-
|
|
191
|
-
|
|
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
|
+
```
|
|
192
173
|
|
|
193
|
-
|
|
194
|
-
|
|
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.
|
|
195
176
|
|
|
196
|
-
|
|
197
|
-
|
|
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
|
|
198
192
|
```
|
|
199
193
|
|
|
200
|
-
`
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
##
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
1. **Making a field required before backfilling.** The drift gate / D1 rejects
|
|
219
|
-
the deploy because existing rows lack it. Widen first.
|
|
220
|
-
2. **Reshaping rows by hand instead of `defineMigration`.** A hand-rolled
|
|
221
|
-
`internalMutation` that `.collect()`s a large table hits transaction limits
|
|
222
|
-
and is not resumable. Use `defineMigration` — it batches and tracks per-shard
|
|
223
|
-
progress.
|
|
224
|
-
3. **Not writing the new shape during the migration window.** Rows created mid-
|
|
225
|
-
migration get missed, leaving unmigrated data after it "completes." Start
|
|
226
|
-
dual-writing in the widen step.
|
|
227
|
-
4. **Skipping the dry run.** `lunora migrate up <id> --dry-run` validates the
|
|
228
|
-
transform before it touches real rows.
|
|
229
|
-
5. **Deleting a field prematurely.** Deprecate with `v.optional` + a comment;
|
|
230
|
-
delete only once nothing references it.
|
|
231
|
-
6. **Migrating the wrong layer.** A D1-backed `.global()` structural change needs
|
|
232
|
-
`lunora migrate generate` (SQL); a Hyperdrive-backed one auto-provisions; a
|
|
233
|
-
data backfill needs a `defineMigration`. Check the table's `.global()` /
|
|
234
|
-
`.shardBy()` modifier — and, for `.global()`, its `backend` — first.
|
|
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.
|
|
235
211
|
|
|
236
212
|
## Checklist
|
|
237
213
|
|
|
238
|
-
- [ ] Identified the
|
|
239
|
-
- [ ] Widened the schema
|
|
240
|
-
- [ ]
|
|
241
|
-
- [ ]
|
|
242
|
-
- [ ]
|
|
243
|
-
- [ ]
|
|
244
|
-
`.global()` structural changes.
|
|
245
|
-
- [ ] Verified completion with `lunora migrate status <id>`.
|
|
246
|
-
- [ ] Narrowed the schema (required / drop old field); removed both-shapes code.
|
|
247
|
-
- [ ] 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.
|