totalum-sdk 1.4.0 → 1.4.1
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/CHANGELOG.md +9 -0
- package/dist/d1/migrate-rules.js +111 -4
- package/package.json +1 -1
- package/totalum-sdk.signatures.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -20,6 +20,15 @@ regenerated), plus the request each call sends to SDK-API (`compat/requests/*.js
|
|
|
20
20
|
- **Dist-tags:** `next` is the newest release; `latest` moves only by a decision at launch. `dev` holds the old
|
|
21
21
|
`0.1.0-dev.N` prereleases.
|
|
22
22
|
|
|
23
|
+
## 1.4.1 — 2026-09-30
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- Migrate mode on a binding (`npm run dev`'s `scripts/migrate.ts --local`) no longer refuses drizzle-kit's table
|
|
28
|
+
rebuild for a DEFAULT, CHECK, collation or NOT NULL-with-default change as destructive: a rebuild that copies every
|
|
29
|
+
column unchanged with its declared type needs no `destructive` (SDK-API's rule, contracts `safeRebuilds`, research 72
|
|
30
|
+
P0). Drops, renames and type changes still do.
|
|
31
|
+
|
|
23
32
|
## 1.4.0 — 2026-09-30
|
|
24
33
|
|
|
25
34
|
### Added (compatible)
|
package/dist/d1/migrate-rules.js
CHANGED
|
@@ -7,7 +7,7 @@ import { TotalumD1Error } from './errors.js';
|
|
|
7
7
|
* table referencing the rebuilt one (D1 ignores `PRAGMA foreign_keys=OFF` inside a batch).
|
|
8
8
|
*
|
|
9
9
|
* `classify` is a copy of SDK-API's `packages/data-worker/guard.mjs` and `keepChildRows` of contracts'
|
|
10
|
-
* `sql-rebuild.ts` (this package has no runtime dependency, plan 05 §1); `test/node/migrate-rules.test.ts` holds both
|
|
10
|
+
* `sql-rebuild.ts` (with `safeRebuilds`; this package has no runtime dependency, plan 05 §1); `test/node/migrate-rules.test.ts` holds both
|
|
11
11
|
* copies to the originals on a corpus.
|
|
12
12
|
*/
|
|
13
13
|
const READ_PRAGMAS = new Set([
|
|
@@ -229,6 +229,106 @@ export function keepChildRows(statements, edges) {
|
|
|
229
229
|
}
|
|
230
230
|
return sent.length > statements.length + 1 ? { sent, at } : null;
|
|
231
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Every column of the database, read before judging a rebuild (`safeRebuilds`); the same filter as `FOREIGN_KEYS_SQL`.
|
|
234
|
+
*/
|
|
235
|
+
export const TABLE_COLUMNS_SQL = `SELECT m.name AS "table", c.name AS name, c.type AS type, c."notnull" AS "notnull"
|
|
236
|
+
FROM sqlite_master m JOIN pragma_table_info(m.name) c
|
|
237
|
+
WHERE m.type = 'table' AND m.name NOT LIKE '\\_cf\\_%' ESCAPE '\\' AND m.name NOT LIKE 'sqlite\\_%' ESCAPE '\\'`;
|
|
238
|
+
const CREATE_NEW = new RegExp(`^\\s*CREATE\\s+TABLE\\s+${NAME}\\s*\\(([\\s\\S]*)\\)\\s*;?\\s*$`, 'i');
|
|
239
|
+
const COPY = new RegExp(`^\\s*INSERT\\s+INTO\\s+${NAME}\\s*\\(([^)]*)\\)\\s*SELECT\\s+([\\s\\S]*?)\\s+FROM\\s+${NAME}\\s*;?\\s*$`, 'i');
|
|
240
|
+
const CONSTRAINT = /^(CONSTRAINT|PRIMARY|NOT|NULL|UNIQUE|CHECK|DEFAULT|COLLATE|REFERENCES|GENERATED|AS)$/i;
|
|
241
|
+
const TABLE_CONSTRAINT = /^(CONSTRAINT|PRIMARY\s+KEY|FOREIGN\s+KEY|UNIQUE|CHECK)\b/i;
|
|
242
|
+
const kind = (type) => type.replace(/\s+/g, '').toUpperCase();
|
|
243
|
+
/** The top-level comma-separated parts of a `CREATE TABLE` body (commas inside parentheses or quotes stay). */
|
|
244
|
+
function parts(body) {
|
|
245
|
+
const out = [];
|
|
246
|
+
let depth = 0;
|
|
247
|
+
let quote = '';
|
|
248
|
+
let cur = '';
|
|
249
|
+
for (const ch of body) {
|
|
250
|
+
if (quote)
|
|
251
|
+
quote = ch === quote ? '' : quote;
|
|
252
|
+
else if (ch === "'" || ch === '"' || ch === '`')
|
|
253
|
+
quote = ch;
|
|
254
|
+
else if (ch === '(')
|
|
255
|
+
depth++;
|
|
256
|
+
else if (ch === ')')
|
|
257
|
+
depth--;
|
|
258
|
+
else if (ch === ',' && depth === 0) {
|
|
259
|
+
out.push(cur.trim());
|
|
260
|
+
cur = '';
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
cur += ch;
|
|
264
|
+
}
|
|
265
|
+
if (cur.trim())
|
|
266
|
+
out.push(cur.trim());
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
/** The column definitions of a `CREATE TABLE` body: name, declared type, `NOT NULL`, `DEFAULT`. */
|
|
270
|
+
function columnsOf(body) {
|
|
271
|
+
return parts(body)
|
|
272
|
+
.filter((d) => !TABLE_CONSTRAINT.test(d))
|
|
273
|
+
.map((d) => {
|
|
274
|
+
const [name = '', ...rest] = d.match(/`[^`]+`|"[^"]+"|\[[^\]]+\]|'[^']*'|\S+/g) ?? [];
|
|
275
|
+
const end = rest.findIndex((w) => CONSTRAINT.test(w));
|
|
276
|
+
return {
|
|
277
|
+
name: unquote(name),
|
|
278
|
+
type: kind(rest.slice(0, end < 0 ? rest.length : end).join('')),
|
|
279
|
+
notnull: /\bNOT\s+NULL\b/i.test(d),
|
|
280
|
+
hasDefault: /\bDEFAULT\b/i.test(d),
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Research 72 P0 gap 1: the indexes of the `DROP TABLE X` / `ALTER TABLE __new_X RENAME TO X` statements of a rebuild
|
|
286
|
+
* that keeps every row as it is — drizzle-kit's `CREATE TABLE __new_X` + `INSERT INTO __new_X(cols) SELECT cols FROM X`
|
|
287
|
+
* copies every column of X (`columns`, read before) unchanged, with its declared type, and adds no `NOT NULL` column or
|
|
288
|
+
* constraint without a `DEFAULT` (older published code still inserts without it, A10.7 (2)). That is a change of a
|
|
289
|
+
* DEFAULT, a CHECK, a collation or `NOT NULL` with a default: not destructive (with `keepChildRows` nothing referencing
|
|
290
|
+
* X loses a row, and a row the new constraints refuse fails the whole migration). A dropped, renamed or retyped column,
|
|
291
|
+
* or a copy through an expression, keeps the rebuild destructive.
|
|
292
|
+
*/
|
|
293
|
+
export function safeRebuilds(statements, columns) {
|
|
294
|
+
const safe = [];
|
|
295
|
+
statements.forEach((sql, i) => {
|
|
296
|
+
const rename = RENAME.exec(sql);
|
|
297
|
+
const from = unquote(rename?.[1] ?? '');
|
|
298
|
+
if (!rename || !/^__new_/i.test(from))
|
|
299
|
+
return;
|
|
300
|
+
const table = unquote(rename[2] ?? '');
|
|
301
|
+
const create = statements
|
|
302
|
+
.map((s) => CREATE_NEW.exec(s))
|
|
303
|
+
.find((m) => m && same(unquote(m[1] ?? ''), from));
|
|
304
|
+
const copies = statements.flatMap((s) => {
|
|
305
|
+
const m = COPY.exec(s);
|
|
306
|
+
return m && same(unquote(m[1] ?? ''), from) && same(unquote(m[4] ?? ''), table) ? [m] : [];
|
|
307
|
+
});
|
|
308
|
+
const drop = statements.findIndex((s) => same(unquote(DROP.exec(s)?.[1] ?? ''), table));
|
|
309
|
+
const copy = copies[0];
|
|
310
|
+
if (!create || !copy || copies.length !== 1 || drop < 0)
|
|
311
|
+
return;
|
|
312
|
+
const list = (s) => s.split(',').map((x) => unquote(x.trim()).toLowerCase());
|
|
313
|
+
const into = list(copy[2] ?? '');
|
|
314
|
+
if (into.join() !== list(copy[3] ?? '').join())
|
|
315
|
+
return;
|
|
316
|
+
const before = columns.filter((c) => same(c.table, table));
|
|
317
|
+
const after = columnsOf(create[2] ?? '');
|
|
318
|
+
const kept = before.length > 0 &&
|
|
319
|
+
before.every((b) => {
|
|
320
|
+
const a = after.find((x) => same(x.name, b.name));
|
|
321
|
+
return (a &&
|
|
322
|
+
into.includes(b.name.toLowerCase()) &&
|
|
323
|
+
a.type === kind(b.type) &&
|
|
324
|
+
(!a.notnull || b.notnull === 1 || a.hasDefault));
|
|
325
|
+
}) &&
|
|
326
|
+
after.every((a) => before.some((b) => same(a.name, b.name)) || !a.notnull || a.hasDefault);
|
|
327
|
+
if (kept)
|
|
328
|
+
safe.push(drop, i);
|
|
329
|
+
});
|
|
330
|
+
return safe;
|
|
331
|
+
}
|
|
232
332
|
// ——— SDK-API's migrate mode (`routes.ts` runStatements / applyKeepingChildRows, `guard.ts` messages) ———
|
|
233
333
|
const REFUSED = {
|
|
234
334
|
SQL_MULTI_STATEMENT: 'Exactly one statement per call',
|
|
@@ -241,7 +341,7 @@ const REFUSED = {
|
|
|
241
341
|
* the original statements' results.
|
|
242
342
|
*/
|
|
243
343
|
export async function migrate(target, statements, destructive) {
|
|
244
|
-
const classified = guardMigration(statements, destructive);
|
|
344
|
+
const classified = guardMigration(statements, destructive, destructive ? [] : await safeOn(target, statements));
|
|
245
345
|
const prepare = (s) => target.prepare(s.sql).bind(...s.params);
|
|
246
346
|
if (statements.length < 2 || !classified.some((c) => c.norm.startsWith('DROP TABLE')))
|
|
247
347
|
return target.batch(statements.map(prepare));
|
|
@@ -255,8 +355,15 @@ export async function migrate(target, statements, destructive) {
|
|
|
255
355
|
const out = await target.batch(sent.map(prepare));
|
|
256
356
|
return plan.at.map((j) => out[j]);
|
|
257
357
|
}
|
|
358
|
+
/** Research 72 P0: the statements of a rebuild that keeps every column and type (not destructive), read as SDK-API does. */
|
|
359
|
+
async function safeOn(target, statements) {
|
|
360
|
+
if (!statements.some((s) => classify(s.sql).norm.startsWith('DROP TABLE')))
|
|
361
|
+
return [];
|
|
362
|
+
const columns = (await target.prepare(TABLE_COLUMNS_SQL).all()).results;
|
|
363
|
+
return safeRebuilds(statements.map((s) => s.sql), columns);
|
|
364
|
+
}
|
|
258
365
|
/** The refusals of a migrate-mode call (`MIGRATION_INVALID`, `DESTRUCTIVE_MIGRATION_NEEDS_CONFIRMATION`), before it runs. */
|
|
259
|
-
export function guardMigration(statements, destructive) {
|
|
366
|
+
export function guardMigration(statements, destructive, safe = []) {
|
|
260
367
|
const classified = statements.map((s, statementIndex) => {
|
|
261
368
|
const c = classify(s.sql);
|
|
262
369
|
if (!c.ok) {
|
|
@@ -268,7 +375,7 @@ export function guardMigration(statements, destructive) {
|
|
|
268
375
|
}
|
|
269
376
|
return c;
|
|
270
377
|
});
|
|
271
|
-
const i = classified.findIndex((c) => c.destructive);
|
|
378
|
+
const i = classified.findIndex((c, k) => c.destructive && !safe.includes(k));
|
|
272
379
|
if (i >= 0 && !destructive) {
|
|
273
380
|
throw new TotalumD1Error('DESTRUCTIVE_MIGRATION_NEEDS_CONFIRMATION', 'D1_ERROR: DESTRUCTIVE_MIGRATION_NEEDS_CONFIRMATION: This migration drops, renames or rewrites a table or column', {
|
|
274
381
|
status: 409,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "totalum-sdk",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "The SDK generated Totalum apps import: every integration namespace over SDK-API (totalum-sdk, or one subpath each: /files, /ai, /web, \u2026) and totalum-sdk/d1, the D1 driver",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|