@runeditrun/rer 0.2.68 → 0.2.70
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/cli/README.md +2 -0
- package/cli/deploy-command.mjs +5 -1
- package/cli/deploy-product.mjs +30 -0
- package/cli/deploy.mjs +3 -1
- package/cli/inspect.mjs +29 -1
- package/global/CHANGELOG.md +16 -0
- package/modules/deploy-kit/README.md +2 -0
- package/modules/deploy-kit/owner.mjs +1 -1
- package/modules/operator-auth/README.md +31 -10
- package/modules/operator-auth/SCHEDULED-POLICY.md +130 -0
- package/modules/operator-auth/accounts/admin.mjs +2 -10
- package/modules/operator-auth/accounts/schema/last-signin.sql +11 -0
- package/modules/operator-auth/accounts/schema/operator-auth.sql +25 -1
- package/modules/operator-auth/accounts/schema/scheduled-policy-hardening.sql +8 -0
- package/modules/operator-auth/accounts/schema/scheduled-policy.sql +18 -0
- package/modules/operator-auth/accounts/status.mjs +18 -0
- package/modules/operator-auth/accounts/store.mjs +3 -0
- package/modules/operator-auth/branding.mjs +5 -2
- package/modules/operator-auth/conformance/README.md +7 -3
- package/modules/operator-auth/conformance/index.d.mts +2 -2
- package/modules/operator-auth/conformance/index.mjs +6 -4
- package/modules/operator-auth/conformance/presentation.mjs +8 -3
- package/modules/operator-auth/dev/index.mjs +2 -1
- package/modules/operator-auth/dev-gate.mjs +5 -3
- package/modules/operator-auth/index.d.mts +6 -2
- package/modules/operator-auth/page.mjs +5 -3
- package/modules/operator-auth/routes.mjs +6 -1
- package/modules/operator-auth/scheduled/definition.mjs +37 -0
- package/modules/operator-auth/scheduled/fence.mjs +27 -0
- package/modules/operator-auth/scheduled/history.mjs +30 -0
- package/modules/operator-auth/scheduled/index.d.mts +21 -0
- package/modules/operator-auth/scheduled/index.mjs +83 -0
- package/modules/operator-auth/scheduled/runner.mjs +65 -0
- package/modules/operator-auth/service.mjs +2 -0
- package/modules/operator-auth/signin/index.mjs +2 -2
- package/modules/operator-auth/signin/one-click.mjs +8 -3
- package/modules/portability/README.md +75 -7
- package/modules/portability/backfills.mjs +89 -0
- package/modules/portability/bridge.mjs +18 -12
- package/modules/portability/export.mjs +5 -1
- package/modules/portability/http.mjs +2 -1
- package/modules/portability/import-lease.mjs +99 -0
- package/modules/portability/import.mjs +45 -16
- package/modules/portability/index.d.mts +4 -4
- package/modules/portability/index.mjs +4 -2
- package/modules/portability/migration-statements.mjs +96 -9
- package/modules/portability/policy-data.mjs +10 -0
- package/onboarding/adapter.mjs +5 -2
- package/onboarding/process-diagnostics.mjs +7 -2
- package/package.json +1 -1
|
@@ -132,6 +132,12 @@ that generation and the content hash, while retained domain and identity rows
|
|
|
132
132
|
remain equal. Re-linking keeps the installation sector and person bindings;
|
|
133
133
|
restored provider sign-in remains off until the owner explicitly enables it.
|
|
134
134
|
|
|
135
|
+
Included standing policy approvals retain their provenance and run references,
|
|
136
|
+
but export revokes them in the snapshot. The source approval stays unchanged.
|
|
137
|
+
Restore refuses an archive containing a live approval, even with valid archive
|
|
138
|
+
checksums. The destination owner must approve its policies again. Included
|
|
139
|
+
operators retain their durable `last_signin_at` when sessions are scrubbed.
|
|
140
|
+
|
|
135
141
|
SQLite VIRTUAL and STORED generated columns retain their visible schema metadata
|
|
136
142
|
and original table SQL. Only writable base cells appear in archived rows or
|
|
137
143
|
restore inserts; the destination's migrated schema computes generated values and
|
|
@@ -286,7 +292,15 @@ contain only `CREATE TABLE` (not `AS SELECT`), `CREATE INDEX` and
|
|
|
286
292
|
restorable tables. A later migration with any other statement that names a
|
|
287
293
|
restorable table, such as an `INSERT`, `UPDATE` or `DELETE` that purges or
|
|
288
294
|
backfills rows or a trigger, is refused with `schema_mismatch`; statements that
|
|
289
|
-
touch only excluded tables are allowed.
|
|
295
|
+
touch only excluded tables are allowed. A data statement (`INSERT`, `REPLACE`,
|
|
296
|
+
`UPDATE` or `DELETE`, including `INSERT ... SELECT` and a leading `WITH`) is
|
|
297
|
+
judged by the table it writes: when that table is outside the restorable
|
|
298
|
+
catalogue (excluded, derived or platform) and no trigger or foreign key action
|
|
299
|
+
(`ON DELETE`/`ON UPDATE CASCADE` or `SET`) carries its writes into a catalogue
|
|
300
|
+
table, it changes no restored row and is allowed whatever it reads. Such a table
|
|
301
|
+
then holds live rows the restore lacks, so a later statement that copies it into
|
|
302
|
+
a catalogue table is refused. A statement this parser cannot bound to one table
|
|
303
|
+
keeps the rule above. An archived table the archive holds no
|
|
290
304
|
rows of restores empty, so an `UPDATE` or `DELETE` of it changes nothing and is
|
|
291
305
|
allowed unless it also names a table the archive holds rows of (which it could
|
|
292
306
|
read into its writes); an `INSERT` into it, or any other statement naming it,
|
|
@@ -294,6 +308,28 @@ is still refused. For archives without a migration record,
|
|
|
294
308
|
every migration after the earliest prefix that gives the archive's schema is
|
|
295
309
|
checked, since the archive may predate any of them.
|
|
296
310
|
|
|
311
|
+
A backfill of a catalogue table the archive does not carry (one a later
|
|
312
|
+
migration adds, restored empty) is **replayed** instead of refused: an
|
|
313
|
+
`INSERT ... SELECT` (or `INSERT ... VALUES`) into that table. Finalize empties
|
|
314
|
+
the table (resetting its `AUTOINCREMENT` counter, as the live upgrade created
|
|
315
|
+
it) and runs each of its later writes in migration order, exactly as written,
|
|
316
|
+
after every archived row is inserted and while the destination's triggers are
|
|
317
|
+
still dropped, in the same atomic D1 transaction as the rest of the restore. The
|
|
318
|
+
table then holds what the live upgrade gave it. Time functions such as
|
|
319
|
+
`strftime(..., 'now')` take the restore's time, as they took the upgrade's; an
|
|
320
|
+
`INSERT ... SELECT` without `ORDER BY` assigns new keys in the order SQLite
|
|
321
|
+
scans the restored rows. Rows the restore normalizes (import safety) are read
|
|
322
|
+
as restored. The restore is refused with
|
|
323
|
+
`schema_mismatch:migration_data:<migration>` instead when the archive carries
|
|
324
|
+
the table (even with no rows), when a write to it is not a plain `INSERT` or
|
|
325
|
+
`REPLACE`, reads any table other than archived ones and itself, selects `*` or
|
|
326
|
+
a secret column, when a trigger on it or a foreign key action from it reaches a
|
|
327
|
+
catalogue table, when any other later statement writes it (directly or through
|
|
328
|
+
a trigger) or changes its columns other than by `ADD COLUMN` (allowed only when
|
|
329
|
+
every replay names its columns), or when the statement cannot be bounded.
|
|
330
|
+
A failed or interrupted apply leaves no replayed row; finalizing again replays
|
|
331
|
+
from the start.
|
|
332
|
+
|
|
297
333
|
The same check runs when the archive's schema identity equals the destination's:
|
|
298
334
|
a later migration that only purges or backfills rows leaves the schema unchanged,
|
|
299
335
|
yet the archive's rows never passed through it. Such an archive is refused with
|
|
@@ -313,9 +349,13 @@ before any staging or audit write. Without a replayer (the HTTP handler, direct
|
|
|
313
349
|
bindings), only identical schemas and `acceptedSchemas` restore.
|
|
314
350
|
|
|
315
351
|
The import status and CLI receipt carry `bridged`: `migrations` (the later
|
|
316
|
-
migrations bridged), `tables` (later tables left empty),
|
|
317
|
-
per table that took defaults), `fromSchema` and
|
|
318
|
-
and
|
|
352
|
+
migrations bridged), `tables` (later tables, left empty unless replayed),
|
|
353
|
+
`columns` (later columns per table that took defaults), `fromSchema` and
|
|
354
|
+
`toSchema`, and with any replay `replayed`: one `{migration, statement, table,
|
|
355
|
+
rows}` per replayed statement in the order run (`statement` is its index, from
|
|
356
|
+
0, in the migration's split statements, `rows` the rows it wrote, recorded in the same
|
|
357
|
+
transaction). The import job keeps it as the restore's journal of replays and
|
|
358
|
+
v1 recovery adds it to its audit record. `archiveHash`, `contentHash`,
|
|
319
359
|
the erasure fence and v1's own checks are unchanged.
|
|
320
360
|
|
|
321
361
|
## Backup-only v1 recovery
|
|
@@ -434,8 +474,32 @@ bridge). The batch repeats the check. Any other row refuses with
|
|
|
434
474
|
export, reset and import commands to run. A Worker import has no replay, so a
|
|
435
475
|
hosted restore still needs such tables in `replaceableTables`.
|
|
436
476
|
|
|
437
|
-
An empty destination must be reserved for restore: pause destination writers
|
|
438
|
-
|
|
477
|
+
An empty destination must be reserved for restore: pause destination writers.
|
|
478
|
+
Product writes to source tables can continue. Import upload, finalize and abort
|
|
479
|
+
requests are serialized by a durable request claim.
|
|
480
|
+
|
|
481
|
+
Imports hold a **five-minute inactivity lease**, created with the job and renewed
|
|
482
|
+
by every upload/finalize request, then every 30 seconds while that request is
|
|
483
|
+
working (including verification, R2 transfers and product callbacks). This is a
|
|
484
|
+
recovery window, not a total restore limit: the measured 20,000-row round trip
|
|
485
|
+
below already takes 171 seconds, and callbacks have no wall-time bound. A fresh
|
|
486
|
+
import returns `409 import_running` with `jobId` and ISO `abortableAt`. The owner
|
|
487
|
+
can find its active job with `GET /api/portability/imports` (`{active:status}`
|
|
488
|
+
or `{active:null}`), and inspect `GET .../imports/:id`: `abortableAt` and `stale` describe
|
|
489
|
+
its lease. The existing authenticated `POST .../imports/:id/abort` (or CLI
|
|
490
|
+
`abort --resume ID`) discards stale staging and owned objects. A live request
|
|
491
|
+
cannot be aborted through that route. An expired upload/finalize returns
|
|
492
|
+
`409 import_lease_expired`; start the restore again.
|
|
493
|
+
|
|
494
|
+
The next import automatically aborts a stale job through that same cleanup path
|
|
495
|
+
before claiming the destination. Applying jobs use the existing owned-object
|
|
496
|
+
recovery: product rows publish in one fenced D1 transaction, so an interrupted
|
|
497
|
+
pre-commit apply has no product rows to undo; a committed job stays complete,
|
|
498
|
+
including after a lost acknowledgement. Cleanup never deletes foreign objects.
|
|
499
|
+
Lease bookkeeping is created privately on existing destinations without a product
|
|
500
|
+
migration; an older unfinished job receives one five-minute grace period. The
|
|
501
|
+
local CLI's `abortAbandoned` remains restricted to a sole operator holding its
|
|
502
|
+
checkout lock; hosted callers must use the lease-aware create/status/abort paths.
|
|
439
503
|
R2 has no compare-and-swap transaction covering bucket emptiness and writes.
|
|
440
504
|
An interrupted applying job can be finalized again; ownership survives restart.
|
|
441
505
|
Keep job status until the client acknowledges completion. Abort completed exports
|
|
@@ -461,7 +525,8 @@ are `{error:code}`. Request bodies are bounded; `maxBodyBytes` can lower the bou
|
|
|
461
525
|
| `POST imports` | Create with `{confirmed:true,manifest}`. |
|
|
462
526
|
| `PUT imports/:id/parts/:index` | Upload part. |
|
|
463
527
|
| `POST imports/:id/finalize` | Apply with `{archiveHash,contentHash?,laterJournal?}`. |
|
|
464
|
-
| `GET imports
|
|
528
|
+
| `GET imports` | Active import belonging to this actor, or null. |
|
|
529
|
+
| `GET imports/:id` | Status including lease expiry and stale flag. |
|
|
465
530
|
| `POST imports/:id/abort` | Clean up unfinished import. |
|
|
466
531
|
|
|
467
532
|
```sh
|
|
@@ -556,14 +621,17 @@ modules/portability/storage.mjs
|
|
|
556
621
|
modules/portability/copies.mjs
|
|
557
622
|
modules/portability/dev-data.mjs
|
|
558
623
|
modules/portability/provider-data.mjs
|
|
624
|
+
modules/portability/policy-data.mjs
|
|
559
625
|
modules/portability/generated.mjs
|
|
560
626
|
modules/portability/restore-safety.mjs
|
|
561
627
|
modules/portability/bridge.mjs
|
|
562
628
|
modules/portability/ledgers.mjs
|
|
563
629
|
modules/portability/migration-statements.mjs
|
|
630
|
+
modules/portability/backfills.mjs
|
|
564
631
|
modules/portability/legacy-safety.mjs
|
|
565
632
|
modules/portability/export.mjs
|
|
566
633
|
modules/portability/import.mjs
|
|
634
|
+
modules/portability/import-lease.mjs
|
|
567
635
|
modules/portability/import-staging.mjs
|
|
568
636
|
modules/portability/import-safety.mjs
|
|
569
637
|
modules/portability/payloads.mjs
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { statement, quote, insist } from './storage.mjs';
|
|
2
|
+
import { tokens, folded } from './generated.mjs';
|
|
3
|
+
import { changesData, dataStatement, reachingTables, triggerBodies, schemaChange, statementNames } from './migration-statements.mjs';
|
|
4
|
+
|
|
5
|
+
// The data statements of later migrations, judged by the tables they write
|
|
6
|
+
// (bridge.mjs placement). Restored rows never pass through a later migration.
|
|
7
|
+
// - A bounded INSERT, REPLACE, UPDATE or DELETE whose target lies outside the
|
|
8
|
+
// restorable catalogue (an excluded, derived or platform table) and whose
|
|
9
|
+
// writes reach no catalogue table through a trigger or foreign key action
|
|
10
|
+
// (reachingTables) changes no restored row, whatever it reads: admitted.
|
|
11
|
+
// - One whose target is a catalogue table the archive does not carry (restored
|
|
12
|
+
// empty) and that today's rule refuses (it reads archived rows: a backfill)
|
|
13
|
+
// is replayed at finalize. Every later write to that table must then be a
|
|
14
|
+
// replayable INSERT: they run in migration order, exactly as written, after
|
|
15
|
+
// the archived rows are inserted and with the destination's triggers still
|
|
16
|
+
// dropped, so the table holds what the live upgrade gave it. A replay may read
|
|
17
|
+
// only archived tables (complete by then) and the table itself, by named
|
|
18
|
+
// columns that are not secret; no trigger on the table may write a catalogue
|
|
19
|
+
// table; no later statement may write it another way (directly or through a
|
|
20
|
+
// trigger) or change its columns other than by ADD COLUMN.
|
|
21
|
+
// - Everything else keeps today's rule (changesData), with the tables admitted
|
|
22
|
+
// writes fill from archived rows counted as archived: copying one into a
|
|
23
|
+
// catalogue table is refused, since the restore does not hold its live rows.
|
|
24
|
+
// Returns the replays in migration order, or refuses with the first migration
|
|
25
|
+
// (in `unapplied` order) holding a statement that cannot be admitted.
|
|
26
|
+
export function laterData({applied,unapplied,replay,catalogue,archived,empty,tables,secrets}) {
|
|
27
|
+
const statements=applied.flatMap(name=>replay.statements(name)),fold=list=>list.map(folded);
|
|
28
|
+
const restorable=fold(catalogue),carried=fold(archived),absent=restorable.filter(name=>!carried.includes(name));
|
|
29
|
+
const reached=reachingTables(statements,carried),writable=reachingTables(statements,restorable);
|
|
30
|
+
const later=unapplied.flatMap(migration=>replay.statements(migration).map((sql,index)=>({migration,index,sql,data:dataStatement(sql)})));
|
|
31
|
+
const known=fold(tables),outside=later.filter(s=>s.data && !writable.includes(s.data.target)),tainted=new Set();
|
|
32
|
+
// Tables an admitted write fills from archived rows (directly, through its
|
|
33
|
+
// target's triggers, or from another such table) hold rows the restore lacks.
|
|
34
|
+
const taint=name=>{if(known.includes(name) && !tainted.has(name)){tainted.add(name);triggerBodies(statements,name).forEach(taint);return true;}};
|
|
35
|
+
for(let grew=true;grew;)grew=outside.filter(s=>s.data.names.some(name=>carried.includes(name) || tainted.has(name))).map(s=>taint(s.data.target)).some(Boolean);
|
|
36
|
+
const refused=new Set(),writes=new Map();
|
|
37
|
+
for(const s of later) {
|
|
38
|
+
if(outside.includes(s))continue;
|
|
39
|
+
s.today=changesData(s.sql,[...reached,...tainted],empty);
|
|
40
|
+
if(s.data && absent.includes(s.data.target)){if(!writes.has(s.data.target))writes.set(s.data.target,[]);writes.get(s.data.target).push(s);}
|
|
41
|
+
else if(s.today)refused.add(s);
|
|
42
|
+
}
|
|
43
|
+
const replays=[];
|
|
44
|
+
for(const [table,list] of writes) {
|
|
45
|
+
if(!list.some(s=>s.today))continue;
|
|
46
|
+
const bad=list.filter(s=>!replayable(s,{table,statements,restorable,writable,carried,tables:known,secrets}));
|
|
47
|
+
const reach=reachingTables(statements,[table]),other=later.filter(s=>!list.includes(s) && writesAnother(s,table,reach,list));
|
|
48
|
+
if(bad.length || other.length){[...bad,...other].forEach(s=>refused.add(s));continue;}
|
|
49
|
+
replays.push(...list);
|
|
50
|
+
}
|
|
51
|
+
const first=later.find(s=>refused.has(s));
|
|
52
|
+
insist(!first,'schema_mismatch:migration_data:'+first?.migration);
|
|
53
|
+
const named=Object.fromEntries(catalogue.map(name=>[folded(name),name]));
|
|
54
|
+
return later.filter(s=>replays.includes(s)).map(s=>({migration:s.migration,statement:s.index,table:named[s.data.target],sql:s.sql}));
|
|
55
|
+
}
|
|
56
|
+
function replayable({data},{table,statements,restorable,writable,carried,tables,secrets}) {
|
|
57
|
+
if(!['insert','replace'].includes(data.verb) || data.star)return false;
|
|
58
|
+
const reads=data.names.filter(name=>tables.includes(name) && name!==table);
|
|
59
|
+
if(!reads.every(name=>carried.includes(name)))return false;
|
|
60
|
+
if(reads.some(name=>Object.entries(secrets).some(([t,columns])=>folded(t)===name && columns.some(c=>data.names.includes(folded(c))))))return false;
|
|
61
|
+
if(triggerBodies(statements,table).some(name=>writable.includes(name)))return false;
|
|
62
|
+
// Foreign key actions: a row the replay deletes or rekeys (REPLACE, upsert) must reach no other catalogue table.
|
|
63
|
+
return !reachingTables(statements,restorable.filter(name=>name!==table)).includes(table);
|
|
64
|
+
}
|
|
65
|
+
// Whether a later statement other than the table's replays writes it, through a
|
|
66
|
+
// trigger as well, or changes its columns other than by ADD COLUMN.
|
|
67
|
+
function writesAnother(s,table,reach,replays) {
|
|
68
|
+
if(s.data)return reach.includes(s.data.target);
|
|
69
|
+
if(!statementNames(s.sql).includes(table))return false;
|
|
70
|
+
const shape=schemaChange(s.sql);
|
|
71
|
+
if(shape==='add column')return !replays.every(r=>r.data.columns);
|
|
72
|
+
return !['create table','create index','drop index','create view','drop view','trigger'].includes(shape);
|
|
73
|
+
}
|
|
74
|
+
// Finalize statements for the job's replays, to run after the archived rows are
|
|
75
|
+
// inserted and before the triggers are recreated: each replayed table is
|
|
76
|
+
// emptied (and its AUTOINCREMENT counter reset, as the live upgrade created it)
|
|
77
|
+
// and the statements run in migration order. Each records the rows it wrote in
|
|
78
|
+
// the job's bridge record (`replayed[i].rows`), in the same transaction.
|
|
79
|
+
export function replayStatements(db,row) {
|
|
80
|
+
const replays=row.metadata.replays??[];
|
|
81
|
+
const reset=[...new Set(replays.map(r=>r.table))].flatMap(name=>{
|
|
82
|
+
const target=row.metadata.targets.find(t=>t.name===name);
|
|
83
|
+
insist(target,'schema_mismatch:replay_table:'+name);
|
|
84
|
+
const sequenced=tokens(target.sql).some(t=>!t.quoted && folded(t.text)==='autoincrement');
|
|
85
|
+
return [statement(db,`DELETE FROM ${quote(name)}`),...(sequenced?[statement(db,'DELETE FROM sqlite_sequence WHERE name=?',name)]:[])];
|
|
86
|
+
});
|
|
87
|
+
return [...reset,...replays.flatMap((r,i)=>[db.prepare(r.sql),
|
|
88
|
+
statement(db,`UPDATE rer_portability_jobs SET metadata=json_set(metadata,'$.bridge.replayed[${i}].rows',changes()) WHERE id=?`,row.id)])];
|
|
89
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { all, quote, insist } from './storage.mjs';
|
|
2
2
|
import { canonicalJson } from './hash.mjs';
|
|
3
3
|
import { tokens, identifier, folded } from './generated.mjs';
|
|
4
|
-
import {
|
|
4
|
+
import { laterData } from './backfills.mjs';
|
|
5
5
|
import { destinationLedger } from './ledgers.mjs';
|
|
6
6
|
|
|
7
7
|
// Applied product migrations in the ledger's own order (ledgers.mjs). A
|
|
@@ -18,8 +18,9 @@ export async function appliedMigrations(db) {
|
|
|
18
18
|
// migrations it records or, for archives that predate that record, of those
|
|
19
19
|
// its own migration ledger records or, without one, of an earlier prefix.
|
|
20
20
|
// Restored rows never pass through a later migration, so one that writes or
|
|
21
|
-
// rewrites restorable data is refused, whether or not it changes the schema
|
|
22
|
-
|
|
21
|
+
// rewrites restorable data is refused, whether or not it changes the schema,
|
|
22
|
+
// unless its statement is replayed at finalize (backfills.mjs).
|
|
23
|
+
async function placement(db,manifest,cat,replay,ledger,history) {
|
|
23
24
|
const applied=ledger.map(m=>m.name);
|
|
24
25
|
insist(canonicalJson(replay.names)===canonicalJson(applied) && await replay.schemaAfter(applied)===cat.identity,'schema_mismatch');
|
|
25
26
|
const recorded=manifest.migrations ?? archivedApplied(history?.(),ledger,replay);
|
|
@@ -44,12 +45,14 @@ async function placement(manifest,cat,replay,ledger,history) {
|
|
|
44
45
|
// such tables (a newly added module's own) change no restored row, unless a
|
|
45
46
|
// trigger from any applied migration carries their writes into one that is
|
|
46
47
|
// restorable. Trigger DDL itself changes no row, nor does an UPDATE or DELETE
|
|
47
|
-
// of a table the archive holds no rows of (migration-statements.mjs).
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
// of a table the archive holds no rows of (migration-statements.mjs). A data
|
|
49
|
+
// statement that writes only tables outside the catalogue is admitted
|
|
50
|
+
// whatever it reads; a backfill of a table the archive lacks is replayed.
|
|
51
|
+
const catalogue=cat.tables.map(t=>t.name),archived=catalogue.filter(name=>manifest.tables.some(t=>t.name===name));
|
|
52
|
+
const empty=archived.filter(name=>!manifest.tables.find(t=>t.name===name).count);
|
|
53
|
+
const tables=(await all(db,"SELECT name FROM sqlite_master WHERE type IN ('table','view')")).map(t=>t.name);
|
|
54
|
+
const replays=laterData({applied,unapplied,replay,catalogue,archived,empty,tables,secrets:manifest.excludedColumns??{}});
|
|
55
|
+
return {earlier,replays};
|
|
53
56
|
}
|
|
54
57
|
// An archive without a migration record that carries its source's migration
|
|
55
58
|
// ledger (an old archive from before that ledger became bookkeeping) records
|
|
@@ -77,7 +80,8 @@ export async function sameSchemaMigrations(db,manifest,cat,migrations,history) {
|
|
|
77
80
|
if(!applied)return;
|
|
78
81
|
insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
|
|
79
82
|
if(!migrations){insist(!manifest.migrations || applied.every(name=>manifest.migrations.includes(name)),'schema_mismatch:migrations');return;}
|
|
80
|
-
|
|
83
|
+
// Same schema: every catalogue table is archived, so nothing is replayed.
|
|
84
|
+
await placement(db,manifest,cat,await migrations(),ledger,history);
|
|
81
85
|
}
|
|
82
86
|
// An archive made before later migrations restores only when those migrations
|
|
83
87
|
// are additive: new tables, new columns that take a default, or indexes (which
|
|
@@ -86,7 +90,7 @@ export async function additiveBridge(db,manifest,cat,migrations,history) {
|
|
|
86
90
|
insist(migrations,'schema_mismatch');
|
|
87
91
|
const replay=await migrations(),ledger=await destinationLedger(db)??[],applied=ledger.map(m=>m.name);
|
|
88
92
|
insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
|
|
89
|
-
const {earlier}=await placement(manifest,cat,replay,ledger,history);
|
|
93
|
+
const {earlier,replays}=await placement(db,manifest,cat,replay,ledger,history);
|
|
90
94
|
const columns={};
|
|
91
95
|
for(const table of manifest.tables) {
|
|
92
96
|
const target=cat.tables.find(t=>t.name===table.name),label=':'+table.name;
|
|
@@ -99,8 +103,10 @@ export async function additiveBridge(db,manifest,cat,migrations,history) {
|
|
|
99
103
|
insist(appended(table.sql,target.sql,added.map(c=>c.name)),'schema_mismatch:definition'+label);
|
|
100
104
|
columns[table.name]=added.map(c=>c.name);
|
|
101
105
|
}
|
|
106
|
+
// replays: the statements finalize runs (backfills.mjs); replayed: the job's record of them.
|
|
102
107
|
return {fromSchema:manifest.schema,toSchema:cat.identity,migrations:applied.filter(name=>!earlier.includes(name)),
|
|
103
|
-
tables:cat.tables.filter(t=>!manifest.tables.some(x=>x.name===t.name)).map(t=>t.name),columns
|
|
108
|
+
tables:cat.tables.filter(t=>!manifest.tables.some(x=>x.name===t.name)).map(t=>t.name),columns,
|
|
109
|
+
...(replays.length?{replays,replayed:replays.map(({migration,statement,table})=>({migration,statement,table,rows:null}))}:{})};
|
|
104
110
|
}
|
|
105
111
|
// The destination definition is the archived one with only the added column
|
|
106
112
|
// definitions inserted, as ALTER TABLE ADD COLUMN writes it: no other column,
|
|
@@ -8,6 +8,7 @@ import { appliedMigrations } from './bridge.mjs';
|
|
|
8
8
|
import { capturePayload, payloadRange } from './payloads.mjs';
|
|
9
9
|
import { dropProviderData } from './provider-data.mjs';
|
|
10
10
|
import { dropDevData } from './dev-data.mjs';
|
|
11
|
+
import { revokePolicyApprovals, refusePolicyApprovals } from './policy-data.mjs';
|
|
11
12
|
export async function createExport(context,actor='direct') {
|
|
12
13
|
const {db,config,buckets}=context,s=await state(db);insist(!s.applying,'import_applying',409);
|
|
13
14
|
const cat=await catalogue(db,config),id=crypto.randomUUID(),objects=[];
|
|
@@ -28,7 +29,8 @@ export async function createExport(context,actor='direct') {
|
|
|
28
29
|
...cat.tables.flatMap(t=>createCopy(db,t,copies[t.name],true)),statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'export',actor,row.phase,canonicalJson(row.metadata)),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
29
30
|
try {
|
|
30
31
|
await dropDevData(db,cat.tables,copies);
|
|
31
|
-
|
|
32
|
+
await dropProviderData(db,cat.tables,copies);
|
|
33
|
+
await revokePolicyApprovals(db,copies);
|
|
32
34
|
for(const t of manifest.tables) t.count=(await statement(db,`SELECT COUNT(*) AS n FROM ${quote(copies[t.name])}`).first()).n;
|
|
33
35
|
for(const [name,producer] of Object.entries(config.extraPayloads).sort()) {
|
|
34
36
|
const descriptor=await capturePayload(db,id,manifest.payloads.length,await producer.produce());manifest.payloads.push({name,...descriptor});
|
|
@@ -52,6 +54,8 @@ async function nextItems(context,row) {
|
|
|
52
54
|
const table=manifest.tables[cursor.table];
|
|
53
55
|
if(!row.metadata.normalized?.includes(table.name)) {
|
|
54
56
|
await normalizeCopy(context,table,copies[table.name]);
|
|
57
|
+
// A product row transform cannot re-enable a delegation after scrubbing.
|
|
58
|
+
if(table.name==='oa_policy_approvals')await refusePolicyApprovals(db,copies);
|
|
55
59
|
row.metadata.normalized??=[];row.metadata.normalized.push(table.name);await save(db,row).run();
|
|
56
60
|
}
|
|
57
61
|
const records=await readCopy(db,table,copies[table.name],cursor.row,Math.min(1000,Math.ceil(partBytes/128)),true);
|
|
@@ -25,6 +25,7 @@ export async function createPortabilityHandler({db,buckets,policy,appOrigin,base
|
|
|
25
25
|
if(id)insist(await service.actor(id)===actor,'job_not_found',404);
|
|
26
26
|
insist(['exports','imports'].includes(collection) && route.length<=4,'route_not_found',404);
|
|
27
27
|
const api=service[collection];
|
|
28
|
+
if(collection==='imports' && request.method==='GET' && !id)return json({active:await api.active(actor)});
|
|
28
29
|
if(request.method==='POST' && !id) {
|
|
29
30
|
const body=await boundedJson(request,maxBodyBytes);
|
|
30
31
|
if(collection==='imports'){insist(body.confirmed===true,'confirmation_required');return json({id:await api.create(body.manifest,actor)},201);}
|
|
@@ -37,6 +38,6 @@ export async function createPortabilityHandler({db,buckets,policy,appOrigin,base
|
|
|
37
38
|
if(collection==='imports' && request.method==='PUT' && id && action==='parts' && /^(0|[1-9]\d*)$/.test(index??''))return json(await api.uploadPart(id,Number(index),await boundedJson(request,maxBodyBytes)));
|
|
38
39
|
if(collection==='imports' && request.method==='POST' && id && action==='finalize' && !index)return json(await api.finalize(id,await boundedJson(request,maxBodyBytes)));
|
|
39
40
|
throw new PortabilityError('route_not_found',404);
|
|
40
|
-
}catch(e){return json({error:e instanceof PortabilityError?e.code:'operation_failed'},e instanceof PortabilityError?e.status:500);}
|
|
41
|
+
}catch(e){return json({error:e instanceof PortabilityError?e.code:'operation_failed',...(e instanceof PortabilityError && e.code==='import_running'?{jobId:e.jobId,abortableAt:e.abortableAt}:{})},e instanceof PortabilityError?e.status:500);}
|
|
41
42
|
};
|
|
42
43
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { statement, guard, job, PortabilityError } from './storage.mjs';
|
|
2
|
+
|
|
3
|
+
// Inactivity, not total restore time: the measured 20k-row round trip takes
|
|
4
|
+
// 171s (README). Finalize also awaits unbounded product hooks and multipart R2
|
|
5
|
+
// work, so the owning request renews every 30s throughout those awaits.
|
|
6
|
+
export const IMPORT_LEASE_MS=5*60*1000;
|
|
7
|
+
const RENEW_MS=30*1000;
|
|
8
|
+
const active=row=>['staging','applying'].includes(row.phase);
|
|
9
|
+
|
|
10
|
+
// Private transfer bookkeeping can be added to already-installed destinations
|
|
11
|
+
// without changing product migrations or the archive's schema identity. Keep
|
|
12
|
+
// it separate from metadata: staging/normalization compare that JSON by CAS.
|
|
13
|
+
export async function ensureImportLeases(db) {
|
|
14
|
+
await db.prepare(`CREATE TABLE IF NOT EXISTS rer_portability_import_leases (
|
|
15
|
+
job TEXT PRIMARY KEY REFERENCES rer_portability_jobs(id) ON DELETE CASCADE,
|
|
16
|
+
expires_at INTEGER NOT NULL, holder TEXT
|
|
17
|
+
)`).run();
|
|
18
|
+
// An import left by an older runtime gets one conservative grace period.
|
|
19
|
+
await statement(db,`INSERT OR IGNORE INTO rer_portability_import_leases(job,expires_at)
|
|
20
|
+
SELECT j.id,? FROM rer_portability_jobs j JOIN rer_portability_state s ON s.import_job=j.id
|
|
21
|
+
WHERE j.kind='import' AND j.phase IN ('staging','applying')`,Date.now()+IMPORT_LEASE_MS).run();
|
|
22
|
+
}
|
|
23
|
+
export const createImportLease=(db,id)=>statement(db,'INSERT INTO rer_portability_import_leases(job,expires_at) VALUES(?,?)',id,Date.now()+IMPORT_LEASE_MS);
|
|
24
|
+
const leaseRow=(db,id)=>statement(db,'SELECT * FROM rer_portability_import_leases WHERE job=?',id).first();
|
|
25
|
+
export async function importLeaseStatus(db,row) {
|
|
26
|
+
const lease=await leaseRow(db,row.id);
|
|
27
|
+
return {abortableAt:active(row)&&lease?new Date(lease.expires_at).toISOString():null,stale:active(row)&&!!lease&&lease.expires_at<=Date.now()};
|
|
28
|
+
}
|
|
29
|
+
export async function importRunning(db,id) {
|
|
30
|
+
const error=new PortabilityError('import_running',409);
|
|
31
|
+
error.jobId=id;error.abortableAt=(await importLeaseStatus(db,await job(db,id,'import'))).abortableAt;
|
|
32
|
+
return error;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Upload, finalize and abort each claim one request. Renewal and takeover are
|
|
36
|
+
// conditional writes on that claim; an expired request cannot revive itself.
|
|
37
|
+
// Every mutating D1 batch is fenced by the same claim, including publication.
|
|
38
|
+
export async function withImportLease(context,id,operation,{abort=false,staleOnly=false,soleOperator=false}={}) {
|
|
39
|
+
const db=context.db,holder=crypto.randomUUID(),now=Date.now(),lease=await leaseRow(db,id);
|
|
40
|
+
if(!lease)throw new PortabilityError('import_lease_expired',409);
|
|
41
|
+
if(staleOnly && lease.expires_at>now)throw await importRunning(db,id);
|
|
42
|
+
if(!abort && lease.expires_at<=now)throw new PortabilityError('import_lease_expired',409);
|
|
43
|
+
if(!soleOperator && lease.holder && lease.expires_at>now)throw await importRunning(db,id);
|
|
44
|
+
// Like staging/publication, claim by a transactional SQL guard. Transport
|
|
45
|
+
// adapters need not expose provider-specific UPDATE result metadata.
|
|
46
|
+
try {
|
|
47
|
+
await db.batch([guard(db,`EXISTS(SELECT 1 FROM rer_portability_import_leases WHERE job=? AND expires_at=? AND holder IS ?)
|
|
48
|
+
AND EXISTS(SELECT 1 FROM rer_portability_state WHERE id=1 AND import_job=?)`,id,lease.expires_at,lease.holder,id),
|
|
49
|
+
statement(db,'UPDATE rer_portability_import_leases SET holder=?,expires_at=? WHERE job=?',holder,now+IMPORT_LEASE_MS,id),
|
|
50
|
+
db.prepare('DELETE FROM rer_portability_guard')]);
|
|
51
|
+
}catch(error){
|
|
52
|
+
const current=await leaseRow(db,id);
|
|
53
|
+
if(current?.holder!==lease.holder || current?.expires_at!==lease.expires_at)throw await importRunning(db,id);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
let stopped=false,timer,pending=Promise.resolve(),failure;
|
|
57
|
+
const owned=async()=>{
|
|
58
|
+
const current=await leaseRow(db,id);
|
|
59
|
+
return current?.holder===holder && current.expires_at>Date.now();
|
|
60
|
+
};
|
|
61
|
+
const renew=async()=>{
|
|
62
|
+
const at=Date.now();
|
|
63
|
+
try {
|
|
64
|
+
await db.batch([guard(db,'EXISTS(SELECT 1 FROM rer_portability_import_leases WHERE job=? AND holder=? AND expires_at>?)',id,holder,at),
|
|
65
|
+
statement(db,'UPDATE rer_portability_import_leases SET expires_at=? WHERE job=?',at+IMPORT_LEASE_MS,id),
|
|
66
|
+
db.prepare('DELETE FROM rer_portability_guard')]);
|
|
67
|
+
}catch(error){if(!await owned())throw new PortabilityError('import_lease_expired',409);throw error;}
|
|
68
|
+
};
|
|
69
|
+
const schedule=()=>{
|
|
70
|
+
timer=setTimeout(()=>{
|
|
71
|
+
pending=renew().catch(error=>{failure=error;}).finally(()=>{if(!stopped && !failure)schedule();});
|
|
72
|
+
return pending;
|
|
73
|
+
},RENEW_MS);
|
|
74
|
+
};
|
|
75
|
+
const fence=()=>guard(db,`EXISTS(SELECT 1 FROM rer_portability_import_leases l
|
|
76
|
+
JOIN rer_portability_state s ON s.import_job=l.job WHERE s.id=1 AND l.job=? AND l.holder=? AND l.expires_at>?)`,id,holder,Date.now());
|
|
77
|
+
const native=new WeakMap();
|
|
78
|
+
const batch=async statements=>{
|
|
79
|
+
if(failure)throw failure;
|
|
80
|
+
try {
|
|
81
|
+
const result=await db.batch([fence(),...statements.map(s=>native.get(s)??s),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
82
|
+
return result.slice(1,-1);
|
|
83
|
+
}catch(error){if(!await owned())throw new PortabilityError('import_lease_expired',409);throw error;}
|
|
84
|
+
};
|
|
85
|
+
const prepare=sql=>{
|
|
86
|
+
const wrap=raw=>{const wrapped=new Proxy(raw,{get(target,key){
|
|
87
|
+
if(key==='bind')return (...values)=>wrap(target.bind(...values));
|
|
88
|
+
if(key==='run')return async()=> (await batch([target]))[0];
|
|
89
|
+
const value=target[key];return typeof value==='function'?value.bind(target):value;
|
|
90
|
+
}});native.set(wrapped,raw);return wrapped;};
|
|
91
|
+
return wrap(db.prepare(sql));
|
|
92
|
+
};
|
|
93
|
+
schedule();
|
|
94
|
+
try{return await operation({...context,db:{prepare,batch},importLease:{owned}});}
|
|
95
|
+
finally {
|
|
96
|
+
stopped=true;clearTimeout(timer);await pending;
|
|
97
|
+
await statement(db,'UPDATE rer_portability_import_leases SET holder=NULL WHERE job=? AND holder=?',id,holder).run();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { all, statement, state, guard, job, save, partQueries, parts, readPart, cleanCopies, quote, stream, insist, run } from './storage.mjs';
|
|
2
2
|
import { stagePart } from './import-staging.mjs';
|
|
3
3
|
import { normalizeStaged } from './import-safety.mjs';
|
|
4
|
+
import { ensureImportLeases, createImportLease, importLeaseStatus, importRunning, withImportLease } from './import-lease.mjs';
|
|
4
5
|
import { refuseProviderData } from './provider-data.mjs';
|
|
6
|
+
import { refusePolicyApprovals } from './policy-data.mjs';
|
|
5
7
|
import { refuseDevData } from './dev-data.mjs';
|
|
6
8
|
import { catalogue, validateRow, bindCell, writableColumns } from './catalogue.mjs';
|
|
7
9
|
import { platformTable, restorableManifest } from './bookkeeping.mjs';
|
|
@@ -12,6 +14,7 @@ import { writeObject } from './payloads.mjs';
|
|
|
12
14
|
import { createCopy,sidecar,readCopy,restoreCopy } from './copies.mjs';
|
|
13
15
|
import { excludedDependentGuards, migrationSeeded, seededCondition } from './restore-safety.mjs';
|
|
14
16
|
import { additiveBridge, sameSchemaMigrations, appliedMigrations } from './bridge.mjs';
|
|
17
|
+
import { replayStatements } from './backfills.mjs';
|
|
15
18
|
import { checkArchivedLedgers, archivedHistory } from './ledgers.mjs';
|
|
16
19
|
import { legacyEmptyGuards, legacyAuditStatements, requireLegacyEmpty } from './legacy-safety.mjs';
|
|
17
20
|
// ledgers: the rows of each migration ledger table the archive carries, as the
|
|
@@ -25,34 +28,43 @@ export async function createImport(context,manifest,actor='direct',legacyRecover
|
|
|
25
28
|
const skippedTables=original.tables.filter(table=>platformTable(table.name)).map(table=>table.name);
|
|
26
29
|
const cat=await catalogue(db,config),upgrade=config.acceptedSchemas[original.schema];
|
|
27
30
|
const same=manifest.schema===cat.identity,history=()=>archivedHistory(original,ledgers);
|
|
28
|
-
const bridge=same || upgrade?
|
|
31
|
+
const {replays,...bridge}=same || upgrade?{}:await additiveBridge(db,manifest,cat,context.migrations,history);
|
|
29
32
|
if(same && !upgrade)await sameSchemaMigrations(db,manifest,cat,context.migrations,history);
|
|
30
|
-
if(!bridge)insist(canonicalJson(manifest.tables.map(x=>x.name))===canonicalJson(cat.tables.map(x=>x.name)),'table_inventory');
|
|
31
|
-
if(!upgrade && !bridge)for(const table of manifest.tables) {
|
|
33
|
+
if(!bridge.fromSchema)insist(canonicalJson(manifest.tables.map(x=>x.name))===canonicalJson(cat.tables.map(x=>x.name)),'table_inventory');
|
|
34
|
+
if(!upgrade && !bridge.fromSchema)for(const table of manifest.tables) {
|
|
32
35
|
const target=cat.tables.find(t=>t.name===table.name);
|
|
33
36
|
insist(canonicalJson(table.columns)===canonicalJson(target.columns) && canonicalJson(table.dependencies)===canonicalJson(target.dependencies),'column_inventory');
|
|
34
37
|
}
|
|
35
38
|
// Bridged tables stage and restore only the archived columns; the destination
|
|
36
39
|
// supplies defaults for later columns and leaves later tables empty.
|
|
37
|
-
const targets=cat.tables.map(t=>bridge?.
|
|
40
|
+
const targets=cat.tables.map(t=>bridge.columns?.[t.name]?{...t,columns:manifest.tables.find(x=>x.name===t.name).columns,added:bridge.columns[t.name]}:t);
|
|
38
41
|
insist(canonicalJson(manifest.excludedColumns)===canonicalJson(config.secretColumns),'secret_inventory');
|
|
39
42
|
insist(canonicalJson(manifest.requiredConfiguration)===canonicalJson(config.requiredConfiguration),'configuration_inventory');
|
|
40
43
|
for(const o of manifest.objects) insist(buckets[o.bucket] && config.buckets[o.bucket] && o.key.startsWith(config.buckets[o.bucket].prefix??''),'bucket_inventory');
|
|
41
44
|
insist(canonicalJson(manifest.payloads.map(x=>x.name))===canonicalJson(Object.keys(config.extraPayloads).sort()),'payload_inventory');
|
|
45
|
+
await ensureImportLeases(db);
|
|
46
|
+
const previous=(await state(db)).import_job;
|
|
47
|
+
if(previous) {
|
|
48
|
+
if(!(await importLeaseStatus(db,await job(db,previous,'import'))).stale)throw await importRunning(db,previous);
|
|
49
|
+
await abortImport(context,previous,{staleOnly:true});
|
|
50
|
+
}
|
|
42
51
|
const id=crypto.randomUUID(),copies=Object.fromEntries(targets.map((t,i)=>[t.name,'rer_portability_stage_'+id.replaceAll('-','')+'_'+i]));
|
|
43
|
-
const row={id,kind:'import',actor,phase:'staging',completed:0,metadata:{manifest:original,...(skippedTables.length?{restoreManifest:manifest,skippedTables}:{}),copies,targets,triggers:cat.triggers,...(bridge?{bridge}:{}),...(legacyRecovery?{legacyRecovery}: {}),...(ledgers?{ledgers}:{})}};
|
|
52
|
+
const row={id,kind:'import',actor,phase:'staging',completed:0,metadata:{manifest:original,...(skippedTables.length?{restoreManifest:manifest,skippedTables}:{}),copies,targets,triggers:cat.triggers,...(bridge.fromSchema?{bridge}:{}),...(replays?{replays}:{}),...(legacyRecovery?{legacyRecovery}: {}),...(ledgers?{ledgers}:{})}};
|
|
44
53
|
try {
|
|
45
54
|
await db.batch([guard(db,'(SELECT import_job FROM rer_portability_state WHERE id=1) IS NULL'),
|
|
46
55
|
statement(db,'UPDATE rer_portability_state SET import_job=? WHERE id=1',id),
|
|
47
56
|
...targets.flatMap(t=>createCopy(db,t,copies[t.name])),
|
|
48
|
-
statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'import',actor,row.phase,canonicalJson(row.metadata)),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
49
|
-
}catch(e){
|
|
57
|
+
statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'import',actor,row.phase,canonicalJson(row.metadata)),createImportLease(db,id),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
58
|
+
}catch(e){const running=(await state(db)).import_job;if(running)throw await importRunning(db,running);throw e;}
|
|
50
59
|
return id;
|
|
51
60
|
}
|
|
52
61
|
export async function uploadPart(context,id,index,part) {
|
|
62
|
+
return withImportLease(context,id,leased=>uploadLeasedPart(leased,id,index,part));
|
|
63
|
+
}
|
|
64
|
+
async function uploadLeasedPart(context,id,index,part) {
|
|
53
65
|
const {db,config}=context,row=await job(db,id,'import');await checkPart(part,index,config.partBytes);
|
|
54
66
|
const previous=await statement(db,'SELECT sha256 FROM rer_portability_parts WHERE job=? AND idx=?',id,index).first();
|
|
55
|
-
if(previous){insist(previous.sha256===part.sha256,'part_conflict',409);return importStatus(context,id);}
|
|
67
|
+
if(previous){insist(previous.sha256===part.sha256,'part_conflict',409);return importStatus(context,id,{includeLease:false});}
|
|
56
68
|
insist(row.phase==='staging','import_phase',409);insist(index===row.completed && index<row.metadata.manifest.partCount,'part_order',409);
|
|
57
69
|
const inserts=[],groups=new Map(),manifest=row.metadata.manifest,upgrade=config.acceptedSchemas[manifest.schema];
|
|
58
70
|
for(const item of part.items) {
|
|
@@ -93,7 +105,7 @@ export async function uploadPart(context,id,index,part) {
|
|
|
93
105
|
}
|
|
94
106
|
try{await stagePart(context,row,part,[...inserts,...partQueries(id,part).slice(1)],stagedCounts);}
|
|
95
107
|
catch(e){const duplicate=await statement(db,'SELECT sha256 FROM rer_portability_parts WHERE job=? AND idx=?',id,index).first();if(!duplicate || duplicate.sha256!==part.sha256)throw e;}
|
|
96
|
-
return importStatus(context,id);
|
|
108
|
+
return importStatus(context,id,{includeLease:false});
|
|
97
109
|
}
|
|
98
110
|
const binaryKey=entry=>canonicalJson(entry.bucket===undefined?['payload',entry.name]:['object',entry.bucket,entry.key]);
|
|
99
111
|
function binaryStream(context,id,entry,indexes) {
|
|
@@ -134,7 +146,11 @@ async function removeOwned(context,id) {
|
|
|
134
146
|
for(const o of owned)await context.buckets[o.bucket].delete(o.key);
|
|
135
147
|
await run(context.db,'DELETE FROM rer_portability_owned WHERE job=?',id);
|
|
136
148
|
}
|
|
137
|
-
export async function finalizeImport(context,id,
|
|
149
|
+
export async function finalizeImport(context,id,options={}) {
|
|
150
|
+
if((await job(context.db,id,'import')).phase==='complete')return importStatus(context,id);
|
|
151
|
+
return withImportLease(context,id,leased=>finalizeLeasedImport(leased,id,options));
|
|
152
|
+
}
|
|
153
|
+
async function finalizeLeasedImport(context,id,{contentHash,archiveHash,laterJournal}={}) {
|
|
138
154
|
const {db,config}=context,row=await job(db,id,'import');
|
|
139
155
|
if(row.phase==='complete')return importStatus(context,id);
|
|
140
156
|
insist(['staging','applying'].includes(row.phase),'import_phase',409);
|
|
@@ -163,7 +179,9 @@ export async function finalizeImport(context,id,{contentHash,archiveHash,laterJo
|
|
|
163
179
|
await bucketsEmpty(context,row);
|
|
164
180
|
await refuseDevData(db,row.metadata.copies,row.metadata.targets);
|
|
165
181
|
await refuseProviderData(db,row.metadata.copies,row.metadata.targets);
|
|
182
|
+
await refusePolicyApprovals(db,row.metadata.copies);
|
|
166
183
|
await normalizeStaged(context,row);
|
|
184
|
+
await refusePolicyApprovals(db,row.metadata.copies);
|
|
167
185
|
if(config.validateStaged) {
|
|
168
186
|
const read=async table=>{insist(row.metadata.copies[table],'staged_table');const target=row.metadata.targets.find(t=>t.name===table);return (await readCopy(db,target,row.metadata.copies[table],0,row.metadata.manifest.tables.find(t=>t.name===table)?.count??0)).map(x=>x.row);};
|
|
169
187
|
insist(await config.validateStaged({db,tables:{...row.metadata.copies},read,manifest})!==false,'staged_validation');
|
|
@@ -187,22 +205,33 @@ export async function finalizeImport(context,id,{contentHash,archiveHash,laterJo
|
|
|
187
205
|
...row.metadata.targets.map(t=>guard(db,`NOT EXISTS(SELECT 1 FROM ${quote(sidecar(row.metadata.copies[t.name]))} s JOIN rer_portability_journal j ON j.source=? AND j.tombstone=s.rer_portability_tombstone)`,manifest.sourceIdentity)),
|
|
188
206
|
...row.metadata.triggers.map(t=>db.prepare(`DROP TRIGGER ${quote(t.name)}`)),
|
|
189
207
|
...replaced.map(t=>db.prepare(`DELETE FROM ${quote(t)}`)),
|
|
190
|
-
...row.metadata.targets.flatMap(t=>restoreCopy(db,t,row.metadata.copies[t.name])),
|
|
191
|
-
...row.metadata.triggers.map(t=>db.prepare(t.sql)),...mergeJournal(db,manifest,checked),...legacyAuditStatements(db,row),
|
|
208
|
+
...row.metadata.targets.flatMap(t=>restoreCopy(db,t,row.metadata.copies[t.name])),save(db,row),...replayStatements(db,row),
|
|
209
|
+
...row.metadata.triggers.map(t=>db.prepare(t.sql)),...mergeJournal(db,manifest,checked),...legacyAuditStatements(db,row),
|
|
192
210
|
db.prepare('UPDATE rer_portability_state SET import_job=NULL,applying=0 WHERE id=1'),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
193
211
|
}catch(e){
|
|
194
212
|
// D1 may have committed even when its acknowledgement failed.
|
|
195
213
|
if((await job(db,id)).phase==='complete')return importStatus(context,id);
|
|
214
|
+
if(!await context.importLease.owned())throw new (await import('./storage.mjs')).PortabilityError('import_lease_expired',409);
|
|
196
215
|
await removeOwned(context,id);row.phase='staging';await db.batch([save(db,row),db.prepare('UPDATE rer_portability_state SET applying=0 WHERE id=1')]);throw e;
|
|
197
216
|
}
|
|
198
217
|
return importStatus(context,id);
|
|
199
218
|
}
|
|
200
|
-
|
|
201
|
-
|
|
219
|
+
// Upload receipts stay idempotent; the status door exposes the moving lease.
|
|
220
|
+
export async function importStatus(context,id,{includeLease=true}={}) {
|
|
221
|
+
const row=await job(context.db,id,'import');return {id,kind:'import',phase:row.phase,completed:row.completed,total:row.metadata.manifest.partCount,...(includeLease?await importLeaseStatus(context.db,row):{}),
|
|
202
222
|
contentHash:row.metadata.contentHash??null,requiredConfiguration:row.phase==='complete'?row.metadata.manifest.requiredConfiguration:[],...(row.metadata.bridge?{bridged:row.metadata.bridge}:{}),...(row.metadata.skippedTables?{skippedTables:row.metadata.skippedTables}:{}),...(row.metadata.safetyChanges?{safetyChanges:row.metadata.safetyChanges}:{})};
|
|
203
223
|
}
|
|
204
|
-
export async function
|
|
224
|
+
export async function activeImport(context,actor='direct') {
|
|
225
|
+
const id=(await state(context.db)).import_job;
|
|
226
|
+
return id && (await job(context.db,id,'import')).actor===actor?importStatus(context,id):null;
|
|
227
|
+
}
|
|
228
|
+
export async function abortImport(context,id,options={}) {
|
|
205
229
|
const row=await job(context.db,id,'import');insist(row.phase!=='complete','import_complete',409);
|
|
230
|
+
if(row.phase==='aborted')return importStatus(context,id);
|
|
231
|
+
return withImportLease(context,id,leased=>abortLeasedImport(leased,row),{abort:true,...options});
|
|
232
|
+
}
|
|
233
|
+
async function abortLeasedImport(context,row) {
|
|
234
|
+
const id=row.id;
|
|
206
235
|
await removeOwned(context,id);await cleanCopies(context.db,row);row.phase='aborted';
|
|
207
236
|
await context.db.batch([save(context.db,row),statement(context.db,'UPDATE rer_portability_state SET import_job=NULL,applying=0 WHERE id=1 AND import_job=?',id)]);
|
|
208
237
|
return importStatus(context,id);
|
|
@@ -213,5 +242,5 @@ export async function abortImport(context,id) {
|
|
|
213
242
|
export async function abortAbandonedImport(context) {
|
|
214
243
|
const id=(await state(context.db)).import_job;
|
|
215
244
|
if(!id)return null;
|
|
216
|
-
await abortImport(context,id);return id;
|
|
245
|
+
await abortImport(context,id,{soleOperator:true});return id;
|
|
217
246
|
}
|
|
@@ -34,15 +34,15 @@ export interface ArchivePolicy {
|
|
|
34
34
|
}
|
|
35
35
|
/** Actual changed rows grouped by table, column and safe replacement value; never original values. */
|
|
36
36
|
export interface SafetyChange {table:string;column:string;value:Cell;rows:number}
|
|
37
|
-
export interface Status {id:string;kind:'export'|'import';phase:'building'|'staging'|'applying'|'complete'|'aborted';completed:number;total:number|null;/** Export only: bytes of the parts built so far. */bytes?:number;contentHash:string|null;requiredConfiguration?:string[];bridged?:Bridge;/** Platform bookkeeping ignored in an older archive. */skippedTables?:string[];/** Durable summary of bounded import normalization. */safetyChanges?:SafetyChange[]}
|
|
37
|
+
export interface Status {id:string;kind:'export'|'import';phase:'building'|'staging'|'applying'|'complete'|'aborted';completed:number;total:number|null;/** Import only: inactivity deadline and whether it has passed. */abortableAt?:string|null;stale?:boolean;/** Export only: bytes of the parts built so far. */bytes?:number;contentHash:string|null;requiredConfiguration?:string[];bridged?:Bridge;/** Platform bookkeeping ignored in an older archive. */skippedTables?:string[];/** Durable summary of bounded import normalization. */safetyChanges?:SafetyChange[]}
|
|
38
38
|
/** Later additive migrations an older archive was restored across. */
|
|
39
|
-
export interface Bridge {fromSchema:string;toSchema:string;migrations:string[];tables:string[];columns:Record<string,string[]
|
|
39
|
+
export interface Bridge {fromSchema:string;toSchema:string;migrations:string[];tables:string[];columns:Record<string,string[]>;/** Later backfills of tables the archive lacks, replayed at finalize in this order; rows is null until the apply commits. */replayed?:{migration:string;statement:number;table:string;rows:number|null}[]}
|
|
40
40
|
/** A runner's replay of the destination's applied migrations: the archive schema identity after a chosen set, and each migration's statements. */
|
|
41
41
|
export interface MigrationReplay {names:string[];statements(name:string):string[];/** The migration file's SHA-256 hex, where the runner read the file. */sha256?(name:string):string|undefined;schemaAfter(names:string[]):Promise<string>}
|
|
42
42
|
export interface PortabilityOptions {db:D1Database;buckets?:Record<string,R2Bucket>;policy:ArchivePolicy;migrations?:()=>Promise<MigrationReplay>}
|
|
43
43
|
export interface Portability {
|
|
44
44
|
exports: {create(actor?:string):Promise<string>;advance(id:string,options?:{expectedPart?:number;maxParts?:number}):Promise<Status>;status(id:string):Promise<Status>;download(id:string):Promise<ReadableStream<Uint8Array>>;abort(id:string):Promise<Status>};
|
|
45
|
-
imports: {
|
|
45
|
+
imports: {active(actor?:string):Promise<Status|null>;/** ledgers: the rows of each migration ledger table an old archive carries, as read from it; they place it in the destination's history and must equal its parts. */create(manifest:Manifest,actor?:string,options?:{ledgers?:Record<string,Row[]>}):Promise<string>;uploadPart(id:string,index:number,part:Part):Promise<Status>;finalize(id:string,options:{archiveHash:string;contentHash?:string;laterJournal?:LaterJournal}):Promise<Status>;status(id:string):Promise<Status>;abort(id:string):Promise<Status>;/** Aborts the import left staged by a crashed restore and returns its id; call only as the destination's sole operator. */abortAbandoned():Promise<string|null>};
|
|
46
46
|
status(id:string):Promise<Status>;actor(id:string):Promise<string>;
|
|
47
47
|
erasureStatements(table:string,primaryKey:Record<string,Cell>):Promise<D1PreparedStatement[]>;
|
|
48
48
|
redactionStatements():Promise<D1PreparedStatement[]>;
|
|
@@ -65,4 +65,4 @@ export const excludedTables:readonly string[];
|
|
|
65
65
|
/** True when no archive under this policy carries `table`. */
|
|
66
66
|
export function excludedTable(table:string,policy?:Pick<ArchivePolicy,'excludeTables'|'includeTables'>):boolean;
|
|
67
67
|
export const MAX_PART_BYTES:number;
|
|
68
|
-
export class PortabilityError extends Error {code:string;status:number;constructor(code:string,status?:number)}
|
|
68
|
+
export class PortabilityError extends Error {code:string;status:number;jobId?:string;abortableAt?:string|null;constructor(code:string,status?:number)}
|