@runeditrun/rer 0.2.24 → 0.2.25
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/README.md +16 -7
- package/cli/browser-runtime.mjs +65 -0
- package/cli/deploy-claim-request.mjs +5 -4
- package/cli/deploy-command.mjs +3 -3
- package/cli/deploy-inputs.mjs +2 -1
- package/cli/deploy.mjs +16 -4
- package/cli/export.mjs +1 -1
- package/cli/hydration/browser.mjs +3 -9
- package/cli/inspect.mjs +3 -1
- package/cli/lifecycle.mjs +4 -1
- package/global/CHANGELOG.md +21 -0
- package/global/README.md +16 -7
- package/global/browser-runtime.mjs +45 -0
- package/global/cli/layout.mjs +8 -4
- package/global/main.mjs +13 -3
- package/install.sh +56 -3
- package/modules/container/README.md +6 -0
- package/modules/container/build.mjs +2 -1
- package/modules/container/image-variables.mjs +27 -0
- package/modules/delivery-producer/declarations.mjs +6 -1
- package/modules/delivery-producer/mail.mjs +66 -0
- package/modules/demo/label.mjs +5 -2
- package/modules/deploy-kit/README.md +6 -4
- package/modules/deploy-kit/asset-health.mjs +28 -0
- package/modules/deploy-kit/d1-import.mjs +22 -4
- package/modules/deploy-kit/diagnostics.mjs +30 -0
- package/modules/deploy-kit/health.mjs +2 -23
- package/modules/deploy-kit/migration-ledger.mjs +6 -2
- package/modules/deploy-kit/migration-retry.mjs +91 -0
- package/modules/deploy-kit/migrations.mjs +9 -5
- package/modules/deploy-kit/owner-cli.mjs +6 -3
- package/modules/deploy-kit/owner-remove.mjs +87 -65
- package/modules/deploy-kit/provider-transport.mjs +28 -5
- package/modules/deploy-kit/provider.mjs +9 -6
- package/modules/deploy-kit/worker.mjs +5 -2
- package/modules/operator-auth/README.md +10 -2
- package/modules/operator-auth/config.mjs +1 -0
- package/modules/operator-auth/conformance/presentation.mjs +2 -2
- package/modules/operator-auth/identity/email-link/confirmation.mjs +2 -1
- package/modules/operator-auth/identity/email-link/expiry.mjs +8 -0
- package/modules/operator-auth/identity/email-link/index.mjs +2 -1
- package/modules/operator-auth/identity/email-link/message.mjs +3 -2
- package/modules/operator-auth/index.d.mts +2 -0
- package/modules/portability/README.md +7 -2
- package/modules/portability/import-staging.mjs +45 -0
- package/modules/portability/import.mjs +8 -8
- package/modules/portability/storage.mjs +5 -2
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ import { checkBoundary } from './boundary.mjs';
|
|
|
8
8
|
import { localDurableObjects } from './durable-objects.mjs';
|
|
9
9
|
import { readMigrationRewrites, withRewrites } from '../deploy-kit/migrations.mjs';
|
|
10
10
|
import { containerMigrationInventory } from './migration-inventory.mjs';
|
|
11
|
+
import { checkImageVariables } from './image-variables.mjs';
|
|
11
12
|
export async function buildContainer(root, options={}) {
|
|
12
13
|
const previous=process.cwd();process.chdir(root);
|
|
13
14
|
try{return await buildImage(root,options);}finally{process.chdir(previous);}
|
|
@@ -74,7 +75,7 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
|
|
|
74
75
|
const entry = engine === 'workerd' ? 'dist/server/'+workerConfig.main : 'dist/server/index.mjs';
|
|
75
76
|
await readFile(join(root,entry));
|
|
76
77
|
if (engine === 'node' && workerConfig.kv_namespaces?.length) throw new Error('Node container does not support KV');
|
|
77
|
-
|
|
78
|
+
await checkImageVariables(root,workerConfig.vars ?? {},declaration.secrets ?? []);
|
|
78
79
|
const manifest = { version:1,engine,entry,cacheContract,runner,config:workerConfig,migrations,portabilityMigrations:await portabilityPlan(split),maxRequestBytes:25*1024*1024 };
|
|
79
80
|
await rm(output,{recursive:true,force:true}); await mkdir(join(output,'app'),{recursive:true});
|
|
80
81
|
await cp(join(root,'dist'),join(output,'app/dist'),{recursive:true}); await writeFile(join(output,'app/container.json'),JSON.stringify(manifest,null,2)+'\n');
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {readFile} from 'node:fs/promises';
|
|
2
|
+
import {deliveryDeclarationSources} from '../../cli/delivery-declarations.mjs';
|
|
3
|
+
import {sourcePath} from '../delivery-producer/migrations.mjs';
|
|
4
|
+
import {usesManagedMail,validateMailSpec} from '../delivery-producer/mail.mjs';
|
|
5
|
+
|
|
6
|
+
/** Admit only the managed-mail contract's public config through legacy secret declarations. */
|
|
7
|
+
export async function checkImageVariables(root,vars,secrets) {
|
|
8
|
+
const overlaps=secrets.filter(secret=>Object.hasOwn(vars,secret.binding));
|
|
9
|
+
if (!overlaps.length) return;
|
|
10
|
+
const publicMail=new Set(),privateNames=new Set();
|
|
11
|
+
for (const [candidate,select] of deliveryDeclarationSources) {
|
|
12
|
+
const path=await sourcePath(root,candidate,{optional:true});
|
|
13
|
+
if (!path) continue;
|
|
14
|
+
const spec=select(JSON.parse(await readFile(path,'utf8')));
|
|
15
|
+
if (!spec) continue;
|
|
16
|
+
for (const name of [...(spec.secretNames ?? []),...(spec.optionalSecretNames ?? [])]) privateNames.add(name);
|
|
17
|
+
if (!usesManagedMail(spec.mail)) continue;
|
|
18
|
+
const names=spec.publicVariableNames ?? Object.keys(spec.publicVariables ?? {});
|
|
19
|
+
const publicVariables=Object.fromEntries(Object.entries(vars).filter(([name])=>names.includes(name)));
|
|
20
|
+
validateMailSpec({...spec,publicVariables,secretNames:spec.secretNames ?? []});
|
|
21
|
+
for (const name of ['MAIL_PROVIDER','MAIL_FROM']) if (typeof publicVariables[name] === 'string') publicMail.add(name);
|
|
22
|
+
}
|
|
23
|
+
for (const {binding} of overlaps) if (!publicMail.has(binding) || privateNames.has(binding)) {
|
|
24
|
+
throw new Error('Secret declared as an image variable: '+binding);
|
|
25
|
+
}
|
|
26
|
+
console.warn(overlaps.map(secret=>secret.binding).join(', ')+' are public mail configuration, not secrets. Remove these bindings from deploy.json secrets; keep them in the mail spec public variables and Wrangler vars. Accepting the legacy declaration; runtime demo settings override the image defaults.');
|
|
27
|
+
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import {array, bindingPattern, keys, string, strings} from './bindings.mjs';
|
|
2
2
|
|
|
3
3
|
const pathPattern = /^\/[A-Za-z0-9/_.~-]*$/;
|
|
4
|
+
const validSignIn = value => value && typeof value === 'object' && !Array.isArray(value)
|
|
5
|
+
&& Object.keys(value).every(key => ['path','loginHint'].includes(key))
|
|
6
|
+
&& typeof value.path === 'string' && /^\/(?!\/)[a-zA-Z0-9/_-]*$/.test(value.path)
|
|
7
|
+
&& typeof value.loginHint === 'boolean';
|
|
4
8
|
const status = (value, label) => {
|
|
5
9
|
if (!Number.isInteger(value) || value < 100 || value > 599) throw Error(`Invalid ${label}`);
|
|
6
10
|
};
|
|
@@ -15,8 +19,9 @@ export function validateDeclarations(spec) {
|
|
|
15
19
|
if (!spec.surfaces.app) throw Error('Surfaces require an app');
|
|
16
20
|
const origins = new Map();
|
|
17
21
|
for (const [name, surface] of Object.entries(spec.surfaces)) {
|
|
18
|
-
keys(surface, ['path','hostnameVariable','originVariable','readiness'], 'surface');
|
|
22
|
+
keys(surface, ['path','hostnameVariable','originVariable','readiness','signIn'], 'surface');
|
|
19
23
|
string(surface.path, 'surface path', pathPattern);
|
|
24
|
+
if (surface.signIn !== undefined && (name !== 'app' || !validSignIn(surface.signIn))) throw Error('Invalid project sign-in declaration');
|
|
20
25
|
if (surface.hostnameVariable !== undefined && surface.originVariable !== undefined) throw Error('Surface must choose one address variable');
|
|
21
26
|
for (const field of ['hostnameVariable','originVariable']) if (surface[field] !== undefined) string(surface[field], field, bindingPattern);
|
|
22
27
|
if (surface.originVariable !== undefined) {
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {keys, strings} from './bindings.mjs';
|
|
2
|
+
|
|
3
|
+
const publicNames = ['MAIL_PROVIDER', 'MAIL_FROM', 'MAIL_RELAY_URL'];
|
|
4
|
+
const providerLabels = {rer: 'RER managed mail', resend: 'Resend', sendgrid: 'SendGrid', cloudflare: 'Cloudflare Email Sending'};
|
|
5
|
+
|
|
6
|
+
/** Managed mail is an explicit capability, independent of direct provider adapters. */
|
|
7
|
+
export function usesManagedMail(mail) {
|
|
8
|
+
if (mail === undefined) return false;
|
|
9
|
+
keys(mail, ['providers'], 'mail');
|
|
10
|
+
const providers = strings(mail.providers, 'mail providers', /^(rer|resend|sendgrid|cloudflare)$/);
|
|
11
|
+
if (!providers.length) throw Error('Mail requires at least one provider');
|
|
12
|
+
return providers.includes('rer');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Compose these fields into the seed's committed setup manifest before packaging. */
|
|
16
|
+
export function generateMailSetupFields(mail) {
|
|
17
|
+
usesManagedMail(mail);
|
|
18
|
+
if (!mail) throw Error('Mail setup requires a provider declaration');
|
|
19
|
+
const condition = provider => ({field: 'MAIL_PROVIDER', equals: provider});
|
|
20
|
+
const presentations = mail.providers.map(provider => ({when: condition(provider), required: true}));
|
|
21
|
+
return [
|
|
22
|
+
{name: 'MAIL_PROVIDER', label: 'Mail provider', kind: 'select', required: true, default: mail.providers[0],
|
|
23
|
+
options: mail.providers.map(value => ({value, label: providerLabels[value]}))},
|
|
24
|
+
{name: 'MAIL_FROM', label: 'Sender address', kind: 'text', required: false, presentations},
|
|
25
|
+
...(mail.providers.includes('rer') ? [
|
|
26
|
+
{name: 'MAIL_RELAY_URL', label: 'Managed mail relay URL', kind: 'url', required: true, when: condition('rer')}
|
|
27
|
+
] : []),
|
|
28
|
+
{name: 'MAIL_API_KEY', label: 'Mail API key', kind: 'secret', input: 'entered', ownerVisible: true,
|
|
29
|
+
required: false, presentations: structuredClone(presentations)},
|
|
30
|
+
...(mail.providers.includes('cloudflare') ? [
|
|
31
|
+
{name: 'MAIL_ACCOUNT_ID', label: 'Cloudflare account ID', kind: 'text', required: true, when: condition('cloudflare')}
|
|
32
|
+
] : [])
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function validateMailSpec(spec) {
|
|
37
|
+
if (!usesManagedMail(spec.mail)) return;
|
|
38
|
+
for (const name of publicNames) {
|
|
39
|
+
if (!Object.hasOwn(spec.publicVariables, name)) throw Error(`Managed mail requires public ${name}`);
|
|
40
|
+
}
|
|
41
|
+
if (![...spec.secretNames, ...(spec.optionalSecretNames ?? [])].includes('MAIL_API_KEY')) {
|
|
42
|
+
throw Error('Managed mail requires secret MAIL_API_KEY');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Check the selected installer's effective fields for RER's assigned provider. */
|
|
47
|
+
export function validateMailSetup(mail, manifest) {
|
|
48
|
+
if (!usesManagedMail(mail) || !manifest) return;
|
|
49
|
+
const provider = manifest.fields.find(field => field.name === 'MAIL_PROVIDER');
|
|
50
|
+
if (provider?.kind !== 'select' || !provider.options.some(option => option.value === 'rer')) {
|
|
51
|
+
throw Error('Managed mail setup requires a MAIL_PROVIDER select with the rer option');
|
|
52
|
+
}
|
|
53
|
+
const matches = condition => condition.field === 'MAIL_PROVIDER'
|
|
54
|
+
&& (condition.nonEmpty === true || condition.equals === 'rer');
|
|
55
|
+
for (const name of [...publicNames, 'MAIL_API_KEY']) {
|
|
56
|
+
const field = manifest.fields.find(item => item.name === name);
|
|
57
|
+
if (!field || (name === 'MAIL_API_KEY' ? field.kind !== 'secret' : field.kind === 'secret')) {
|
|
58
|
+
throw Error(`Managed mail setup requires ${name === 'MAIL_API_KEY' ? 'secret' : 'public'} ${name}`);
|
|
59
|
+
}
|
|
60
|
+
const presentation = field.presentations?.find(item => matches(item.when));
|
|
61
|
+
if ((field.when && !matches(field.when)) || (field.presentations && !presentation)
|
|
62
|
+
|| (presentation?.required ?? field.required) !== true) {
|
|
63
|
+
throw Error(`Managed mail setup requires ${name} with provider rer`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
package/modules/demo/label.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { allow, sha256 } from '../operator-auth/csp.mjs';
|
|
|
4
4
|
// client-side route changes, whose head manager writes a fresh title. The script
|
|
5
5
|
// is one constant text (so one constant CSP hash); the suffix reaches it as data
|
|
6
6
|
// on the label's style element, never as code.
|
|
7
|
-
const SCRIPT = "(()=>{const suffix=document.querySelector('style[data-rer-label]').dataset.rerLabel,update=()=>{const
|
|
7
|
+
const SCRIPT = "(()=>{const suffix=document.querySelector('style[data-rer-label]').dataset.rerLabel,update=()=>{const base=document.title.replace(/\\s*(?:Demo|Dev)$/,''),title=(base?base+' ':'')+suffix;if(document.title!==title)document.title=title};update();new MutationObserver(update).observe(document.head,{childList:true,subtree:true,characterData:true})})()";
|
|
8
8
|
|
|
9
9
|
export async function decorateLabel(response, mode) {
|
|
10
10
|
if (!response.body || !response.headers.get('content-type')?.toLowerCase().startsWith('text/html')) return response;
|
|
@@ -21,7 +21,10 @@ export async function decorateLabel(response, mode) {
|
|
|
21
21
|
headers.delete('content-length');
|
|
22
22
|
headers.delete('etag');
|
|
23
23
|
const addition = `<style data-rer-label="${suffix}">${style}</style><script>${SCRIPT}</script>`;
|
|
24
|
-
const titled = html.replace(/(<title\b[^>]*>)([\s\S]*?)(<\/title>)/i, (_, open, title, close) =>
|
|
24
|
+
const titled = html.replace(/(<title\b[^>]*>)([\s\S]*?)(<\/title>)/i, (_, open, title, close) => {
|
|
25
|
+
const base = title.replace(/\s*(?:Demo|Dev)$/, '');
|
|
26
|
+
return `${open}${base ? `${base} ` : ''}${suffix}${close}`;
|
|
27
|
+
});
|
|
25
28
|
const body = /<\/head\s*>/i.test(titled) ? titled.replace(/<\/head\s*>/i, `${addition}</head>`) : addition + titled;
|
|
26
29
|
return new Response(body, { status: response.status, statusText: response.statusText, headers });
|
|
27
30
|
}
|
|
@@ -40,7 +40,7 @@ The maintained default-seed `/health` returns the existing `status: 'ok'` plus t
|
|
|
40
40
|
} }
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
Assets upload with `run_worker_first: true` (`RUN_WORKER_FIRST` in `render.mjs`). Every request reaches the Worker first, so the seed's routing, origin admission, `no-store` document policy and private-path refusals decide what is served, and the Base delivery producer accepts no other setting. A built file therefore reaches the browser only through the Worker: seed entries call `staticAssetResponse(request, env)` from `foundation/runtime/static-assets.ts` first. After the deploy identity passes, health requests one built file (the first artifact asset that is neither an HTML document nor under a dot path)
|
|
43
|
+
Assets upload with `run_worker_first: true` (`RUN_WORKER_FIRST` in `render.mjs`). Every request reaches the Worker first, so the seed's routing, origin admission, `no-store` document policy and private-path refusals decide what is served, and the Base delivery producer accepts no other setting. A built file therefore reaches the browser only through the Worker: seed entries call `staticAssetResponse(request, env)` from `foundation/runtime/static-assets.ts` first. After the deploy identity passes, health requests one built file (the first artifact asset that is neither an HTML document nor under a dot path) without credentials, retries a 5xx or transport failure once within the existing readiness deadline, and fails with `asset-http-failed` unless it answers 200; the result is recorded as `health.asset`. `rer verify` serves the local build with the same setting (`cli/hosted-output.mjs`).
|
|
44
44
|
|
|
45
45
|
The controller supplies the two public variables and Cloudflare version-metadata binding. The parent owns default-seed source/types, package scripts, shared manifests and workflows. Keep the staged-deploy descriptor blocked until the three-stage isolation and actual build/health integration pass; the original deploy-then-health executor cannot safely run installation/build with its credentials.
|
|
46
46
|
|
|
@@ -52,7 +52,7 @@ Cron reads require the documented `result: {schedules: [...]}` envelope, includi
|
|
|
52
52
|
|
|
53
53
|
`workerConsumerScript(consumer)` (also exported from `index.mjs`) decodes Worker consumer identities from either documented `script_name` or the observed GET response string `script`. It returns the exact name using the plan's resource-name grammar, or `null` for missing, malformed, conflicting, or non-Worker identities. If both fields are present, each must be valid and both must agree; a malformed alias is never ignored. Reconciliation checks every consumer on queues with a requested consumer before any trigger write and rejects foreign or duplicate consumers. Writes continue to use the documented `script_name` request field. Read-only health and retained-receipt repeats accept either response variant without repairs.
|
|
54
54
|
|
|
55
|
-
D1/Queue/DO account listings fully paginate with bounded page/request budgets. R2 follows provider cursors. Missing pagination evidence, duplicate names, permission errors, partial success, redirects and transport failures fail visibly. Requests go only to Cloudflare's exact account API. Requests have a 30-second timeout and bounded response size. Before any HTTP status, GET/HEAD reads and writes that provably failed before connecting (DNS failure, connection refusal or connect timeout) may retry once, sharing the original timeout and request budget. The first read reserves half the timeout for a retry; caller deadlines and cancellation still apply. HTTP responses, failed response-body reads and ambiguous write failures are
|
|
55
|
+
D1/Queue/DO account listings fully paginate with bounded page/request budgets. R2 follows provider cursors. Missing pagination evidence, duplicate names, permission errors, partial success, redirects and transport failures fail visibly. Requests go only to Cloudflare's exact account API. Requests have a 30-second timeout and bounded response size. Before any HTTP status, GET/HEAD reads and writes that provably failed before connecting (DNS failure, connection refusal or connect timeout) may retry once, sharing the original timeout and request budget. The first read reserves half the timeout for a retry; caller deadlines and cancellation still apply. Other HTTP responses, failed response-body reads and ambiguous write failures are not replayed. Two scoped exceptions share the original request timeout and budget: content-addressed asset session/upload requests retry a 5xx or transport failure (including a broken response body) once; owner-account removal retries GET/DELETE 5xx responses up to three attempts, with bounded backoff, and treats a retried DELETE 404 as absent before the normal absence readback. Final asset failures name the built path and HTTP status (or missing status). Removal failures name the count and resources still present or not yet confirmed absent, retain their ownership proof, and give the same removal command to finish. Asset upload tokens are used only on the documented same-account assets endpoint.
|
|
56
56
|
|
|
57
57
|
The first receipt binds source commit SHA and tree, artifact/config SHA-256, effective deployment fingerprint, account/Worker, actual resource IDs, migration hashes, secret names and active provider deployment/version/etag. It contains no secret value. The CLI creates it exclusively at mode 0600 before the first health request; it is an identity receipt, not a claim that health passed (health attempts are added afterwards, see below). A failed health request still exits nonzero, while the retained identity allows the next explicit invocation to authenticate state without another upload. For the second deployment use the **same artifact, run/attempt and receipt**. Current provider state must match the prior receipt. The kit performs no resource creation, Worker/asset upload, SQL mutation, Cron/consumer update, or secret rewrite when unchanged. Read-only D1 queries use the provider's POST query transport; a POST is not by itself a database mutation. The receipt remains byte-identical on a repeat. Missing resources, drift or stale state fail without repair during this repeat.
|
|
58
58
|
|
|
@@ -200,7 +200,9 @@ No Single Redirect (dynamic redirect ruleset) permission is needed. Base's refer
|
|
|
200
200
|
|
|
201
201
|
## Migration safety
|
|
202
202
|
|
|
203
|
-
SQL files have ordered unique names and SHA-256 identities. `d1_migrations` holds controller history with ordered IDs, names, timestamps, SHA-256 hashes and states. Native archives already exclude this installation table. Standard Wrangler name-only inserts remain possible, but their missing hashes/states block subsequent Base deployment pending reviewed adoption. Applied entries must be an unchanged prefix of the desired set. A new migration gets an `applying` claim before its SQL and completion marker are submitted together through the [Cloudflare D1 SQL import API](https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/), using the same whole-file protocol as Wrangler to preserve compound trigger bodies and atomic SQL/completion. Uploads use bounded HTTPS R2 destinations, without account credentials; polling is bounded and signed URLs
|
|
203
|
+
SQL files have ordered unique names and SHA-256 identities. `d1_migrations` holds controller history with ordered IDs, names, timestamps, SHA-256 hashes and states. Native archives already exclude this installation table. Standard Wrangler name-only inserts remain possible, but their missing hashes/states block subsequent Base deployment pending reviewed adoption. Applied entries must be an unchanged prefix of the desired set. A new migration gets an `applying` claim before its SQL and completion marker are submitted together through the [Cloudflare D1 SQL import API](https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/), using the same whole-file protocol as Wrangler to preserve compound trigger bodies and atomic SQL/completion. Uploads use bounded HTTPS R2 destinations, without account credentials; polling is bounded and signed URLs are never diagnostic output. A failed import keeps Cloudflare's own codes and messages on the error (`providerRefusal`, which `rer deploy` prints, with credentials and URLs removed and each message cut to 300 characters) and names the migration it was running.
|
|
204
|
+
|
|
205
|
+
An import that did not complete leaves its `applying` claim. The next deploy of the same migration bytes runs it again only when it proves none of it applied (`migration-retry.mjs`): the import is atomic, so the migration's effects that would make a second run fail (a CREATE without IF NOT EXISTS, ADD COLUMN, RENAME) and that last to the end of the file must all be absent. Then no attempt committed, and one that commits late makes the re-run fail whole instead of applying twice. A migration with no such effect, or with any of them present, stays refused as `uncertain-migration-claim`; the message names the migration, what is present, and the exact step: undo what is present and deploy again, or confirm by hand and delete or complete its claim with `wrangler d1 execute`. The kit never re-runs a migration whose effects may be present, rolls back user data, or marks an unknown outcome successful. Any other unconfirmed or changed history is still `changed-or-uncertain-migration-history`. Readback checks every final identity.
|
|
204
206
|
|
|
205
207
|
A seed that had to rewrite a migration it already released declares the rewrite
|
|
206
208
|
in `migrations/rewrites.json`: `{ "version": 1, "rewrites": [{ "migration":
|
|
@@ -221,7 +223,7 @@ without treating quoted semicolons as terminators or changing migration hashes.
|
|
|
221
223
|
|
|
222
224
|
A lexical guard rejects standalone transaction/resource control statements (`BEGIN`, `COMMIT`, `END`, `ROLLBACK`, `SAVEPOINT`, `RELEASE`, `ATTACH`, `DETACH`, `VACUUM`) at statement boundaries, including after a trigger. Bare `ROLLBACK` is also refused in conflict clauses and `RAISE`, which can roll back a batch without a standalone control statement. SQLite `CREATE [TEMP|TEMPORARY] TRIGGER` bodies and nested `CASE ... END` expressions are supported; quoted text and comments cannot create statement boundaries. Both ledger names are reserved in bare, double-quoted, backtick and bracket identifiers, including schema-qualified references. [SQLite also accepts single-quoted identifiers](https://www.sqlite.org/lang_keywords.html), so exact controller ledger names are reserved in every token, including data strings, relation names, virtual-table options and introspection arguments. Comments and longer descriptive strings remain allowed. This intentionally conservative policy does not attempt to authorize arbitrary SQLite name grammars. Incomplete quotes, block comments, parentheses, CASE expressions or trigger bodies and NUL bytes fail closed; trigger header expressions must close before BEGIN and body expressions before their statement semicolon. This guard protects controller custody; SQLite still validates application SQL syntax.
|
|
223
225
|
|
|
224
|
-
The old kit `_rer_deploy_migrations` ledger is moved atomically into `d1_migrations` only after every applied name/hash/state proves an exact desired-history prefix. An ordinary deploy performs this one-time metadata transition even for unchanged code; subsequent redeploys write nothing, and health remains read-only. Existing Wrangler `d1_migrations` records lack content hashes. Automatic adoption would fabricate historical certainty, so those installations require separately reviewed history/hash adoption before this kit proceeds unless their ordered rows exactly match a prefix already attested by the old kit ledger. That transition preserves their IDs and timestamps. The kit can extend its own previous migrations while retaining existing application rows. Local SQLite tests prove failure rollback in the test transport; the provider's actual failure atomicity, retained data, and two-deploy no-mutation behavior remain live proof obligations.
|
|
226
|
+
The old kit `_rer_deploy_migrations` ledger is moved atomically into `d1_migrations` only after every applied name/hash/state proves an exact desired-history prefix. An ordinary deploy performs this one-time metadata transition even for unchanged code; subsequent redeploys write nothing, and health remains read-only. Existing Wrangler `d1_migrations` records lack content hashes. Automatic adoption would fabricate historical certainty, so those installations require separately reviewed history/hash adoption before this kit proceeds unless their ordered rows exactly match a prefix already attested by the old kit ledger. That transition preserves their IDs and timestamps. The kit can extend its own previous migrations while retaining existing application rows. Local SQLite tests prove failure rollback in the test transport; the provider's actual failure atomicity, retained data, and two-deploy no-mutation behavior remain live proof obligations. An `applying` claim, including one left by a lost remote response, is re-run only on the proof above.
|
|
225
227
|
|
|
226
228
|
## Local operations and source templates
|
|
227
229
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { nameAssetFailure } from './diagnostics.mjs';
|
|
2
|
+
|
|
3
|
+
/** Probe a real built file after identity health. A transient read gets one replay;
|
|
4
|
+
* a fresh edge missing a hashed file may settle within the existing route window. */
|
|
5
|
+
export async function servedAsset(origin, path, request, timeoutMs, {window, windowMs = 0, clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve, ms))} = {}) {
|
|
6
|
+
const start = clock(), budget = Math.min(windowMs || timeoutMs, window?.remaining() ?? Infinity);
|
|
7
|
+
const deadline = AbortSignal.timeout(Math.max(1, Math.ceil(budget)));
|
|
8
|
+
let transient = 0, failure;
|
|
9
|
+
const fail = (code, status) => nameAssetFailure(new Error(code), [path], status);
|
|
10
|
+
for (;;) {
|
|
11
|
+
const remaining = Math.min(budget - (clock() - start), window?.remaining() ?? Infinity);
|
|
12
|
+
if (remaining <= 0 || deadline.aborted) throw failure ?? fail('asset-readiness-timeout');
|
|
13
|
+
let response;
|
|
14
|
+
try { response = await request(`${origin}/${path.split('/').map(encodeURIComponent).join('/')}`, {redirect:'manual',signal:AbortSignal.any([deadline,AbortSignal.timeout(Math.max(1, Math.ceil(Math.min(timeoutMs,remaining / (transient ? 1 : 2)))))]),headers:{'Cache-Control':'no-cache'}}); }
|
|
15
|
+
catch { failure = fail('asset-transport-failed'); }
|
|
16
|
+
if (response) {
|
|
17
|
+
await response.body?.cancel().catch(() => {});
|
|
18
|
+
if (response.status === 200 && !response.redirected) return {path,status:200};
|
|
19
|
+
failure = fail('asset-http-failed', response.status);
|
|
20
|
+
if (response.redirected || response.status !== 404 && (response.status < 500 || response.status > 599)) throw failure;
|
|
21
|
+
}
|
|
22
|
+
const propagating = response?.status === 404 && clock() - start + 2000 <= windowMs;
|
|
23
|
+
if (!propagating && (++transient > 1 || response?.status === 404)) throw failure;
|
|
24
|
+
const rest = Math.min(budget - (clock() - start), window?.remaining() ?? Infinity);
|
|
25
|
+
if (rest <= 0 || deadline.aborted) throw failure;
|
|
26
|
+
await sleep(Math.min(2000,rest));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { assert, uuid } from './validation.mjs';
|
|
3
|
+
import { recordDeployFailure } from './diagnostics.mjs';
|
|
3
4
|
|
|
4
5
|
// Wrangler's remote --file protocol. D1 imports execute the whole file atomically,
|
|
5
6
|
// including trigger bodies; the query endpoint's semicolon splitting cannot.
|
|
6
|
-
|
|
7
|
+
// Cloudflare's own reason for an import that failed or did not finish, kept on the
|
|
8
|
+
// error as providerRefusal (rer deploy names it) and in the deploy diagnostic: its
|
|
9
|
+
// codes and messages without credentials, URLs (signed upload destinations) or
|
|
10
|
+
// control characters, each cut to 300 characters.
|
|
11
|
+
function importFailure(code, id, response, redact) {
|
|
12
|
+
const clean = text => redact(text).replace(/\bhttps?:\/\/\S+/gi, '[URL omitted]').replace(/[\x00-\x1f\x7f-\x9f]+/g, ' ').trim().slice(0, 300);
|
|
13
|
+
const said = [response?.error, ...(Array.isArray(response?.errors) ? response.errors : [])]
|
|
14
|
+
.map(item => typeof item === 'string' ? { message:item } : item)
|
|
15
|
+
.filter(item => Number.isSafeInteger(item?.code) || typeof item?.message === 'string' && item.message.trim()).slice(0, 3)
|
|
16
|
+
.map(item => ({ ...(Number.isSafeInteger(item.code) ? { code:item.code } : {}), ...(typeof item.message === 'string' ? { message:clean(item.message) } : {}) }));
|
|
17
|
+
const status = typeof response?.status === 'string' && /^[a-z_]{1,40}$/.test(response.status) ? response.status : undefined;
|
|
18
|
+
const errors = said.length ? said : status ? [{ message:`import status ${status}` }] : [];
|
|
19
|
+
const error = new Error(code);
|
|
20
|
+
error.providerRefusal = { operation:`POST /d1/database/${id}/import`, errors };
|
|
21
|
+
return recordDeployFailure(error, { providerCodes:errors.filter(item => item.code !== undefined).map(item => item.code), output:errors.map(item => item.message).filter(Boolean).join('\n') });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function importD1({ envelope, upload, redact = String }, id, sql) {
|
|
7
25
|
assert(uuid(id) && typeof sql === 'string' && Buffer.byteLength(sql) <= 4 * 1024 * 1024, 'invalid-d1-import');
|
|
8
26
|
const bytes = Buffer.from(sql), etag = createHash('md5').update(bytes).digest('hex');
|
|
9
27
|
const post = async body => (await envelope(`/d1/database/${id}/import`, { method:'POST', body })).result;
|
|
@@ -15,12 +33,12 @@ export async function importD1({ envelope, upload }, id, sql) {
|
|
|
15
33
|
}
|
|
16
34
|
for (let polls = 0; polls < 100; polls++) {
|
|
17
35
|
// REST places errors on the envelope; validate nested errors when supplied.
|
|
18
|
-
|
|
36
|
+
if (!(response?.success === true && response.type === 'import' && response.status !== 'error' && (!Object.hasOwn(response, 'errors') || Array.isArray(response.errors) && !response.errors.length))) throw importFailure('d1-import-failed', id, response, redact);
|
|
19
37
|
if (response.status === 'complete') {
|
|
20
|
-
|
|
38
|
+
if (!(Number.isSafeInteger(response.result?.num_queries) && response.result.num_queries > 0 && typeof response.result.final_bookmark === 'string' && response.result.final_bookmark.length > 0)) throw importFailure('d1-import-invalid-result', id, response, redact);
|
|
21
39
|
return;
|
|
22
40
|
}
|
|
23
|
-
|
|
41
|
+
if (!(response.status === 'active' && typeof response.at_bookmark === 'string' && response.at_bookmark.length > 0 && response.at_bookmark.length <= 1024)) throw importFailure('d1-import-uncertain', id, response, redact);
|
|
24
42
|
response = await post({ action:'poll', current_bookmark:response.at_bookmark });
|
|
25
43
|
}
|
|
26
44
|
throw new Error('d1-import-poll-limit');
|
|
@@ -25,6 +25,24 @@ export function providerRefusal(error, redact = String) {
|
|
|
25
25
|
const said = refusal.errors.map(item => [item?.code, typeof item?.message === 'string' ? redact(item.message).slice(0,300) : undefined].filter(part => part !== undefined && part !== '').join(' ')).filter(Boolean).slice(0,3);
|
|
26
26
|
return `Cloudflare refused ${redact(refusal.operation).slice(0,200)}${said.length ? `: ${said.join('; ')}` : ''}`;
|
|
27
27
|
}
|
|
28
|
+
/** What a migration that failed or stays refused means for the next run, from the
|
|
29
|
+
* error's own `migration` or `migrationClaim` (set by any copy of the kit) and its
|
|
30
|
+
* `d1:<name>` resource; undefined otherwise. */
|
|
31
|
+
export function migrationFailure(error, again = 'rer deploy') {
|
|
32
|
+
const safe = value => String(value).replace(/[^\w. -]/g,'?').slice(0,120);
|
|
33
|
+
const database = /^d1:([a-z0-9-]+)$/.exec(error?.resource ?? '')?.[1];
|
|
34
|
+
const claim = error?.migrationClaim;
|
|
35
|
+
if (typeof claim?.name === 'string') {
|
|
36
|
+
const name = safe(claim.name).replaceAll(' ','?');
|
|
37
|
+
const run = sql => `npx wrangler d1 execute ${database ?? '<database>'} --remote --command "${sql} WHERE name='${name}' AND state='applying'"`;
|
|
38
|
+
const present = (Array.isArray(claim.present) ? claim.present : []).slice(0,5).map(safe);
|
|
39
|
+
return `Migration ${name} in ${database ? `D1 ${database}` : 'its D1 database'} was started but never confirmed, and rer runs it again only when it can prove none of it applied. ${present.length
|
|
40
|
+
? `Some of its changes are present (${present.join(', ')}). Undo them so the database is as it was before ${name}, then run ${again} again: rer checks again and runs it.`
|
|
41
|
+
: `It creates no table, index, view, trigger or column, so rer cannot check whether it applied. If none of its changes are present, delete its claim with ${run('DELETE FROM d1_migrations')}, then run ${again} again.`} If it did apply in full, record that instead with ${run("UPDATE d1_migrations SET state='applied'")}.`;
|
|
42
|
+
}
|
|
43
|
+
if (typeof error?.migration === 'string') return `Migration ${safe(error.migration)} did not apply; the next ${again} runs it again once rer proves none of it applied.`;
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
28
46
|
/** Safe call identity travels with errors from independently vendored kits. */
|
|
29
47
|
export function providerTransport(error, redact = String) {
|
|
30
48
|
const detail = error?.providerTransport;
|
|
@@ -95,3 +113,15 @@ export function providerFailure(error, redact = String) {
|
|
|
95
113
|
const clean = value => redact(String(value)).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').replace(/\b[a-f0-9]{32}\b/gi,'[REDACTED]').slice(0,300);
|
|
96
114
|
return `Cloudflare call ${clean(call.operation)}: ${clean(call.reason ?? error.message)} (no Cloudflare error code or message supplied)`;
|
|
97
115
|
}
|
|
116
|
+
|
|
117
|
+
// Asset paths are build inputs; the CLI still redacts them before display.
|
|
118
|
+
export function nameAssetFailure(error, paths, status = error.status ?? error.providerTransport?.status) {
|
|
119
|
+
error.assetFailure = {paths, status};
|
|
120
|
+
return error;
|
|
121
|
+
}
|
|
122
|
+
export function assetFailure(error, redact = String) {
|
|
123
|
+
const detail = error?.assetFailure;
|
|
124
|
+
if (!Array.isArray(detail?.paths)) return undefined;
|
|
125
|
+
const names = detail.paths.slice(0,8).map(path => redact(String(path)).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').slice(0,300));
|
|
126
|
+
return `Asset ${names.join(', ')}${detail.paths.length > 8 ? ` (+${detail.paths.length - 8} more)` : ''}: ${Number.isInteger(detail.status) ? `HTTP ${detail.status}` : 'no HTTP status (transport failure)'}`;
|
|
127
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { servedAsset } from './asset-health.mjs';
|
|
1
2
|
import { readinessAPI } from './readiness-window.mjs';
|
|
2
3
|
import { verifyServiceGrants } from './service-grants.mjs';
|
|
3
4
|
import { assert, stable } from './validation.mjs';
|
|
@@ -47,32 +48,10 @@ export async function waitForDeploymentHealth(url, {
|
|
|
47
48
|
throw healthError(`Deployment did not become healthy at ${url} within ${timeoutMs}ms; last result: ${lastResult}.`, lastResult === 'network error' ? 'health-transport-failed' : 'health-http-failed', lastResult);
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
/** An unauthenticated GET of a built file; anything but 200 means the browser gets no assets.
|
|
51
|
-
* After an upload (`windowMs`), an edge still serving the previous version answers 404 for
|
|
52
|
-
* the new build's hashed files: 404, 5xx and transport failures are retried within the window. */
|
|
53
|
-
async function servedAsset(origin, path, request, timeoutMs, {window, windowMs = 0, clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve, ms))} = {}) {
|
|
54
|
-
const start = clock();
|
|
55
|
-
for (;;) {
|
|
56
|
-
let response, failure;
|
|
57
|
-
const remaining = window?.remaining() ?? timeoutMs;
|
|
58
|
-
assert(remaining > 0, 'asset-readiness-timeout');
|
|
59
|
-
try { response = await request(`${origin}/${path.split('/').map(encodeURIComponent).join('/')}`,{redirect:'manual',signal:AbortSignal.timeout(Math.max(1, Math.ceil(Math.min(timeoutMs,remaining)))),headers:{'Cache-Control':'no-cache'}}); }
|
|
60
|
-
catch { failure = 'asset-transport-failed'; }
|
|
61
|
-
if (response) {
|
|
62
|
-
await response.body?.cancel().catch(() => {});
|
|
63
|
-
if (response.status === 200 && !response.redirected) return {path,status:response.status};
|
|
64
|
-
failure = 'asset-http-failed';
|
|
65
|
-
if (response.redirected || response.status !== 404 && response.status < 500) throw new Error(failure);
|
|
66
|
-
}
|
|
67
|
-
if (clock() - start + 2000 > windowMs) throw new Error(failure);
|
|
68
|
-
await sleep(Math.min(2000,window?.remaining() ?? 2000));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
51
|
/** Authenticated provider readback, then the unauthenticated account-derived /health.
|
|
73
52
|
* Without `readiness`, /health gets the single bounded window of waitForDeploymentHealth.
|
|
74
53
|
* With `readiness` (deploy after an upload) it gets the recorded 90-second wait of health-request.mjs.
|
|
75
|
-
* `asset`, a built file's path,
|
|
54
|
+
* `asset`, a built file's path, must answer 200 after at most one transient replay. */
|
|
76
55
|
export async function verifyHealth({api,plan,receipt,fetch:request=globalThis.fetch,timeoutMs=30000,intervalMs=2000,now,sleep,readiness,asset}) {
|
|
77
56
|
if (readiness?.window) api = readinessAPI(api, readiness.window);
|
|
78
57
|
assert(receipt?.version === 1 && receipt.accountId === api.accountId && receipt.worker === plan.worker,'receipt-target-mismatch');
|
|
@@ -47,7 +47,7 @@ const same = (a, b) => a.length === b.length && a.every((row, index) => row.name
|
|
|
47
47
|
export async function readLedger(api, id, migrations, query) {
|
|
48
48
|
const read = async sql => (await query(api, id, sql))[0].results;
|
|
49
49
|
const tables = (await read(`SELECT name FROM sqlite_master WHERE type='table' AND name IN ('${legacy}','${ledger}')`)).map(row => row.name);
|
|
50
|
-
let old = [], applied = [], transition = '';
|
|
50
|
+
let old = [], applied = [], transition = '', claimed = false;
|
|
51
51
|
if (tables.includes(legacy)) {
|
|
52
52
|
const columns = await read(`SELECT name FROM pragma_table_info('${legacy}')`);
|
|
53
53
|
assert(columns.length === 3 && columns.every(row => ['name','sha256','state'].includes(row.name)), 'invalid-migration-ledger');
|
|
@@ -59,6 +59,10 @@ export async function readLedger(api, id, migrations, query) {
|
|
|
59
59
|
assert(['id','name','applied_at'].every(name => columns.includes(name)), 'invalid-migration-ledger');
|
|
60
60
|
if (columns.includes('sha256') && columns.includes('state')) {
|
|
61
61
|
applied = await read(`SELECT name, sha256, state FROM ${ledger} ORDER BY id`);
|
|
62
|
+
// The last row may claim this build's next migration, exact bytes, whose import
|
|
63
|
+
// did not complete; migrate re-runs it only on proof (migration-retry.mjs).
|
|
64
|
+
const last = applied.at(-1);
|
|
65
|
+
if (!tables.includes(legacy) && last?.state === 'applying' && last.name === migrations[applied.length - 1]?.name && last.sha256 === migrations[applied.length - 1].sha256) claimed = Boolean(applied.pop());
|
|
62
66
|
assert(validPrefix(applied, migrations), 'changed-or-uncertain-migration-history');
|
|
63
67
|
if (tables.includes(legacy)) assert(same(applied, old), 'changed-or-uncertain-migration-history');
|
|
64
68
|
} else {
|
|
@@ -78,7 +82,7 @@ export async function readLedger(api, id, migrations, query) {
|
|
|
78
82
|
applied = old;
|
|
79
83
|
}
|
|
80
84
|
if (tables.includes(legacy)) transition += `DROP TABLE ${legacy};`;
|
|
81
|
-
return { applied, transition, exists:tables.includes(ledger) || tables.includes(legacy) };
|
|
85
|
+
return { applied, transition, claimed, exists:tables.includes(ledger) || tables.includes(legacy) };
|
|
82
86
|
}
|
|
83
87
|
function insert(row) {
|
|
84
88
|
return `INSERT INTO ${ledger}(name,sha256,state) VALUES (${literal(row.name)},${literal(row.sha256)},'applied');\n`;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { sqlTokens } from './sql-tokens.mjs';
|
|
2
|
+
import { literal } from './migration-ledger.mjs';
|
|
3
|
+
|
|
4
|
+
// A migration claimed `applying` whose import never completed is re-run only on
|
|
5
|
+
// proof that none of it applied. D1 imports the SQL and its completion marker as
|
|
6
|
+
// one atomic file, so one statement that fails when its effect already exists
|
|
7
|
+
// (CREATE without IF NOT EXISTS, ADD COLUMN, RENAME) and whose effect lasts to the
|
|
8
|
+
// end of the file proves it: its effect absent now means no attempt committed, and
|
|
9
|
+
// an earlier attempt that commits late makes the re-run fail whole instead of
|
|
10
|
+
// applying twice. Every such effect must be absent; any present one is refused.
|
|
11
|
+
|
|
12
|
+
const word = token => token?.kind === 'word' ? token.value.toUpperCase() : '';
|
|
13
|
+
const lower = token => token?.kind === 'word' || token?.kind === 'identifier' ? token.value.toLowerCase() : undefined;
|
|
14
|
+
const symbol = (token, value) => token?.kind === 'symbol' && token.value === value;
|
|
15
|
+
|
|
16
|
+
// Trigger bodies split here too; their parts are DML and add or remove no guard.
|
|
17
|
+
function statements(sql) {
|
|
18
|
+
const list = [[]];
|
|
19
|
+
for (const token of sqlTokens(sql)) symbol(token, ';') ? list.push([]) : list.at(-1).push(token);
|
|
20
|
+
return list.filter(statement => statement.length);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// [main.]name at index; anything in another schema is not checked.
|
|
24
|
+
function named(statement, index) {
|
|
25
|
+
if (!symbol(statement[index + 1], '.')) return { name:lower(statement[index]), next:index + 1 };
|
|
26
|
+
return { name:lower(statement[index]) === 'main' ? lower(statement[index + 2]) : undefined, next:index + 3 };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function create(statement) {
|
|
30
|
+
let index = 1;
|
|
31
|
+
if (['TEMP', 'TEMPORARY'].includes(word(statement[index]))) return undefined;
|
|
32
|
+
if (['UNIQUE', 'VIRTUAL'].includes(word(statement[index]))) index++;
|
|
33
|
+
const type = word(statement[index++]).toLowerCase();
|
|
34
|
+
if (!['table', 'index', 'view', 'trigger'].includes(type) || word(statement[index]) === 'IF') return undefined;
|
|
35
|
+
const target = named(statement, index);
|
|
36
|
+
if (!target.name) return undefined;
|
|
37
|
+
if (type === 'table' || type === 'view') return { type, object:target.name, table:target.name };
|
|
38
|
+
const on = statement.findIndex((token, at) => at >= target.next && word(token) === 'ON');
|
|
39
|
+
const table = on < 0 ? undefined : named(statement, on + 1).name;
|
|
40
|
+
return table && { type, object:target.name, table };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The effects of a migration whose absence proves it never applied. */
|
|
44
|
+
export function retryGuards(sql) {
|
|
45
|
+
let guards = [];
|
|
46
|
+
for (const statement of statements(sql)) {
|
|
47
|
+
const first = word(statement[0]);
|
|
48
|
+
if (first === 'CREATE') {
|
|
49
|
+
const guard = create(statement);
|
|
50
|
+
if (guard) guards.push(guard);
|
|
51
|
+
} else if (first === 'ALTER' && word(statement[1]) === 'TABLE') {
|
|
52
|
+
const { name:table, next } = named(statement, 2);
|
|
53
|
+
let index = next;
|
|
54
|
+
const action = word(statement[index++]);
|
|
55
|
+
if (word(statement[index]) === 'COLUMN') index++;
|
|
56
|
+
const column = lower(statement[index]);
|
|
57
|
+
if (!table) guards = [];
|
|
58
|
+
else if (action === 'ADD' && column) guards.push({ table, column });
|
|
59
|
+
else if (action === 'RENAME' && word(statement[next + 1]) === 'TO') {
|
|
60
|
+
const to = named(statement, next + 2).name;
|
|
61
|
+
guards = guards.filter(guard => guard.table !== table);
|
|
62
|
+
if (to) guards.push({ type:'table', object:to, table:to });
|
|
63
|
+
} else if (action === 'RENAME' && column && word(statement[index + 1]) === 'TO' && lower(statement[index + 2])) {
|
|
64
|
+
guards = guards.filter(guard => !(guard.table === table && guard.column === column));
|
|
65
|
+
guards.push({ table, column:lower(statement[index + 2]) });
|
|
66
|
+
} else if (action === 'DROP' && column) guards = guards.filter(guard => !(guard.table === table && guard.column === column));
|
|
67
|
+
else guards = [];
|
|
68
|
+
} else if (first === 'DROP') {
|
|
69
|
+
const type = word(statement[1]);
|
|
70
|
+
const { name } = named(statement, word(statement[2]) === 'IF' ? 4 : 2);
|
|
71
|
+
// A later drop removes the effect it would prove; an unreadable one proves nothing.
|
|
72
|
+
guards = name ? guards.filter(guard => guard.object !== name && !(['TABLE', 'VIEW'].includes(type) && guard.table === name)) : [];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return guards;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const label = guard => guard.column === undefined ? `${guard.type} ${guard.object}` : `column ${guard.table}.${guard.column}`;
|
|
79
|
+
|
|
80
|
+
/** Refuse unless the claimed migration's effects are provably absent: it then
|
|
81
|
+
* may run again. `read(sql)` returns one SELECT's rows. */
|
|
82
|
+
export async function assertRetryable(read, migration) {
|
|
83
|
+
const guards = retryGuards(migration.sql), present = [];
|
|
84
|
+
for (let start = 0; start < guards.length; start += 50) {
|
|
85
|
+
const part = guards.slice(start, start + 50).map((guard, index) => guard.column === undefined
|
|
86
|
+
? `SELECT ${index} AS guard FROM sqlite_master WHERE type=${literal(guard.type)} AND lower(name)=${literal(guard.object)}`
|
|
87
|
+
: `SELECT ${index} AS guard FROM pragma_table_info(${literal(guard.table)}) WHERE lower(name)=${literal(guard.column)}`);
|
|
88
|
+
for (const row of await read(part.join(' UNION ALL '))) present.push(label(guards[start + row.guard]));
|
|
89
|
+
}
|
|
90
|
+
if (!guards.length || present.length) throw Object.assign(new Error('uncertain-migration-claim'), { migrationClaim:{ name:migration.name, present:[...new Set(present)] } });
|
|
91
|
+
}
|
|
@@ -4,6 +4,7 @@ import { regularBytes } from './artifact.mjs';
|
|
|
4
4
|
import { assertMigrationSqlSafe } from './sql-safety.mjs';
|
|
5
5
|
import { sqlTokens } from './sql-tokens.mjs';
|
|
6
6
|
import { ledger, literal, createLedger, readLedger, recorded } from './migration-ledger.mjs';
|
|
7
|
+
import { assertRetryable } from './migration-retry.mjs';
|
|
7
8
|
export function validateMigrations(migrations) {
|
|
8
9
|
let previous = '';
|
|
9
10
|
for (const migration of migrations) {
|
|
@@ -54,7 +55,7 @@ export async function query(api, id, sql) {
|
|
|
54
55
|
}
|
|
55
56
|
export async function migrate(api, id, migrations, { apply = true, upgrade = apply } = {}) {
|
|
56
57
|
validateMigrations(migrations);
|
|
57
|
-
const { applied, transition, exists } = await readLedger(api, id, migrations, query);
|
|
58
|
+
const { applied, transition, exists, claimed } = await readLedger(api, id, migrations, query);
|
|
58
59
|
const pending = migrations.slice(applied.length);
|
|
59
60
|
assert(apply || pending.length === 0,'pending-migrations');
|
|
60
61
|
if (transition && upgrade) {
|
|
@@ -63,14 +64,17 @@ export async function migrate(api, id, migrations, { apply = true, upgrade = app
|
|
|
63
64
|
assert(!verified.transition && verified.applied.length === applied.length, 'migration-readback-mismatch');
|
|
64
65
|
}
|
|
65
66
|
if (pending.length && !exists) await query(api,id,createLedger);
|
|
66
|
-
for (const migration of pending) {
|
|
67
|
-
// Claim before execution. A
|
|
68
|
-
|
|
67
|
+
for (const [index, migration] of pending.entries()) {
|
|
68
|
+
// Claim before execution. A claim left by an import that did not complete runs
|
|
69
|
+
// again only when its effects are provably absent; otherwise it stays refused.
|
|
70
|
+
if (claimed && index === 0) await assertRetryable(async sql => (await query(api,id,sql))[0].results, migration);
|
|
71
|
+
else await query(api,id,`INSERT INTO ${ledger}(name,sha256,state) VALUES (${literal(migration.name)},${literal(migration.sha256)},'applying')`);
|
|
69
72
|
const last = sqlTokens(migration.sql).at(-1);
|
|
70
73
|
// A second terminator after trailing comments creates an empty D1 statement.
|
|
71
74
|
// Keep the newline even when terminated: a final line comment must end.
|
|
72
75
|
const separator = last?.kind === 'symbol' && last.value === ';' ? '' : '; ';
|
|
73
|
-
await api.importSql(id,`${migration.sql}\n${separator}UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)};`)
|
|
76
|
+
await api.importSql(id,`${migration.sql}\n${separator}UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)};`)
|
|
77
|
+
.catch(error => { error.migration ??= migration.name; throw error; });
|
|
74
78
|
}
|
|
75
79
|
if (pending.length) {
|
|
76
80
|
const final = (await query(api,id,`SELECT name, sha256, state FROM ${ledger} ORDER BY id`))[0].results;
|
|
@@ -8,7 +8,7 @@ import { assert, hexId } from './validation.mjs';
|
|
|
8
8
|
import { regularBytes } from './artifact.mjs';
|
|
9
9
|
import { packageBuild } from './prepare.mjs';
|
|
10
10
|
import { cloudflare } from './provider.mjs';
|
|
11
|
-
import { ownerTarget, ownerPlan, ownerDeploy, ownerHealth, settleOwnerState, sameDeployment, localOnlyBinding, withStateLock, readOwnerState, writeOwnerState } from './owner.mjs';
|
|
11
|
+
import { ownerTarget, ownerWorkerName, ownerPlan, ownerDeploy, ownerHealth, settleOwnerState, sameDeployment, localOnlyBinding, withStateLock, readOwnerState, writeOwnerState } from './owner.mjs';
|
|
12
12
|
import { activeWorker } from './worker.mjs';
|
|
13
13
|
import { ownerSecret } from './owner-secret.mjs';
|
|
14
14
|
import { ownerRemove } from './owner-remove.mjs';
|
|
@@ -55,8 +55,11 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
55
55
|
const deadline = AbortSignal.timeout(Math.max(1, Math.ceil(remaining)));
|
|
56
56
|
return request(input, {...init,signal:init.signal ? AbortSignal.any([init.signal,deadline]) : deadline});
|
|
57
57
|
};
|
|
58
|
-
const api = deploymentDiagnostics(cloudflare({accountId:env.CLOUDFLARE_ACCOUNT_ID,token:env.CLOUDFLARE_API_TOKEN,fetch,...(command === 'remove' ? {maxRequests:Infinity} : {})}),{onCreated});
|
|
59
|
-
|
|
58
|
+
const api = deploymentDiagnostics(cloudflare({accountId:env.CLOUDFLARE_ACCOUNT_ID,token:env.CLOUDFLARE_API_TOKEN,fetch,...(command === 'remove' ? {maxRequests:Infinity,removal:true} : {})}),{onCreated});
|
|
59
|
+
// Removal needs the recorded account/name identity, not a workers.dev route.
|
|
60
|
+
const target = command === 'remove'
|
|
61
|
+
? {accountId:api.accountId,project:declaration.project,environment:values.environment,worker:ownerWorkerName(declaration.project,values.environment)}
|
|
62
|
+
: await ownerTarget(api,{project:declaration.project,environment:values.environment});
|
|
60
63
|
const statePath = values.state ? resolve(root,values.state) : join(root,'.rer','deploy',`${target.environment}.json`);
|
|
61
64
|
if (command === 'remove') return {ok:true,...await ownerRemove({api,target,statePath}).catch(error => { throw api.annotateFailure(error,'remove'); })};
|
|
62
65
|
if (command === 'secret') {
|