@wervt/platform 0.0.8 → 0.0.9

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/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { DestructiveAllowance, DestructiveChange, DestructiveMigrationError, DestructiveStatement, MigrationOp, TableName, allowDropArgs, checkDestructiveMigrations, destructiveStatements, migrationOps, parseAllowDrop, pendingMigrations } from "./migration-guard.js";
1
2
  import { APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, POOLER_ROLE, ProvisionOptions, RoleTimeouts, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
2
- import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, ObjectDetails, ObjectListing, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
3
- export { ABANDONED_UPLOAD_GRACE_MS, APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, MAX_KEY_BYTES, ObjectDetails, ObjectListing, POOLER_ROLE, ProvisionOptions, RoleTimeouts, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
3
+ import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, ObjectDetails, ObjectListing, QuotaError, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
4
+ export { ABANDONED_UPLOAD_GRACE_MS, APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, DestructiveAllowance, DestructiveChange, DestructiveMigrationError, DestructiveStatement, EXTENSIONS_SCHEMA, MAX_KEY_BYTES, MigrationOp, ObjectDetails, ObjectListing, POOLER_ROLE, ProvisionOptions, QuotaError, RoleTimeouts, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, TableName, allowDropArgs, appDatabaseUrl, checkDestructiveMigrations, companionKeyOf, createStorage, dbPassword, dbRole, destructiveStatements, dropDatabase, hmac, metaKeyOf, migrateAs, migrationOps, parseAllowDrop, parseDuration, parseObjectList, pendingMigrations, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  import { APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
2
- import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
3
- export { ABANDONED_UPLOAD_GRACE_MS, APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, MAX_KEY_BYTES, POOLER_ROLE, StorageError, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
2
+ import { DestructiveMigrationError, allowDropArgs, checkDestructiveMigrations, destructiveStatements, migrationOps, parseAllowDrop, pendingMigrations } from "./migration-guard.js";
3
+ import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, QuotaError, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
4
+ export { ABANDONED_UPLOAD_GRACE_MS, APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, DestructiveMigrationError, EXTENSIONS_SCHEMA, MAX_KEY_BYTES, POOLER_ROLE, QuotaError, StorageError, allowDropArgs, appDatabaseUrl, checkDestructiveMigrations, companionKeyOf, createStorage, dbPassword, dbRole, destructiveStatements, dropDatabase, hmac, metaKeyOf, migrateAs, migrationOps, parseAllowDrop, parseDuration, parseObjectList, pendingMigrations, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
@@ -0,0 +1,103 @@
1
+ import postgres from "postgres";
2
+ /** What a migration statement does to tables that may hold data. */
3
+ export type MigrationOp = {
4
+ kind: 'create-table';
5
+ table: TableName;
6
+ ifNotExists: boolean;
7
+ } | {
8
+ kind: 'rename-table';
9
+ table: TableName;
10
+ to: string;
11
+ } | {
12
+ kind: 'drop-table' | 'truncate' | 'delete-all';
13
+ table: TableName;
14
+ } | {
15
+ kind: 'drop-column';
16
+ table: TableName;
17
+ column: string;
18
+ } | {
19
+ kind: 'drop-schema';
20
+ schema: string;
21
+ };
22
+ export interface TableName {
23
+ /** Undefined when unqualified: the app's own schema, first on its search_path. */
24
+ schema?: string;
25
+ name: string;
26
+ }
27
+ /** What each statement of a migration does to tables, in order. */
28
+ export declare function migrationOps(sql: string): MigrationOp[];
29
+ /** A destructive statement in the pending migrations, before row counts. */
30
+ export interface DestructiveStatement {
31
+ kind: 'drop-table' | 'truncate' | 'delete-all' | 'drop-column' | 'drop-schema';
32
+ /** Postgres schema the data is in. */
33
+ schema: string;
34
+ /** Undefined for `drop-schema`, which takes all of the schema's tables. */
35
+ table?: string;
36
+ column?: string;
37
+ /** The migration's folder name. */
38
+ migration: string;
39
+ }
40
+ /**
41
+ * The destructive statements of `migrations` (pending ones, oldest first) that hit data that
42
+ * exists before they run. A table the same batch creates starts empty, so dropping it or its
43
+ * columns loses nothing; a table the batch renames is tracked back to its old name.
44
+ */
45
+ export declare function destructiveStatements(migrations: {
46
+ name: string;
47
+ sql: string;
48
+ }[], schema: string): DestructiveStatement[];
49
+ /**
50
+ * The migrations Drizzle's migrator would run, oldest first: local ones whose name isn't in the
51
+ * schema's `__drizzle_migrations` yet. A journal from before drizzle-orm 1.0 has no names; its
52
+ * rows are matched by timestamp and hash, like the migrator's own upgrade does.
53
+ */
54
+ export declare function pendingMigrations(sql: postgres.Sql, schema: string, migrationsFolder: string): Promise<{
55
+ name: string;
56
+ sql: string;
57
+ }[]>;
58
+ /** A destructive statement with the rows it would take. */
59
+ export interface DestructiveChange extends DestructiveStatement {
60
+ /** Rows in the table (all of the schema's tables for `drop-schema`) right now. */
61
+ rows: number;
62
+ /** What `--allow-drop` names it by: the table, or `schema.table` outside the app's schema. */
63
+ key: string;
64
+ /** The count an `--allow-drop` gave for it, when that no longer matches. */
65
+ allowed?: number;
66
+ }
67
+ export interface DestructiveAllowance {
68
+ /** Lets every destructive statement through. */
69
+ all?: boolean;
70
+ /** Lets a table's statements through while it still holds exactly this many rows. */
71
+ drop?: Record<string, number>;
72
+ }
73
+ /**
74
+ * The pending migrations' destructive statements that would take rows, and those of them
75
+ * `allow` doesn't let through. Runs no migration and changes nothing, so a deploy can stop
76
+ * before anything happened.
77
+ */
78
+ export declare function checkDestructiveMigrations({ app, adminUrl, migrationsFolder, allow }: {
79
+ app: string;
80
+ adminUrl: string;
81
+ migrationsFolder: string;
82
+ allow?: DestructiveAllowance;
83
+ }): Promise<{
84
+ changes: DestructiveChange[];
85
+ blocked: DestructiveChange[];
86
+ }>;
87
+ /** `--allow-drop` arguments that let exactly these changes through, one per table. */
88
+ export declare function allowDropArgs(changes: DestructiveChange[]): string[];
89
+ /** Thrown by a deploy whose migrations would take rows nobody allowed it to. */
90
+ export declare class DestructiveMigrationError extends Error {
91
+ readonly changes: DestructiveChange[];
92
+ readonly allowDrop: string[];
93
+ readonly code = "destructive-migration";
94
+ constructor(changes: DestructiveChange[], allowDrop?: string[]);
95
+ /** The fields the deploy's progress stream sends with the error. */
96
+ details(): {
97
+ code: string;
98
+ destructive: DestructiveChange[];
99
+ allowDrop: string[];
100
+ };
101
+ }
102
+ /** Parses `--allow-drop` values (`users=12`), throwing on anything else. */
103
+ export declare function parseAllowDrop(values: string[]): Record<string, number>;
@@ -0,0 +1,447 @@
1
+ import { dbRole } from "./provision.js";
2
+ import { readMigrationFiles } from "drizzle-orm/migrator";
3
+ import postgres from "postgres";
4
+ // src/migration-guard.ts
5
+ const BREAKPOINT = "--> statement-breakpoint";
6
+ /**
7
+ * Splits SQL into tokens. Comments, string literals and dollar-quoted bodies never count as SQL,
8
+ * so a `DROP TABLE` in a comment or a column default doesn't trip the guard. `end` marks a
9
+ * statement's end: a `;` or drizzle-kit's breakpoint marker.
10
+ */
11
+ function tokenize(sql) {
12
+ const tokens = [];
13
+ let i = 0;
14
+ const n = sql.length;
15
+ while (i < n) {
16
+ const c = sql[i];
17
+ if (/\s/.test(c)) i++;
18
+ else if (c === "-" && sql[i + 1] === "-") {
19
+ if (sql.startsWith(BREAKPOINT, i)) tokens.push({ type: "end" });
20
+ const nl = sql.indexOf("\n", i);
21
+ i = nl < 0 ? n : nl + 1;
22
+ } else if (c === "/" && sql[i + 1] === "*") {
23
+ let depth = 1;
24
+ i += 2;
25
+ while (i < n && depth) {
26
+ const pair = sql.slice(i, i + 2);
27
+ if (pair === "/*" || pair === "*/") {
28
+ depth += pair === "/*" ? 1 : -1;
29
+ i += 2;
30
+ } else i++;
31
+ }
32
+ } else if (c === "'") {
33
+ const escapes = /^[eE]$/.test(sql[i - 1] ?? "") && !/[\w$]/.test(sql[i - 2] ?? "");
34
+ if (escapes) tokens.pop();
35
+ i++;
36
+ while (i < n) if (escapes && sql[i] === "\\") i += 2;
37
+ else if (sql[i] === "'" && sql[i + 1] === "'") i += 2;
38
+ else if (sql[i] === "'") break;
39
+ else i++;
40
+ i++;
41
+ tokens.push({ type: "string" });
42
+ } else if (c === "\"") {
43
+ let value = "";
44
+ i++;
45
+ while (i < n) if (sql[i] === "\"" && sql[i + 1] === "\"") {
46
+ value += "\"";
47
+ i += 2;
48
+ } else if (sql[i] === "\"") break;
49
+ else value += sql[i++];
50
+ i++;
51
+ tokens.push({
52
+ type: "ident",
53
+ value
54
+ });
55
+ } else if (c === "$" && /^\$(?:[A-Za-z_][\w]*)?\$/.test(sql.slice(i, i + 64))) {
56
+ const tag = /^\$(?:[A-Za-z_][\w]*)?\$/.exec(sql.slice(i))[0];
57
+ const close = sql.indexOf(tag, i + tag.length);
58
+ i = close < 0 ? n : close + tag.length;
59
+ tokens.push({ type: "string" });
60
+ } else if (/[A-Za-z_]/.test(c)) {
61
+ const word = /^[A-Za-z_][\w$]*/.exec(sql.slice(i))[0];
62
+ i += word.length;
63
+ tokens.push({
64
+ type: "word",
65
+ value: word.toLowerCase()
66
+ });
67
+ } else if (c === ";") {
68
+ tokens.push({ type: "end" });
69
+ i++;
70
+ } else {
71
+ tokens.push({
72
+ type: "punct",
73
+ value: c
74
+ });
75
+ i++;
76
+ }
77
+ }
78
+ return tokens;
79
+ }
80
+ function statements(sql) {
81
+ const result = [];
82
+ let current = [];
83
+ for (const token of tokenize(sql)) if (token.type === "end") {
84
+ if (current.length) result.push(current);
85
+ current = [];
86
+ } else current.push(token);
87
+ if (current.length) result.push(current);
88
+ return result;
89
+ }
90
+ var Reader = class {
91
+ tokens;
92
+ i = 0;
93
+ constructor(tokens) {
94
+ this.tokens = tokens;
95
+ }
96
+ peek(offset = 0) {
97
+ return this.tokens[this.i + offset];
98
+ }
99
+ /** Consumes the keywords when they come next, in order. */
100
+ accept(...words) {
101
+ for (const [k, word] of words.entries()) {
102
+ const token = this.peek(k);
103
+ if (token?.type !== "word" || token.value !== word) return false;
104
+ }
105
+ this.i += words.length;
106
+ return true;
107
+ }
108
+ acceptPunct(value) {
109
+ const token = this.peek();
110
+ if (token?.type !== "punct" || token.value !== value) return false;
111
+ this.i++;
112
+ return true;
113
+ }
114
+ identifier() {
115
+ const token = this.peek();
116
+ if (token?.type !== "word" && token?.type !== "ident") return void 0;
117
+ this.i++;
118
+ return token.value;
119
+ }
120
+ /** `name`, `schema.name` or `"Schema"."Name"`. */
121
+ tableName() {
122
+ const first = this.identifier();
123
+ if (first === void 0) return void 0;
124
+ if (!this.acceptPunct(".")) return { name: first };
125
+ const second = this.identifier();
126
+ return second === void 0 ? void 0 : {
127
+ schema: first,
128
+ name: second
129
+ };
130
+ }
131
+ /** Comma-separated names, as DROP TABLE and TRUNCATE take them. */
132
+ tableNames() {
133
+ const names = [];
134
+ do {
135
+ this.accept("only");
136
+ const name = this.tableName();
137
+ if (!name) break;
138
+ names.push(name);
139
+ this.acceptPunct("*");
140
+ } while (this.acceptPunct(","));
141
+ return names;
142
+ }
143
+ done() {
144
+ return this.i >= this.tokens.length;
145
+ }
146
+ /** The rest of the statement split at commas outside parentheses, like ALTER TABLE's actions. */
147
+ splitRest() {
148
+ const parts = [[]];
149
+ let depth = 0;
150
+ for (const token of this.tokens.slice(this.i)) {
151
+ if (token.type === "punct" && token.value === "(") depth++;
152
+ if (token.type === "punct" && token.value === ")") depth--;
153
+ if (token.type === "punct" && token.value === "," && depth === 0) parts.push([]);
154
+ else parts.at(-1).push(token);
155
+ }
156
+ return parts;
157
+ }
158
+ /** Whether `word` appears in the rest of the statement outside parentheses. */
159
+ hasTopLevel(word) {
160
+ let depth = 0;
161
+ for (const token of this.tokens.slice(this.i)) {
162
+ if (token.type === "punct" && token.value === "(") depth++;
163
+ if (token.type === "punct" && token.value === ")") depth--;
164
+ if (depth === 0 && token.type === "word" && token.value === word) return true;
165
+ }
166
+ return false;
167
+ }
168
+ };
169
+ function statementOps(tokens) {
170
+ const r = new Reader(tokens);
171
+ if (r.accept("drop", "table")) {
172
+ r.accept("if", "exists");
173
+ return r.tableNames().map((table) => ({
174
+ kind: "drop-table",
175
+ table
176
+ }));
177
+ }
178
+ if (r.accept("drop", "schema")) {
179
+ r.accept("if", "exists");
180
+ const schemas = [];
181
+ do {
182
+ const schema = r.identifier();
183
+ if (schema === void 0) break;
184
+ schemas.push(schema);
185
+ } while (r.acceptPunct(","));
186
+ return schemas.map((schema) => ({
187
+ kind: "drop-schema",
188
+ schema
189
+ }));
190
+ }
191
+ if (r.accept("truncate")) {
192
+ r.accept("table");
193
+ return r.tableNames().map((table) => ({
194
+ kind: "truncate",
195
+ table
196
+ }));
197
+ }
198
+ if (r.accept("delete", "from")) {
199
+ r.accept("only");
200
+ const table = r.tableName();
201
+ if (!table || r.hasTopLevel("where")) return [];
202
+ return [{
203
+ kind: "delete-all",
204
+ table
205
+ }];
206
+ }
207
+ if (r.accept("create")) {
208
+ for (const modifier of [
209
+ "global",
210
+ "local",
211
+ "temporary",
212
+ "temp",
213
+ "unlogged"
214
+ ]) r.accept(modifier);
215
+ if (!r.accept("table")) return [];
216
+ const ifNotExists = r.accept("if", "not", "exists");
217
+ const table = r.tableName();
218
+ return table ? [{
219
+ kind: "create-table",
220
+ table,
221
+ ifNotExists
222
+ }] : [];
223
+ }
224
+ if (r.accept("alter", "table")) {
225
+ r.accept("if", "exists");
226
+ r.accept("only");
227
+ const table = r.tableName();
228
+ if (!table) return [];
229
+ r.acceptPunct("*");
230
+ if (r.accept("rename", "to")) {
231
+ const to = r.identifier();
232
+ return to === void 0 ? [] : [{
233
+ kind: "rename-table",
234
+ table,
235
+ to
236
+ }];
237
+ }
238
+ const ops = [];
239
+ for (const action of r.splitRest()) {
240
+ const a = new Reader(action);
241
+ if (!a.accept("drop")) continue;
242
+ if (a.accept("constraint")) continue;
243
+ a.accept("column");
244
+ a.accept("if", "exists");
245
+ const column = a.identifier();
246
+ if (column !== void 0) ops.push({
247
+ kind: "drop-column",
248
+ table,
249
+ column
250
+ });
251
+ }
252
+ return ops;
253
+ }
254
+ return [];
255
+ }
256
+ /** What each statement of a migration does to tables, in order. */
257
+ function migrationOps(sql) {
258
+ return statements(sql).flatMap(statementOps);
259
+ }
260
+ /**
261
+ * The destructive statements of `migrations` (pending ones, oldest first) that hit data that
262
+ * exists before they run. A table the same batch creates starts empty, so dropping it or its
263
+ * columns loses nothing; a table the batch renames is tracked back to its old name.
264
+ */
265
+ function destructiveStatements(migrations, schema) {
266
+ const key = (s, name) => `${s ?? schema}\u0000${name}`;
267
+ const created = /* @__PURE__ */ new Set();
268
+ const renamed = /* @__PURE__ */ new Map();
269
+ const found = [];
270
+ for (const migration of migrations) for (const op of migrationOps(migration.sql)) {
271
+ if (op.kind === "drop-schema") {
272
+ found.push({
273
+ kind: op.kind,
274
+ schema: op.schema,
275
+ migration: migration.name
276
+ });
277
+ continue;
278
+ }
279
+ const s = op.table.schema ?? schema;
280
+ const k = key(s, op.table.name);
281
+ if (op.kind === "create-table") {
282
+ if (!op.ifNotExists) created.add(k);
283
+ continue;
284
+ }
285
+ if (op.kind === "rename-table") {
286
+ const to = key(s, op.to);
287
+ if (created.delete(k)) created.add(to);
288
+ else renamed.set(to, renamed.get(k) ?? op.table.name);
289
+ renamed.delete(k);
290
+ continue;
291
+ }
292
+ if (created.has(k)) {
293
+ if (op.kind === "drop-table") created.delete(k);
294
+ continue;
295
+ }
296
+ const original = renamed.get(k) ?? op.table.name;
297
+ found.push({
298
+ kind: op.kind,
299
+ schema: s,
300
+ table: original,
301
+ ...op.kind === "drop-column" ? { column: op.column } : {},
302
+ migration: migration.name
303
+ });
304
+ if (op.kind === "drop-table") renamed.delete(k);
305
+ }
306
+ return found;
307
+ }
308
+ /**
309
+ * The migrations Drizzle's migrator would run, oldest first: local ones whose name isn't in the
310
+ * schema's `__drizzle_migrations` yet. A journal from before drizzle-orm 1.0 has no names; its
311
+ * rows are matched by timestamp and hash, like the migrator's own upgrade does.
312
+ */
313
+ async function pendingMigrations(sql, schema, migrationsFolder) {
314
+ const local = readMigrationFiles({ migrationsFolder });
315
+ const table = `"${schema.replaceAll("\"", "\"\"")}"."__drizzle_migrations"`;
316
+ const [state] = await sql`select to_regclass(${table}) is not null as exists`;
317
+ const applied = /* @__PURE__ */ new Set();
318
+ if (state?.exists) {
319
+ if ((await sql`
320
+ select column_name as name from information_schema.columns
321
+ where table_schema = ${schema} and table_name = '__drizzle_migrations'`).some((c) => c.name === "name")) {
322
+ const rows = await sql.unsafe(`select name, hash, created_at from ${table}`);
323
+ for (const row of rows) if (row.name) applied.add(row.name);
324
+ matchUnnamed(local, rows.filter((row) => !row.name), applied);
325
+ } else matchUnnamed(local, await sql.unsafe(`select hash, created_at from ${table}`), applied);
326
+ }
327
+ return local.filter((m) => !m.name || !applied.has(m.name)).map((m) => ({
328
+ name: m.name ?? "",
329
+ sql: m.sql.join("\n--> statement-breakpoint\n")
330
+ }));
331
+ }
332
+ function matchUnnamed(local, rows, applied) {
333
+ for (const row of rows) {
334
+ const stamp = String(row.created_at ?? "");
335
+ const millis = Number(`${stamp.slice(0, -3)}000`);
336
+ const candidates = local.filter((m) => m.folderMillis === millis);
337
+ const match = candidates.length === 1 ? candidates[0] : candidates.find((m) => m.hash === row.hash) ?? local.find((m) => m.hash === row.hash);
338
+ if (match?.name) applied.add(match.name);
339
+ }
340
+ }
341
+ const quote = (name) => `"${name.replaceAll("\"", "\"\"")}"`;
342
+ async function countRows(sql, schema, table) {
343
+ const tables = table ? [table] : (await sql`
344
+ select c.relname as name from pg_class c join pg_namespace n on n.oid = c.relnamespace
345
+ where n.nspname = ${schema} and c.relkind in ('r', 'p')
346
+ and c.relname <> '__drizzle_migrations'`).map((row) => row.name);
347
+ let rows = 0;
348
+ for (const name of tables) {
349
+ const qualified = `${quote(schema)}.${quote(name)}`;
350
+ const [found] = await sql`
351
+ select to_regclass(${qualified}) is not null as exists`;
352
+ if (!found?.exists) continue;
353
+ const [row] = await sql.unsafe(`select count(*)::bigint as count from ${qualified}`);
354
+ rows += Number(row?.count ?? 0);
355
+ }
356
+ return rows;
357
+ }
358
+ /**
359
+ * The pending migrations' destructive statements that would take rows, and those of them
360
+ * `allow` doesn't let through. Runs no migration and changes nothing, so a deploy can stop
361
+ * before anything happened.
362
+ */
363
+ async function checkDestructiveMigrations({ app, adminUrl, migrationsFolder, allow = {} }) {
364
+ const schema = dbRole(app);
365
+ const sql = postgres(adminUrl, {
366
+ max: 1,
367
+ onnotice: () => {}
368
+ });
369
+ try {
370
+ const pending = await pendingMigrations(sql, schema, migrationsFolder);
371
+ const changes = [];
372
+ const counts = /* @__PURE__ */ new Map();
373
+ for (const statement of destructiveStatements(pending, schema)) {
374
+ const id = `${statement.schema}\u0000${statement.table ?? ""}`;
375
+ if (!counts.has(id)) counts.set(id, await countRows(sql, statement.schema, statement.table));
376
+ const rows = counts.get(id);
377
+ if (!rows) continue;
378
+ const name = statement.table ?? statement.schema;
379
+ const key = statement.table && statement.schema !== schema ? `${statement.schema}.${name}` : name;
380
+ changes.push({
381
+ ...statement,
382
+ rows,
383
+ key
384
+ });
385
+ }
386
+ return {
387
+ changes,
388
+ blocked: allow.all ? [] : changes.filter((c) => allow.drop?.[c.key] !== c.rows).map((c) => allow.drop?.[c.key] === void 0 ? c : {
389
+ ...c,
390
+ allowed: allow.drop[c.key]
391
+ })
392
+ };
393
+ } finally {
394
+ await sql.end();
395
+ }
396
+ }
397
+ const KIND_LABELS = {
398
+ "drop-table": "DROP TABLE",
399
+ "drop-column": "DROP COLUMN",
400
+ truncate: "TRUNCATE",
401
+ "delete-all": "DELETE without WHERE",
402
+ "drop-schema": "DROP SCHEMA"
403
+ };
404
+ function describeChange(c) {
405
+ if (c.kind === "drop-schema") return `schema ${c.key}`;
406
+ return c.column ? `${c.key}.${c.column}` : c.key;
407
+ }
408
+ /** `--allow-drop` arguments that let exactly these changes through, one per table. */
409
+ function allowDropArgs(changes) {
410
+ return [...new Map(changes.map((c) => [c.key, `${c.key}=${c.rows}`])).values()];
411
+ }
412
+ /** Thrown by a deploy whose migrations would take rows nobody allowed it to. */
413
+ var DestructiveMigrationError = class extends Error {
414
+ changes;
415
+ allowDrop;
416
+ code = "destructive-migration";
417
+ constructor(changes, allowDrop = allowDropArgs(changes)) {
418
+ const lines = changes.map((c) => ` ${describeChange(c)}: ${KIND_LABELS[c.kind]} in ${c.migration}, ${c.rows} ${c.rows === 1 ? "row" : "rows"}${c.allowed === void 0 ? "" : ` (allowed for ${c.allowed})`}`);
419
+ super([
420
+ "Pending migrations would delete data, so nothing was migrated or released:",
421
+ ...lines,
422
+ `Deploy again with ${allowDrop.map((a) => `--allow-drop ${a}`).join(" ")} if that's intended (or --allow-destructive).`
423
+ ].join("\n"));
424
+ this.changes = changes;
425
+ this.allowDrop = allowDrop;
426
+ this.name = "DestructiveMigrationError";
427
+ }
428
+ /** The fields the deploy's progress stream sends with the error. */
429
+ details() {
430
+ return {
431
+ code: this.code,
432
+ destructive: this.changes,
433
+ allowDrop: this.allowDrop
434
+ };
435
+ }
436
+ };
437
+ /** Parses `--allow-drop` values (`users=12`), throwing on anything else. */
438
+ function parseAllowDrop(values) {
439
+ const drop = {};
440
+ for (const value of values.flatMap((v) => v.split(",")).filter(Boolean)) {
441
+ const match = /^(.+)=(\d+)$/.exec(value.trim());
442
+ if (!match) throw new Error(`--allow-drop takes TABLE=ROWS, like users=12, not ${value}`);
443
+ drop[match[1]] = Number(match[2]);
444
+ }
445
+ return drop;
446
+ }
447
+ export { DestructiveMigrationError, allowDropArgs, checkDestructiveMigrations, destructiveStatements, migrationOps, parseAllowDrop, pendingMigrations };
package/dist/provision.js CHANGED
@@ -1,6 +1,6 @@
1
+ import postgres from "postgres";
1
2
  import { drizzle } from "drizzle-orm/postgres-js";
2
3
  import { migrate } from "drizzle-orm/postgres-js/migrator";
3
- import postgres from "postgres";
4
4
  // src/provision.ts
5
5
  const DEFAULT_ROLE_TIMEOUTS = {
6
6
  statement: "30s",
package/dist/storage.d.ts CHANGED
@@ -20,6 +20,8 @@ export interface StorageQuota {
20
20
  maxBytes: number | null;
21
21
  }
22
22
  export declare class StorageError extends Error {}
23
+ /** Garage refused a write because the bucket's quota is used up. */
24
+ export declare class QuotaError extends StorageError {}
23
25
  /**
24
26
  * How old abandoned direct-upload bytes must be before the sweep deletes them. The browser calls
25
27
  * `completeUpload()` right after its `PUT` finishes, so a day leaves any upload in flight alone;
@@ -80,6 +82,11 @@ export declare function createStorage(options: StorageOptions): {
80
82
  limit?: number;
81
83
  }) => Promise<ObjectListing | undefined>;
82
84
  describe: (app: string, key: string) => Promise<ObjectDetails | undefined>;
85
+ read: (app: string, key: string) => Promise<{
86
+ body: ReadableStream<Uint8Array<ArrayBuffer>>;
87
+ size: number | undefined;
88
+ contentType: string | null;
89
+ } | undefined>;
83
90
  downloadUrl: (app: string, key: string, { publicUrl, expiresIn }: {
84
91
  publicUrl: string;
85
92
  expiresIn?: number;
package/dist/storage.js CHANGED
@@ -2,6 +2,8 @@ import { storageCredentials } from "./provision.js";
2
2
  import { AwsClient } from "aws4fetch";
3
3
  // src/storage.ts
4
4
  var StorageError = class extends Error {};
5
+ /** Garage refused a write because the bucket's quota is used up. */
6
+ var QuotaError = class extends StorageError {};
5
7
  /**
6
8
  * ablage marks bytes uploaded straight from browsers (`x-amz-meta-ablage-direct-upload`). Its
7
9
  * metadata object (`<group>/meta/<id>`) is only written once `completeUpload()` has checked them,
@@ -231,13 +233,19 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
231
233
  const creds = await storageCredentials(app, options.secret);
232
234
  return Boolean(await exists(`GetBucketInfo?globalAlias=${creds.bucket}`));
233
235
  }
236
+ async function listing(client, url, bucket) {
237
+ const res = await client.fetch(url);
238
+ const body = await res.text();
239
+ if (!res.ok) throw new StorageError(`listing ${bucket}: ${res.status} ${body}`);
240
+ return body;
241
+ }
234
242
  /** Deletes every object and unfinished upload, then the bucket. Only with "drop data". */
235
243
  async function drop(app) {
236
244
  const { creds, client, base } = await s3(app);
237
245
  const bucket = await exists(`GetBucketInfo?globalAlias=${creds.bucket}`);
238
246
  if (bucket) {
239
247
  for (;;) {
240
- const keys = [...(await (await client.fetch(`${base}?list-type=2&max-keys=1000`)).text()).matchAll(/<Key>([^<]*)<\/Key>/g)].map((m) => m[1]);
248
+ const keys = [...(await listing(client, `${base}?list-type=2&max-keys=1000`, creds.bucket)).matchAll(/<Key>([^<]*)<\/Key>/g)].map((m) => m[1]);
241
249
  if (!keys.length) break;
242
250
  const body = `<Delete><Quiet>true</Quiet>${keys.map((key) => `<Object><Key>${key}</Key></Object>`).join("")}</Delete>`;
243
251
  const res = await client.fetch(`${base}?delete`, {
@@ -247,7 +255,7 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
247
255
  if (!res.ok) throw new StorageError(`emptying ${creds.bucket}: ${await res.text()}`);
248
256
  }
249
257
  for (;;) {
250
- const uploads = [...(await (await client.fetch(`${base}?uploads&max-uploads=1000`)).text()).matchAll(/<Key>([^<]*)<\/Key>\s*<UploadId>([^<]*)<\/UploadId>/g)].map((m) => ({
258
+ const uploads = [...(await listing(client, `${base}?uploads&max-uploads=1000`, creds.bucket)).matchAll(/<Key>([^<]*)<\/Key>\s*<UploadId>([^<]*)<\/UploadId>/g)].map((m) => ({
251
259
  key: m[1],
252
260
  id: m[2]
253
261
  }));
@@ -308,6 +316,22 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
308
316
  };
309
317
  }
310
318
  /**
319
+ * The object's bytes as Garage sends them, for `wervt storage get`, which can't count on
320
+ * reaching the public S3 URL. Undefined when it doesn't exist.
321
+ */
322
+ async function read(app, key) {
323
+ const { creds, client, base } = await s3(app);
324
+ const res = await client.fetch(objectUrl(base, key));
325
+ if (res.status === 404) return void 0;
326
+ if (!res.ok || !res.body) throw new StorageError(`reading ${key} in ${creds.bucket}: ${res.status}`);
327
+ const length = res.headers.get("content-length");
328
+ return {
329
+ body: res.body,
330
+ size: length === null ? void 0 : Number(length),
331
+ contentType: res.headers.get("content-type")
332
+ };
333
+ }
334
+ /**
311
335
  * A presigned download link on `publicUrl`, the S3 API as browsers reach it, so files go from
312
336
  * Garage straight to the browser. Always an attachment, never shown inline: the file is the
313
337
  * app's users' content and may well be HTML.
@@ -353,7 +377,10 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
353
377
  headers: { "content-type": contentType },
354
378
  body
355
379
  });
356
- if (!res.ok) throw new StorageError(`writing ${key} to ${creds.bucket}: ${await res.text()}`);
380
+ if (res.ok) return;
381
+ const text = await res.text();
382
+ if (res.status === 403 && /quota/i.test(text)) throw new QuotaError(`${creds.bucket} is full: ${text}`);
383
+ throw new StorageError(`writing ${key} to ${creds.bucket}: ${text}`);
357
384
  }
358
385
  return {
359
386
  ensureLayout,
@@ -362,6 +389,7 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
362
389
  usage,
363
390
  list,
364
391
  describe,
392
+ read,
365
393
  downloadUrl,
366
394
  put,
367
395
  remove,
@@ -370,4 +398,4 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
370
398
  drop
371
399
  };
372
400
  }
373
- export { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList };
401
+ export { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, QuotaError, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wervt/platform",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Provisioning for wervt apps: database roles, migrations, PgBouncer and object storage. Used by the control plane and local development.",
5
5
  "keywords": [
6
6
  "garage",
@@ -32,7 +32,7 @@
32
32
  }
33
33
  },
34
34
  "dependencies": {
35
- "@wervt/core": "^0.0.7",
35
+ "@wervt/core": "^0.0.8",
36
36
  "aws4fetch": "^1.0.20",
37
37
  "postgres": "^3.4.9"
38
38
  },