@hot-updater/plugin-core 1.1.16 → 1.1.18
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/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/db/coreDatabase.cjs +10 -5
- package/dist/engine/db/coreDatabase.mjs +10 -5
- package/dist/engine/db/engineMigrator.cjs +4 -3
- package/dist/engine/db/engineMigrator.mjs +4 -3
- package/dist/engine/db/engineSqlMigrator.cjs +2 -1
- package/dist/engine/db/engineSqlMigrator.d.cts +3 -1
- package/dist/engine/db/engineSqlMigrator.d.mts +3 -1
- package/dist/engine/db/engineSqlMigrator.mjs +3 -2
- package/dist/engine/db/schemaSettings.cjs +17 -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 +17 -5
- package/dist/engine/db/settingsMigrator.cjs +33 -1
- package/dist/engine/db/settingsMigrator.d.cts +3 -1
- package/dist/engine/db/settingsMigrator.d.mts +3 -1
- package/dist/engine/db/settingsMigrator.mjs +33 -3
- 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/package.json +2 -2
|
@@ -33,6 +33,23 @@ const SETTINGS_TABLE = {
|
|
|
33
33
|
/** The engine's storage-layout generation; it changes only with a migration. */
|
|
34
34
|
const ENGINE_SCHEMA_KEY = "schema.engine";
|
|
35
35
|
const CORE_SCHEMA_KEY = "schema.core";
|
|
36
|
+
/** Compares dotted numeric versions, such as "1" and "1.2.0"; NaN when either is not one. */
|
|
37
|
+
const compareVersions = (left, right) => {
|
|
38
|
+
const parse = (version) => /^\d+(?:\.\d+)*$/u.test(version) ? version.split(".").map(Number) : [NaN];
|
|
39
|
+
const [a, b] = [parse(left), parse(right)];
|
|
40
|
+
if ([...a, ...b].some(Number.isNaN)) return NaN;
|
|
41
|
+
for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
|
|
42
|
+
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
43
|
+
if (difference !== 0) return difference;
|
|
44
|
+
}
|
|
45
|
+
return 0;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The `schema.core` a newer release recorded, which `hot-updater db migrate`
|
|
49
|
+
* never moves back: the fix is to upgrade, not to migrate.
|
|
50
|
+
*/
|
|
51
|
+
const newerCore = (settings) => settings.find(({ key, expected, found }) => key === CORE_SCHEMA_KEY && found !== null && compareVersions(found, expected) > 0);
|
|
52
|
+
const newerMessage = (adapterName, { key, expected, found }) => `Hot Updater schema setting "${key}" for ${adapterName} is "${found}"; expected "${expected}". The database was migrated by Hot Updater ${found}: upgrade hot-updater and the @hot-updater packages to ${found} or later.`;
|
|
36
53
|
const stored = ({ key, expected, found }) => `"${key}" is ${found === null ? "missing" : `"${found}"`}; expected "${expected}"`;
|
|
37
54
|
const settingMessage = (adapterName, { key, expected, found }) => `Hot Updater schema setting "${key}" for ${adapterName} is ${found === null ? "missing" : `"${found}"`}; expected "${expected}". Run \`hot-updater db migrate\`.`;
|
|
38
55
|
/** The plugin a `schema.<id>` setting belongs to; undefined for core's and the engine's. */
|
|
@@ -41,7 +58,10 @@ const listed = (items) => items.length < 2 ? items.join("") : `${items.slice(0,
|
|
|
41
58
|
/** How to create plugins' tables when the fence was given no fix. */
|
|
42
59
|
const PLUGIN_TABLES_FIX = "Run `hot-updater db migrate`. A managed server runs only its provider's plugins, and rerunning `hot-updater init --provider <provider>` applies the provider's migrations. Run `hot-updater db` commands in the server's project: they load the server file named on the command line, or src/hotUpdater.* or src/db.*, and don't read hot-updater.config.ts.";
|
|
43
60
|
const pluginsMessage = (adapterName, plugins, settings, fix) => `The tables of ${plugins.length === 1 ? "plugin" : "plugins"} ${listed(plugins.map((id) => `"${id}"`))} are not migrated on ${adapterName}: schema ${settings.length === 1 ? "setting" : "settings"} ${settings.map(stored).join(", and ")}. ${fix}`;
|
|
44
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* The database needs `hot-updater db migrate`, a new database, or a newer
|
|
63
|
+
* Hot Updater; the handler answers 503.
|
|
64
|
+
*/
|
|
45
65
|
var HotUpdaterSchemaMigrationRequiredError = class extends Error {
|
|
46
66
|
adapterName;
|
|
47
67
|
currentVersion;
|
|
@@ -56,7 +76,8 @@ var HotUpdaterSchemaMigrationRequiredError = class extends Error {
|
|
|
56
76
|
constructor(adapterName, currentVersion, setting, options) {
|
|
57
77
|
const settings = options?.settings ?? (setting === void 0 ? [] : [setting]);
|
|
58
78
|
const plugins = settings.flatMap((mismatch) => pluginOf(mismatch) ?? []);
|
|
59
|
-
|
|
79
|
+
const newer = newerCore(settings);
|
|
80
|
+
super(newer !== void 0 ? newerMessage(adapterName, newer) : setting === void 0 ? currentVersion === void 0 ? `Hot Updater database schema is not initialized for ${adapterName}. Run \`hot-updater db migrate\` before using this adapter.` : `Hot Updater v1 cannot migrate schema ${currentVersion} in place. Create a new empty database and run \`hot-updater db migrate\`.` : plugins.length > 0 && plugins.length === settings.length ? pluginsMessage(adapterName, plugins, settings, options?.fix ?? PLUGIN_TABLES_FIX) : settingMessage(adapterName, setting), options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
60
81
|
this.adapterName = adapterName;
|
|
61
82
|
this.currentVersion = currentVersion;
|
|
62
83
|
this.setting = setting;
|
|
@@ -150,6 +171,7 @@ exports.ENGINE_SCHEMA_KEY = ENGINE_SCHEMA_KEY;
|
|
|
150
171
|
exports.HotUpdaterSchemaMigrationRequiredError = HotUpdaterSchemaMigrationRequiredError;
|
|
151
172
|
exports.SETTINGS_TABLE = SETTINGS_TABLE;
|
|
152
173
|
exports.checkSchemaFence = checkSchemaFence;
|
|
174
|
+
exports.compareVersions = compareVersions;
|
|
153
175
|
exports.fencedName = fencedName;
|
|
154
176
|
exports.isMissingSchemaError = isMissingSchemaError;
|
|
155
177
|
exports.readSettings = readSettings;
|
|
@@ -13,7 +13,10 @@ interface SchemaSettingMismatch {
|
|
|
13
13
|
readonly expected: string;
|
|
14
14
|
readonly found: string | null;
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* The database needs `hot-updater db migrate`, a new database, or a newer
|
|
18
|
+
* Hot Updater; the handler answers 503.
|
|
19
|
+
*/
|
|
17
20
|
declare class HotUpdaterSchemaMigrationRequiredError extends Error {
|
|
18
21
|
readonly adapterName: string;
|
|
19
22
|
readonly currentVersion: string | undefined;
|
|
@@ -13,7 +13,10 @@ interface SchemaSettingMismatch {
|
|
|
13
13
|
readonly expected: string;
|
|
14
14
|
readonly found: string | null;
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* The database needs `hot-updater db migrate`, a new database, or a newer
|
|
18
|
+
* Hot Updater; the handler answers 503.
|
|
19
|
+
*/
|
|
17
20
|
declare class HotUpdaterSchemaMigrationRequiredError extends Error {
|
|
18
21
|
readonly adapterName: string;
|
|
19
22
|
readonly currentVersion: string | undefined;
|
|
@@ -33,6 +33,23 @@ const SETTINGS_TABLE = {
|
|
|
33
33
|
/** The engine's storage-layout generation; it changes only with a migration. */
|
|
34
34
|
const ENGINE_SCHEMA_KEY = "schema.engine";
|
|
35
35
|
const CORE_SCHEMA_KEY = "schema.core";
|
|
36
|
+
/** Compares dotted numeric versions, such as "1" and "1.2.0"; NaN when either is not one. */
|
|
37
|
+
const compareVersions = (left, right) => {
|
|
38
|
+
const parse = (version) => /^\d+(?:\.\d+)*$/u.test(version) ? version.split(".").map(Number) : [NaN];
|
|
39
|
+
const [a, b] = [parse(left), parse(right)];
|
|
40
|
+
if ([...a, ...b].some(Number.isNaN)) return NaN;
|
|
41
|
+
for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
|
|
42
|
+
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
43
|
+
if (difference !== 0) return difference;
|
|
44
|
+
}
|
|
45
|
+
return 0;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The `schema.core` a newer release recorded, which `hot-updater db migrate`
|
|
49
|
+
* never moves back: the fix is to upgrade, not to migrate.
|
|
50
|
+
*/
|
|
51
|
+
const newerCore = (settings) => settings.find(({ key, expected, found }) => key === CORE_SCHEMA_KEY && found !== null && compareVersions(found, expected) > 0);
|
|
52
|
+
const newerMessage = (adapterName, { key, expected, found }) => `Hot Updater schema setting "${key}" for ${adapterName} is "${found}"; expected "${expected}". The database was migrated by Hot Updater ${found}: upgrade hot-updater and the @hot-updater packages to ${found} or later.`;
|
|
36
53
|
const stored = ({ key, expected, found }) => `"${key}" is ${found === null ? "missing" : `"${found}"`}; expected "${expected}"`;
|
|
37
54
|
const settingMessage = (adapterName, { key, expected, found }) => `Hot Updater schema setting "${key}" for ${adapterName} is ${found === null ? "missing" : `"${found}"`}; expected "${expected}". Run \`hot-updater db migrate\`.`;
|
|
38
55
|
/** The plugin a `schema.<id>` setting belongs to; undefined for core's and the engine's. */
|
|
@@ -41,7 +58,10 @@ const listed = (items) => items.length < 2 ? items.join("") : `${items.slice(0,
|
|
|
41
58
|
/** How to create plugins' tables when the fence was given no fix. */
|
|
42
59
|
const PLUGIN_TABLES_FIX = "Run `hot-updater db migrate`. A managed server runs only its provider's plugins, and rerunning `hot-updater init --provider <provider>` applies the provider's migrations. Run `hot-updater db` commands in the server's project: they load the server file named on the command line, or src/hotUpdater.* or src/db.*, and don't read hot-updater.config.ts.";
|
|
43
60
|
const pluginsMessage = (adapterName, plugins, settings, fix) => `The tables of ${plugins.length === 1 ? "plugin" : "plugins"} ${listed(plugins.map((id) => `"${id}"`))} are not migrated on ${adapterName}: schema ${settings.length === 1 ? "setting" : "settings"} ${settings.map(stored).join(", and ")}. ${fix}`;
|
|
44
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* The database needs `hot-updater db migrate`, a new database, or a newer
|
|
63
|
+
* Hot Updater; the handler answers 503.
|
|
64
|
+
*/
|
|
45
65
|
var HotUpdaterSchemaMigrationRequiredError = class extends Error {
|
|
46
66
|
adapterName;
|
|
47
67
|
currentVersion;
|
|
@@ -56,7 +76,8 @@ var HotUpdaterSchemaMigrationRequiredError = class extends Error {
|
|
|
56
76
|
constructor(adapterName, currentVersion, setting, options) {
|
|
57
77
|
const settings = options?.settings ?? (setting === void 0 ? [] : [setting]);
|
|
58
78
|
const plugins = settings.flatMap((mismatch) => pluginOf(mismatch) ?? []);
|
|
59
|
-
|
|
79
|
+
const newer = newerCore(settings);
|
|
80
|
+
super(newer !== void 0 ? newerMessage(adapterName, newer) : setting === void 0 ? currentVersion === void 0 ? `Hot Updater database schema is not initialized for ${adapterName}. Run \`hot-updater db migrate\` before using this adapter.` : `Hot Updater v1 cannot migrate schema ${currentVersion} in place. Create a new empty database and run \`hot-updater db migrate\`.` : plugins.length > 0 && plugins.length === settings.length ? pluginsMessage(adapterName, plugins, settings, options?.fix ?? PLUGIN_TABLES_FIX) : settingMessage(adapterName, setting), options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
60
81
|
this.adapterName = adapterName;
|
|
61
82
|
this.currentVersion = currentVersion;
|
|
62
83
|
this.setting = setting;
|
|
@@ -146,4 +167,4 @@ const withSchemaFence = (adapter, adapterName, expected, fix) => {
|
|
|
146
167
|
};
|
|
147
168
|
};
|
|
148
169
|
//#endregion
|
|
149
|
-
export { ENGINE_SCHEMA_KEY, HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE, checkSchemaFence, fencedName, isMissingSchemaError, readSettings, withSchemaFence };
|
|
170
|
+
export { ENGINE_SCHEMA_KEY, HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE, checkSchemaFence, compareVersions, fencedName, isMissingSchemaError, readSettings, withSchemaFence };
|
|
@@ -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 [];
|
|
@@ -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,19 +1,20 @@
|
|
|
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
|
+
require_settingsMigrator.refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
17
18
|
if (Object.entries(options.settings).every(([key, value]) => stored.get(key) === value)) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
18
19
|
return {
|
|
19
20
|
operations: [...options.schema.tables.map((table) => ({
|
|
@@ -29,7 +30,7 @@ const createEngineMigrator = (options) => {
|
|
|
29
30
|
type: "custom",
|
|
30
31
|
description: `Write the schema settings: ${Object.keys(options.settings).join(", ")}`
|
|
31
32
|
}]],
|
|
32
|
-
execute: () => require_schemaSettings.migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings)
|
|
33
|
+
execute: () => require_schemaSettings.migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings, options.ordered)
|
|
33
34
|
};
|
|
34
35
|
};
|
|
35
36
|
return {
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { HOT_UPDATER_SCHEMA_VERSION } from "../core/schema.mjs";
|
|
2
2
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
3
|
+
import { refuseNewerSchema, refuseV0Database, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
3
4
|
import { migrateSchema } from "./schemaSettings.mjs";
|
|
4
|
-
import { refuseV0Database, storedSchemaVersion } from "./settingsMigrator.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
|
+
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
17
18
|
if (Object.entries(options.settings).every(([key, value]) => stored.get(key) === value)) return getEmptyMigrationResult();
|
|
18
19
|
return {
|
|
19
20
|
operations: [...options.schema.tables.map((table) => ({
|
|
@@ -29,7 +30,7 @@ const createEngineMigrator = (options) => {
|
|
|
29
30
|
type: "custom",
|
|
30
31
|
description: `Write the schema settings: ${Object.keys(options.settings).join(", ")}`
|
|
31
32
|
}]],
|
|
32
|
-
execute: () => migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings)
|
|
33
|
+
execute: () => migrateSchema(options.adapter, options.adapterName, options.schema.tables, options.settings, options.ordered)
|
|
33
34
|
};
|
|
34
35
|
};
|
|
35
36
|
return {
|
|
@@ -16,7 +16,7 @@ const run = async (executor, statements) => {
|
|
|
16
16
|
/**
|
|
17
17
|
* `hot-updater db migrate` and `db generate` for an SQL provider on the
|
|
18
18
|
* engine: the generated SQL schema, applied when a settings row is missing or
|
|
19
|
-
* different, and refused on a v0 database. A SQL adapter's `createMigrator`
|
|
19
|
+
* different, and refused on a v0 database or one a newer release migrated. A SQL adapter's `createMigrator`
|
|
20
20
|
* returns it, as `kyselyAdapter`'s does.
|
|
21
21
|
*/
|
|
22
22
|
const createEngineSqlMigrator = (options) => {
|
|
@@ -26,6 +26,7 @@ const createEngineSqlMigrator = (options) => {
|
|
|
26
26
|
const makeResult = async (migrate = {}) => {
|
|
27
27
|
const stored = await read();
|
|
28
28
|
require_settingsMigrator.refuseV0Database(options.adapterName, stored);
|
|
29
|
+
require_settingsMigrator.refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
29
30
|
if (Object.entries(options.settings).every(([key, value]) => stored.get(key) === value)) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
30
31
|
const statements = require_engineSql.generateEngineSql(options.executor.dialect, options.schema, options.settings, options);
|
|
31
32
|
return {
|
|
@@ -9,11 +9,13 @@ 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
|
|
15
17
|
* engine: the generated SQL schema, applied when a settings row is missing or
|
|
16
|
-
* different, and refused on a v0 database. A SQL adapter's `createMigrator`
|
|
18
|
+
* different, and refused on a v0 database or one a newer release migrated. A SQL adapter's `createMigrator`
|
|
17
19
|
* returns it, as `kyselyAdapter`'s does.
|
|
18
20
|
*/
|
|
19
21
|
declare const createEngineSqlMigrator: (options: EngineSqlMigratorOptions) => Migrator;
|
|
@@ -9,11 +9,13 @@ 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
|
|
15
17
|
* engine: the generated SQL schema, applied when a settings row is missing or
|
|
16
|
-
* different, and refused on a v0 database. A SQL adapter's `createMigrator`
|
|
18
|
+
* different, and refused on a v0 database or one a newer release migrated. A SQL adapter's `createMigrator`
|
|
17
19
|
* returns it, as `kyselyAdapter`'s does.
|
|
18
20
|
*/
|
|
19
21
|
declare const createEngineSqlMigrator: (options: EngineSqlMigratorOptions) => Migrator;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HOT_UPDATER_SCHEMA_VERSION } from "../core/schema.mjs";
|
|
2
2
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
3
3
|
import { generateEngineSql } from "./engineSql.mjs";
|
|
4
|
-
import { readStoredSettings, refuseV0Database, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
4
|
+
import { readStoredSettings, refuseNewerSchema, refuseV0Database, storedSchemaVersion } from "./settingsMigrator.mjs";
|
|
5
5
|
//#region src/engine/db/engineSqlMigrator.ts
|
|
6
6
|
/** Runs the statements in one transaction where the dialect's DDL is transactional. */
|
|
7
7
|
const run = async (executor, statements) => {
|
|
@@ -16,7 +16,7 @@ const run = async (executor, statements) => {
|
|
|
16
16
|
/**
|
|
17
17
|
* `hot-updater db migrate` and `db generate` for an SQL provider on the
|
|
18
18
|
* engine: the generated SQL schema, applied when a settings row is missing or
|
|
19
|
-
* different, and refused on a v0 database. A SQL adapter's `createMigrator`
|
|
19
|
+
* different, and refused on a v0 database or one a newer release migrated. A SQL adapter's `createMigrator`
|
|
20
20
|
* returns it, as `kyselyAdapter`'s does.
|
|
21
21
|
*/
|
|
22
22
|
const createEngineSqlMigrator = (options) => {
|
|
@@ -26,6 +26,7 @@ const createEngineSqlMigrator = (options) => {
|
|
|
26
26
|
const makeResult = async (migrate = {}) => {
|
|
27
27
|
const stored = await read();
|
|
28
28
|
refuseV0Database(options.adapterName, stored);
|
|
29
|
+
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
29
30
|
if (Object.entries(options.settings).every(([key, value]) => stored.get(key) === value)) return getEmptyMigrationResult();
|
|
30
31
|
const statements = generateEngineSql(options.executor.dialect, options.schema, options.settings, options);
|
|
31
32
|
return {
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
const require_fence = require("../database/fence.cjs");
|
|
2
|
+
const require_settingsMigrator = require("./settingsMigrator.cjs");
|
|
2
3
|
//#region src/engine/db/schemaSettings.ts
|
|
3
4
|
const storedSettings = async (adapter, keys) => {
|
|
4
5
|
const rows = await require_fence.readSettings(adapter, keys);
|
|
5
6
|
return new Map(keys.map((key, position) => [key, rows[position] ?? null]));
|
|
6
7
|
};
|
|
7
|
-
/**
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Writes the settings rows after every table exists, and refuses a database
|
|
10
|
+
* whose `ordered` settings a newer release recorded.
|
|
11
|
+
*/
|
|
12
|
+
const writeSchemaSettings = async (adapter, adapterName, settings, ordered) => {
|
|
9
13
|
const stored = await storedSettings(adapter, Object.keys(settings));
|
|
14
|
+
require_settingsMigrator.refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
10
15
|
const ops = Object.entries(settings).flatMap(([key, value]) => {
|
|
11
16
|
const row = stored.get(key);
|
|
12
17
|
if (row === null || row === void 0) return [{
|
|
@@ -31,11 +36,18 @@ const writeSchemaSettings = async (adapter, adapterName, settings) => {
|
|
|
31
36
|
if (ops.length === 0) return;
|
|
32
37
|
if (!(await adapter.write(ops)).ok) throw new Error(`${adapterName}: the schema settings changed during the migration; run it again.`);
|
|
33
38
|
};
|
|
34
|
-
/**
|
|
35
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Creates or updates every table, then writes the settings rows last. A
|
|
41
|
+
* database a newer release migrated is refused before anything is written:
|
|
42
|
+
* its settings are read first, and a store without them yet, whose read
|
|
43
|
+
* fails, has none to refuse.
|
|
44
|
+
*/
|
|
45
|
+
const migrateSchema = async (adapter, adapterName, tables, settings, ordered) => {
|
|
36
46
|
if (adapter.migrations === void 0) throw new Error(`${adapterName} creates its tables with its own tooling; write only the settings rows.`);
|
|
47
|
+
const stored = await storedSettings(adapter, Object.keys(settings)).catch(() => void 0);
|
|
48
|
+
if (stored !== void 0) require_settingsMigrator.refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
37
49
|
await adapter.migrations.apply([...tables, require_fence.SETTINGS_TABLE]);
|
|
38
|
-
await writeSchemaSettings(adapter, adapterName, settings);
|
|
50
|
+
await writeSchemaSettings(adapter, adapterName, settings, ordered);
|
|
39
51
|
};
|
|
40
52
|
//#endregion
|
|
41
53
|
exports.migrateSchema = migrateSchema;
|
|
@@ -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,17 @@
|
|
|
1
1
|
import { SETTINGS_TABLE, readSettings } from "../database/fence.mjs";
|
|
2
|
+
import { refuseNewerSchema } from "./settingsMigrator.mjs";
|
|
2
3
|
//#region src/engine/db/schemaSettings.ts
|
|
3
4
|
const storedSettings = async (adapter, keys) => {
|
|
4
5
|
const rows = await readSettings(adapter, keys);
|
|
5
6
|
return new Map(keys.map((key, position) => [key, rows[position] ?? null]));
|
|
6
7
|
};
|
|
7
|
-
/**
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Writes the settings rows after every table exists, and refuses a database
|
|
10
|
+
* whose `ordered` settings a newer release recorded.
|
|
11
|
+
*/
|
|
12
|
+
const writeSchemaSettings = async (adapter, adapterName, settings, ordered) => {
|
|
9
13
|
const stored = await storedSettings(adapter, Object.keys(settings));
|
|
14
|
+
refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
10
15
|
const ops = Object.entries(settings).flatMap(([key, value]) => {
|
|
11
16
|
const row = stored.get(key);
|
|
12
17
|
if (row === null || row === void 0) return [{
|
|
@@ -31,11 +36,18 @@ const writeSchemaSettings = async (adapter, adapterName, settings) => {
|
|
|
31
36
|
if (ops.length === 0) return;
|
|
32
37
|
if (!(await adapter.write(ops)).ok) throw new Error(`${adapterName}: the schema settings changed during the migration; run it again.`);
|
|
33
38
|
};
|
|
34
|
-
/**
|
|
35
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Creates or updates every table, then writes the settings rows last. A
|
|
41
|
+
* database a newer release migrated is refused before anything is written:
|
|
42
|
+
* its settings are read first, and a store without them yet, whose read
|
|
43
|
+
* fails, has none to refuse.
|
|
44
|
+
*/
|
|
45
|
+
const migrateSchema = async (adapter, adapterName, tables, settings, ordered) => {
|
|
36
46
|
if (adapter.migrations === void 0) throw new Error(`${adapterName} creates its tables with its own tooling; write only the settings rows.`);
|
|
47
|
+
const stored = await storedSettings(adapter, Object.keys(settings)).catch(() => void 0);
|
|
48
|
+
if (stored !== void 0) refuseNewerSchema(adapterName, new Map([...stored].map(([key, row]) => [key, row?.value])), settings, ordered);
|
|
37
49
|
await adapter.migrations.apply([...tables, SETTINGS_TABLE]);
|
|
38
|
-
await writeSchemaSettings(adapter, adapterName, settings);
|
|
50
|
+
await writeSchemaSettings(adapter, adapterName, settings, ordered);
|
|
39
51
|
};
|
|
40
52
|
//#endregion
|
|
41
53
|
export { migrateSchema, writeSchemaSettings };
|
|
@@ -36,9 +36,38 @@ const refuseV0Database = (adapterName, stored) => {
|
|
|
36
36
|
throw new require_fence.HotUpdaterSchemaMigrationRequiredError(adapterName, version);
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
|
+
* The settings whose versions only move forward when a `ToolingTarget` names
|
|
40
|
+
* none. `schema.engine` is a storage layout, not a release: a store refuses
|
|
41
|
+
* another layout itself.
|
|
42
|
+
*/
|
|
43
|
+
const DEFAULT_ORDERED_SETTINGS = ["schema.core"];
|
|
44
|
+
/**
|
|
45
|
+
* Refuses a database a newer release migrated, one whose `schema.core` is
|
|
46
|
+
* newer than this release's: writing this release's settings would move the
|
|
47
|
+
* schema back, and every server of the newer release would answer 503. Only
|
|
48
|
+
* `ordered` settings are compared, `schema.core` among them, and the message
|
|
49
|
+
* lists each that is newer. An official plugin's
|
|
50
|
+
* newer version alone is no newer release: Insights recorded "1.2.0" in
|
|
51
|
+
* 1.0.0-rc.23 and rc.24, then went back to "1.0.0" with the same tables.
|
|
52
|
+
*/
|
|
53
|
+
const refuseNewerSchema = (adapterName, stored, settings, ordered = DEFAULT_ORDERED_SETTINGS) => {
|
|
54
|
+
const newer = ordered.flatMap((key) => {
|
|
55
|
+
const found = stored.get(key);
|
|
56
|
+
const expected = settings[key];
|
|
57
|
+
return typeof found === "string" && expected !== void 0 && require_fence.compareVersions(found, expected) > 0 ? [{
|
|
58
|
+
key,
|
|
59
|
+
found,
|
|
60
|
+
expected
|
|
61
|
+
}] : [];
|
|
62
|
+
});
|
|
63
|
+
const core = newer.find(({ key }) => key === "schema.core");
|
|
64
|
+
if (core === void 0) return;
|
|
65
|
+
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.`);
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
39
68
|
* `hot-updater db migrate` for an ORM that applies the DDL itself (Drizzle,
|
|
40
69
|
* Prisma): it sets what the ORM cannot declare, writes the settings rows, and
|
|
41
|
-
* refuses a v0 database. The `createMigrator` of an adapter whose ORM
|
|
70
|
+
* refuses a v0 database or one a newer release migrated. The `createMigrator` of an adapter whose ORM
|
|
42
71
|
* creates the tables returns it, as the Drizzle and Prisma adapters' do.
|
|
43
72
|
*/
|
|
44
73
|
const createSettingsMigrator = (options) => {
|
|
@@ -48,6 +77,7 @@ const createSettingsMigrator = (options) => {
|
|
|
48
77
|
const stored = await read();
|
|
49
78
|
if (stored === null) throw new Error(`${options.adapterName}: the Hot Updater tables are missing. Run \`hot-updater db generate\`, apply the schema with ${options.applyTables}, then migrate again.`);
|
|
50
79
|
refuseV0Database(options.adapterName, stored);
|
|
80
|
+
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
51
81
|
if (migrate.updateSettings === false || Object.entries(options.settings).every(([key, value]) => stored.get(key) === value)) return require_fixedMigratorShared.getEmptyMigrationResult();
|
|
52
82
|
const fixups = options.fixups?.statements ?? [];
|
|
53
83
|
const statements = [...fixups, ...require_engineSql.settingsStatements(options.executor.dialect, options.settings)];
|
|
@@ -85,7 +115,9 @@ const createSettingsMigrator = (options) => {
|
|
|
85
115
|
};
|
|
86
116
|
};
|
|
87
117
|
//#endregion
|
|
118
|
+
exports.DEFAULT_ORDERED_SETTINGS = DEFAULT_ORDERED_SETTINGS;
|
|
88
119
|
exports.createSettingsMigrator = createSettingsMigrator;
|
|
89
120
|
exports.readStoredSettings = readStoredSettings;
|
|
121
|
+
exports.refuseNewerSchema = refuseNewerSchema;
|
|
90
122
|
exports.refuseV0Database = refuseV0Database;
|
|
91
123
|
exports.storedSchemaVersion = storedSchemaVersion;
|
|
@@ -5,13 +5,15 @@ import { SqlExecutor } from "../database/sql/sqlAdapter.cjs";
|
|
|
5
5
|
/**
|
|
6
6
|
* `hot-updater db migrate` for an ORM that applies the DDL itself (Drizzle,
|
|
7
7
|
* Prisma): it sets what the ORM cannot declare, writes the settings rows, and
|
|
8
|
-
* refuses a v0 database. The `createMigrator` of an adapter whose ORM
|
|
8
|
+
* refuses a v0 database or one a newer release migrated. The `createMigrator` of an adapter whose ORM
|
|
9
9
|
* creates the tables returns it, as the Drizzle and Prisma adapters' do.
|
|
10
10
|
*/
|
|
11
11
|
declare const createSettingsMigrator: (options: {
|
|
12
12
|
readonly adapterName: string;
|
|
13
13
|
readonly executor: SqlExecutor;
|
|
14
14
|
readonly settings: SchemaSettings;
|
|
15
|
+
/** The settings whose versions only move forward; `ToolingTarget.ordered`. */
|
|
16
|
+
readonly ordered?: readonly string[];
|
|
15
17
|
/** How the ORM applies the tables, for the error when they are missing. */
|
|
16
18
|
readonly applyTables: string;
|
|
17
19
|
/** What the ORM cannot declare, set before the settings rows. */
|
|
@@ -5,13 +5,15 @@ import { SqlExecutor } from "../database/sql/sqlAdapter.mjs";
|
|
|
5
5
|
/**
|
|
6
6
|
* `hot-updater db migrate` for an ORM that applies the DDL itself (Drizzle,
|
|
7
7
|
* Prisma): it sets what the ORM cannot declare, writes the settings rows, and
|
|
8
|
-
* refuses a v0 database. The `createMigrator` of an adapter whose ORM
|
|
8
|
+
* refuses a v0 database or one a newer release migrated. The `createMigrator` of an adapter whose ORM
|
|
9
9
|
* creates the tables returns it, as the Drizzle and Prisma adapters' do.
|
|
10
10
|
*/
|
|
11
11
|
declare const createSettingsMigrator: (options: {
|
|
12
12
|
readonly adapterName: string;
|
|
13
13
|
readonly executor: SqlExecutor;
|
|
14
14
|
readonly settings: SchemaSettings;
|
|
15
|
+
/** The settings whose versions only move forward; `ToolingTarget.ordered`. */
|
|
16
|
+
readonly ordered?: readonly string[];
|
|
15
17
|
/** How the ORM applies the tables, for the error when they are missing. */
|
|
16
18
|
readonly applyTables: string;
|
|
17
19
|
/** What the ORM cannot declare, set before the settings rows. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HOT_UPDATER_SCHEMA_VERSION } from "../core/schema.mjs";
|
|
2
|
-
import { HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE, isMissingSchemaError } from "../database/fence.mjs";
|
|
2
|
+
import { HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE, compareVersions, isMissingSchemaError } from "../database/fence.mjs";
|
|
3
3
|
import { getEmptyMigrationResult } from "./fixedMigratorShared.mjs";
|
|
4
4
|
import { quoteSql } from "../database/sql/sqlSchema.mjs";
|
|
5
5
|
import { settingsStatements } from "./engineSql.mjs";
|
|
@@ -36,9 +36,38 @@ const refuseV0Database = (adapterName, stored) => {
|
|
|
36
36
|
throw new HotUpdaterSchemaMigrationRequiredError(adapterName, version);
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
|
+
* The settings whose versions only move forward when a `ToolingTarget` names
|
|
40
|
+
* none. `schema.engine` is a storage layout, not a release: a store refuses
|
|
41
|
+
* another layout itself.
|
|
42
|
+
*/
|
|
43
|
+
const DEFAULT_ORDERED_SETTINGS = ["schema.core"];
|
|
44
|
+
/**
|
|
45
|
+
* Refuses a database a newer release migrated, one whose `schema.core` is
|
|
46
|
+
* newer than this release's: writing this release's settings would move the
|
|
47
|
+
* schema back, and every server of the newer release would answer 503. Only
|
|
48
|
+
* `ordered` settings are compared, `schema.core` among them, and the message
|
|
49
|
+
* lists each that is newer. An official plugin's
|
|
50
|
+
* newer version alone is no newer release: Insights recorded "1.2.0" in
|
|
51
|
+
* 1.0.0-rc.23 and rc.24, then went back to "1.0.0" with the same tables.
|
|
52
|
+
*/
|
|
53
|
+
const refuseNewerSchema = (adapterName, stored, settings, ordered = DEFAULT_ORDERED_SETTINGS) => {
|
|
54
|
+
const newer = ordered.flatMap((key) => {
|
|
55
|
+
const found = stored.get(key);
|
|
56
|
+
const expected = settings[key];
|
|
57
|
+
return typeof found === "string" && expected !== void 0 && compareVersions(found, expected) > 0 ? [{
|
|
58
|
+
key,
|
|
59
|
+
found,
|
|
60
|
+
expected
|
|
61
|
+
}] : [];
|
|
62
|
+
});
|
|
63
|
+
const core = newer.find(({ key }) => key === "schema.core");
|
|
64
|
+
if (core === void 0) return;
|
|
65
|
+
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.`);
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
39
68
|
* `hot-updater db migrate` for an ORM that applies the DDL itself (Drizzle,
|
|
40
69
|
* Prisma): it sets what the ORM cannot declare, writes the settings rows, and
|
|
41
|
-
* refuses a v0 database. The `createMigrator` of an adapter whose ORM
|
|
70
|
+
* refuses a v0 database or one a newer release migrated. The `createMigrator` of an adapter whose ORM
|
|
42
71
|
* creates the tables returns it, as the Drizzle and Prisma adapters' do.
|
|
43
72
|
*/
|
|
44
73
|
const createSettingsMigrator = (options) => {
|
|
@@ -48,6 +77,7 @@ const createSettingsMigrator = (options) => {
|
|
|
48
77
|
const stored = await read();
|
|
49
78
|
if (stored === null) throw new Error(`${options.adapterName}: the Hot Updater tables are missing. Run \`hot-updater db generate\`, apply the schema with ${options.applyTables}, then migrate again.`);
|
|
50
79
|
refuseV0Database(options.adapterName, stored);
|
|
80
|
+
refuseNewerSchema(options.adapterName, stored, options.settings, options.ordered);
|
|
51
81
|
if (migrate.updateSettings === false || Object.entries(options.settings).every(([key, value]) => stored.get(key) === value)) return getEmptyMigrationResult();
|
|
52
82
|
const fixups = options.fixups?.statements ?? [];
|
|
53
83
|
const statements = [...fixups, ...settingsStatements(options.executor.dialect, options.settings)];
|
|
@@ -85,4 +115,4 @@ const createSettingsMigrator = (options) => {
|
|
|
85
115
|
};
|
|
86
116
|
};
|
|
87
117
|
//#endregion
|
|
88
|
-
export { createSettingsMigrator, readStoredSettings, refuseV0Database, storedSchemaVersion };
|
|
118
|
+
export { DEFAULT_ORDERED_SETTINGS, createSettingsMigrator, readStoredSettings, refuseNewerSchema, refuseV0Database, storedSchemaVersion };
|
|
@@ -56,6 +56,12 @@ interface Migrator {
|
|
|
56
56
|
interface ToolingTarget {
|
|
57
57
|
readonly schema: ResolvedSchema;
|
|
58
58
|
readonly settings: SchemaSettings;
|
|
59
|
+
/**
|
|
60
|
+
* The settings keys whose versions only move forward: core's and the
|
|
61
|
+
* official plugins'. A migrator refuses a database that
|
|
62
|
+
* recorded a newer one than `settings` instead of writing it back.
|
|
63
|
+
*/
|
|
64
|
+
readonly ordered?: readonly string[];
|
|
59
65
|
}
|
|
60
66
|
type SchemaGenerator = (version: string | "latest", name?: string, target?: ToolingTarget) => {
|
|
61
67
|
code: string;
|
|
@@ -56,6 +56,12 @@ interface Migrator {
|
|
|
56
56
|
interface ToolingTarget {
|
|
57
57
|
readonly schema: ResolvedSchema;
|
|
58
58
|
readonly settings: SchemaSettings;
|
|
59
|
+
/**
|
|
60
|
+
* The settings keys whose versions only move forward: core's and the
|
|
61
|
+
* official plugins'. A migrator refuses a database that
|
|
62
|
+
* recorded a newer one than `settings` instead of writing it back.
|
|
63
|
+
*/
|
|
64
|
+
readonly ordered?: readonly string[];
|
|
59
65
|
}
|
|
60
66
|
type SchemaGenerator = (version: string | "latest", name?: string, target?: ToolingTarget) => {
|
|
61
67
|
code: string;
|
package/dist/index.cjs
CHANGED
|
@@ -31,10 +31,10 @@ const require_memoryAdapter = require("./database/memoryAdapter.cjs");
|
|
|
31
31
|
const require_verifyAdapter = require("./database/verifyAdapter.cjs");
|
|
32
32
|
const require_fence = require("./engine/database/fence.cjs");
|
|
33
33
|
const require_readMeter = require("./engine/database/readMeter.cjs");
|
|
34
|
-
const require_schemaSettings = require("./engine/db/schemaSettings.cjs");
|
|
35
34
|
const require_sqlSchema = require("./engine/database/sql/sqlSchema.cjs");
|
|
36
35
|
const require_engineSql = require("./engine/db/engineSql.cjs");
|
|
37
36
|
const require_settingsMigrator = require("./engine/db/settingsMigrator.cjs");
|
|
37
|
+
const require_schemaSettings = require("./engine/db/schemaSettings.cjs");
|
|
38
38
|
const require_coreDatabase = require("./engine/db/coreDatabase.cjs");
|
|
39
39
|
const require_createEngine = require("./engine/createEngine.cjs");
|
|
40
40
|
const require_kvAdapter = require("./engine/database/kv/kvAdapter.cjs");
|
package/dist/index.mjs
CHANGED
|
@@ -30,10 +30,10 @@ import { createMemoryAdapter } from "./database/memoryAdapter.mjs";
|
|
|
30
30
|
import { DatabaseAdapterContractError, verifyAdapter } from "./database/verifyAdapter.mjs";
|
|
31
31
|
import { HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE } from "./engine/database/fence.mjs";
|
|
32
32
|
import { meterReads } from "./engine/database/readMeter.mjs";
|
|
33
|
-
import { writeSchemaSettings } from "./engine/db/schemaSettings.mjs";
|
|
34
33
|
import { createTableStatements, isMultiIndex, quoteSql, sqlTableShapes } from "./engine/database/sql/sqlSchema.mjs";
|
|
35
34
|
import { generateEngineSql } from "./engine/db/engineSql.mjs";
|
|
36
35
|
import { createSettingsMigrator } from "./engine/db/settingsMigrator.mjs";
|
|
36
|
+
import { writeSchemaSettings } from "./engine/db/schemaSettings.mjs";
|
|
37
37
|
import { aggregateBatchingTables, coreSchema, coreSettings, coreTarget, createEngineDatabase, migrateCoreSchema, toolingTargetOf } from "./engine/db/coreDatabase.mjs";
|
|
38
38
|
import { createEngine } from "./engine/createEngine.mjs";
|
|
39
39
|
import { createKvAdapter, encodeKvKey } from "./engine/database/kv/kvAdapter.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hot-updater/plugin-core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React Native OTA solution for self-hosted",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"es-toolkit": "^1.32.0",
|
|
46
46
|
"mime": "^4.0.4",
|
|
47
47
|
"verkit": "0.4.0",
|
|
48
|
-
"@hot-updater/protocol": "1.1.
|
|
48
|
+
"@hot-updater/protocol": "1.1.18"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@electric-sql/pglite": "0.4.1",
|