@hot-updater/plugin-core 1.2.0-rc.50 → 1.2.0-rc.52
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/coreApi.d.cts +32 -1
- package/dist/coreApi.d.mts +32 -1
- package/dist/database/adapter.d.cts +12 -0
- package/dist/database/adapter.d.mts +12 -0
- package/dist/databaseRows.cjs +4 -2
- package/dist/databaseRows.mjs +4 -2
- package/dist/engine/core/schema.cjs +55 -3
- package/dist/engine/core/schema.d.cts +27 -0
- package/dist/engine/core/schema.d.mts +27 -0
- package/dist/engine/core/schema.mjs +55 -4
- package/dist/engine/database/fence.cjs +24 -2
- package/dist/engine/database/fence.d.cts +4 -1
- package/dist/engine/database/fence.d.mts +4 -1
- package/dist/engine/database/fence.mjs +24 -3
- package/dist/engine/database/sql/sqlAdapter.cjs +27 -8
- package/dist/engine/database/sql/sqlAdapter.mjs +28 -9
- package/dist/engine/db/engineMigrator.cjs +32 -9
- package/dist/engine/db/engineMigrator.mjs +33 -10
- package/dist/engine/db/engineSql.cjs +25 -0
- package/dist/engine/db/engineSql.d.cts +13 -1
- package/dist/engine/db/engineSql.d.mts +13 -1
- package/dist/engine/db/engineSql.mjs +26 -2
- package/dist/engine/db/engineSqlMigrator.cjs +12 -6
- package/dist/engine/db/engineSqlMigrator.mjs +13 -7
- package/dist/engine/db/schemaSettings.cjs +22 -2
- package/dist/engine/db/schemaSettings.mjs +23 -4
- package/dist/engine/db/settingsMigrator.cjs +41 -21
- package/dist/engine/db/settingsMigrator.mjs +42 -22
- package/dist/engine/db/sqlSchemaState.cjs +36 -8
- package/dist/engine/db/sqlSchemaState.mjs +36 -9
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -3
- package/dist/releaseCatalogCompiler.cjs +89 -7
- package/dist/releaseCatalogCompiler.d.cts +12 -1
- package/dist/releaseCatalogCompiler.d.mts +12 -1
- package/dist/releaseCatalogCompiler.mjs +89 -8
- package/dist/releaseCatalogMutation.d.cts +8 -2
- package/dist/releaseCatalogMutation.d.mts +8 -2
- package/dist/types/databaseRows.d.cts +7 -0
- package/dist/types/databaseRows.d.mts +7 -0
- package/package.json +2 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const require_adapter = require("../../../database/adapter.cjs");
|
|
2
2
|
const require_values = require("../../../database/values.cjs");
|
|
3
3
|
const require_sqlSchema = require("./sqlSchema.cjs");
|
|
4
|
+
const require_sqlSchemaState = require("../../db/sqlSchemaState.cjs");
|
|
4
5
|
//#region src/engine/database/sql/sqlAdapter.ts
|
|
5
6
|
/** Where a batch write records its first failed op, before any change applies. */
|
|
6
7
|
const WRITE_GUARD_TABLE = {
|
|
@@ -279,14 +280,32 @@ const createSqlAdapter = ({ executor, tablePrefix = "", maxOps = 1e3, maxParams
|
|
|
279
280
|
return done;
|
|
280
281
|
})).at(-1)?.changes ?? 0;
|
|
281
282
|
},
|
|
282
|
-
migrations: {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
283
|
+
migrations: {
|
|
284
|
+
async apply(tables) {
|
|
285
|
+
const created = executor.batch && !tables.includes(WRITE_GUARD_TABLE) ? [...tables, WRITE_GUARD_TABLE] : tables;
|
|
286
|
+
const statements = require_sqlSchema.createTableStatements(dialect, created, tablePrefix);
|
|
287
|
+
for (const text of statements) await executor.execute({
|
|
288
|
+
sql: text,
|
|
289
|
+
params: []
|
|
290
|
+
});
|
|
291
|
+
},
|
|
292
|
+
async addColumns(table, columns, { dryRun = false } = {}) {
|
|
293
|
+
const [shape] = require_sqlSchema.sqlTableShapes(dialect, [table], tablePrefix);
|
|
294
|
+
const state = await require_sqlSchemaState.readSqlSchemaState(executor);
|
|
295
|
+
if (shape === void 0 || !state.hasTable(shape.name)) return [];
|
|
296
|
+
const missing = columns.filter((column) => !state.hasColumn(shape.name, column));
|
|
297
|
+
if (dryRun) return missing;
|
|
298
|
+
for (const name of missing) {
|
|
299
|
+
const column = shape.columns.find((candidate) => candidate.name === name);
|
|
300
|
+
if (column === void 0 || column.notNull) throw new Error(`${table.name}.${name} can't be added to an existing table: only an optional column can.`);
|
|
301
|
+
await executor.execute({
|
|
302
|
+
sql: `ALTER TABLE ${require_sqlSchema.quoteSql(dialect, shape.name)} ADD COLUMN ${require_sqlSchema.quoteSql(dialect, name)} ${column.type}`,
|
|
303
|
+
params: []
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return missing;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
290
309
|
};
|
|
291
310
|
};
|
|
292
311
|
//#endregion
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey } from "../../../database/adapter.mjs";
|
|
2
2
|
import { indexEntries, indexOrderTuple, normalizeStoredRow } from "../../../database/values.mjs";
|
|
3
|
-
import { createTableStatements, isMultiIndex, pruneStatements, quoteSql } from "./sqlSchema.mjs";
|
|
3
|
+
import { createTableStatements, isMultiIndex, pruneStatements, quoteSql, sqlTableShapes } from "./sqlSchema.mjs";
|
|
4
|
+
import { readSqlSchemaState } from "../../db/sqlSchemaState.mjs";
|
|
4
5
|
//#region src/engine/database/sql/sqlAdapter.ts
|
|
5
6
|
/** Where a batch write records its first failed op, before any change applies. */
|
|
6
7
|
const WRITE_GUARD_TABLE = {
|
|
@@ -279,14 +280,32 @@ const createSqlAdapter = ({ executor, tablePrefix = "", maxOps = 1e3, maxParams
|
|
|
279
280
|
return done;
|
|
280
281
|
})).at(-1)?.changes ?? 0;
|
|
281
282
|
},
|
|
282
|
-
migrations: {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
283
|
+
migrations: {
|
|
284
|
+
async apply(tables) {
|
|
285
|
+
const created = executor.batch && !tables.includes(WRITE_GUARD_TABLE) ? [...tables, WRITE_GUARD_TABLE] : tables;
|
|
286
|
+
const statements = createTableStatements(dialect, created, tablePrefix);
|
|
287
|
+
for (const text of statements) await executor.execute({
|
|
288
|
+
sql: text,
|
|
289
|
+
params: []
|
|
290
|
+
});
|
|
291
|
+
},
|
|
292
|
+
async addColumns(table, columns, { dryRun = false } = {}) {
|
|
293
|
+
const [shape] = sqlTableShapes(dialect, [table], tablePrefix);
|
|
294
|
+
const state = await readSqlSchemaState(executor);
|
|
295
|
+
if (shape === void 0 || !state.hasTable(shape.name)) return [];
|
|
296
|
+
const missing = columns.filter((column) => !state.hasColumn(shape.name, column));
|
|
297
|
+
if (dryRun) return missing;
|
|
298
|
+
for (const name of missing) {
|
|
299
|
+
const column = shape.columns.find((candidate) => candidate.name === name);
|
|
300
|
+
if (column === void 0 || column.notNull) throw new Error(`${table.name}.${name} can't be added to an existing table: only an optional column can.`);
|
|
301
|
+
await executor.execute({
|
|
302
|
+
sql: `ALTER TABLE ${quoteSql(dialect, shape.name)} ADD COLUMN ${quoteSql(dialect, name)} ${column.type}`,
|
|
303
|
+
params: []
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return missing;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
290
309
|
};
|
|
291
310
|
};
|
|
292
311
|
//#endregion
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_schema = require("../core/schema.cjs");
|
|
2
|
+
const require_fence = require("../database/fence.cjs");
|
|
2
3
|
const require_fixedMigratorShared = require("./fixedMigratorShared.cjs");
|
|
3
4
|
const require_settingsMigrator = require("./settingsMigrator.cjs");
|
|
4
5
|
const require_schemaSettings = require("./schemaSettings.cjs");
|
|
@@ -17,23 +18,45 @@ const createEngineMigrator = (options) => {
|
|
|
17
18
|
require_settingsMigrator.refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
18
19
|
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
19
20
|
if (changed.length === 0) {
|
|
21
|
+
const columns = await require_schemaSettings.addLateColumns(options.adapter, options.schema.tables, { dryRun: true });
|
|
20
22
|
const lacking = await require_schemaSettings.fillLateIndexes(options.adapter, options.schema.tables, { dryRun: true });
|
|
21
|
-
if (lacking.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
23
|
+
if (columns.length === 0 && lacking.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
22
24
|
return {
|
|
23
|
-
operations: [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
operations: [
|
|
26
|
+
...columns.map((column) => ({
|
|
27
|
+
type: "custom",
|
|
28
|
+
description: `Add column: ${column}`
|
|
29
|
+
})),
|
|
30
|
+
...columns.length === 0 ? [] : [{
|
|
31
|
+
type: "custom",
|
|
32
|
+
description: "Create the tables and indexes the database lacks"
|
|
33
|
+
}],
|
|
34
|
+
...lacking.length === 0 ? [] : [{
|
|
35
|
+
type: "custom",
|
|
36
|
+
description: `Write the index items rows lack: ${lacking.join(", ")}`
|
|
37
|
+
}]
|
|
38
|
+
],
|
|
27
39
|
execute: async () => {
|
|
40
|
+
if (columns.length > 0) {
|
|
41
|
+
await require_schemaSettings.addLateColumns(options.adapter, options.schema.tables);
|
|
42
|
+
await options.adapter.migrations.apply([...options.schema.tables, require_fence.SETTINGS_TABLE]);
|
|
43
|
+
}
|
|
28
44
|
await require_schemaSettings.fillLateIndexes(options.adapter, options.schema.tables);
|
|
29
45
|
}
|
|
30
46
|
};
|
|
31
47
|
}
|
|
32
48
|
return {
|
|
33
|
-
operations: [
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
49
|
+
operations: [
|
|
50
|
+
...(await require_schemaSettings.addLateColumns(options.adapter, options.schema.tables, { dryRun: true }).catch(() => [])).map((column) => ({
|
|
51
|
+
type: "custom",
|
|
52
|
+
description: `Add column: ${column}`
|
|
53
|
+
})),
|
|
54
|
+
{
|
|
55
|
+
type: "custom",
|
|
56
|
+
description: "Create the tables and indexes the database lacks"
|
|
57
|
+
},
|
|
58
|
+
...migrate.updateSettings === false ? [] : [require_settingsMigrator.settingsOperation(stored, changed)]
|
|
59
|
+
],
|
|
37
60
|
execute: () => require_schemaSettings.migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings, options.ordered)
|
|
38
61
|
};
|
|
39
62
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { HOT_UPDATER_SCHEMA_VERSION } from "../core/schema.mjs";
|
|
2
|
+
import { SETTINGS_TABLE } from "../database/fence.mjs";
|
|
2
3
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
3
4
|
import { refuseNewerSchema, refuseV0Database, settingsOperation, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
4
|
-
import { fillLateIndexes, migrateSchema } from "./schemaSettings.mjs";
|
|
5
|
+
import { addLateColumns, fillLateIndexes, migrateSchema } from "./schemaSettings.mjs";
|
|
5
6
|
//#region src/engine/db/engineMigrator.ts
|
|
6
7
|
/**
|
|
7
8
|
* `hot-updater db migrate` for a store whose adapter creates its own tables
|
|
@@ -17,23 +18,45 @@ const createEngineMigrator = (options) => {
|
|
|
17
18
|
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
18
19
|
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
19
20
|
if (changed.length === 0) {
|
|
21
|
+
const columns = await addLateColumns(options.adapter, options.schema.tables, { dryRun: true });
|
|
20
22
|
const lacking = await fillLateIndexes(options.adapter, options.schema.tables, { dryRun: true });
|
|
21
|
-
if (lacking.length === 0) return getEmptyMigrationResult();
|
|
23
|
+
if (columns.length === 0 && lacking.length === 0) return getEmptyMigrationResult();
|
|
22
24
|
return {
|
|
23
|
-
operations: [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
operations: [
|
|
26
|
+
...columns.map((column) => ({
|
|
27
|
+
type: "custom",
|
|
28
|
+
description: `Add column: ${column}`
|
|
29
|
+
})),
|
|
30
|
+
...columns.length === 0 ? [] : [{
|
|
31
|
+
type: "custom",
|
|
32
|
+
description: "Create the tables and indexes the database lacks"
|
|
33
|
+
}],
|
|
34
|
+
...lacking.length === 0 ? [] : [{
|
|
35
|
+
type: "custom",
|
|
36
|
+
description: `Write the index items rows lack: ${lacking.join(", ")}`
|
|
37
|
+
}]
|
|
38
|
+
],
|
|
27
39
|
execute: async () => {
|
|
40
|
+
if (columns.length > 0) {
|
|
41
|
+
await addLateColumns(options.adapter, options.schema.tables);
|
|
42
|
+
await options.adapter.migrations.apply([...options.schema.tables, SETTINGS_TABLE]);
|
|
43
|
+
}
|
|
28
44
|
await fillLateIndexes(options.adapter, options.schema.tables);
|
|
29
45
|
}
|
|
30
46
|
};
|
|
31
47
|
}
|
|
32
48
|
return {
|
|
33
|
-
operations: [
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
49
|
+
operations: [
|
|
50
|
+
...(await addLateColumns(options.adapter, options.schema.tables, { dryRun: true }).catch(() => [])).map((column) => ({
|
|
51
|
+
type: "custom",
|
|
52
|
+
description: `Add column: ${column}`
|
|
53
|
+
})),
|
|
54
|
+
{
|
|
55
|
+
type: "custom",
|
|
56
|
+
description: "Create the tables and indexes the database lacks"
|
|
57
|
+
},
|
|
58
|
+
...migrate.updateSettings === false ? [] : [settingsOperation(stored, changed)]
|
|
59
|
+
],
|
|
37
60
|
execute: () => migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings, options.ordered)
|
|
38
61
|
};
|
|
39
62
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_schema = require("../core/schema.cjs");
|
|
1
2
|
const require_fence = require("../database/fence.cjs");
|
|
2
3
|
const require_sqlSchema = require("../database/sql/sqlSchema.cjs");
|
|
3
4
|
//#region src/engine/db/engineSql.ts
|
|
@@ -21,6 +22,30 @@ const settingsStatements = (dialect, settings, tablePrefix = "") => {
|
|
|
21
22
|
* database foreign keys: the engine keeps references itself.
|
|
22
23
|
*/
|
|
23
24
|
const generateEngineSql = (dialect, schema, settings, options = {}) => [...require_sqlSchema.createTableStatements(dialect, [...schema.tables, require_fence.SETTINGS_TABLE], options.tablePrefix), ...settingsStatements(dialect, settings, options.tablePrefix)];
|
|
25
|
+
/**
|
|
26
|
+
* A managed provider's numbered migration needs these: an
|
|
27
|
+
* `ALTER TABLE … ADD COLUMN` for each column added to a table a released
|
|
28
|
+
* schema created, where neither an `earlier` migration's `CREATE TABLE` nor
|
|
29
|
+
* an earlier `ALTER TABLE … ADD COLUMN` has it. A
|
|
30
|
+
* migration that diffs statements skips the changed `CREATE TABLE IF NOT
|
|
31
|
+
* EXISTS`, which an existing table ignores anyway, so the D1 and Supabase
|
|
32
|
+
* generators put these first, before the statements that index the column.
|
|
33
|
+
* SQLite has no `ADD COLUMN IF NOT EXISTS`, so only a migration that runs once
|
|
34
|
+
* holds them, never statements a later migration repeats.
|
|
35
|
+
*/
|
|
36
|
+
const lateColumnStatements = (dialect, schema, earlier, { tablePrefix = "" } = {}) => {
|
|
37
|
+
const quote = (name) => require_sqlSchema.quoteSql(dialect, name);
|
|
38
|
+
const shapes = require_sqlSchema.sqlTableShapes(dialect, schema.tables, tablePrefix);
|
|
39
|
+
return require_schema.LATE_COLUMNS.flatMap(({ table, column }) => {
|
|
40
|
+
const name = tablePrefix + table;
|
|
41
|
+
const created = earlier.find((sql) => sql.startsWith(`CREATE TABLE IF NOT EXISTS ${quote(name)} (`));
|
|
42
|
+
const declared = shapes.find((shape) => shape.name === name)?.columns.find((candidate) => candidate.name === column);
|
|
43
|
+
const added = earlier.some((sql) => sql.replace(" ADD COLUMN IF NOT EXISTS ", " ADD COLUMN ").startsWith(`ALTER TABLE ${quote(name)} ADD COLUMN ${quote(column)} `));
|
|
44
|
+
if (created === void 0 || declared === void 0 || added || created.includes(`${quote(column)} `)) return [];
|
|
45
|
+
return [`ALTER TABLE ${quote(name)} ADD COLUMN ${dialect === "postgresql" ? "IF NOT EXISTS " : ""}${quote(column)} ${declared.type}`];
|
|
46
|
+
});
|
|
47
|
+
};
|
|
24
48
|
//#endregion
|
|
25
49
|
exports.generateEngineSql = generateEngineSql;
|
|
50
|
+
exports.lateColumnStatements = lateColumnStatements;
|
|
26
51
|
exports.settingsStatements = settingsStatements;
|
|
@@ -12,5 +12,17 @@ interface EngineSqlOptions {
|
|
|
12
12
|
* database foreign keys: the engine keeps references itself.
|
|
13
13
|
*/
|
|
14
14
|
declare const generateEngineSql: (dialect: SqlDialect, schema: ResolvedSchema, settings: SchemaSettings, options?: EngineSqlOptions) => string[];
|
|
15
|
+
/**
|
|
16
|
+
* A managed provider's numbered migration needs these: an
|
|
17
|
+
* `ALTER TABLE … ADD COLUMN` for each column added to a table a released
|
|
18
|
+
* schema created, where neither an `earlier` migration's `CREATE TABLE` nor
|
|
19
|
+
* an earlier `ALTER TABLE … ADD COLUMN` has it. A
|
|
20
|
+
* migration that diffs statements skips the changed `CREATE TABLE IF NOT
|
|
21
|
+
* EXISTS`, which an existing table ignores anyway, so the D1 and Supabase
|
|
22
|
+
* generators put these first, before the statements that index the column.
|
|
23
|
+
* SQLite has no `ADD COLUMN IF NOT EXISTS`, so only a migration that runs once
|
|
24
|
+
* holds them, never statements a later migration repeats.
|
|
25
|
+
*/
|
|
26
|
+
declare const lateColumnStatements: (dialect: SqlDialect, schema: ResolvedSchema, earlier: readonly string[], { tablePrefix }?: EngineSqlOptions) => string[];
|
|
15
27
|
//#endregion
|
|
16
|
-
export { EngineSqlOptions, generateEngineSql };
|
|
28
|
+
export { EngineSqlOptions, generateEngineSql, lateColumnStatements };
|
|
@@ -12,5 +12,17 @@ interface EngineSqlOptions {
|
|
|
12
12
|
* database foreign keys: the engine keeps references itself.
|
|
13
13
|
*/
|
|
14
14
|
declare const generateEngineSql: (dialect: SqlDialect, schema: ResolvedSchema, settings: SchemaSettings, options?: EngineSqlOptions) => string[];
|
|
15
|
+
/**
|
|
16
|
+
* A managed provider's numbered migration needs these: an
|
|
17
|
+
* `ALTER TABLE … ADD COLUMN` for each column added to a table a released
|
|
18
|
+
* schema created, where neither an `earlier` migration's `CREATE TABLE` nor
|
|
19
|
+
* an earlier `ALTER TABLE … ADD COLUMN` has it. A
|
|
20
|
+
* migration that diffs statements skips the changed `CREATE TABLE IF NOT
|
|
21
|
+
* EXISTS`, which an existing table ignores anyway, so the D1 and Supabase
|
|
22
|
+
* generators put these first, before the statements that index the column.
|
|
23
|
+
* SQLite has no `ADD COLUMN IF NOT EXISTS`, so only a migration that runs once
|
|
24
|
+
* holds them, never statements a later migration repeats.
|
|
25
|
+
*/
|
|
26
|
+
declare const lateColumnStatements: (dialect: SqlDialect, schema: ResolvedSchema, earlier: readonly string[], { tablePrefix }?: EngineSqlOptions) => string[];
|
|
15
27
|
//#endregion
|
|
16
|
-
export { EngineSqlOptions, generateEngineSql };
|
|
28
|
+
export { EngineSqlOptions, generateEngineSql, lateColumnStatements };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { LATE_COLUMNS } from "../core/schema.mjs";
|
|
1
2
|
import { SETTINGS_TABLE } from "../database/fence.mjs";
|
|
2
|
-
import { createTableStatements, quoteSql } from "../database/sql/sqlSchema.mjs";
|
|
3
|
+
import { createTableStatements, quoteSql, sqlTableShapes } from "../database/sql/sqlSchema.mjs";
|
|
3
4
|
//#region src/engine/db/engineSql.ts
|
|
4
5
|
const literal = (text) => `'${text.replaceAll("'", "''")}'`;
|
|
5
6
|
/** Upserts of the settings rows the fence reads. */
|
|
@@ -21,5 +22,28 @@ const settingsStatements = (dialect, settings, tablePrefix = "") => {
|
|
|
21
22
|
* database foreign keys: the engine keeps references itself.
|
|
22
23
|
*/
|
|
23
24
|
const generateEngineSql = (dialect, schema, settings, options = {}) => [...createTableStatements(dialect, [...schema.tables, SETTINGS_TABLE], options.tablePrefix), ...settingsStatements(dialect, settings, options.tablePrefix)];
|
|
25
|
+
/**
|
|
26
|
+
* A managed provider's numbered migration needs these: an
|
|
27
|
+
* `ALTER TABLE … ADD COLUMN` for each column added to a table a released
|
|
28
|
+
* schema created, where neither an `earlier` migration's `CREATE TABLE` nor
|
|
29
|
+
* an earlier `ALTER TABLE … ADD COLUMN` has it. A
|
|
30
|
+
* migration that diffs statements skips the changed `CREATE TABLE IF NOT
|
|
31
|
+
* EXISTS`, which an existing table ignores anyway, so the D1 and Supabase
|
|
32
|
+
* generators put these first, before the statements that index the column.
|
|
33
|
+
* SQLite has no `ADD COLUMN IF NOT EXISTS`, so only a migration that runs once
|
|
34
|
+
* holds them, never statements a later migration repeats.
|
|
35
|
+
*/
|
|
36
|
+
const lateColumnStatements = (dialect, schema, earlier, { tablePrefix = "" } = {}) => {
|
|
37
|
+
const quote = (name) => quoteSql(dialect, name);
|
|
38
|
+
const shapes = sqlTableShapes(dialect, schema.tables, tablePrefix);
|
|
39
|
+
return LATE_COLUMNS.flatMap(({ table, column }) => {
|
|
40
|
+
const name = tablePrefix + table;
|
|
41
|
+
const created = earlier.find((sql) => sql.startsWith(`CREATE TABLE IF NOT EXISTS ${quote(name)} (`));
|
|
42
|
+
const declared = shapes.find((shape) => shape.name === name)?.columns.find((candidate) => candidate.name === column);
|
|
43
|
+
const added = earlier.some((sql) => sql.replace(" ADD COLUMN IF NOT EXISTS ", " ADD COLUMN ").startsWith(`ALTER TABLE ${quote(name)} ADD COLUMN ${quote(column)} `));
|
|
44
|
+
if (created === void 0 || declared === void 0 || added || created.includes(`${quote(column)} `)) return [];
|
|
45
|
+
return [`ALTER TABLE ${quote(name)} ADD COLUMN ${dialect === "postgresql" ? "IF NOT EXISTS " : ""}${quote(column)} ${declared.type}`];
|
|
46
|
+
});
|
|
47
|
+
};
|
|
24
48
|
//#endregion
|
|
25
|
-
export { generateEngineSql, settingsStatements };
|
|
49
|
+
export { generateEngineSql, lateColumnStatements, settingsStatements };
|
|
@@ -55,13 +55,19 @@ const createEngineSqlMigrator = (options) => {
|
|
|
55
55
|
require_settingsMigrator.refuseV0Database(options.adapterName, stored);
|
|
56
56
|
require_settingsMigrator.refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
57
57
|
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
58
|
-
|
|
58
|
+
const missing = require_sqlSchemaState.missingSqlSchema(require_sqlSchemaState.targetShapes(options.executor.dialect, options.schema, options.tablePrefix).filter((shape) => shape.name !== (options.tablePrefix ?? "") + require_fence.SETTINGS_TABLE.name), await require_sqlSchemaState.readSqlSchemaState(options.executor), options.tablePrefix);
|
|
59
|
+
if (changed.length === 0 && missing.columns.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
60
|
+
const addColumns = require_sqlSchemaState.addColumnStatements(options.executor.dialect, missing.columns);
|
|
59
61
|
const statements = require_engineSql.generateEngineSql(options.executor.dialect, options.schema, options.settings, options);
|
|
60
62
|
const settingsSql = require_engineSql.settingsStatements(options.executor.dialect, options.settings, options.tablePrefix);
|
|
61
|
-
const tablesSql = statements.slice(0, -settingsSql.length || void 0);
|
|
62
|
-
const
|
|
63
|
+
const tablesSql = [...addColumns, ...statements.slice(0, -settingsSql.length || void 0)];
|
|
64
|
+
const writesSettings = changed.length > 0 && migrate.updateSettings !== false;
|
|
63
65
|
return {
|
|
64
66
|
operations: [
|
|
67
|
+
...missing.columns.map(({ table, column }) => ({
|
|
68
|
+
type: "custom",
|
|
69
|
+
description: `Add column: ${column.name} to ${table}`
|
|
70
|
+
})),
|
|
65
71
|
...missing.tables.map((table) => ({
|
|
66
72
|
type: "create-table",
|
|
67
73
|
value: {
|
|
@@ -73,10 +79,10 @@ const createEngineSqlMigrator = (options) => {
|
|
|
73
79
|
type: "custom",
|
|
74
80
|
description: `Create index: ${index} on ${table}`
|
|
75
81
|
})),
|
|
76
|
-
...
|
|
82
|
+
...writesSettings ? [require_settingsMigrator.settingsOperation(stored, changed)] : []
|
|
77
83
|
],
|
|
78
|
-
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
79
|
-
execute: () => run(options.executor, tablesSql, settingsSql, options.schema, options.tablePrefix)
|
|
84
|
+
getSQL: () => [...addColumns, ...statements].map((statement) => `${statement};`).join("\n\n"),
|
|
85
|
+
execute: () => run(options.executor, tablesSql, changed.length > 0 ? settingsSql : [], options.schema, options.tablePrefix)
|
|
80
86
|
};
|
|
81
87
|
};
|
|
82
88
|
return {
|
|
@@ -3,7 +3,7 @@ import { SETTINGS_TABLE } from "../database/fence.mjs";
|
|
|
3
3
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
4
4
|
import { quoteSql } from "../database/sql/sqlSchema.mjs";
|
|
5
5
|
import { generateEngineSql, settingsStatements } from "./engineSql.mjs";
|
|
6
|
-
import { missingSqlSchema, readSqlSchemaState, targetShapes } from "./sqlSchemaState.mjs";
|
|
6
|
+
import { addColumnStatements, missingSqlSchema, readSqlSchemaState, targetShapes } from "./sqlSchemaState.mjs";
|
|
7
7
|
import { readStoredSettings, refuseNewerSchema, refuseV0Database, settingsOperation, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
8
8
|
//#region src/engine/db/engineSqlMigrator.ts
|
|
9
9
|
/**
|
|
@@ -55,13 +55,19 @@ const createEngineSqlMigrator = (options) => {
|
|
|
55
55
|
refuseV0Database(options.adapterName, stored);
|
|
56
56
|
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
57
57
|
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
58
|
-
|
|
58
|
+
const missing = missingSqlSchema(targetShapes(options.executor.dialect, options.schema, options.tablePrefix).filter((shape) => shape.name !== (options.tablePrefix ?? "") + SETTINGS_TABLE.name), await readSqlSchemaState(options.executor), options.tablePrefix);
|
|
59
|
+
if (changed.length === 0 && missing.columns.length === 0) return getEmptyMigrationResult();
|
|
60
|
+
const addColumns = addColumnStatements(options.executor.dialect, missing.columns);
|
|
59
61
|
const statements = generateEngineSql(options.executor.dialect, options.schema, options.settings, options);
|
|
60
62
|
const settingsSql = settingsStatements(options.executor.dialect, options.settings, options.tablePrefix);
|
|
61
|
-
const tablesSql = statements.slice(0, -settingsSql.length || void 0);
|
|
62
|
-
const
|
|
63
|
+
const tablesSql = [...addColumns, ...statements.slice(0, -settingsSql.length || void 0)];
|
|
64
|
+
const writesSettings = changed.length > 0 && migrate.updateSettings !== false;
|
|
63
65
|
return {
|
|
64
66
|
operations: [
|
|
67
|
+
...missing.columns.map(({ table, column }) => ({
|
|
68
|
+
type: "custom",
|
|
69
|
+
description: `Add column: ${column.name} to ${table}`
|
|
70
|
+
})),
|
|
65
71
|
...missing.tables.map((table) => ({
|
|
66
72
|
type: "create-table",
|
|
67
73
|
value: {
|
|
@@ -73,10 +79,10 @@ const createEngineSqlMigrator = (options) => {
|
|
|
73
79
|
type: "custom",
|
|
74
80
|
description: `Create index: ${index} on ${table}`
|
|
75
81
|
})),
|
|
76
|
-
...
|
|
82
|
+
...writesSettings ? [settingsOperation(stored, changed)] : []
|
|
77
83
|
],
|
|
78
|
-
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
79
|
-
execute: () => run(options.executor, tablesSql, settingsSql, options.schema, options.tablePrefix)
|
|
84
|
+
getSQL: () => [...addColumns, ...statements].map((statement) => `${statement};`).join("\n\n"),
|
|
85
|
+
execute: () => run(options.executor, tablesSql, changed.length > 0 ? settingsSql : [], options.schema, options.tablePrefix)
|
|
80
86
|
};
|
|
81
87
|
};
|
|
82
88
|
return {
|
|
@@ -54,8 +54,26 @@ const fillLateIndexes = async (adapter, tables, { dryRun = false } = {}) => {
|
|
|
54
54
|
return lacking;
|
|
55
55
|
};
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
57
|
+
* The `LATE_COLUMNS` among `tables` that an existing table lacks, as
|
|
58
|
+
* `<table>.<column>`, added unless `dryRun`: none on a store without
|
|
59
|
+
* `migrations.addColumns`, or for a table not created yet.
|
|
60
|
+
*/
|
|
61
|
+
const addLateColumns = async (adapter, tables, { dryRun = false } = {}) => {
|
|
62
|
+
const addColumns = adapter.migrations?.addColumns;
|
|
63
|
+
if (addColumns === void 0) return [];
|
|
64
|
+
const lacking = [];
|
|
65
|
+
for (const table of tables) {
|
|
66
|
+
const columns = require_schema.LATE_COLUMNS.filter(({ table: name }) => name === table.name).map(({ column }) => column);
|
|
67
|
+
if (columns.length === 0) continue;
|
|
68
|
+
const added = await addColumns(table, columns, { dryRun });
|
|
69
|
+
lacking.push(...added.map((column) => `${table.name}.${column}`));
|
|
70
|
+
}
|
|
71
|
+
return lacking;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Adds the late columns existing tables lack, creates or updates every
|
|
75
|
+
* table, writes the late indexes' missing items, then writes the settings
|
|
76
|
+
* rows last. A database a newer release migrated is
|
|
59
77
|
* refused before anything is written: its settings are read first, and a
|
|
60
78
|
* store without them yet, whose read fails, has none to refuse.
|
|
61
79
|
*/
|
|
@@ -63,11 +81,13 @@ const migrateSchema = async (adapter, adapterName, tables, settings, ordered) =>
|
|
|
63
81
|
if (adapter.migrations === void 0) throw new Error(`${adapterName} creates its tables with its own tooling; write only the settings rows.`);
|
|
64
82
|
const stored = await storedSettings(adapter, Object.keys(settings)).catch(() => void 0);
|
|
65
83
|
if (stored !== void 0) require_settingsMigrator.refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
84
|
+
await addLateColumns(adapter, tables);
|
|
66
85
|
await adapter.migrations.apply([...tables, require_fence.SETTINGS_TABLE]);
|
|
67
86
|
await fillLateIndexes(adapter, tables);
|
|
68
87
|
await writeSchemaSettings(adapter, adapterName, settings, ordered);
|
|
69
88
|
};
|
|
70
89
|
//#endregion
|
|
90
|
+
exports.addLateColumns = addLateColumns;
|
|
71
91
|
exports.fillLateIndexes = fillLateIndexes;
|
|
72
92
|
exports.migrateSchema = migrateSchema;
|
|
73
93
|
exports.writeSchemaSettings = writeSchemaSettings;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LATE_INDEXES } from "../core/schema.mjs";
|
|
1
|
+
import { LATE_COLUMNS, LATE_INDEXES } from "../core/schema.mjs";
|
|
2
2
|
import { SETTINGS_TABLE, readSettings } from "../database/fence.mjs";
|
|
3
3
|
import { refuseNewerSchema } from "./settingsMigrator.mjs";
|
|
4
4
|
//#region src/engine/db/schemaSettings.ts
|
|
@@ -54,8 +54,26 @@ const fillLateIndexes = async (adapter, tables, { dryRun = false } = {}) => {
|
|
|
54
54
|
return lacking;
|
|
55
55
|
};
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
57
|
+
* The `LATE_COLUMNS` among `tables` that an existing table lacks, as
|
|
58
|
+
* `<table>.<column>`, added unless `dryRun`: none on a store without
|
|
59
|
+
* `migrations.addColumns`, or for a table not created yet.
|
|
60
|
+
*/
|
|
61
|
+
const addLateColumns = async (adapter, tables, { dryRun = false } = {}) => {
|
|
62
|
+
const addColumns = adapter.migrations?.addColumns;
|
|
63
|
+
if (addColumns === void 0) return [];
|
|
64
|
+
const lacking = [];
|
|
65
|
+
for (const table of tables) {
|
|
66
|
+
const columns = LATE_COLUMNS.filter(({ table: name }) => name === table.name).map(({ column }) => column);
|
|
67
|
+
if (columns.length === 0) continue;
|
|
68
|
+
const added = await addColumns(table, columns, { dryRun });
|
|
69
|
+
lacking.push(...added.map((column) => `${table.name}.${column}`));
|
|
70
|
+
}
|
|
71
|
+
return lacking;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Adds the late columns existing tables lack, creates or updates every
|
|
75
|
+
* table, writes the late indexes' missing items, then writes the settings
|
|
76
|
+
* rows last. A database a newer release migrated is
|
|
59
77
|
* refused before anything is written: its settings are read first, and a
|
|
60
78
|
* store without them yet, whose read fails, has none to refuse.
|
|
61
79
|
*/
|
|
@@ -63,9 +81,10 @@ const migrateSchema = async (adapter, adapterName, tables, settings, ordered) =>
|
|
|
63
81
|
if (adapter.migrations === void 0) throw new Error(`${adapterName} creates its tables with its own tooling; write only the settings rows.`);
|
|
64
82
|
const stored = await storedSettings(adapter, Object.keys(settings)).catch(() => void 0);
|
|
65
83
|
if (stored !== void 0) refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
84
|
+
await addLateColumns(adapter, tables);
|
|
66
85
|
await adapter.migrations.apply([...tables, SETTINGS_TABLE]);
|
|
67
86
|
await fillLateIndexes(adapter, tables);
|
|
68
87
|
await writeSchemaSettings(adapter, adapterName, settings, ordered);
|
|
69
88
|
};
|
|
70
89
|
//#endregion
|
|
71
|
-
export { fillLateIndexes, migrateSchema, writeSchemaSettings };
|
|
90
|
+
export { addLateColumns, fillLateIndexes, migrateSchema, writeSchemaSettings };
|
|
@@ -36,17 +36,6 @@ const refuseV0Database = (adapterName, stored) => {
|
|
|
36
36
|
if (typeof version !== "string" || stored.has("schema.engine")) return;
|
|
37
37
|
throw new require_fence.HotUpdaterSchemaMigrationRequiredError(adapterName, version);
|
|
38
38
|
};
|
|
39
|
-
/** Compares dotted numeric versions, such as "1" and "1.2.0"; NaN when either is not one. */
|
|
40
|
-
const compareVersions = (left, right) => {
|
|
41
|
-
const parse = (version) => /^\d+(?:\.\d+)*$/u.test(version) ? version.split(".").map(Number) : [NaN];
|
|
42
|
-
const [a, b] = [parse(left), parse(right)];
|
|
43
|
-
if ([...a, ...b].some(Number.isNaN)) return NaN;
|
|
44
|
-
for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
|
|
45
|
-
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
46
|
-
if (difference !== 0) return difference;
|
|
47
|
-
}
|
|
48
|
-
return 0;
|
|
49
|
-
};
|
|
50
39
|
/**
|
|
51
40
|
* The settings whose versions only move forward when a `ToolingTarget` names
|
|
52
41
|
* none. `schema.engine` is a storage layout, not a release: a store refuses
|
|
@@ -66,7 +55,7 @@ const refuseNewerSchema = (adapterName, stored, settings, ordered = DEFAULT_ORDE
|
|
|
66
55
|
const newer = ordered.flatMap((key) => {
|
|
67
56
|
const found = stored.get(key);
|
|
68
57
|
const expected = settings[key];
|
|
69
|
-
return typeof found === "string" && expected !== void 0 && compareVersions(found, expected) > 0 ? [{
|
|
58
|
+
return typeof found === "string" && expected !== void 0 && require_fence.compareVersions(found, expected) > 0 ? [{
|
|
70
59
|
key,
|
|
71
60
|
found,
|
|
72
61
|
expected
|
|
@@ -77,7 +66,7 @@ const refuseNewerSchema = (adapterName, stored, settings, ordered = DEFAULT_ORDE
|
|
|
77
66
|
throw new Error(`${adapterName}: the database was migrated by Hot Updater ${core.found} (${newer.map(({ key, found, expected }) => `${key} "${found}", this CLI's "${expected}"`).join("; ")}). Upgrade hot-updater to ${core.found} or later; nothing was written.`);
|
|
78
67
|
};
|
|
79
68
|
/**
|
|
80
|
-
* The tables or indexes an ORM has yet to apply, found before `db migrate`
|
|
69
|
+
* The tables, late columns or indexes an ORM has yet to apply, found before `db migrate`
|
|
81
70
|
* writes the settings rows, so the fence never passes over missing tables.
|
|
82
71
|
*/
|
|
83
72
|
var HotUpdaterSchemaTablesMissingError = class extends Error {
|
|
@@ -86,7 +75,11 @@ var HotUpdaterSchemaTablesMissingError = class extends Error {
|
|
|
86
75
|
applyTables;
|
|
87
76
|
constructor(adapterName, missing, applyTables) {
|
|
88
77
|
const all = missing.tables.includes(require_fence.SETTINGS_TABLE.name);
|
|
89
|
-
const items = [
|
|
78
|
+
const items = [
|
|
79
|
+
...missing.tables.length === 0 ? [] : [`table${missing.tables.length === 1 ? "" : "s"} ${missing.tables.join(", ")}`],
|
|
80
|
+
...missing.columns.length === 0 ? [] : [`column${missing.columns.length === 1 ? "" : "s"} ${missing.columns.map(({ table, column }) => `${table}.${column.name}`).join(", ")}`],
|
|
81
|
+
...missing.indexes.length === 0 ? [] : [`index${missing.indexes.length === 1 ? "" : "es"} ${missing.indexes.map(({ index }) => index).join(", ")}`]
|
|
82
|
+
];
|
|
90
83
|
super(`${adapterName}: ${all ? "the Hot Updater tables are missing" : `the database lacks ${items.join(" and ")}`}. Run \`hot-updater db generate\`, apply it with ${applyTables}, then migrate again.`);
|
|
91
84
|
this.adapterName = adapterName;
|
|
92
85
|
this.missing = missing;
|
|
@@ -112,19 +105,46 @@ const createSettingsMigrator = (options) => {
|
|
|
112
105
|
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
113
106
|
const shapes = require_sqlSchemaState.targetShapes(executor.dialect, options.schema);
|
|
114
107
|
const missing = require_sqlSchemaState.missingSqlSchema(shapes, await require_sqlSchemaState.readSqlSchemaState(executor));
|
|
115
|
-
|
|
108
|
+
const lateTables = new Set(missing.columns.map(({ table }) => table));
|
|
109
|
+
const lateIndexes = missing.indexes.filter(({ table }) => lateTables.has(table));
|
|
110
|
+
const blocking = missing.indexes.filter(({ table }) => !lateTables.has(table));
|
|
111
|
+
if (missing.tables.length > 0 || blocking.length > 0) throw new HotUpdaterSchemaTablesMissingError(options.adapterName, {
|
|
112
|
+
tables: missing.tables,
|
|
113
|
+
columns: [],
|
|
114
|
+
indexes: blocking
|
|
115
|
+
}, options.applyTables);
|
|
116
|
+
const quote = (name) => require_sqlSchema.quoteSql(executor.dialect, name);
|
|
117
|
+
const lateSchema = [...require_sqlSchemaState.addColumnStatements(executor.dialect, missing.columns), ...lateIndexes.map(({ table, index: name }) => {
|
|
118
|
+
const index = shapes.find((shape) => shape.name === table).indexes.find((candidate) => candidate.kind === "index" && candidate.name === name);
|
|
119
|
+
return index.kind === "index" ? `CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quote(name)} ON ${quote(table)} (${index.columns.map(quote).join(", ")})` : "";
|
|
120
|
+
})];
|
|
116
121
|
const collations = options.collate === void 0 ? {
|
|
117
122
|
statements: [],
|
|
118
123
|
columns: 0
|
|
119
124
|
} : await require_sqlSchemaState.collationStatements(executor, shapes.filter((shape) => shape.name !== require_fence.SETTINGS_TABLE.name), options.collate);
|
|
120
125
|
const changed = migrate.updateSettings === false ? [] : Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
121
|
-
if (collations.statements.length === 0 && changed.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
122
|
-
const statements = [
|
|
126
|
+
if (lateSchema.length === 0 && collations.statements.length === 0 && changed.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
127
|
+
const statements = [
|
|
128
|
+
...lateSchema,
|
|
129
|
+
...collations.statements,
|
|
130
|
+
...changed.length === 0 ? [] : require_engineSql.settingsStatements(executor.dialect, options.settings)
|
|
131
|
+
];
|
|
123
132
|
return {
|
|
124
|
-
operations: [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
133
|
+
operations: [
|
|
134
|
+
...missing.columns.map(({ table, column }) => ({
|
|
135
|
+
type: "custom",
|
|
136
|
+
description: `Add column: ${column.name} to ${table}`
|
|
137
|
+
})),
|
|
138
|
+
...lateIndexes.map(({ table, index }) => ({
|
|
139
|
+
type: "custom",
|
|
140
|
+
description: `Create index: ${index} on ${table}`
|
|
141
|
+
})),
|
|
142
|
+
...collations.columns === 0 ? [] : [{
|
|
143
|
+
type: "custom",
|
|
144
|
+
description: `Set the byte-order collation the ORM cannot declare on ${collations.columns} column${collations.columns === 1 ? "" : "s"} of ${collations.statements.length} table${collations.statements.length === 1 ? "" : "s"}`
|
|
145
|
+
}],
|
|
146
|
+
...changed.length === 0 ? [] : [settingsOperation(stored, changed)]
|
|
147
|
+
],
|
|
128
148
|
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
129
149
|
execute: async () => {
|
|
130
150
|
for (const sql of statements) await executor.execute({
|