masterrecord 1.3.1 → 1.3.2
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/CHANGES.md +15 -0
- package/Migrations/cli.js +20 -8
- package/Migrations/contextInit.js +39 -0
- package/context.js +6 -1
- package/package.json +1 -1
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.3.2 — migration CLI auto-creates a missing MySQL/Postgres database
|
|
4
|
+
|
|
5
|
+
**Bug (critical for first deploys):** `update-database` printed *"Instantiating Context (this will create the database if it doesn't exist)…"*, but it `await`ed the raw `context._initPromise` directly. When the target database didn't exist, that promise rejected with `Unknown database 'X'` (MySQL) / `database "X" does not exist` (Postgres), and the catch block merely reported failure and exited — it **never invoked the auto-create** (`_createDatabaseFromConfig` + retry) that lives in `schema._ensureReady()`. So a first-time deploy against an empty server failed with a confusing error despite the CLI claiming it would create the database. (Confirmed present through 1.3.1.)
|
|
6
|
+
|
|
7
|
+
**Fix:**
|
|
8
|
+
- New shared bootstrap `Migrations/contextInit.js` → `instantiateReadyContext(ContextCtor)` routes context construction through `schema._ensureReady()`, which auto-creates a missing MySQL/Postgres database and retries the connection. It marks the returned context `_ready` so later `_execute()`/`query()` calls don't re-await the now-settled (rejected) original init promise. SQLite is unaffected (the driver creates the file on open).
|
|
9
|
+
- `update-database`, `update-database-restart`, and `update-database-all` now use it. `update-database-restart`/`-all` additionally never awaited async MySQL/Postgres init at all — also fixed.
|
|
10
|
+
|
|
11
|
+
**Deep-dive findings (fixed in the same release):**
|
|
12
|
+
- `context.query()` blindly awaited `_initPromise`, so after the auto-create retry swapped in a live engine it could still re-throw the stale rejection. It now goes through `_ensureReady()` (short-circuits on `_ready`).
|
|
13
|
+
- `add-migration-all` constructed contexts **without** `MASTERRECORD_SCHEMA_ONLY`, unlike the single `add-migration` — so generating migrations for all contexts needlessly required a reachable database (and hit the missing-DB init path on MySQL/Postgres). Now schema-only, matching `add-migration`.
|
|
14
|
+
- Reviewed the remaining construction sites: `ensure-database` (dedicated create — correct), per-migration `new Migration(ContextCtor)` (each runs `schema._ensureReady()` internally — correct), and the rollback commands `update-database-down`/`-target` (auto-create is intentionally **not** applied — you can't roll back a database that was never created; `-down` already awaits init, `-target` never queries its bootstrap context).
|
|
15
|
+
|
|
16
|
+
New tests: `test/cli-context-init.test.js` (SQLite: bootstrap returns a ready, queryable context) and a gated `[engine] migration bootstrap AUTO-CREATES a missing database` integration test (drops a throwaway DB, bootstraps, asserts it was created and is queryable). Full suite green (0 fail); 0 lint errors. The live auto-create assertion runs in CI against the MySQL/Postgres service containers.
|
|
17
|
+
|
|
3
18
|
## v1.3.1 — portable snapshots + heterogeneous batch inserts
|
|
4
19
|
|
|
5
20
|
**Fixed — Windows-generated migration snapshots broke Linux deploys (critical).**
|
package/Migrations/cli.js
CHANGED
|
@@ -11,6 +11,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
11
11
|
import { globSync } from 'glob';
|
|
12
12
|
import { resolveMigrationsDirectory, toPosixPath } from './pathUtils.js';
|
|
13
13
|
import Migration from './migrations.js';
|
|
14
|
+
import { instantiateReadyContext } from './contextInit.js';
|
|
14
15
|
|
|
15
16
|
const __filename = fileURLToPath(import.meta.url);
|
|
16
17
|
const __dirname = path.dirname(__filename);
|
|
@@ -473,12 +474,12 @@ program.option('-V', 'output the version');
|
|
|
473
474
|
console.log(`\n🔍 Instantiating Context (this will create the database if it doesn't exist)...`);
|
|
474
475
|
|
|
475
476
|
try{
|
|
476
|
-
|
|
477
|
-
//
|
|
478
|
-
//
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
477
|
+
// Route through the schema layer so a missing MySQL/Postgres
|
|
478
|
+
// database is AUTO-CREATED (and the connection retried) before any
|
|
479
|
+
// migration runs — honoring the message printed just above. Awaiting
|
|
480
|
+
// the raw _initPromise here used to just report "Unknown database"
|
|
481
|
+
// and exit without ever creating it.
|
|
482
|
+
contextInstance = await instantiateReadyContext(ContextCtor);
|
|
482
483
|
}catch(err){
|
|
483
484
|
console.error(`\n❌ Error - Failed to instantiate Context`);
|
|
484
485
|
console.error(`\nContext file: ${contextAbs}`);
|
|
@@ -792,7 +793,9 @@ program.option('-V', 'output the version');
|
|
|
792
793
|
}
|
|
793
794
|
var contextInstance;
|
|
794
795
|
try{
|
|
795
|
-
|
|
796
|
+
// Auto-create a missing MySQL/Postgres database (and await async
|
|
797
|
+
// init) via the schema layer before applying migrations.
|
|
798
|
+
contextInstance = await instantiateReadyContext(ContextCtor);
|
|
796
799
|
}catch(err){
|
|
797
800
|
console.error(`\n❌ Error - Failed to construct Context from '${contextAbs}'`);
|
|
798
801
|
console.error(`\nThis usually happens when:`);
|
|
@@ -1030,9 +1033,16 @@ program.option('-V', 'output the version');
|
|
|
1030
1033
|
}
|
|
1031
1034
|
let contextInstance;
|
|
1032
1035
|
try{
|
|
1036
|
+
// Generating a migration only needs entity metadata, not a live DB.
|
|
1037
|
+
// Schema-only mode skips connection/init — matching `add-migration`
|
|
1038
|
+
// (without it, add-migration-all needlessly required a reachable
|
|
1039
|
+
// database and would hit the missing-DB init path on MySQL/Postgres).
|
|
1040
|
+
process.env.MASTERRECORD_SCHEMA_ONLY = '1';
|
|
1033
1041
|
contextInstance = new ContextCtor();
|
|
1042
|
+
delete process.env.MASTERRECORD_SCHEMA_ONLY;
|
|
1034
1043
|
contextInstances.push(contextInstance);
|
|
1035
1044
|
}catch(err){
|
|
1045
|
+
delete process.env.MASTERRECORD_SCHEMA_ONLY;
|
|
1036
1046
|
console.error(`⚠️ Skipping ${path.basename(contextAbs)}: failed to construct Context`);
|
|
1037
1047
|
console.error(` Details: ${err.message}`);
|
|
1038
1048
|
continue;
|
|
@@ -1147,7 +1157,9 @@ program.option('-V', 'output the version');
|
|
|
1147
1157
|
}
|
|
1148
1158
|
var contextInstance;
|
|
1149
1159
|
try{
|
|
1150
|
-
|
|
1160
|
+
// Auto-create a missing MySQL/Postgres database (and await async
|
|
1161
|
+
// init) via the schema layer before applying migrations.
|
|
1162
|
+
contextInstance = await instantiateReadyContext(ContextCtor);
|
|
1151
1163
|
contextInstances.push(contextInstance);
|
|
1152
1164
|
}catch(err){
|
|
1153
1165
|
console.error(`⚠️ Skipping ${entry.ctxName}: failed to construct Context`);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// version 1.3.2 - ESM only
|
|
2
|
+
import schema from './schema.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Instantiate a user context and ensure its database is reachable for
|
|
6
|
+
* migrations, AUTO-CREATING a missing MySQL/Postgres database (and retrying the
|
|
7
|
+
* connection) via the schema layer — the same path `ensure-database` uses.
|
|
8
|
+
*
|
|
9
|
+
* Why this exists: the migration CLI used to await the raw
|
|
10
|
+
* `context._initPromise` directly. When the target database didn't exist yet,
|
|
11
|
+
* that promise rejected with "Unknown database 'X'" (MySQL) /
|
|
12
|
+
* `database "X" does not exist` (Postgres), and the CLI's catch block merely
|
|
13
|
+
* reported failure and exited — it never invoked the auto-create that lives in
|
|
14
|
+
* `schema._ensureReady()` (`_createDatabaseFromConfig` + retry). So the CLI's
|
|
15
|
+
* own message "this will create the database if it doesn't exist" was a lie for
|
|
16
|
+
* a first-time deploy against an empty server. Routing through
|
|
17
|
+
* `schema._ensureReady()` makes that promise real.
|
|
18
|
+
*
|
|
19
|
+
* SQLite is unaffected (the driver creates the file on open); for SQLite this
|
|
20
|
+
* is just a normal construct-and-connect.
|
|
21
|
+
*
|
|
22
|
+
* @param {Function} ContextCtor - The user's context class (constructor)
|
|
23
|
+
* @returns {Promise<object>} A ready context whose engine/pool is live
|
|
24
|
+
* @throws Propagates a genuine connection/config failure (not "db missing")
|
|
25
|
+
*/
|
|
26
|
+
export async function instantiateReadyContext(ContextCtor) {
|
|
27
|
+
const initSchema = new schema(ContextCtor);
|
|
28
|
+
await initSchema._ensureReady();
|
|
29
|
+
const ctx = initSchema.context;
|
|
30
|
+
// After _ensureReady() the engine/pool is live — including the
|
|
31
|
+
// create-database-then-retry path, which swaps in a fresh engine/pool but
|
|
32
|
+
// leaves the original (now-settled, possibly rejected) _initPromise in
|
|
33
|
+
// place. Mark the context ready so later context._execute() calls
|
|
34
|
+
// short-circuit context._ensureReady() instead of re-awaiting that promise.
|
|
35
|
+
ctx._ready = true;
|
|
36
|
+
return ctx;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default instantiateReadyContext;
|
package/context.js
CHANGED
|
@@ -2118,7 +2118,12 @@ class context {
|
|
|
2118
2118
|
* await ctx.execute('UPDATE Step SET run_id = ? WHERE id = ?', ['run_x', 1]); // sqlite/mysql
|
|
2119
2119
|
*/
|
|
2120
2120
|
async query(sql, params = []) {
|
|
2121
|
-
|
|
2121
|
+
// Honor the readiness flag rather than blindly awaiting _initPromise:
|
|
2122
|
+
// after the migration auto-create path swaps in a live engine, the
|
|
2123
|
+
// original _initPromise may be settled-rejected, and re-awaiting it
|
|
2124
|
+
// would throw even though the connection is healthy. _ensureReady()
|
|
2125
|
+
// short-circuits on _ready and validates the engine.
|
|
2126
|
+
await this._ensureReady();
|
|
2122
2127
|
if (!this._SQLEngine) {
|
|
2123
2128
|
throw new DatabaseConnectionError(
|
|
2124
2129
|
'Cannot run query: database engine not initialized. Ensure you have awaited env() before querying.',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./MasterRecord.js",
|