@hot-updater/plugin-core 1.2.0-rc.49 → 1.2.0-rc.50
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 +20 -1
- package/dist/coreApi.d.mts +20 -1
- package/dist/database/adapter.d.cts +12 -0
- package/dist/database/adapter.d.mts +12 -0
- package/dist/engine/core/schema.cjs +21 -2
- package/dist/engine/core/schema.d.cts +4 -0
- package/dist/engine/core/schema.d.mts +4 -0
- package/dist/engine/core/schema.mjs +21 -3
- package/dist/engine/database/engineTransaction.cjs +21 -1
- package/dist/engine/database/engineTransaction.d.cts +5 -1
- package/dist/engine/database/engineTransaction.d.mts +5 -1
- package/dist/engine/database/engineTransaction.mjs +21 -1
- package/dist/engine/database/kv/kvAdapter.cjs +80 -4
- package/dist/engine/database/kv/kvAdapter.mjs +80 -4
- package/dist/engine/database/sql/sqlSchema.cjs +6 -2
- package/dist/engine/database/sql/sqlSchema.d.cts +1 -1
- package/dist/engine/database/sql/sqlSchema.d.mts +1 -1
- package/dist/engine/database/sql/sqlSchema.mjs +6 -2
- package/dist/engine/db/coreDatabase.cjs +10 -5
- package/dist/engine/db/coreDatabase.d.cts +2 -1
- package/dist/engine/db/coreDatabase.d.mts +2 -1
- package/dist/engine/db/coreDatabase.mjs +10 -5
- package/dist/engine/db/engineMigrator.cjs +21 -16
- package/dist/engine/db/engineMigrator.mjs +22 -17
- package/dist/engine/db/engineSqlMigrator.cjs +23 -22
- package/dist/engine/db/engineSqlMigrator.d.cts +2 -0
- package/dist/engine/db/engineSqlMigrator.d.mts +2 -0
- package/dist/engine/db/engineSqlMigrator.mjs +25 -24
- package/dist/engine/db/schemaSettings.cjs +36 -5
- package/dist/engine/db/schemaSettings.d.cts +5 -2
- package/dist/engine/db/schemaSettings.d.mts +5 -2
- package/dist/engine/db/schemaSettings.mjs +36 -6
- package/dist/engine/db/settingsMigrator.cjs +92 -14
- package/dist/engine/db/settingsMigrator.d.cts +18 -8
- package/dist/engine/db/settingsMigrator.d.mts +18 -8
- package/dist/engine/db/settingsMigrator.mjs +89 -15
- package/dist/engine/db/sqlSchemaState.cjs +108 -0
- package/dist/engine/db/sqlSchemaState.mjs +104 -0
- package/dist/engine/db/types.d.cts +6 -0
- package/dist/engine/db/types.d.mts +6 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/serverPlugin/coreReads.d.cts +10 -1
- package/dist/serverPlugin/coreReads.d.mts +10 -1
- package/dist/types/databaseConfig.d.cts +2 -1
- package/dist/types/databaseConfig.d.mts +2 -1
- package/package.json +2 -2
|
@@ -15,8 +15,11 @@ const coreSettings = {
|
|
|
15
15
|
/** What `hot-updater db` creates for a server without plugins. */
|
|
16
16
|
const coreTarget = {
|
|
17
17
|
schema: coreSchema,
|
|
18
|
-
settings: coreSettings
|
|
18
|
+
settings: coreSettings,
|
|
19
|
+
ordered: ["schema.core"]
|
|
19
20
|
};
|
|
21
|
+
/** The brand Hot Updater's own plugin factories set, read as the server reads it. */
|
|
22
|
+
const isOfficialPlugin = (plugin) => Reflect.get(plugin, Symbol.for("@hot-updater/server/official-plugin")) === true;
|
|
20
23
|
/**
|
|
21
24
|
* The log and lease tables a database with `aggregateBatching` writes
|
|
22
25
|
* beside core's and the plugins', which a provider's access policy names,
|
|
@@ -40,15 +43,16 @@ const toolingTargetOf = (plugins) => plugins.length === 0 ? coreTarget : {
|
|
|
40
43
|
settings: {
|
|
41
44
|
...coreSettings,
|
|
42
45
|
...pluginSettings(plugins)
|
|
43
|
-
}
|
|
46
|
+
},
|
|
47
|
+
ordered: [...coreTarget.ordered, ...plugins.flatMap((plugin) => isOfficialPlugin(plugin) ? [`schema.${plugin.id}`] : [])]
|
|
44
48
|
};
|
|
45
49
|
/**
|
|
46
50
|
* Creates core's tables and those of `plugins`, then writes their settings
|
|
47
51
|
* rows: what a provider's setup runs for the plugins its server runs.
|
|
48
52
|
*/
|
|
49
53
|
const migrateCoreSchema = (adapter, name, plugins = []) => {
|
|
50
|
-
const { schema, settings } = toolingTargetOf(plugins);
|
|
51
|
-
return require_schemaSettings.migrateSchema(adapter, name, schema.tables, settings);
|
|
54
|
+
const { schema, settings, ordered } = toolingTargetOf(plugins);
|
|
55
|
+
return require_schemaSettings.migrateSchema(adapter, name, schema.tables, settings, ordered);
|
|
52
56
|
};
|
|
53
57
|
/**
|
|
54
58
|
* A provider's database on the storage engine: its adapter behind the schema
|
|
@@ -59,11 +63,12 @@ const migrateCoreSchema = (adapter, name, plugins = []) => {
|
|
|
59
63
|
*/
|
|
60
64
|
const createEngineDatabase = ({ name, adapter, onCachedRoutesChange, aggregateBatching, retry, readSettings: readStored }) => {
|
|
61
65
|
const fenced = require_fence.withSchemaFence(adapter, name, coreSettings);
|
|
62
|
-
const createMigrator = ({ schema, settings } = coreTarget) => require_engineMigrator.createEngineMigrator({
|
|
66
|
+
const createMigrator = ({ schema, settings, ordered } = coreTarget) => require_engineMigrator.createEngineMigrator({
|
|
63
67
|
adapterName: name,
|
|
64
68
|
adapter,
|
|
65
69
|
schema,
|
|
66
70
|
settings,
|
|
71
|
+
ordered,
|
|
67
72
|
readSettings: readStored ?? (async () => {
|
|
68
73
|
const rows = await require_fence.readSettings(adapter, Object.keys(settings)).catch((error) => {
|
|
69
74
|
if (require_fence.isMissingSchemaError(error)) return [];
|
|
@@ -55,7 +55,8 @@ interface EngineDatabaseOptions {
|
|
|
55
55
|
/**
|
|
56
56
|
* How a transaction that conflicts with another write runs again: more
|
|
57
57
|
* attempts for a store many writers contend on, and `onRetry` to observe
|
|
58
|
-
* each rerun. 8 attempts by default
|
|
58
|
+
* each rerun. 8 attempts by default; an aggregate write that lost to
|
|
59
|
+
* another writer's commit is resent without spending one.
|
|
59
60
|
*/
|
|
60
61
|
readonly retry?: RetryOptions;
|
|
61
62
|
/**
|
|
@@ -55,7 +55,8 @@ interface EngineDatabaseOptions {
|
|
|
55
55
|
/**
|
|
56
56
|
* How a transaction that conflicts with another write runs again: more
|
|
57
57
|
* attempts for a store many writers contend on, and `onRetry` to observe
|
|
58
|
-
* each rerun. 8 attempts by default
|
|
58
|
+
* each rerun. 8 attempts by default; an aggregate write that lost to
|
|
59
|
+
* another writer's commit is resent without spending one.
|
|
59
60
|
*/
|
|
60
61
|
readonly retry?: RetryOptions;
|
|
61
62
|
/**
|
|
@@ -15,8 +15,11 @@ const coreSettings = {
|
|
|
15
15
|
/** What `hot-updater db` creates for a server without plugins. */
|
|
16
16
|
const coreTarget = {
|
|
17
17
|
schema: coreSchema,
|
|
18
|
-
settings: coreSettings
|
|
18
|
+
settings: coreSettings,
|
|
19
|
+
ordered: ["schema.core"]
|
|
19
20
|
};
|
|
21
|
+
/** The brand Hot Updater's own plugin factories set, read as the server reads it. */
|
|
22
|
+
const isOfficialPlugin = (plugin) => Reflect.get(plugin, Symbol.for("@hot-updater/server/official-plugin")) === true;
|
|
20
23
|
/**
|
|
21
24
|
* The log and lease tables a database with `aggregateBatching` writes
|
|
22
25
|
* beside core's and the plugins', which a provider's access policy names,
|
|
@@ -40,15 +43,16 @@ const toolingTargetOf = (plugins) => plugins.length === 0 ? coreTarget : {
|
|
|
40
43
|
settings: {
|
|
41
44
|
...coreSettings,
|
|
42
45
|
...pluginSettings(plugins)
|
|
43
|
-
}
|
|
46
|
+
},
|
|
47
|
+
ordered: [...coreTarget.ordered, ...plugins.flatMap((plugin) => isOfficialPlugin(plugin) ? [`schema.${plugin.id}`] : [])]
|
|
44
48
|
};
|
|
45
49
|
/**
|
|
46
50
|
* Creates core's tables and those of `plugins`, then writes their settings
|
|
47
51
|
* rows: what a provider's setup runs for the plugins its server runs.
|
|
48
52
|
*/
|
|
49
53
|
const migrateCoreSchema = (adapter, name, plugins = []) => {
|
|
50
|
-
const { schema, settings } = toolingTargetOf(plugins);
|
|
51
|
-
return migrateSchema(adapter, name, schema.tables, settings);
|
|
54
|
+
const { schema, settings, ordered } = toolingTargetOf(plugins);
|
|
55
|
+
return migrateSchema(adapter, name, schema.tables, settings, ordered);
|
|
52
56
|
};
|
|
53
57
|
/**
|
|
54
58
|
* A provider's database on the storage engine: its adapter behind the schema
|
|
@@ -59,11 +63,12 @@ const migrateCoreSchema = (adapter, name, plugins = []) => {
|
|
|
59
63
|
*/
|
|
60
64
|
const createEngineDatabase = ({ name, adapter, onCachedRoutesChange, aggregateBatching, retry, readSettings: readStored }) => {
|
|
61
65
|
const fenced = withSchemaFence(adapter, name, coreSettings);
|
|
62
|
-
const createMigrator = ({ schema, settings } = coreTarget) => createEngineMigrator({
|
|
66
|
+
const createMigrator = ({ schema, settings, ordered } = coreTarget) => createEngineMigrator({
|
|
63
67
|
adapterName: name,
|
|
64
68
|
adapter,
|
|
65
69
|
schema,
|
|
66
70
|
settings,
|
|
71
|
+
ordered,
|
|
67
72
|
readSettings: readStored ?? (async () => {
|
|
68
73
|
const rows = await readSettings(adapter, Object.keys(settings)).catch((error) => {
|
|
69
74
|
if (isMissingSchemaError(error)) return [];
|
|
@@ -1,35 +1,40 @@
|
|
|
1
1
|
const require_schema = require("../core/schema.cjs");
|
|
2
2
|
const require_fixedMigratorShared = require("./fixedMigratorShared.cjs");
|
|
3
|
-
const require_schemaSettings = require("./schemaSettings.cjs");
|
|
4
3
|
const require_settingsMigrator = require("./settingsMigrator.cjs");
|
|
4
|
+
const require_schemaSettings = require("./schemaSettings.cjs");
|
|
5
5
|
//#region src/engine/db/engineMigrator.ts
|
|
6
6
|
/**
|
|
7
7
|
* `hot-updater db migrate` for a store whose adapter creates its own tables
|
|
8
8
|
* (MongoDB and the key-value stores): the tables and indexes, then the
|
|
9
9
|
* settings rows, applied when a setting is missing or different, and refused
|
|
10
|
-
* on a v0 database.
|
|
10
|
+
* on a v0 database or one a newer release migrated.
|
|
11
11
|
*/
|
|
12
12
|
const createEngineMigrator = (options) => {
|
|
13
13
|
const getVersion = async () => require_settingsMigrator.storedSchemaVersion(await options.readSettings());
|
|
14
14
|
const makeResult = async (migrate = {}) => {
|
|
15
15
|
const stored = await options.readSettings();
|
|
16
16
|
require_settingsMigrator.refuseV0Database(options.adapterName, stored);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
require_settingsMigrator.refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
18
|
+
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
19
|
+
if (changed.length === 0) {
|
|
20
|
+
const lacking = await require_schemaSettings.fillLateIndexes(options.adapter, options.schema.tables, { dryRun: true });
|
|
21
|
+
if (lacking.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
22
|
+
return {
|
|
23
|
+
operations: [{
|
|
24
|
+
type: "custom",
|
|
25
|
+
description: `Write the index items rows lack: ${lacking.join(", ")}`
|
|
26
|
+
}],
|
|
27
|
+
execute: async () => {
|
|
28
|
+
await require_schemaSettings.fillLateIndexes(options.adapter, options.schema.tables);
|
|
27
29
|
}
|
|
28
|
-
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
operations: [{
|
|
29
34
|
type: "custom",
|
|
30
|
-
description:
|
|
31
|
-
}]],
|
|
32
|
-
execute: () => require_schemaSettings.migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings)
|
|
35
|
+
description: "Create the tables and indexes the database lacks"
|
|
36
|
+
}, ...migrate.updateSettings === false ? [] : [require_settingsMigrator.settingsOperation(stored, changed)]],
|
|
37
|
+
execute: () => require_schemaSettings.migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings, options.ordered)
|
|
33
38
|
};
|
|
34
39
|
};
|
|
35
40
|
return {
|
|
@@ -1,35 +1,40 @@
|
|
|
1
1
|
import { HOT_UPDATER_SCHEMA_VERSION } from "../core/schema.mjs";
|
|
2
2
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { refuseNewerSchema, refuseV0Database, settingsOperation, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
4
|
+
import { fillLateIndexes, migrateSchema } from "./schemaSettings.mjs";
|
|
5
5
|
//#region src/engine/db/engineMigrator.ts
|
|
6
6
|
/**
|
|
7
7
|
* `hot-updater db migrate` for a store whose adapter creates its own tables
|
|
8
8
|
* (MongoDB and the key-value stores): the tables and indexes, then the
|
|
9
9
|
* settings rows, applied when a setting is missing or different, and refused
|
|
10
|
-
* on a v0 database.
|
|
10
|
+
* on a v0 database or one a newer release migrated.
|
|
11
11
|
*/
|
|
12
12
|
const createEngineMigrator = (options) => {
|
|
13
13
|
const getVersion = async () => storedSchemaVersion(await options.readSettings());
|
|
14
14
|
const makeResult = async (migrate = {}) => {
|
|
15
15
|
const stored = await options.readSettings();
|
|
16
16
|
refuseV0Database(options.adapterName, stored);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
18
|
+
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
19
|
+
if (changed.length === 0) {
|
|
20
|
+
const lacking = await fillLateIndexes(options.adapter, options.schema.tables, { dryRun: true });
|
|
21
|
+
if (lacking.length === 0) return getEmptyMigrationResult();
|
|
22
|
+
return {
|
|
23
|
+
operations: [{
|
|
24
|
+
type: "custom",
|
|
25
|
+
description: `Write the index items rows lack: ${lacking.join(", ")}`
|
|
26
|
+
}],
|
|
27
|
+
execute: async () => {
|
|
28
|
+
await fillLateIndexes(options.adapter, options.schema.tables);
|
|
27
29
|
}
|
|
28
|
-
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
operations: [{
|
|
29
34
|
type: "custom",
|
|
30
|
-
description:
|
|
31
|
-
}]],
|
|
32
|
-
execute: () => migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings)
|
|
35
|
+
description: "Create the tables and indexes the database lacks"
|
|
36
|
+
}, ...migrate.updateSettings === false ? [] : [settingsOperation(stored, changed)]],
|
|
37
|
+
execute: () => migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings, options.ordered)
|
|
33
38
|
};
|
|
34
39
|
};
|
|
35
40
|
return {
|
|
@@ -3,10 +3,9 @@ const require_fence = require("../database/fence.cjs");
|
|
|
3
3
|
const require_fixedMigratorShared = require("./fixedMigratorShared.cjs");
|
|
4
4
|
const require_sqlSchema = require("../database/sql/sqlSchema.cjs");
|
|
5
5
|
const require_engineSql = require("./engineSql.cjs");
|
|
6
|
+
const require_sqlSchemaState = require("./sqlSchemaState.cjs");
|
|
6
7
|
const require_settingsMigrator = require("./settingsMigrator.cjs");
|
|
7
8
|
//#region src/engine/db/engineSqlMigrator.ts
|
|
8
|
-
/** Each table, and the index tables of its multi-valued indexes. */
|
|
9
|
-
const flatten = (shapes) => shapes.flatMap((shape) => [shape, ...flatten(shape.indexes.flatMap((index) => index.kind === "table" ? [index.table] : []))]);
|
|
10
9
|
/**
|
|
11
10
|
* MySQL declares a table's indexes in its CREATE TABLE, which a table an
|
|
12
11
|
* earlier release created skips, and has no CREATE INDEX IF NOT EXISTS: the
|
|
@@ -14,13 +13,11 @@ const flatten = (shapes) => shapes.flatMap((shape) => [shape, ...flatten(shape.i
|
|
|
14
13
|
*/
|
|
15
14
|
const missingMysqlIndexes = async (executor, schema, tablePrefix) => {
|
|
16
15
|
const quote = (name) => require_sqlSchema.quoteSql("mysql", name);
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const shapes = require_sqlSchemaState.targetShapes("mysql", schema, tablePrefix);
|
|
17
|
+
return require_sqlSchemaState.missingSqlSchema(shapes, await require_sqlSchemaState.readSqlSchemaState(executor)).indexes.map(({ table, index: name }) => {
|
|
18
|
+
const index = shapes.find((shape) => shape.name === table).indexes.find((candidate) => candidate.kind === "index" && candidate.name === name);
|
|
19
|
+
return `CREATE ${index.kind === "index" && index.unique ? "UNIQUE " : ""}INDEX ${quote(name)} ON ${quote(table)} (${index.kind === "index" ? index.columns.map(quote).join(", ") : ""})`;
|
|
20
20
|
});
|
|
21
|
-
const folded = (table, index) => JSON.stringify([String(table).toLowerCase(), String(index).toLowerCase()]);
|
|
22
|
-
const present = new Set(rows.map(({ t, i }) => folded(t, i)));
|
|
23
|
-
return flatten(require_sqlSchema.sqlTableShapes("mysql", [...schema.tables, require_fence.SETTINGS_TABLE], tablePrefix)).flatMap((shape) => shape.indexes.flatMap((index) => index.kind === "index" && !present.has(folded(shape.name, index.name)) ? [`CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quote(index.name)} ON ${quote(shape.name)} (${index.columns.map(quote).join(", ")})`] : []));
|
|
24
21
|
};
|
|
25
22
|
/**
|
|
26
23
|
* Runs the tables' statements, then the settings rows last, in one
|
|
@@ -56,24 +53,28 @@ const createEngineSqlMigrator = (options) => {
|
|
|
56
53
|
const makeResult = async (migrate = {}) => {
|
|
57
54
|
const stored = await read();
|
|
58
55
|
require_settingsMigrator.refuseV0Database(options.adapterName, stored);
|
|
59
|
-
|
|
56
|
+
require_settingsMigrator.refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
57
|
+
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
58
|
+
if (changed.length === 0) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
60
59
|
const statements = require_engineSql.generateEngineSql(options.executor.dialect, options.schema, options.settings, options);
|
|
61
60
|
const settingsSql = require_engineSql.settingsStatements(options.executor.dialect, options.settings, options.tablePrefix);
|
|
62
61
|
const tablesSql = statements.slice(0, -settingsSql.length || void 0);
|
|
62
|
+
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));
|
|
63
63
|
return {
|
|
64
|
-
operations: [
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
64
|
+
operations: [
|
|
65
|
+
...missing.tables.map((table) => ({
|
|
66
|
+
type: "create-table",
|
|
67
|
+
value: {
|
|
68
|
+
ormName: table,
|
|
69
|
+
columns: {}
|
|
70
|
+
}
|
|
71
|
+
})),
|
|
72
|
+
...missing.indexes.map(({ table, index }) => ({
|
|
73
|
+
type: "custom",
|
|
74
|
+
description: `Create index: ${index} on ${table}`
|
|
75
|
+
})),
|
|
76
|
+
...migrate.updateSettings === false ? [] : [require_settingsMigrator.settingsOperation(stored, changed)]
|
|
77
|
+
],
|
|
77
78
|
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
78
79
|
execute: () => run(options.executor, tablesSql, settingsSql, options.schema, options.tablePrefix)
|
|
79
80
|
};
|
|
@@ -9,6 +9,8 @@ interface EngineSqlMigratorOptions extends EngineSqlOptions {
|
|
|
9
9
|
readonly executor: SqlExecutor;
|
|
10
10
|
readonly schema: ResolvedSchema;
|
|
11
11
|
readonly settings: SchemaSettings;
|
|
12
|
+
/** The settings whose versions only move forward; `ToolingTarget.ordered`. */
|
|
13
|
+
readonly ordered?: readonly string[];
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
16
|
* `hot-updater db migrate` and `db generate` for an SQL provider on the
|
|
@@ -9,6 +9,8 @@ interface EngineSqlMigratorOptions extends EngineSqlOptions {
|
|
|
9
9
|
readonly executor: SqlExecutor;
|
|
10
10
|
readonly schema: ResolvedSchema;
|
|
11
11
|
readonly settings: SchemaSettings;
|
|
12
|
+
/** The settings whose versions only move forward; `ToolingTarget.ordered`. */
|
|
13
|
+
readonly ordered?: readonly string[];
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
16
|
* `hot-updater db migrate` and `db generate` for an SQL provider on the
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { HOT_UPDATER_SCHEMA_VERSION } from "../core/schema.mjs";
|
|
2
2
|
import { SETTINGS_TABLE } from "../database/fence.mjs";
|
|
3
3
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
4
|
-
import { quoteSql
|
|
4
|
+
import { quoteSql } from "../database/sql/sqlSchema.mjs";
|
|
5
5
|
import { generateEngineSql, settingsStatements } from "./engineSql.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { missingSqlSchema, readSqlSchemaState, targetShapes } from "./sqlSchemaState.mjs";
|
|
7
|
+
import { readStoredSettings, refuseNewerSchema, refuseV0Database, settingsOperation, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
7
8
|
//#region src/engine/db/engineSqlMigrator.ts
|
|
8
|
-
/** Each table, and the index tables of its multi-valued indexes. */
|
|
9
|
-
const flatten = (shapes) => shapes.flatMap((shape) => [shape, ...flatten(shape.indexes.flatMap((index) => index.kind === "table" ? [index.table] : []))]);
|
|
10
9
|
/**
|
|
11
10
|
* MySQL declares a table's indexes in its CREATE TABLE, which a table an
|
|
12
11
|
* earlier release created skips, and has no CREATE INDEX IF NOT EXISTS: the
|
|
@@ -14,13 +13,11 @@ const flatten = (shapes) => shapes.flatMap((shape) => [shape, ...flatten(shape.i
|
|
|
14
13
|
*/
|
|
15
14
|
const missingMysqlIndexes = async (executor, schema, tablePrefix) => {
|
|
16
15
|
const quote = (name) => quoteSql("mysql", name);
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const shapes = targetShapes("mysql", schema, tablePrefix);
|
|
17
|
+
return missingSqlSchema(shapes, await readSqlSchemaState(executor)).indexes.map(({ table, index: name }) => {
|
|
18
|
+
const index = shapes.find((shape) => shape.name === table).indexes.find((candidate) => candidate.kind === "index" && candidate.name === name);
|
|
19
|
+
return `CREATE ${index.kind === "index" && index.unique ? "UNIQUE " : ""}INDEX ${quote(name)} ON ${quote(table)} (${index.kind === "index" ? index.columns.map(quote).join(", ") : ""})`;
|
|
20
20
|
});
|
|
21
|
-
const folded = (table, index) => JSON.stringify([String(table).toLowerCase(), String(index).toLowerCase()]);
|
|
22
|
-
const present = new Set(rows.map(({ t, i }) => folded(t, i)));
|
|
23
|
-
return flatten(sqlTableShapes("mysql", [...schema.tables, SETTINGS_TABLE], tablePrefix)).flatMap((shape) => shape.indexes.flatMap((index) => index.kind === "index" && !present.has(folded(shape.name, index.name)) ? [`CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quote(index.name)} ON ${quote(shape.name)} (${index.columns.map(quote).join(", ")})`] : []));
|
|
24
21
|
};
|
|
25
22
|
/**
|
|
26
23
|
* Runs the tables' statements, then the settings rows last, in one
|
|
@@ -56,24 +53,28 @@ const createEngineSqlMigrator = (options) => {
|
|
|
56
53
|
const makeResult = async (migrate = {}) => {
|
|
57
54
|
const stored = await read();
|
|
58
55
|
refuseV0Database(options.adapterName, stored);
|
|
59
|
-
|
|
56
|
+
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
57
|
+
const changed = Object.entries(options.settings).filter(([key, value]) => stored.get(key) !== value);
|
|
58
|
+
if (changed.length === 0) return getEmptyMigrationResult();
|
|
60
59
|
const statements = generateEngineSql(options.executor.dialect, options.schema, options.settings, options);
|
|
61
60
|
const settingsSql = settingsStatements(options.executor.dialect, options.settings, options.tablePrefix);
|
|
62
61
|
const tablesSql = statements.slice(0, -settingsSql.length || void 0);
|
|
62
|
+
const missing = missingSqlSchema(targetShapes(options.executor.dialect, options.schema, options.tablePrefix).filter((shape) => shape.name !== (options.tablePrefix ?? "") + SETTINGS_TABLE.name), await readSqlSchemaState(options.executor));
|
|
63
63
|
return {
|
|
64
|
-
operations: [
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
64
|
+
operations: [
|
|
65
|
+
...missing.tables.map((table) => ({
|
|
66
|
+
type: "create-table",
|
|
67
|
+
value: {
|
|
68
|
+
ormName: table,
|
|
69
|
+
columns: {}
|
|
70
|
+
}
|
|
71
|
+
})),
|
|
72
|
+
...missing.indexes.map(({ table, index }) => ({
|
|
73
|
+
type: "custom",
|
|
74
|
+
description: `Create index: ${index} on ${table}`
|
|
75
|
+
})),
|
|
76
|
+
...migrate.updateSettings === false ? [] : [settingsOperation(stored, changed)]
|
|
77
|
+
],
|
|
77
78
|
getSQL: () => statements.map((statement) => `${statement};`).join("\n\n"),
|
|
78
79
|
execute: () => run(options.executor, tablesSql, settingsSql, options.schema, options.tablePrefix)
|
|
79
80
|
};
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
const require_schema = require("../core/schema.cjs");
|
|
1
2
|
const require_fence = require("../database/fence.cjs");
|
|
3
|
+
const require_settingsMigrator = require("./settingsMigrator.cjs");
|
|
2
4
|
//#region src/engine/db/schemaSettings.ts
|
|
3
5
|
const storedSettings = async (adapter, keys) => {
|
|
4
6
|
const rows = await require_fence.readSettings(adapter, keys);
|
|
5
7
|
return new Map(keys.map((key, position) => [key, rows[position] ?? null]));
|
|
6
8
|
};
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Writes the settings rows after every table exists, and refuses a database
|
|
11
|
+
* whose `ordered` settings a newer release recorded.
|
|
12
|
+
*/
|
|
13
|
+
const writeSchemaSettings = async (adapter, adapterName, settings, ordered) => {
|
|
9
14
|
const stored = await storedSettings(adapter, Object.keys(settings));
|
|
15
|
+
require_settingsMigrator.refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
10
16
|
const ops = Object.entries(settings).flatMap(([key, value]) => {
|
|
11
17
|
const row = stored.get(key);
|
|
12
18
|
if (row === null || row === void 0) return [{
|
|
@@ -31,12 +37,37 @@ const writeSchemaSettings = async (adapter, adapterName, settings) => {
|
|
|
31
37
|
if (ops.length === 0) return;
|
|
32
38
|
if (!(await adapter.write(ops)).ok) throw new Error(`${adapterName}: the schema settings changed during the migration; run it again.`);
|
|
33
39
|
};
|
|
34
|
-
/**
|
|
35
|
-
|
|
40
|
+
/**
|
|
41
|
+
* The `LATE_INDEXES` among `tables` whose items some rows lack, as
|
|
42
|
+
* `<table>.<index>`, written unless `dryRun`: none on a store without
|
|
43
|
+
* `migrations.fillIndex`.
|
|
44
|
+
*/
|
|
45
|
+
const fillLateIndexes = async (adapter, tables, { dryRun = false } = {}) => {
|
|
46
|
+
const fillIndex = adapter.migrations?.fillIndex;
|
|
47
|
+
if (fillIndex === void 0) return [];
|
|
48
|
+
const lacking = [];
|
|
49
|
+
for (const { table: name, index } of require_schema.LATE_INDEXES) {
|
|
50
|
+
const table = tables.find((candidate) => candidate.name === name);
|
|
51
|
+
if (table === void 0) continue;
|
|
52
|
+
if (await fillIndex(table, index, { dryRun }) > 0) lacking.push(`${name}.${index}`);
|
|
53
|
+
}
|
|
54
|
+
return lacking;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Creates or updates every table, writes the late indexes' missing items,
|
|
58
|
+
* then writes the settings rows last. A database a newer release migrated is
|
|
59
|
+
* refused before anything is written: its settings are read first, and a
|
|
60
|
+
* store without them yet, whose read fails, has none to refuse.
|
|
61
|
+
*/
|
|
62
|
+
const migrateSchema = async (adapter, adapterName, tables, settings, ordered) => {
|
|
36
63
|
if (adapter.migrations === void 0) throw new Error(`${adapterName} creates its tables with its own tooling; write only the settings rows.`);
|
|
64
|
+
const stored = await storedSettings(adapter, Object.keys(settings)).catch(() => void 0);
|
|
65
|
+
if (stored !== void 0) require_settingsMigrator.refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
37
66
|
await adapter.migrations.apply([...tables, require_fence.SETTINGS_TABLE]);
|
|
38
|
-
await
|
|
67
|
+
await fillLateIndexes(adapter, tables);
|
|
68
|
+
await writeSchemaSettings(adapter, adapterName, settings, ordered);
|
|
39
69
|
};
|
|
40
70
|
//#endregion
|
|
71
|
+
exports.fillLateIndexes = fillLateIndexes;
|
|
41
72
|
exports.migrateSchema = migrateSchema;
|
|
42
73
|
exports.writeSchemaSettings = writeSchemaSettings;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { DatabaseAdapter } from "../../database/adapter.cjs";
|
|
2
2
|
import { SchemaSettings } from "../database/fence.cjs";
|
|
3
3
|
//#region src/engine/db/schemaSettings.d.ts
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Writes the settings rows after every table exists, and refuses a database
|
|
6
|
+
* whose `ordered` settings a newer release recorded.
|
|
7
|
+
*/
|
|
8
|
+
declare const writeSchemaSettings: (adapter: DatabaseAdapter, adapterName: string, settings: SchemaSettings, ordered?: readonly string[]) => Promise<void>;
|
|
6
9
|
//#endregion
|
|
7
10
|
export { writeSchemaSettings };
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { DatabaseAdapter } from "../../database/adapter.mjs";
|
|
2
2
|
import { SchemaSettings } from "../database/fence.mjs";
|
|
3
3
|
//#region src/engine/db/schemaSettings.d.ts
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Writes the settings rows after every table exists, and refuses a database
|
|
6
|
+
* whose `ordered` settings a newer release recorded.
|
|
7
|
+
*/
|
|
8
|
+
declare const writeSchemaSettings: (adapter: DatabaseAdapter, adapterName: string, settings: SchemaSettings, ordered?: readonly string[]) => Promise<void>;
|
|
6
9
|
//#endregion
|
|
7
10
|
export { writeSchemaSettings };
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import { LATE_INDEXES } from "../core/schema.mjs";
|
|
1
2
|
import { SETTINGS_TABLE, readSettings } from "../database/fence.mjs";
|
|
3
|
+
import { refuseNewerSchema } from "./settingsMigrator.mjs";
|
|
2
4
|
//#region src/engine/db/schemaSettings.ts
|
|
3
5
|
const storedSettings = async (adapter, keys) => {
|
|
4
6
|
const rows = await readSettings(adapter, keys);
|
|
5
7
|
return new Map(keys.map((key, position) => [key, rows[position] ?? null]));
|
|
6
8
|
};
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Writes the settings rows after every table exists, and refuses a database
|
|
11
|
+
* whose `ordered` settings a newer release recorded.
|
|
12
|
+
*/
|
|
13
|
+
const writeSchemaSettings = async (adapter, adapterName, settings, ordered) => {
|
|
9
14
|
const stored = await storedSettings(adapter, Object.keys(settings));
|
|
15
|
+
refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
10
16
|
const ops = Object.entries(settings).flatMap(([key, value]) => {
|
|
11
17
|
const row = stored.get(key);
|
|
12
18
|
if (row === null || row === void 0) return [{
|
|
@@ -31,11 +37,35 @@ const writeSchemaSettings = async (adapter, adapterName, settings) => {
|
|
|
31
37
|
if (ops.length === 0) return;
|
|
32
38
|
if (!(await adapter.write(ops)).ok) throw new Error(`${adapterName}: the schema settings changed during the migration; run it again.`);
|
|
33
39
|
};
|
|
34
|
-
/**
|
|
35
|
-
|
|
40
|
+
/**
|
|
41
|
+
* The `LATE_INDEXES` among `tables` whose items some rows lack, as
|
|
42
|
+
* `<table>.<index>`, written unless `dryRun`: none on a store without
|
|
43
|
+
* `migrations.fillIndex`.
|
|
44
|
+
*/
|
|
45
|
+
const fillLateIndexes = async (adapter, tables, { dryRun = false } = {}) => {
|
|
46
|
+
const fillIndex = adapter.migrations?.fillIndex;
|
|
47
|
+
if (fillIndex === void 0) return [];
|
|
48
|
+
const lacking = [];
|
|
49
|
+
for (const { table: name, index } of LATE_INDEXES) {
|
|
50
|
+
const table = tables.find((candidate) => candidate.name === name);
|
|
51
|
+
if (table === void 0) continue;
|
|
52
|
+
if (await fillIndex(table, index, { dryRun }) > 0) lacking.push(`${name}.${index}`);
|
|
53
|
+
}
|
|
54
|
+
return lacking;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Creates or updates every table, writes the late indexes' missing items,
|
|
58
|
+
* then writes the settings rows last. A database a newer release migrated is
|
|
59
|
+
* refused before anything is written: its settings are read first, and a
|
|
60
|
+
* store without them yet, whose read fails, has none to refuse.
|
|
61
|
+
*/
|
|
62
|
+
const migrateSchema = async (adapter, adapterName, tables, settings, ordered) => {
|
|
36
63
|
if (adapter.migrations === void 0) throw new Error(`${adapterName} creates its tables with its own tooling; write only the settings rows.`);
|
|
64
|
+
const stored = await storedSettings(adapter, Object.keys(settings)).catch(() => void 0);
|
|
65
|
+
if (stored !== void 0) refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
37
66
|
await adapter.migrations.apply([...tables, SETTINGS_TABLE]);
|
|
38
|
-
await
|
|
67
|
+
await fillLateIndexes(adapter, tables);
|
|
68
|
+
await writeSchemaSettings(adapter, adapterName, settings, ordered);
|
|
39
69
|
};
|
|
40
70
|
//#endregion
|
|
41
|
-
export { migrateSchema, writeSchemaSettings };
|
|
71
|
+
export { fillLateIndexes, migrateSchema, writeSchemaSettings };
|