@hot-updater/plugin-core 1.2.0-rc.50 → 1.2.0-rc.51
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/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 +40 -9
- package/dist/engine/db/settingsMigrator.mjs +41 -10
- package/dist/engine/db/sqlSchemaState.cjs +36 -8
- package/dist/engine/db/sqlSchemaState.mjs +36 -9
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +2 -2
- package/dist/releaseCatalogCompiler.cjs +18 -7
- package/dist/releaseCatalogCompiler.mjs +18 -7
- package/dist/releaseCatalogMutation.d.cts +6 -0
- package/dist/releaseCatalogMutation.d.mts +6 -0
- package/dist/types/databaseRows.d.cts +7 -0
- package/dist/types/databaseRows.d.mts +7 -0
- package/package.json +2 -2
|
@@ -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 };
|
|
@@ -77,7 +77,7 @@ const refuseNewerSchema = (adapterName, stored, settings, ordered = DEFAULT_ORDE
|
|
|
77
77
|
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
78
|
};
|
|
79
79
|
/**
|
|
80
|
-
* The tables or indexes an ORM has yet to apply, found before `db migrate`
|
|
80
|
+
* The tables, late columns or indexes an ORM has yet to apply, found before `db migrate`
|
|
81
81
|
* writes the settings rows, so the fence never passes over missing tables.
|
|
82
82
|
*/
|
|
83
83
|
var HotUpdaterSchemaTablesMissingError = class extends Error {
|
|
@@ -86,7 +86,11 @@ var HotUpdaterSchemaTablesMissingError = class extends Error {
|
|
|
86
86
|
applyTables;
|
|
87
87
|
constructor(adapterName, missing, applyTables) {
|
|
88
88
|
const all = missing.tables.includes(require_fence.SETTINGS_TABLE.name);
|
|
89
|
-
const items = [
|
|
89
|
+
const items = [
|
|
90
|
+
...missing.tables.length === 0 ? [] : [`table${missing.tables.length === 1 ? "" : "s"} ${missing.tables.join(", ")}`],
|
|
91
|
+
...missing.columns.length === 0 ? [] : [`column${missing.columns.length === 1 ? "" : "s"} ${missing.columns.map(({ table, column }) => `${table}.${column.name}`).join(", ")}`],
|
|
92
|
+
...missing.indexes.length === 0 ? [] : [`index${missing.indexes.length === 1 ? "" : "es"} ${missing.indexes.map(({ index }) => index).join(", ")}`]
|
|
93
|
+
];
|
|
90
94
|
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
95
|
this.adapterName = adapterName;
|
|
92
96
|
this.missing = missing;
|
|
@@ -112,19 +116,46 @@ const createSettingsMigrator = (options) => {
|
|
|
112
116
|
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
113
117
|
const shapes = require_sqlSchemaState.targetShapes(executor.dialect, options.schema);
|
|
114
118
|
const missing = require_sqlSchemaState.missingSqlSchema(shapes, await require_sqlSchemaState.readSqlSchemaState(executor));
|
|
115
|
-
|
|
119
|
+
const lateTables = new Set(missing.columns.map(({ table }) => table));
|
|
120
|
+
const lateIndexes = missing.indexes.filter(({ table }) => lateTables.has(table));
|
|
121
|
+
const blocking = missing.indexes.filter(({ table }) => !lateTables.has(table));
|
|
122
|
+
if (missing.tables.length > 0 || blocking.length > 0) throw new HotUpdaterSchemaTablesMissingError(options.adapterName, {
|
|
123
|
+
tables: missing.tables,
|
|
124
|
+
columns: [],
|
|
125
|
+
indexes: blocking
|
|
126
|
+
}, options.applyTables);
|
|
127
|
+
const quote = (name) => require_sqlSchema.quoteSql(executor.dialect, name);
|
|
128
|
+
const lateSchema = [...require_sqlSchemaState.addColumnStatements(executor.dialect, missing.columns), ...lateIndexes.map(({ table, index: name }) => {
|
|
129
|
+
const index = shapes.find((shape) => shape.name === table).indexes.find((candidate) => candidate.kind === "index" && candidate.name === name);
|
|
130
|
+
return index.kind === "index" ? `CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quote(name)} ON ${quote(table)} (${index.columns.map(quote).join(", ")})` : "";
|
|
131
|
+
})];
|
|
116
132
|
const collations = options.collate === void 0 ? {
|
|
117
133
|
statements: [],
|
|
118
134
|
columns: 0
|
|
119
135
|
} : await require_sqlSchemaState.collationStatements(executor, shapes.filter((shape) => shape.name !== require_fence.SETTINGS_TABLE.name), options.collate);
|
|
120
136
|
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 = [
|
|
137
|
+
if (lateSchema.length === 0 && collations.statements.length === 0 && changed.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
138
|
+
const statements = [
|
|
139
|
+
...lateSchema,
|
|
140
|
+
...collations.statements,
|
|
141
|
+
...changed.length === 0 ? [] : require_engineSql.settingsStatements(executor.dialect, options.settings)
|
|
142
|
+
];
|
|
123
143
|
return {
|
|
124
|
-
operations: [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
144
|
+
operations: [
|
|
145
|
+
...missing.columns.map(({ table, column }) => ({
|
|
146
|
+
type: "custom",
|
|
147
|
+
description: `Add column: ${column.name} to ${table}`
|
|
148
|
+
})),
|
|
149
|
+
...lateIndexes.map(({ table, index }) => ({
|
|
150
|
+
type: "custom",
|
|
151
|
+
description: `Create index: ${index} on ${table}`
|
|
152
|
+
})),
|
|
153
|
+
...collations.columns === 0 ? [] : [{
|
|
154
|
+
type: "custom",
|
|
155
|
+
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"}`
|
|
156
|
+
}],
|
|
157
|
+
...changed.length === 0 ? [] : [settingsOperation(stored, changed)]
|
|
158
|
+
],
|
|
128
159
|
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
129
160
|
execute: async () => {
|
|
130
161
|
for (const sql of statements) await executor.execute({
|
|
@@ -3,7 +3,7 @@ import { HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE, isMissingSchema
|
|
|
3
3
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
4
4
|
import { quoteSql } from "../database/sql/sqlSchema.mjs";
|
|
5
5
|
import { settingsStatements } from "./engineSql.mjs";
|
|
6
|
-
import { collationStatements, missingSqlSchema, readSqlSchemaState, targetShapes } from "./sqlSchemaState.mjs";
|
|
6
|
+
import { addColumnStatements, collationStatements, missingSqlSchema, readSqlSchemaState, targetShapes } from "./sqlSchemaState.mjs";
|
|
7
7
|
//#region src/engine/db/settingsMigrator.ts
|
|
8
8
|
/**
|
|
9
9
|
* Every settings row by key and value alone, so a v0 layout reads too; null
|
|
@@ -77,7 +77,7 @@ const refuseNewerSchema = (adapterName, stored, settings, ordered = DEFAULT_ORDE
|
|
|
77
77
|
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
78
|
};
|
|
79
79
|
/**
|
|
80
|
-
* The tables or indexes an ORM has yet to apply, found before `db migrate`
|
|
80
|
+
* The tables, late columns or indexes an ORM has yet to apply, found before `db migrate`
|
|
81
81
|
* writes the settings rows, so the fence never passes over missing tables.
|
|
82
82
|
*/
|
|
83
83
|
var HotUpdaterSchemaTablesMissingError = class extends Error {
|
|
@@ -86,7 +86,11 @@ var HotUpdaterSchemaTablesMissingError = class extends Error {
|
|
|
86
86
|
applyTables;
|
|
87
87
|
constructor(adapterName, missing, applyTables) {
|
|
88
88
|
const all = missing.tables.includes(SETTINGS_TABLE.name);
|
|
89
|
-
const items = [
|
|
89
|
+
const items = [
|
|
90
|
+
...missing.tables.length === 0 ? [] : [`table${missing.tables.length === 1 ? "" : "s"} ${missing.tables.join(", ")}`],
|
|
91
|
+
...missing.columns.length === 0 ? [] : [`column${missing.columns.length === 1 ? "" : "s"} ${missing.columns.map(({ table, column }) => `${table}.${column.name}`).join(", ")}`],
|
|
92
|
+
...missing.indexes.length === 0 ? [] : [`index${missing.indexes.length === 1 ? "" : "es"} ${missing.indexes.map(({ index }) => index).join(", ")}`]
|
|
93
|
+
];
|
|
90
94
|
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
95
|
this.adapterName = adapterName;
|
|
92
96
|
this.missing = missing;
|
|
@@ -112,19 +116,46 @@ const createSettingsMigrator = (options) => {
|
|
|
112
116
|
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
113
117
|
const shapes = targetShapes(executor.dialect, options.schema);
|
|
114
118
|
const missing = missingSqlSchema(shapes, await readSqlSchemaState(executor));
|
|
115
|
-
|
|
119
|
+
const lateTables = new Set(missing.columns.map(({ table }) => table));
|
|
120
|
+
const lateIndexes = missing.indexes.filter(({ table }) => lateTables.has(table));
|
|
121
|
+
const blocking = missing.indexes.filter(({ table }) => !lateTables.has(table));
|
|
122
|
+
if (missing.tables.length > 0 || blocking.length > 0) throw new HotUpdaterSchemaTablesMissingError(options.adapterName, {
|
|
123
|
+
tables: missing.tables,
|
|
124
|
+
columns: [],
|
|
125
|
+
indexes: blocking
|
|
126
|
+
}, options.applyTables);
|
|
127
|
+
const quote = (name) => quoteSql(executor.dialect, name);
|
|
128
|
+
const lateSchema = [...addColumnStatements(executor.dialect, missing.columns), ...lateIndexes.map(({ table, index: name }) => {
|
|
129
|
+
const index = shapes.find((shape) => shape.name === table).indexes.find((candidate) => candidate.kind === "index" && candidate.name === name);
|
|
130
|
+
return index.kind === "index" ? `CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quote(name)} ON ${quote(table)} (${index.columns.map(quote).join(", ")})` : "";
|
|
131
|
+
})];
|
|
116
132
|
const collations = options.collate === void 0 ? {
|
|
117
133
|
statements: [],
|
|
118
134
|
columns: 0
|
|
119
135
|
} : await collationStatements(executor, shapes.filter((shape) => shape.name !== SETTINGS_TABLE.name), options.collate);
|
|
120
136
|
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 getEmptyMigrationResult();
|
|
122
|
-
const statements = [
|
|
137
|
+
if (lateSchema.length === 0 && collations.statements.length === 0 && changed.length === 0) return getEmptyMigrationResult();
|
|
138
|
+
const statements = [
|
|
139
|
+
...lateSchema,
|
|
140
|
+
...collations.statements,
|
|
141
|
+
...changed.length === 0 ? [] : settingsStatements(executor.dialect, options.settings)
|
|
142
|
+
];
|
|
123
143
|
return {
|
|
124
|
-
operations: [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
144
|
+
operations: [
|
|
145
|
+
...missing.columns.map(({ table, column }) => ({
|
|
146
|
+
type: "custom",
|
|
147
|
+
description: `Add column: ${column.name} to ${table}`
|
|
148
|
+
})),
|
|
149
|
+
...lateIndexes.map(({ table, index }) => ({
|
|
150
|
+
type: "custom",
|
|
151
|
+
description: `Create index: ${index} on ${table}`
|
|
152
|
+
})),
|
|
153
|
+
...collations.columns === 0 ? [] : [{
|
|
154
|
+
type: "custom",
|
|
155
|
+
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"}`
|
|
156
|
+
}],
|
|
157
|
+
...changed.length === 0 ? [] : [settingsOperation(stored, changed)]
|
|
158
|
+
],
|
|
128
159
|
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
129
160
|
execute: async () => {
|
|
130
161
|
for (const sql of statements) await executor.execute({
|
|
@@ -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/sqlSchemaState.ts
|
|
@@ -29,34 +30,54 @@ const fold = (dialect, name) => dialect === "mysql" ? String(name).toLowerCase()
|
|
|
29
30
|
const CATALOG = {
|
|
30
31
|
postgresql: {
|
|
31
32
|
tables: "SELECT c.relname AS t FROM pg_class c WHERE c.relkind IN ('r', 'p') AND pg_table_is_visible(c.oid)",
|
|
32
|
-
indexes: "SELECT t.relname AS t, c.relname AS i FROM pg_index x JOIN pg_class c ON c.oid = x.indexrelid JOIN pg_class t ON t.oid = x.indrelid WHERE pg_table_is_visible(t.oid)"
|
|
33
|
+
indexes: "SELECT t.relname AS t, c.relname AS i FROM pg_index x JOIN pg_class c ON c.oid = x.indexrelid JOIN pg_class t ON t.oid = x.indrelid WHERE pg_table_is_visible(t.oid)",
|
|
34
|
+
columns: "SELECT c.relname AS t, a.attname AS c FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid WHERE c.relkind IN ('r', 'p') AND a.attnum > 0 AND NOT a.attisdropped AND pg_table_is_visible(c.oid)"
|
|
33
35
|
},
|
|
34
36
|
mysql: {
|
|
35
37
|
tables: "SELECT TABLE_NAME AS t FROM information_schema.tables WHERE TABLE_SCHEMA = DATABASE()",
|
|
36
|
-
indexes: "SELECT DISTINCT TABLE_NAME AS t, INDEX_NAME AS i FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE()"
|
|
38
|
+
indexes: "SELECT DISTINCT TABLE_NAME AS t, INDEX_NAME AS i FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE()",
|
|
39
|
+
columns: "SELECT TABLE_NAME AS t, COLUMN_NAME AS c FROM information_schema.columns WHERE TABLE_SCHEMA = DATABASE()"
|
|
37
40
|
},
|
|
38
41
|
sqlite: {
|
|
39
42
|
tables: "SELECT name AS t FROM sqlite_master WHERE type = 'table'",
|
|
40
|
-
indexes: "SELECT tbl_name AS t, name AS i FROM sqlite_master WHERE type = 'index'"
|
|
43
|
+
indexes: "SELECT tbl_name AS t, name AS i FROM sqlite_master WHERE type = 'index'",
|
|
44
|
+
columns: "SELECT m.name AS t, p.name AS c FROM sqlite_master m JOIN pragma_table_info(m.name) p WHERE m.type = 'table'"
|
|
41
45
|
}
|
|
42
46
|
};
|
|
43
|
-
/** Reads which tables and
|
|
47
|
+
/** Reads which tables, indexes and columns exist: three catalog reads. */
|
|
44
48
|
const readSqlSchemaState = async (executor) => {
|
|
45
49
|
const { dialect } = executor;
|
|
46
50
|
const read = (sql) => executor.execute({
|
|
47
51
|
sql,
|
|
48
52
|
params: []
|
|
49
53
|
});
|
|
50
|
-
const [tables, indexes] = await Promise.all([
|
|
54
|
+
const [tables, indexes, columns] = await Promise.all([
|
|
55
|
+
read(CATALOG[dialect].tables),
|
|
56
|
+
read(CATALOG[dialect].indexes),
|
|
57
|
+
read(CATALOG[dialect].columns)
|
|
58
|
+
]);
|
|
59
|
+
const columnNames = new Set(columns.rows.map(({ t, c }) => JSON.stringify([fold(dialect, t), fold(dialect, c)])));
|
|
51
60
|
const tableNames = new Set(tables.rows.map(({ t }) => fold(dialect, t)));
|
|
52
61
|
const indexNames = new Set(indexes.rows.map(({ t, i }) => JSON.stringify([fold(dialect, t), fold(dialect, i)])));
|
|
53
62
|
return {
|
|
54
63
|
hasTable: (table) => tableNames.has(fold(dialect, table)),
|
|
55
|
-
hasIndex: (table, index) => indexNames.has(JSON.stringify([fold(dialect, table), fold(dialect, index)]))
|
|
64
|
+
hasIndex: (table, index) => indexNames.has(JSON.stringify([fold(dialect, table), fold(dialect, index)])),
|
|
65
|
+
hasColumn: (table, column) => columnNames.has(JSON.stringify([fold(dialect, table), fold(dialect, column)]))
|
|
56
66
|
};
|
|
57
67
|
};
|
|
58
|
-
/**
|
|
59
|
-
|
|
68
|
+
/**
|
|
69
|
+
* The declared tables, indexes and late columns the database lacks. An index
|
|
70
|
+
* on a late column is missing too while the column is.
|
|
71
|
+
*/
|
|
72
|
+
const missingSqlSchema = (shapes, state, tablePrefix = "") => ({
|
|
73
|
+
columns: require_schema.LATE_COLUMNS.flatMap(({ table, column }) => {
|
|
74
|
+
const shape = shapes.find(({ name }) => name === tablePrefix + table);
|
|
75
|
+
const declared = shape?.columns.find(({ name }) => name === column);
|
|
76
|
+
return shape === void 0 || declared === void 0 || !state.hasTable(shape.name) || state.hasColumn(shape.name, column) ? [] : [{
|
|
77
|
+
table: shape.name,
|
|
78
|
+
column: declared
|
|
79
|
+
}];
|
|
80
|
+
}),
|
|
60
81
|
tables: shapes.filter((shape) => !state.hasTable(shape.name)).map((shape) => shape.name),
|
|
61
82
|
indexes: shapes.filter((shape) => state.hasTable(shape.name)).flatMap((shape) => shape.indexes.flatMap((index) => index.kind === "index" && !state.hasIndex(shape.name, index.name) ? [{
|
|
62
83
|
table: shape.name,
|
|
@@ -100,7 +121,14 @@ const collationStatements = async (executor, shapes, collate) => {
|
|
|
100
121
|
columns
|
|
101
122
|
};
|
|
102
123
|
};
|
|
124
|
+
/**
|
|
125
|
+
* `ALTER TABLE … ADD COLUMN` for each late column a table lacks, which run
|
|
126
|
+
* before the statements that create the table's indexes. Each is nullable,
|
|
127
|
+
* so rows written before it read as null.
|
|
128
|
+
*/
|
|
129
|
+
const addColumnStatements = (dialect, columns) => columns.map(({ table, column }) => `ALTER TABLE ${require_sqlSchema.quoteSql(dialect, table)} ADD COLUMN ${require_sqlSchema.quoteSql(dialect, column.name)} ${column.type}`);
|
|
103
130
|
//#endregion
|
|
131
|
+
exports.addColumnStatements = addColumnStatements;
|
|
104
132
|
exports.collationStatements = collationStatements;
|
|
105
133
|
exports.flattenShapes = flattenShapes;
|
|
106
134
|
exports.missingSqlSchema = missingSqlSchema;
|