@pithy-sh/core 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capability/capability.d.ts.map +1 -1
- package/dist/capability/capability.js +2 -0
- package/dist/capability/settings.d.ts +15 -2
- package/dist/capability/settings.d.ts.map +1 -1
- package/dist/data/databases.d.ts +11 -0
- package/dist/data/databases.d.ts.map +1 -1
- package/dist/migrations/registry.d.ts +6 -0
- package/dist/migrations/registry.d.ts.map +1 -1
- package/dist/migrations/registry.js +2 -0
- package/dist/migrations/retained.d.ts +101 -0
- package/dist/migrations/retained.d.ts.map +1 -0
- package/dist/migrations/retained.js +243 -0
- package/dist/migrations/retainedDeclaration.d.ts +15 -0
- package/dist/migrations/retainedDeclaration.d.ts.map +1 -0
- package/dist/migrations/retainedDeclaration.js +49 -0
- package/dist/migrations/runner.d.ts +44 -7
- package/dist/migrations/runner.d.ts.map +1 -1
- package/dist/migrations/runner.js +41 -15
- package/dist/naming/environment.d.ts +12 -0
- package/dist/naming/environment.d.ts.map +1 -1
- package/dist/naming/environment.js +38 -1
- package/dist/naming/feature.d.ts +7 -1
- package/dist/naming/feature.d.ts.map +1 -1
- package/dist/naming/feature.js +10 -3
- package/dist/naming/provisionScope.d.ts +65 -1
- package/dist/naming/provisionScope.d.ts.map +1 -1
- package/dist/naming/provisionScope.js +62 -5
- package/dist/naming/resourceNames.d.ts +5 -2
- package/dist/naming/resourceNames.d.ts.map +1 -1
- package/dist/naming/resourceNames.js +1 -1
- package/dist/text/comments.d.ts +14 -0
- package/dist/text/comments.d.ts.map +1 -1
- package/dist/text/comments.js +28 -2
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/workflow/host.d.ts +8 -0
- package/dist/workflow/host.d.ts.map +1 -1
- package/dist/workflow/host.js +5 -2
- package/dist/workflow/hostEnv.d.ts +1 -1
- package/dist/workflow/hostEnv.js +1 -1
- package/package.json +1 -1
- package/src/capability/capability.ts +6 -0
- package/src/capability/settings.ts +15 -2
- package/src/data/databases.ts +11 -0
- package/src/migrations/registry.ts +10 -0
- package/src/migrations/retained.ts +319 -0
- package/src/migrations/retainedDeclaration.ts +56 -0
- package/src/migrations/runner.ts +89 -15
- package/src/naming/environment.ts +43 -0
- package/src/naming/feature.ts +10 -3
- package/src/naming/provisionScope.ts +98 -5
- package/src/naming/resourceNames.ts +6 -3
- package/src/text/comments.ts +31 -1
- package/src/version.generated.ts +1 -1
- package/src/workflow/host.ts +14 -4
- package/src/workflow/hostEnv.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"comments.d.ts","sourceRoot":"","sources":["../../src/text/comments.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"comments.d.ts","sourceRoot":"","sources":["../../src/text/comments.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE9D"}
|
package/dist/text/comments.js
CHANGED
|
@@ -62,6 +62,26 @@
|
|
|
62
62
|
* `worker-safety.test.ts` exists to enforce, so the gate now polices the stripper it reads with.
|
|
63
63
|
*/
|
|
64
64
|
function blankComments(source) {
|
|
65
|
+
return blank(source, false);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* `source` with every comment **and every string's contents** blanked — for a gate asking whether a name is
|
|
69
|
+
* *called*, where {@link blankComments}' own rule points the wrong way.
|
|
70
|
+
*
|
|
71
|
+
* `blankComments` keeps strings because its callers look for a forbidden read, and a string that merely contains
|
|
72
|
+
* one is a false positive, the safe direction. A gate looking for a *required* call inverts that: a validator named
|
|
73
|
+
* only inside an error message — `"requireEnvironment(args.env)"` — is a call that never runs, and counting it
|
|
74
|
+
* passes the file. So this blanks the contents too, delimiters kept, character for character.
|
|
75
|
+
*
|
|
76
|
+
* **A template literal goes whole, `${}` included.** A call written only inside an interpolation is not seen. For
|
|
77
|
+
* a presence check that is the safe direction again — the file is flagged, never passed — and a scan for a read
|
|
78
|
+
* belongs on {@link blankComments}, where the interpolation stays.
|
|
79
|
+
*/
|
|
80
|
+
function blankCommentsAndStrings(source) {
|
|
81
|
+
return blank(source, true);
|
|
82
|
+
}
|
|
83
|
+
/** The one walk behind both: comments always blanked, a quoted run kept or blanked inside its delimiters. */
|
|
84
|
+
function blank(source, strings) {
|
|
65
85
|
let out = "";
|
|
66
86
|
let previous = "";
|
|
67
87
|
let index = 0;
|
|
@@ -76,7 +96,8 @@ function blankComments(source) {
|
|
|
76
96
|
}
|
|
77
97
|
if (char === "\"" || char === "'" || char === "`") {
|
|
78
98
|
const stop = endOfQuoted(source, index, char, char !== "`");
|
|
79
|
-
|
|
99
|
+
const run = source.slice(index, stop);
|
|
100
|
+
out += strings ? blankInside(run, char) : run;
|
|
80
101
|
index = stop;
|
|
81
102
|
previous = char;
|
|
82
103
|
continue;
|
|
@@ -94,6 +115,11 @@ function blankComments(source) {
|
|
|
94
115
|
}
|
|
95
116
|
return out;
|
|
96
117
|
}
|
|
118
|
+
/** A quoted run with everything between its delimiters blanked, newlines kept. An unterminated run has no closer. */
|
|
119
|
+
function blankInside(run, quote) {
|
|
120
|
+
const closed = run.length > 1 && run.endsWith(quote);
|
|
121
|
+
return `${quote}${run.slice(1, closed ? -1 : void 0).replace(/[^\n]/g, " ")}${closed ? quote : ""}`;
|
|
122
|
+
}
|
|
97
123
|
/**
|
|
98
124
|
* After one of these, a `/` opens a regex literal; after anything else it divides. The empty string is
|
|
99
125
|
* the start of the file. A wrong guess is bounded — see {@link endOfRegex}.
|
|
@@ -160,4 +186,4 @@ function endOfRegex(source, start) {
|
|
|
160
186
|
return source.length;
|
|
161
187
|
}
|
|
162
188
|
//#endregion
|
|
163
|
-
export { blankComments };
|
|
189
|
+
export { blankComments, blankCommentsAndStrings };
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
/** This package's npm name — the join key against a release feed. */
|
|
4
4
|
export declare const PACKAGE_NAME = "@pithy-sh/core";
|
|
5
5
|
/** This package's version, stamped from its own package.json at generation time. */
|
|
6
|
-
export declare const PACKAGE_VERSION = "0.
|
|
6
|
+
export declare const PACKAGE_VERSION = "0.7.0";
|
|
7
7
|
//# sourceMappingURL=version.generated.d.ts.map
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
/** This package's npm name — the join key against a release feed. */
|
|
5
5
|
const PACKAGE_NAME = "@pithy-sh/core";
|
|
6
6
|
/** This package's version, stamped from its own package.json at generation time. */
|
|
7
|
-
const PACKAGE_VERSION = "0.
|
|
7
|
+
const PACKAGE_VERSION = "0.7.0";
|
|
8
8
|
//#endregion
|
|
9
9
|
export { PACKAGE_NAME, PACKAGE_VERSION };
|
package/dist/workflow/host.d.ts
CHANGED
|
@@ -127,6 +127,14 @@ export interface WorkflowHostParams {
|
|
|
127
127
|
* not depend on `@pithy-sh/secrets`, which owns that naming.
|
|
128
128
|
*/
|
|
129
129
|
masterKeySecretName?: string;
|
|
130
|
+
/**
|
|
131
|
+
* Secrets Store binding name → the entry it resolves to in this environment, for every store secret the
|
|
132
|
+
* host reads besides the master key. Filled **only** for the bindings listed, like
|
|
133
|
+
* {@link databaseNames}; the names come from the capability, which composes them with the same namer
|
|
134
|
+
* provisioning created the entries with. Core composes none of them itself, for the reason
|
|
135
|
+
* {@link masterKeySecretName} gives.
|
|
136
|
+
*/
|
|
137
|
+
secretNames?: Record<string, string>;
|
|
130
138
|
/** Vars merged over the template's, e.g. the capability's config serialized as one JSON blob. */
|
|
131
139
|
vars?: Record<string, string>;
|
|
132
140
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/workflow/host.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,qBAAqB,EAAwC,MAAM,UAAU,CAAC;AAC5F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,uGAAuG;AACvG,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,iDAAiD;AACjD,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,+CAA+C;AAC/C,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,8GAA8G;AAC9G,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,4GAA4G;AAC5G,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,kEAAkE;AAClE,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,kGAAkG;AAClG,MAAM,WAAW,uBAAuB;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,sGAAsG;AACtG,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MA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B,mBAAmB,CAAC,QAAQ,EAAE,oBAAoB,EAAE,MAAM,EAAE,kBAAkB,GAAG,oBAAoB,CA2FpH;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,gBAAgB,EAC1B,KAAK,EAAE,qBAAqB,GAC3B;IAAE,SAAS,EAAE,mBAAmB,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAsBvD"}
|
package/dist/workflow/host.js
CHANGED
|
@@ -59,11 +59,14 @@ function resolveWorkflowHost(template, params) {
|
|
|
59
59
|
if (resolved.send_email) resolved.send_email = resolved.send_email.map((entry) => withRemote(entry, entry.name, remote));
|
|
60
60
|
if (resolved.secrets_store_secrets && params.secretsStoreId !== void 0) {
|
|
61
61
|
const storeId = params.secretsStoreId;
|
|
62
|
-
const
|
|
62
|
+
const names = {
|
|
63
|
+
...params.masterKeySecretName !== void 0 ? { SECRETS_ENCRYPTION_KEYS: params.masterKeySecretName } : {},
|
|
64
|
+
...params.secretNames
|
|
65
|
+
};
|
|
63
66
|
resolved.secrets_store_secrets = resolved.secrets_store_secrets.map((entry) => ({
|
|
64
67
|
...entry,
|
|
65
68
|
store_id: storeId,
|
|
66
|
-
secret_name:
|
|
69
|
+
secret_name: Object.hasOwn(names, entry.binding) ? names[entry.binding] : entry.secret_name
|
|
67
70
|
}));
|
|
68
71
|
}
|
|
69
72
|
if (resolved.workflows) {
|
|
@@ -40,7 +40,7 @@ export type HostEnvProviderKind = z.infer<typeof HostEnvProviderKind>;
|
|
|
40
40
|
/**
|
|
41
41
|
* What provides one field. The whole point of the declaration: a report that says `BASE_URL is
|
|
42
42
|
* missing` sends a developer looking, and a report that says `Var BASE_URL in the host's
|
|
43
|
-
* wrangler.jsonc. Run pithy email provision
|
|
43
|
+
* wrangler.jsonc. Run pithy email provision.` ends the search.
|
|
44
44
|
*/
|
|
45
45
|
export declare const HostEnvProvider: z.ZodObject<{
|
|
46
46
|
kind: z.ZodEnum<{
|
package/dist/workflow/hostEnv.js
CHANGED
|
@@ -41,7 +41,7 @@ const HostEnvProviderKind = z.enum([
|
|
|
41
41
|
/**
|
|
42
42
|
* What provides one field. The whole point of the declaration: a report that says `BASE_URL is
|
|
43
43
|
* missing` sends a developer looking, and a report that says `Var BASE_URL in the host's
|
|
44
|
-
* wrangler.jsonc. Run pithy email provision
|
|
44
|
+
* wrangler.jsonc. Run pithy email provision.` ends the search.
|
|
45
45
|
*/
|
|
46
46
|
const HostEnvProvider = z.object({
|
|
47
47
|
kind: HostEnvProviderKind.describe("Which kind of thing fills this field."),
|
package/package.json
CHANGED
|
@@ -19,6 +19,7 @@ import type { LocaleContext } from "../i18n/locale";
|
|
|
19
19
|
import type { Translator } from "../i18n/translator";
|
|
20
20
|
import type { KvNamespaceSpecMap } from "../kv/namespaces";
|
|
21
21
|
import type { Logger } from "../logger/logger";
|
|
22
|
+
import { declareRetained } from "../migrations/retainedDeclaration";
|
|
22
23
|
import type { SeedSet } from "../seed/seed";
|
|
23
24
|
import type { WorkflowSpecMap } from "../workflow/spec";
|
|
24
25
|
import { BindingSpec, type BindingSpecInput } from "./bindings";
|
|
@@ -595,6 +596,11 @@ export function defineCapability<
|
|
|
595
596
|
const Name extends string = string,
|
|
596
597
|
const Workflows extends WorkflowSpecMap = Record<never, never>,
|
|
597
598
|
>(input: CapabilityInput<Databases, Namespaces, Name, Workflows>): Capability<Databases, Namespaces, Name, Workflows> {
|
|
599
|
+
// A retained table is declared on the `down`s that could drop it the moment the capability exists, so a
|
|
600
|
+
// migration the runner is handed carries it however its provider was built (#588).
|
|
601
|
+
for (const spec of Object.values(input.databases ?? {})) {
|
|
602
|
+
if (spec.migrations && spec.retained) declareRetained(spec.migrations, spec.retained);
|
|
603
|
+
}
|
|
598
604
|
return {
|
|
599
605
|
...input,
|
|
600
606
|
requiredBindings: input.requiredBindings.map((binding) => BindingSpec.parse(binding)),
|
|
@@ -112,8 +112,21 @@ export interface SettingsAccountReader {
|
|
|
112
112
|
d1Databases(): Promise<readonly string[]>;
|
|
113
113
|
/** Whether this account holds a Cloudflare zone covering the hostname — the prerequisite for onboarding it. */
|
|
114
114
|
zone(hostname: string): Promise<boolean>;
|
|
115
|
-
/**
|
|
116
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Whether that environment's **D1 vault** holds a value under this registry name — asked of its manager
|
|
117
|
+
* Worker, the only thing that can open it.
|
|
118
|
+
*
|
|
119
|
+
* Named for the store it asks, because it used to be `secret()` — "does this secret have a value" — and
|
|
120
|
+
* that question has two stores behind it (#596). A check asking it about a `cf-secrets-store` secret got a
|
|
121
|
+
* confident `false` from a vault the secret never lives in. Ask {@link storeEntry} about one of those.
|
|
122
|
+
*/
|
|
123
|
+
vaultSecret(request: { name: string; environment: string }): Promise<boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* Whether the account's **Secrets Store** holds an entry of exactly this name. The name is the entry's, not
|
|
126
|
+
* the registry's: compose it with `environmentScope(project, env).secretEntry(...)`, the namer provisioning
|
|
127
|
+
* created it with, so the check looks where the value went.
|
|
128
|
+
*/
|
|
129
|
+
storeEntry(name: string): Promise<boolean>;
|
|
117
130
|
}
|
|
118
131
|
|
|
119
132
|
/** What the account tier is told: everything the local tier gets, plus the account it may ask. */
|
package/src/data/databases.ts
CHANGED
|
@@ -29,6 +29,17 @@ export interface DatabaseSpec<Tables extends SchemaMap = SchemaMap> {
|
|
|
29
29
|
migrations?: Record<string, Migration>;
|
|
30
30
|
/** Sort order within this database relative to other capabilities (core low, app high). Required with `migrations`. */
|
|
31
31
|
migrationOrder?: number;
|
|
32
|
+
/**
|
|
33
|
+
* The tables in this slice whose rows **exist nowhere else** — a vault's sealed credentials, a
|
|
34
|
+
* suppression list of people who asked not to be mailed. Keys of `tables`, declared beside the
|
|
35
|
+
* `migrations` that create them (required with them).
|
|
36
|
+
*
|
|
37
|
+
* A schema `down` is right to drop a table; for these it destroys the only copy. So while one of them
|
|
38
|
+
* holds rows, no `down` runs against this database — a rollback, a `seed --redo` reset, a
|
|
39
|
+
* `remove --drop` — unless the operator passes the exact row count (`--destroy-retained <n>`). The
|
|
40
|
+
* refusal lives in the migration runner, so a new command that reverses migrations inherits it (#588).
|
|
41
|
+
*/
|
|
42
|
+
retained?: readonly (keyof Tables & string)[];
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
/** A capability's databases: database name → {@link DatabaseSpec}. */
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import type { Migration, MigrationProvider } from "kysely/migration";
|
|
5
5
|
import { InternalError } from "../error/pithyError";
|
|
6
|
+
import { declareRetained } from "./retainedDeclaration";
|
|
6
7
|
|
|
7
8
|
/** Width of the zero-padded `order` prefix in a composed key — the stable anchor. */
|
|
8
9
|
const ORDER_DIGITS = 4;
|
|
@@ -29,6 +30,12 @@ export interface NamespacedMigrations {
|
|
|
29
30
|
order: number;
|
|
30
31
|
/** Stable, per-namespace keys, each `^\d{4}_[a-z0-9_]+$` (e.g. "0001_init"). */
|
|
31
32
|
migrations: Record<string, Migration>;
|
|
33
|
+
/**
|
|
34
|
+
* The table keys (camelCase, as the capability's `tables` declares them) this set's capability retains
|
|
35
|
+
* in this database — rows that exist nowhere else. Recorded on this set's `down`s, and the runner refuses
|
|
36
|
+
* any `down` against the database while one of them holds rows. See `./retained`.
|
|
37
|
+
*/
|
|
38
|
+
retained?: readonly string[];
|
|
32
39
|
}
|
|
33
40
|
|
|
34
41
|
// These guard build-time registry invariants — a capability author wired its migrations wrong.
|
|
@@ -85,6 +92,9 @@ function composeDatabase(database: string, sets: NamespacedMigrations[]): Migrat
|
|
|
85
92
|
}
|
|
86
93
|
}
|
|
87
94
|
|
|
95
|
+
// Recorded on each migration's own `down`, so the runner enforces it on this provider and on anything
|
|
96
|
+
// later built from these same migrations — no caller has to remember to pass it along (#588).
|
|
97
|
+
for (const set of sets) declareRetained(set.migrations, set.retained ?? []);
|
|
88
98
|
return {
|
|
89
99
|
getMigrations: async (): Promise<Record<string, Migration>> => composed,
|
|
90
100
|
};
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
5
|
+
import { CamelCasePlugin } from "kysely";
|
|
6
|
+
import type { Migration, MigrationProvider } from "kysely/migration";
|
|
7
|
+
import { ValidationError } from "../error/pithyError";
|
|
8
|
+
import { declareRetainedDown, retainedTablesOf, retainedTablesOfDown } from "./retainedDeclaration";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* **A retained table holds rows that exist nowhere else (#588).**
|
|
12
|
+
*
|
|
13
|
+
* A migration's `down` is the tested inverse of its `up`, and for schema that is exactly right. For a
|
|
14
|
+
* vault it is a `drop table` on the only copy of a credential Google issued. `0100_secrets_0001_init` is
|
|
15
|
+
* the whole history of the secrets database, so one `pithy migrate --rollback` — which steps back one
|
|
16
|
+
* migration in every composed database — emptied it in place, on staging, with nothing said first.
|
|
17
|
+
*
|
|
18
|
+
* So a capability **declares** a table retained, beside the migrations that create it
|
|
19
|
+
* (`DatabaseSpec.retained`), and the declaration is recorded on those migrations' own `down` functions
|
|
20
|
+
* (`./retainedDeclaration`) — by `defineCapability`, and again by `createMigrationRegistry` for a set that
|
|
21
|
+
* carries it. The refusal is on the operation that runs a `down`, not on a list of commands:
|
|
22
|
+
* {@link guardRetained} wraps every `down` a provider yields, and the runner applies it to every provider
|
|
23
|
+
* it is handed.
|
|
24
|
+
*
|
|
25
|
+
* ## What is TRUE
|
|
26
|
+
*
|
|
27
|
+
* **No `down` runs against a database while a table declared retained in that database holds rows**,
|
|
28
|
+
* beyond what the caller's {@link RetainedBudget} agreed to destroy. A count, not a boolean, so a flag
|
|
29
|
+
* somebody has in their shell history for another database does not also authorize this one — and one
|
|
30
|
+
* budget spent across a whole run, so an agreement to five rows cannot destroy five in every database.
|
|
31
|
+
*
|
|
32
|
+
* The budget is the operator's number and nothing else. The CLI's preflight also demands it *equal* the
|
|
33
|
+
* rows in scope; this floor demands only that no more are spent, and it is deliberately not handed the
|
|
34
|
+
* preflight's count — a guard fed the answer by the check above it is not a second guard.
|
|
35
|
+
*
|
|
36
|
+
* ## Its reach, stated so it can be checked
|
|
37
|
+
*
|
|
38
|
+
* - **Database-wide, deliberately.** A `down` cannot be inspected for which tables it drops without running
|
|
39
|
+
* it, so any `down` against a database holding retained rows is refused — including a migration of a
|
|
40
|
+
* different capability that never touches them. Over-refusing costs an override; under-refusing costs a
|
|
41
|
+
* vault.
|
|
42
|
+
* - **Every `down` through `runner.ts`** — `rollbackMigration`, `resetMigrations`, `dropMigrations` — whose
|
|
43
|
+
* function was declared: the registry's providers, the fan-out's merged ones, and a provider written by
|
|
44
|
+
* hand, so long as it yields the capability's own migration objects (or spreads of them).
|
|
45
|
+
* - **Counted over the set the runner is handed as the database.** Rollback and reset are handed one
|
|
46
|
+
* provider, and Kysely refuses one that does not span the ledger, so it is the database's. A drop reverses a
|
|
47
|
+
* part, and is handed the part and the database separately (`DropSelection`): it counts the database's,
|
|
48
|
+
* and refuses to reverse a migration the database's set does not carry. The CLI's `remove --drop` builds
|
|
49
|
+
* that set from the Worker's whole composition and every Worker discovered beside it — a capability sharing
|
|
50
|
+
* `SECRETS` and declaring nothing is refused while the vault holds rows.
|
|
51
|
+
* - **Not seen:** a drop whose caller hands the part as the database too (`{ database: part, reverse: part }`
|
|
52
|
+
* counts only the part); for `remove --drop`, a table declared only by a Worker whose config does not load;
|
|
53
|
+
* a Kysely `Migrator` constructed outside `runner.ts`; a `down` whose capability was never constructed in
|
|
54
|
+
* this process and whose set never passed through the registry; a `down` re-wrapped in a new function by
|
|
55
|
+
* anything but {@link beforeEachDown} or {@link beforeEachMigration}; and a table no capability declares — a
|
|
56
|
+
* capability removed from the config takes its declaration with it.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/** The snake_case the `CamelCasePlugin` writes, read from the plugin itself so there is one definition. */
|
|
60
|
+
class TableNames extends CamelCasePlugin {
|
|
61
|
+
sqlName(key: string): string {
|
|
62
|
+
return this.snakeCase(key);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const NAMES = new TableNames();
|
|
67
|
+
|
|
68
|
+
/** The SQL name of a declared table key — `pithySecretsSystemSecrets` → `pithy_secrets_system_secrets`. */
|
|
69
|
+
export function retainedTableName(key: string): string {
|
|
70
|
+
return NAMES.sqlName(key);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One retained table that holds rows, on the binding it was counted through. */
|
|
74
|
+
export interface RetainedRows {
|
|
75
|
+
/** The D1 binding the table lives behind — the name an adopter recognizes. */
|
|
76
|
+
binding: string;
|
|
77
|
+
/** The SQL table name. */
|
|
78
|
+
table: string;
|
|
79
|
+
/** How many rows it holds. Always positive: an empty or absent table is not at risk. */
|
|
80
|
+
rows: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The SQL names of the retained tables a provider's migrations declare. Reads no database. */
|
|
84
|
+
export async function retainedTableNames(provider: MigrationProvider): Promise<string[]> {
|
|
85
|
+
return retainedTablesOf(await provider.getMigrations()).map(retainedTableName);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Count the rows in every retained table these migrations declare that is present in the database.
|
|
90
|
+
* Read-only. Returns only the tables holding rows, so an empty result means nothing retained is at risk.
|
|
91
|
+
*/
|
|
92
|
+
export async function countRetainedRows(
|
|
93
|
+
database: D1Database,
|
|
94
|
+
migrations: Readonly<Record<string, Migration>>,
|
|
95
|
+
binding: string,
|
|
96
|
+
): Promise<RetainedRows[]> {
|
|
97
|
+
const counted: RetainedRows[] = [];
|
|
98
|
+
for (const key of retainedTablesOf(migrations)) {
|
|
99
|
+
const table = retainedTableName(key);
|
|
100
|
+
const present = await database
|
|
101
|
+
.prepare("select name from sqlite_master where type = 'table' and name = ?")
|
|
102
|
+
.bind(table)
|
|
103
|
+
.first<{ name: string }>();
|
|
104
|
+
if (!present) continue;
|
|
105
|
+
// The name came from a capability's declaration and through the plugin's own snake-casing, never from
|
|
106
|
+
// input, and it has just been matched against `sqlite_master` — quoting it is belt, not braces.
|
|
107
|
+
const row = await database.prepare(`select count(*) as n from "${table}"`).first<{ n: number }>();
|
|
108
|
+
const rows = row?.n ?? 0;
|
|
109
|
+
if (rows > 0) counted.push({ binding, table, rows });
|
|
110
|
+
}
|
|
111
|
+
return counted;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The refusals raised ahead of a `down`. A `Migrator` catches whatever a `down` throws and hands it back as
|
|
116
|
+
* a result, and the runner turns that into "Couldn't roll back" with a remedy to fix the migration — which
|
|
117
|
+
* is wrong for a refusal, where no migration is broken. So the runner asks {@link isDownRefusal} and
|
|
118
|
+
* rethrows one unchanged.
|
|
119
|
+
*/
|
|
120
|
+
const REFUSALS = new WeakSet<object>();
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Mark an error as a refusal to run a `down`, so it reaches the operator as itself rather than as a failed
|
|
124
|
+
* migration. For a guard written outside this module — the CLI's shared-database floor is one.
|
|
125
|
+
*/
|
|
126
|
+
export function downRefusal<E extends ValidationError>(error: E): E {
|
|
127
|
+
REFUSALS.add(error);
|
|
128
|
+
return error;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Whether `error` refused a `down` before it ran — as opposed to a migration that failed. */
|
|
132
|
+
export function isDownRefusal(error: unknown): error is ValidationError {
|
|
133
|
+
return typeof error === "object" && error !== null && REFUSALS.has(error);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** `5 rows`, `1 row`. */
|
|
137
|
+
function rowsLabel(count: number): string {
|
|
138
|
+
return `${count} row${count === 1 ? "" : "s"}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* What a refusal was raised ahead of, as the sentence ends: `Refused before <this>.` A `down` for the
|
|
143
|
+
* runner and the migration fan-out; a database deletion for a teardown that removes the database whole
|
|
144
|
+
* (#591), where "any down ran" would name an operation that was never going to happen.
|
|
145
|
+
*/
|
|
146
|
+
export type RefusedBefore = "any down against them ran" | "anything was deleted";
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The refusal, in one wording for every place that raises it — the fan-out's preflight, which names every
|
|
150
|
+
* database before any of them moves, {@link guardRetained}, which is the floor under it, and a teardown
|
|
151
|
+
* that deletes a database holding retained tables (`pithy secrets deprovision`, #591).
|
|
152
|
+
*
|
|
153
|
+
* `consent` is what the caller passed. The action always prints the number that would work, because that
|
|
154
|
+
* number is the one fact the operator has to read before typing it.
|
|
155
|
+
*/
|
|
156
|
+
export function retainedRefusal(
|
|
157
|
+
atRisk: readonly RetainedRows[],
|
|
158
|
+
consent: number | undefined,
|
|
159
|
+
before: RefusedBefore = "any down against them ran",
|
|
160
|
+
): ValidationError {
|
|
161
|
+
return downRefusal(refusalFor(atRisk, consent, before));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* **The preflight: refuse unless the operator counted exactly the retained rows in scope.**
|
|
166
|
+
*
|
|
167
|
+
* Absent a count, only an empty scope passes; given one, only the same number does — a count that is too
|
|
168
|
+
* high is a count of something else, and agreeing to it would let a stale number from shell history through
|
|
169
|
+
* the day the rows it described are joined by more. Call it with every database the operation will touch,
|
|
170
|
+
* counted before the first of them moves, so the refusal names them all with nothing changed.
|
|
171
|
+
*
|
|
172
|
+
* The floor under it is {@link RetainedBudget}, fed the operator's number and never this function's
|
|
173
|
+
* count.
|
|
174
|
+
*/
|
|
175
|
+
export function assertRetainedAgreed(
|
|
176
|
+
atRisk: readonly RetainedRows[],
|
|
177
|
+
consent: number | undefined,
|
|
178
|
+
before: RefusedBefore = "any down against them ran",
|
|
179
|
+
): void {
|
|
180
|
+
const total = atRisk.reduce((sum, entry) => sum + entry.rows, 0);
|
|
181
|
+
const agreed = consent === undefined ? total === 0 : consent === total;
|
|
182
|
+
if (!agreed) throw retainedRefusal(atRisk, consent, before);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function refusalFor(
|
|
186
|
+
atRisk: readonly RetainedRows[],
|
|
187
|
+
consent: number | undefined,
|
|
188
|
+
before: RefusedBefore,
|
|
189
|
+
): ValidationError {
|
|
190
|
+
const total = atRisk.reduce((sum, entry) => sum + entry.rows, 0);
|
|
191
|
+
const named = atRisk.map((entry) => `${entry.table} on ${entry.binding} (${rowsLabel(entry.rows)})`).join(", ");
|
|
192
|
+
const mismatch =
|
|
193
|
+
consent === undefined ? "" : `--destroy-retained ${consent} does not match the ${rowsLabel(total)} at risk. `;
|
|
194
|
+
if (total === 0) {
|
|
195
|
+
return new ValidationError({
|
|
196
|
+
message: `${mismatch}Nothing retained is at risk. Refused before ${before === "anything was deleted" ? before : "any down ran"}.`,
|
|
197
|
+
action: "Run it again without --destroy-retained.",
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return new ValidationError({
|
|
201
|
+
message: `${mismatch}Retained ${rowsLabel(total)} would be dropped: ${named}. Refused before ${before}.`,
|
|
202
|
+
action: `They exist nowhere else. Back them up, or pass --destroy-retained ${total} to drop them.`,
|
|
203
|
+
detail:
|
|
204
|
+
"A retained table is declared by its capability. Any down against its database, and any deletion of the database, is refused while it holds rows.",
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* **How many retained rows a run has agreed to destroy, spent as each database is reached.**
|
|
210
|
+
*
|
|
211
|
+
* One per run, shared by every database in it. Absent an agreement it holds nothing, so any retained row
|
|
212
|
+
* refuses. A database whose rows exceed what is left is refused whole — nothing is spent on a refusal.
|
|
213
|
+
*/
|
|
214
|
+
export class RetainedBudget {
|
|
215
|
+
/** What the caller agreed to, as they typed it — the number a refusal quotes back. */
|
|
216
|
+
readonly agreed: number | undefined;
|
|
217
|
+
#remaining: number;
|
|
218
|
+
|
|
219
|
+
constructor(agreed: number | undefined) {
|
|
220
|
+
this.agreed = agreed;
|
|
221
|
+
this.#remaining = agreed ?? 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Spend `rows` if the budget covers them. False, and nothing spent, when it does not. */
|
|
225
|
+
spend(rows: number): boolean {
|
|
226
|
+
if (rows > this.#remaining) return false;
|
|
227
|
+
this.#remaining -= rows;
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Wrap a provider so no `down` it yields runs while the database holds retained rows the budget does not
|
|
234
|
+
* cover. The check runs once, at the first `down`, and its answer stands for the rest of the run: a reset
|
|
235
|
+
* whose first `down` dropped the vault must not refuse its second for finding it empty.
|
|
236
|
+
*
|
|
237
|
+
* Counted at the `down`, not before: a row written after somebody else counted is a row this refuses.
|
|
238
|
+
*/
|
|
239
|
+
export function guardRetained(
|
|
240
|
+
provider: MigrationProvider,
|
|
241
|
+
database: D1Database,
|
|
242
|
+
options: { binding: string; budget: RetainedBudget },
|
|
243
|
+
): MigrationProvider {
|
|
244
|
+
let cleared: Promise<void> | undefined;
|
|
245
|
+
const check = async (): Promise<void> => {
|
|
246
|
+
const atRisk = await countRetainedRows(database, await provider.getMigrations(), options.binding);
|
|
247
|
+
const total = atRisk.reduce((sum, entry) => sum + entry.rows, 0);
|
|
248
|
+
if (!options.budget.spend(total)) throw retainedRefusal(atRisk, options.budget.agreed);
|
|
249
|
+
};
|
|
250
|
+
return beforeEachDown(provider, async () => {
|
|
251
|
+
cleared ??= check();
|
|
252
|
+
await cleared;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Which way one migration is about to move — Kysely's own spelling, as a `MigrationResult` carries it. */
|
|
257
|
+
export type MigrationDirection = "Up" | "Down";
|
|
258
|
+
|
|
259
|
+
/** Called ahead of one migration's body, with its composed name and the direction it is about to move. */
|
|
260
|
+
export type BeforeMigration = (name: string, direction: MigrationDirection) => Promise<void>;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Wrap a provider so `before` runs ahead of every migration body it yields, in each of `directions` — and a
|
|
264
|
+
* throw from `before` means that body never runs. A direction not listed passes through untouched.
|
|
265
|
+
*
|
|
266
|
+
* **Each new `down` keeps the declaration of the one it wraps.** That is the reason this is the one way to
|
|
267
|
+
* wrap a provider's migrations: a hand-rolled wrapper is a new function the declaration has never heard of,
|
|
268
|
+
* and {@link guardRetained} handed it would find nothing declared and let every `down` through.
|
|
269
|
+
*/
|
|
270
|
+
function wrapEach(
|
|
271
|
+
provider: MigrationProvider,
|
|
272
|
+
before: BeforeMigration,
|
|
273
|
+
directions: readonly MigrationDirection[],
|
|
274
|
+
): MigrationProvider {
|
|
275
|
+
return {
|
|
276
|
+
getMigrations: async (): Promise<Record<string, Migration>> => {
|
|
277
|
+
const migrations = await provider.getMigrations();
|
|
278
|
+
return Object.fromEntries(
|
|
279
|
+
Object.entries(migrations).map(([name, migration]) => {
|
|
280
|
+
const { up, down } = migration;
|
|
281
|
+
const wrappedUp = directions.includes("Up")
|
|
282
|
+
? async (db: Parameters<typeof up>[0]): Promise<void> => {
|
|
283
|
+
await before(name, "Up");
|
|
284
|
+
await up(db);
|
|
285
|
+
}
|
|
286
|
+
: up;
|
|
287
|
+
if (!down || !directions.includes("Down")) return [name, { ...migration, up: wrappedUp }];
|
|
288
|
+
const wrappedDown = async (db: Parameters<typeof down>[0]): Promise<void> => {
|
|
289
|
+
await before(name, "Down");
|
|
290
|
+
await down(db);
|
|
291
|
+
};
|
|
292
|
+
declareRetainedDown(wrappedDown, retainedTablesOfDown(down));
|
|
293
|
+
const wrapped: Migration = { up: wrappedUp, down: wrappedDown };
|
|
294
|
+
return [name, wrapped];
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Wrap a provider so `before` runs ahead of every `down` it yields — and a throw from `before` means the
|
|
303
|
+
* `down` never runs. Ups pass through untouched. A declared `down` stays declared: see {@link wrapEach}.
|
|
304
|
+
*/
|
|
305
|
+
export function beforeEachDown(provider: MigrationProvider, before: () => Promise<void>): MigrationProvider {
|
|
306
|
+
return wrapEach(provider, () => before(), ["Down"]);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Wrap a provider so `before` hears each migration's name and direction ahead of its body, up or down (#583).
|
|
311
|
+
*
|
|
312
|
+
* The seam a caller uses to say which migration it is on while it is on it: the CLI raises a `▸` step here,
|
|
313
|
+
* because a remote migration is a D1 round trip per statement and an operator watching nothing happen
|
|
314
|
+
* cannot tell a slow schema change from a hung one. Core carries no terminal concern — it hands over the
|
|
315
|
+
* name and the direction, and what is done with them is the caller's. A declared `down` stays declared.
|
|
316
|
+
*/
|
|
317
|
+
export function beforeEachMigration(provider: MigrationProvider, before: BeforeMigration): MigrationProvider {
|
|
318
|
+
return wrapEach(provider, before, ["Up", "Down"]);
|
|
319
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { Migration } from "kysely/migration";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* **Where a retained declaration is kept: on the `down` that would destroy the table (#588).**
|
|
8
|
+
*
|
|
9
|
+
* A capability says which of its tables are retained (`DatabaseSpec.retained`). The runner has to know it
|
|
10
|
+
* at the moment a `down` is about to run, and by then all it holds is a `MigrationProvider` — Kysely's
|
|
11
|
+
* interface, rebuilt by the registry, merged by the fan-out, wrapped by `batchedProvider`, and written by
|
|
12
|
+
* hand in every capability's own migration tests. A declaration kept on any of those objects is lost the
|
|
13
|
+
* first time somebody builds a new one.
|
|
14
|
+
*
|
|
15
|
+
* The `down` function survives all of that. A registry keeps the capability's migration object; a spread
|
|
16
|
+
* that rebuilds the record keeps the same `down`; a hand-built provider in a test names the very
|
|
17
|
+
* `secrets_0001_init` the capability ships. So the declaration is keyed on the function itself, and any
|
|
18
|
+
* provider that yields it carries it.
|
|
19
|
+
*
|
|
20
|
+
* Kept apart from `./retained` so `defineCapability` can declare without importing Kysely's runtime.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** The destructive function → the retained table keys (camelCase, as `tables` declares them) it can drop. */
|
|
24
|
+
const DECLARED = new WeakMap<object, readonly string[]>();
|
|
25
|
+
|
|
26
|
+
/** Record that every `down` in `migrations` runs against a database holding these retained tables. */
|
|
27
|
+
export function declareRetained(migrations: Readonly<Record<string, Migration>>, tables: readonly string[]): void {
|
|
28
|
+
if (tables.length === 0) return;
|
|
29
|
+
for (const migration of Object.values(migrations)) {
|
|
30
|
+
if (migration.down) declareRetainedDown(migration.down, tables);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Record one `down` — for a wrapper that replaces a declared `down` with its own function. */
|
|
35
|
+
export function declareRetainedDown(down: object, tables: readonly string[]): void {
|
|
36
|
+
if (tables.length === 0) return;
|
|
37
|
+
DECLARED.set(down, [...new Set([...(DECLARED.get(down) ?? []), ...tables])]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The retained table keys declared on one `down`. */
|
|
41
|
+
export function retainedTablesOfDown(down: object): readonly string[] {
|
|
42
|
+
return DECLARED.get(down) ?? [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The retained table keys any migration in a set declares — the database-wide answer, since a `down` is
|
|
47
|
+
* refused for rows in *any* retained table of the database it runs against.
|
|
48
|
+
*/
|
|
49
|
+
export function retainedTablesOf(migrations: Readonly<Record<string, Migration>>): string[] {
|
|
50
|
+
const tables = new Set<string>();
|
|
51
|
+
for (const migration of Object.values(migrations)) {
|
|
52
|
+
if (!migration.down) continue;
|
|
53
|
+
for (const table of retainedTablesOfDown(migration.down)) tables.add(table);
|
|
54
|
+
}
|
|
55
|
+
return [...tables].sort();
|
|
56
|
+
}
|