@objectstack/driver-sql 10.0.0 → 10.2.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/index.mjs CHANGED
@@ -2,6 +2,108 @@
2
2
  import { parseAutonumberFormat, renderAutonumber, missingFieldValues } from "@objectstack/spec/data";
3
3
  import { StorageNameMapping } from "@objectstack/spec/system";
4
4
  import { ExternalSchemaModeViolationError } from "@objectstack/spec/shared";
5
+
6
+ // src/schema-drift.ts
7
+ var BUILTIN_COLUMNS = /* @__PURE__ */ new Set(["id", "created_at", "updated_at"]);
8
+ function fieldHasColumn(field) {
9
+ if (field?.multiple) return true;
10
+ return (field?.type ?? "string") !== "formula";
11
+ }
12
+ function enforcesVarcharLength(dialect) {
13
+ return dialect === "postgres" || dialect === "mysql";
14
+ }
15
+ function diffManagedTable(args) {
16
+ const { table, fields, columns, dialect } = args;
17
+ const out = [];
18
+ const columnsByName = new Map(columns.map((c) => [c.name, c]));
19
+ const expectedColumns = /* @__PURE__ */ new Set();
20
+ for (const [fieldName, field] of Object.entries(fields ?? {})) {
21
+ if (BUILTIN_COLUMNS.has(fieldName)) continue;
22
+ if (!fieldHasColumn(field)) continue;
23
+ expectedColumns.add(fieldName);
24
+ const col = columnsByName.get(fieldName);
25
+ if (!col) continue;
26
+ const expectNullable = field.required !== true;
27
+ if (expectNullable && !col.nullable) {
28
+ out.push({
29
+ kind: "nullability_mismatch",
30
+ remoteName: table,
31
+ table,
32
+ column: fieldName,
33
+ expected: "NULL",
34
+ actual: "NOT NULL",
35
+ severity: "warning",
36
+ category: "safe",
37
+ op: { type: "relax_not_null", table, column: fieldName },
38
+ message: `${table}.${fieldName}: metadata is optional but the column is NOT NULL \u2014 writes that omit it fail. Run "os migrate" to relax it.`
39
+ });
40
+ } else if (!expectNullable && col.nullable) {
41
+ out.push({
42
+ kind: "nullability_mismatch",
43
+ remoteName: table,
44
+ table,
45
+ column: fieldName,
46
+ expected: "NOT NULL",
47
+ actual: "NULL",
48
+ severity: "error",
49
+ category: "destructive",
50
+ op: { type: "tighten_not_null", table, column: fieldName },
51
+ message: `${table}.${fieldName}: metadata is required but the column is nullable \u2014 existing nulls must be backfilled. Run "os migrate apply --allow-destructive".`
52
+ });
53
+ }
54
+ if (enforcesVarcharLength(dialect) && typeof field.maxLength === "number" && typeof col.maxLength === "number" && field.maxLength !== col.maxLength) {
55
+ if (field.maxLength > col.maxLength) {
56
+ out.push({
57
+ kind: "type_mismatch",
58
+ remoteName: table,
59
+ table,
60
+ column: fieldName,
61
+ expected: `varchar(${field.maxLength})`,
62
+ actual: `varchar(${col.maxLength})`,
63
+ severity: "warning",
64
+ category: "safe",
65
+ op: { type: "widen_varchar", table, column: fieldName, to: field.maxLength, from: col.maxLength },
66
+ message: `${table}.${fieldName}: metadata allows ${field.maxLength} chars but the column caps at ${col.maxLength} \u2014 widen via "os migrate".`
67
+ });
68
+ } else {
69
+ out.push({
70
+ kind: "type_mismatch",
71
+ remoteName: table,
72
+ table,
73
+ column: fieldName,
74
+ expected: `varchar(${field.maxLength})`,
75
+ actual: `varchar(${col.maxLength})`,
76
+ severity: "error",
77
+ category: "destructive",
78
+ op: { type: "narrow_varchar", table, column: fieldName, to: field.maxLength, from: col.maxLength },
79
+ message: `${table}.${fieldName}: metadata caps at ${field.maxLength} chars but the column allows ${col.maxLength} \u2014 narrowing may truncate. "os migrate apply --allow-destructive".`
80
+ });
81
+ }
82
+ }
83
+ }
84
+ for (const col of columns) {
85
+ if (BUILTIN_COLUMNS.has(col.name)) continue;
86
+ if (expectedColumns.has(col.name)) continue;
87
+ out.push({
88
+ kind: "unmapped_column",
89
+ remoteName: table,
90
+ table,
91
+ column: col.name,
92
+ expected: "(absent)",
93
+ actual: col.type,
94
+ severity: "warning",
95
+ category: "destructive",
96
+ op: { type: "drop_column", table, column: col.name },
97
+ message: `${table}.${col.name}: column exists in the database but not in metadata (orphaned) \u2014 "os migrate apply --allow-destructive" to drop it.`
98
+ });
99
+ }
100
+ return out;
101
+ }
102
+ function driftKey(d) {
103
+ return `${d.table}.${d.column ?? ""}:${d.kind}`;
104
+ }
105
+
106
+ // src/sql-driver.ts
5
107
  import knex from "knex";
6
108
  import { nanoid } from "nanoid";
7
109
  import { createHash } from "crypto";
@@ -115,8 +217,19 @@ var SqlDriver = class {
115
217
  this.logger = {
116
218
  warn: (msg, meta) => console.warn(msg, meta ?? "")
117
219
  };
118
- const { schemaMode, ...knexConfig } = config;
220
+ /**
221
+ * Metadata field defs for every table this driver manages, captured during
222
+ * `initObjects` (tableName → fields). The source of truth that
223
+ * {@link detectManagedDrift} diffs the physical schema against.
224
+ */
225
+ this.managedObjectFields = /* @__PURE__ */ new Map();
226
+ /** Declared indexes per managed table (tableName → indexes[]), captured in `initObjects`. Used to recreate indexes after a SQLite table rebuild. */
227
+ this.managedObjectIndexes = /* @__PURE__ */ new Map();
228
+ /** De-dup set for boot-time drift warnings (keyed by {@link driftKey}). */
229
+ this.driftWarned = /* @__PURE__ */ new Set();
230
+ const { schemaMode, autoMigrate, ...knexConfig } = config;
119
231
  this.schemaMode = schemaMode ?? "managed";
232
+ this.autoMigrate = autoMigrate ?? "off";
120
233
  this.config = knexConfig;
121
234
  this.knex = knex(knexConfig);
122
235
  }
@@ -1026,6 +1139,10 @@ var SqlDriver = class {
1026
1139
  await this.ensureDatabaseExists();
1027
1140
  for (const obj of objects) {
1028
1141
  const tableName = StorageNameMapping.resolveTableName(obj);
1142
+ this.managedObjectFields.set(tableName, obj.fields ?? {});
1143
+ if (Array.isArray(obj.indexes)) {
1144
+ this.managedObjectIndexes.set(tableName, obj.indexes);
1145
+ }
1029
1146
  const jsonCols = [];
1030
1147
  const booleanCols = [];
1031
1148
  const numericCols = [];
@@ -1118,7 +1235,266 @@ var SqlDriver = class {
1118
1235
  const physicalColumns = new Set(Object.keys(colInfo));
1119
1236
  await this.syncDeclaredIndexes(tableName, declaredIndexes, physicalColumns);
1120
1237
  }
1238
+ if (exists) {
1239
+ await this.reconcileAndWarnDrift(tableName, obj.fields ?? {});
1240
+ }
1241
+ }
1242
+ }
1243
+ // ── Managed-schema drift & reconcile (#2186) ───────────────────────────────
1244
+ /** Canonical dialect name for the drift differ. */
1245
+ get dialectName() {
1246
+ if (this.isSqlite) return "sqlite";
1247
+ if (this.isPostgres) return "postgres";
1248
+ if (this.isMysql) return "mysql";
1249
+ return "unknown";
1250
+ }
1251
+ /** True only when running under `NODE_ENV=production` — auto-DDL is force-disabled there. */
1252
+ isProductionEnv() {
1253
+ try {
1254
+ return (process.env.NODE_ENV ?? "").toLowerCase() === "production";
1255
+ } catch {
1256
+ return false;
1257
+ }
1258
+ }
1259
+ /** Diff one table's metadata fields against its physical columns. */
1260
+ async detectTableDrift(tableName, fields) {
1261
+ const cols = await this.introspectColumns(tableName);
1262
+ const physical = cols.map((c) => ({
1263
+ name: c.name,
1264
+ type: c.type,
1265
+ nullable: c.nullable,
1266
+ maxLength: c.maxLength
1267
+ }));
1268
+ return diffManagedTable({ table: tableName, fields, columns: physical, dialect: this.dialectName });
1269
+ }
1270
+ /**
1271
+ * Detect every managed-schema divergence between metadata and the physical
1272
+ * database. Metadata is the source of truth. Returns one entry per drift,
1273
+ * sorted by table then column. Used by `os migrate` (P3) and tests.
1274
+ *
1275
+ * @param objects optional explicit object list; defaults to whatever
1276
+ * `initObjects` last synced (captured in {@link managedObjectFields}).
1277
+ */
1278
+ async detectManagedDrift(objects) {
1279
+ const tables = /* @__PURE__ */ new Map();
1280
+ if (objects) {
1281
+ for (const o of objects) tables.set(StorageNameMapping.resolveTableName(o), o.fields ?? {});
1282
+ } else {
1283
+ for (const [t, f] of this.managedObjectFields) tables.set(t, f);
1284
+ }
1285
+ const out = [];
1286
+ for (const [tableName, fields] of tables) {
1287
+ if (!await this.knex.schema.hasTable(tableName)) continue;
1288
+ out.push(...await this.detectTableDrift(tableName, fields));
1289
+ }
1290
+ out.sort((a, b) => a.table === b.table ? (a.column ?? "").localeCompare(b.column ?? "") : a.table.localeCompare(b.table));
1291
+ return out;
1292
+ }
1293
+ /**
1294
+ * Boot-time per-table drift handling (P1 + P2): detect divergence, in dev
1295
+ * auto-reconcile the *safe* (loosening) subset when `autoMigrate==='safe'`,
1296
+ * then WARN once per remaining divergence with an actionable hint.
1297
+ */
1298
+ async reconcileAndWarnDrift(tableName, fields) {
1299
+ let drift;
1300
+ try {
1301
+ drift = await this.detectTableDrift(tableName, fields);
1302
+ } catch (e) {
1303
+ this.logger.warn(`[schema-drift] could not introspect '${tableName}' for drift detection`, e?.message ?? e);
1304
+ return;
1305
+ }
1306
+ if (drift.length === 0) return;
1307
+ const autoOn = this.autoMigrate === "safe" && this.schemaMode === "managed";
1308
+ if (autoOn && this.isProductionEnv()) {
1309
+ this.logger.warn(
1310
+ `[schema-drift] autoMigrate='safe' is ignored under NODE_ENV=production \u2014 schema is never auto-altered in production. Run 'os migrate' deliberately.`
1311
+ );
1312
+ } else if (autoOn) {
1313
+ const safe = drift.filter((d) => d.category === "safe");
1314
+ if (safe.length > 0) {
1315
+ try {
1316
+ const { applied } = await this.applyMigrationEntries(safe, { allowDestructive: false });
1317
+ for (const d of applied) {
1318
+ (this.logger.info ?? this.logger.warn)(`[schema-drift] auto-reconciled ${d.op.type} on ${d.table}.${d.column}`);
1319
+ }
1320
+ drift = await this.detectTableDrift(tableName, fields);
1321
+ } catch (e) {
1322
+ this.logger.warn(`[schema-drift] dev auto-reconcile failed for '${tableName}' \u2014 falling back to warning`, e?.message ?? e);
1323
+ }
1324
+ }
1325
+ }
1326
+ for (const d of drift) {
1327
+ const k = driftKey(d);
1328
+ if (this.driftWarned.has(k)) continue;
1329
+ this.driftWarned.add(k);
1330
+ this.logger.warn(`[schema-drift] ${d.message}`);
1331
+ }
1332
+ }
1333
+ /**
1334
+ * Apply a set of drift entries to the physical schema. Destructive entries
1335
+ * are skipped unless `allowDestructive` is set. Postgres/MySQL alter columns
1336
+ * in place; SQLite (which cannot alter constraints in place) rebuilds each
1337
+ * affected table (copy → swap) applying only the requested edits.
1338
+ *
1339
+ * @returns the entries actually applied and those skipped (e.g. destructive
1340
+ * without `allowDestructive`, or unsupported on the dialect).
1341
+ */
1342
+ async applyMigrationEntries(entries, opts = {}) {
1343
+ this.assertSchemaMutable("reconcileManagedSchema");
1344
+ const allowDestructive = opts.allowDestructive === true;
1345
+ const applied = [];
1346
+ const skipped = [];
1347
+ const candidates = entries.filter((d) => {
1348
+ if (d.category === "destructive" && !allowDestructive) {
1349
+ skipped.push(d);
1350
+ return false;
1351
+ }
1352
+ return true;
1353
+ });
1354
+ if (candidates.length === 0) return { applied, skipped };
1355
+ const byTable = /* @__PURE__ */ new Map();
1356
+ for (const d of candidates) {
1357
+ (byTable.get(d.table) ?? byTable.set(d.table, []).get(d.table)).push(d);
1121
1358
  }
1359
+ for (const [table, ents] of byTable) {
1360
+ try {
1361
+ if (this.isSqlite) {
1362
+ await this.rebuildSqliteTablePatched(table, ents);
1363
+ applied.push(...ents);
1364
+ } else {
1365
+ for (const d of ents) {
1366
+ const ok = await this.applyDriftOpInPlace(d.op);
1367
+ (ok ? applied : skipped).push(d);
1368
+ }
1369
+ }
1370
+ } catch (e) {
1371
+ this.logger.warn(`[schema-drift] failed to reconcile '${table}'`, e?.message ?? e);
1372
+ for (const d of ents) if (!applied.includes(d)) skipped.push(d);
1373
+ }
1374
+ }
1375
+ return { applied, skipped };
1376
+ }
1377
+ /** Apply a single drift op in place (Postgres / MySQL). Returns false if unsupported. */
1378
+ async applyDriftOpInPlace(op) {
1379
+ const { table, column } = op;
1380
+ if (this.isPostgres) {
1381
+ switch (op.type) {
1382
+ case "relax_not_null":
1383
+ await this.knex.raw("ALTER TABLE ?? ALTER COLUMN ?? DROP NOT NULL", [table, column]);
1384
+ return true;
1385
+ case "tighten_not_null":
1386
+ await this.knex.raw("ALTER TABLE ?? ALTER COLUMN ?? SET NOT NULL", [table, column]);
1387
+ return true;
1388
+ case "widen_varchar":
1389
+ case "narrow_varchar":
1390
+ await this.knex.raw(`ALTER TABLE ?? ALTER COLUMN ?? TYPE varchar(${op.to})`, [table, column]);
1391
+ return true;
1392
+ case "drop_column":
1393
+ await this.knex.raw("ALTER TABLE ?? DROP COLUMN ??", [table, column]);
1394
+ return true;
1395
+ }
1396
+ }
1397
+ if (this.isMysql) {
1398
+ const info = await this.knex(table).columnInfo();
1399
+ const ci = info?.[column];
1400
+ const colType = ci?.type ? /char/i.test(ci.type) && ci.maxLength ? `${ci.type}(${ci.maxLength})` : ci.type : void 0;
1401
+ switch (op.type) {
1402
+ case "relax_not_null":
1403
+ if (!colType) return false;
1404
+ await this.knex.raw(`ALTER TABLE ?? MODIFY ?? ${colType} NULL`, [table, column]);
1405
+ return true;
1406
+ case "tighten_not_null":
1407
+ if (!colType) return false;
1408
+ await this.knex.raw(`ALTER TABLE ?? MODIFY ?? ${colType} NOT NULL`, [table, column]);
1409
+ return true;
1410
+ case "widen_varchar":
1411
+ case "narrow_varchar":
1412
+ await this.knex.raw(`ALTER TABLE ?? MODIFY ?? varchar(${op.to})`, [table, column]);
1413
+ return true;
1414
+ case "drop_column":
1415
+ await this.knex.raw("ALTER TABLE ?? DROP COLUMN ??", [table, column]);
1416
+ return true;
1417
+ }
1418
+ }
1419
+ this.logger.warn(`[schema-drift] ${op.type} on ${table}.${column} is unsupported on dialect '${this.dialectName}' \u2014 skipped`);
1420
+ return false;
1421
+ }
1422
+ /**
1423
+ * Rebuild a SQLite table applying a set of column edits (relax/tighten NOT
1424
+ * NULL, drop column), preserving all other columns and their data. Follows
1425
+ * the official SQLite procedure: create patched table → copy → drop → rename.
1426
+ * varchar widen/narrow are no-ops on SQLite (dynamic typing) and ignored.
1427
+ *
1428
+ * Unique field-level constraints and declared indexes are recreated from
1429
+ * metadata afterwards (the source of truth). DB-level foreign keys declared
1430
+ * by `lookup` fields are not re-added (ObjectStack enforces relationships at
1431
+ * the application layer, not via SQLite FK constraints).
1432
+ */
1433
+ async rebuildSqliteTablePatched(table, ents) {
1434
+ const relax = /* @__PURE__ */ new Set();
1435
+ const tighten = /* @__PURE__ */ new Set();
1436
+ const drop = /* @__PURE__ */ new Set();
1437
+ for (const e of ents) {
1438
+ if (e.op.type === "relax_not_null") relax.add(e.op.column);
1439
+ else if (e.op.type === "tighten_not_null") tighten.add(e.op.column);
1440
+ else if (e.op.type === "drop_column") drop.add(e.op.column);
1441
+ }
1442
+ const physical = await this.introspectColumns(table);
1443
+ const kept = physical.filter((c) => !drop.has(c.name));
1444
+ const keptNames = kept.map((c) => c.name);
1445
+ const fields = this.managedObjectFields.get(table) ?? {};
1446
+ const tmp = `__os_mig_${table}`;
1447
+ await this.knex.raw("PRAGMA foreign_keys = OFF");
1448
+ try {
1449
+ await this.knex.transaction(async (trx) => {
1450
+ await trx.schema.dropTableIfExists(tmp);
1451
+ await trx.schema.createTable(tmp, (t) => {
1452
+ for (const c of kept) {
1453
+ const col = this.buildRebuiltColumn(t, c);
1454
+ if (!col) continue;
1455
+ const nullable = relax.has(c.name) ? true : tighten.has(c.name) ? false : c.nullable;
1456
+ if (!nullable && c.name !== "id") col.notNullable();
1457
+ if (c.name === "created_at" || c.name === "updated_at") col.defaultTo(this.knex.fn.now());
1458
+ }
1459
+ });
1460
+ const colList = keptNames.map((n) => `"${n}"`).join(", ");
1461
+ await trx.raw(`INSERT INTO "${tmp}" (${colList}) SELECT ${colList} FROM "${table}"`);
1462
+ await trx.schema.dropTable(table);
1463
+ await trx.schema.renameTable(tmp, table);
1464
+ });
1465
+ } finally {
1466
+ await this.knex.raw("PRAGMA foreign_keys = ON");
1467
+ }
1468
+ try {
1469
+ const keptSet = new Set(keptNames);
1470
+ for (const [name, field] of Object.entries(fields)) {
1471
+ if (field?.unique && keptSet.has(name)) {
1472
+ const idx = `uniq_${table}_${name}`;
1473
+ await this.knex.raw("CREATE UNIQUE INDEX IF NOT EXISTS ?? ON ?? (??)", [idx, table, name]);
1474
+ }
1475
+ }
1476
+ const declared = this.managedObjectIndexes.get(table);
1477
+ if (Array.isArray(declared) && declared.length > 0) {
1478
+ await this.syncDeclaredIndexes(table, declared, keptSet);
1479
+ }
1480
+ } catch (e) {
1481
+ this.logger.warn(`[schema-drift] could not fully recreate indexes for '${table}' after rebuild`, e?.message ?? e);
1482
+ }
1483
+ }
1484
+ /** Map an introspected SQLite column to a knex builder for the rebuilt table. */
1485
+ buildRebuiltColumn(t, c) {
1486
+ if (c.name === "id") return t.string("id").primary();
1487
+ const ty = (c.type || "text").toLowerCase();
1488
+ if (ty.includes("int")) return t.integer(c.name);
1489
+ if (/(real|floa|doub|num|dec)/.test(ty)) return t.float(c.name);
1490
+ if (ty.includes("bool")) return t.boolean(c.name);
1491
+ if (ty.includes("datetime") || ty.includes("timestamp")) return t.timestamp(c.name);
1492
+ if (ty === "date") return t.date(c.name);
1493
+ if (ty === "time") return t.time(c.name);
1494
+ if (ty.includes("json")) return t.json(c.name);
1495
+ if (ty.includes("blob") || ty.includes("binary")) return t.binary(c.name);
1496
+ if (ty.includes("text")) return t.text(c.name);
1497
+ return t.string(c.name);
1122
1498
  }
1123
1499
  /**
1124
1500
  * Build a deterministic index name for a declared index so repeated
@@ -2186,7 +2562,11 @@ var index_default = {
2186
2562
  }
2187
2563
  };
2188
2564
  export {
2565
+ BUILTIN_COLUMNS,
2189
2566
  SqlDriver,
2190
- index_default as default
2567
+ index_default as default,
2568
+ diffManagedTable,
2569
+ driftKey,
2570
+ fieldHasColumn
2191
2571
  };
2192
2572
  //# sourceMappingURL=index.mjs.map