masterrecord 1.0.1 → 1.0.3
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/Entity/entityTrackerModel.js +8 -1
- package/Migrations/cli.js +155 -23
- package/QueryLanguage/queryMethods.js +9 -1
- package/context.js +20 -3
- package/package.json +1 -1
- package/postgresEngine.js +15 -0
- package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +14 -0
- package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/1700000002000_CreateBook_migration.js +14 -0
- package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/1700000003000_CreateReview_migration.js +14 -0
- package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/multimigctx_contextSnapShot.json +104 -0
- package/test/fixtures/multi-migration-MFc2cl/app/models/multimigctx.js +26 -0
- package/test/fixtures/multi-migration-MFc2cl/config/environments/env.multimig.json +1 -0
- package/test/fixtures/multi-migration-MFc2cl/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-MFc2cl/package.json +1 -0
- package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +14 -0
- package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/1700000002000_CreateBook_migration.js +14 -0
- package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/1700000003000_CreateReview_migration.js +14 -0
- package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/multimigctx_contextSnapShot.json +96 -0
- package/test/fixtures/multi-migration-gWKaXD/app/models/multimigctx.js +26 -0
- package/test/fixtures/multi-migration-gWKaXD/config/environments/env.multimig.json +1 -0
- package/test/fixtures/multi-migration-gWKaXD/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-gWKaXD/package.json +1 -0
- package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +14 -0
- package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/1700000002000_CreateBook_migration.js +14 -0
- package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/1700000003000_CreateReview_migration.js +14 -0
- package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/multimigctx_contextSnapShot.json +96 -0
- package/test/fixtures/multi-migration-vsWfKD/app/models/multimigctx.js +26 -0
- package/test/fixtures/multi-migration-vsWfKD/config/environments/env.multimig.json +1 -0
- package/test/fixtures/multi-migration-vsWfKD/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-vsWfKD/package.json +1 -0
- package/test/fixtures/setter-and-update-drop/config/environments/env.setterdrop.json +1 -0
- package/test/fixtures/setter-and-update-drop/db/setdropctx.sqlite +0 -0
- package/test/setter-and-update-drop.test.js +168 -0
- package/test/update-database-multi.test.js +239 -0
|
@@ -108,7 +108,14 @@ class EntityTrackerModel {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
modelClass.__state = "modified";
|
|
111
|
-
|
|
111
|
+
// Deduplicate: setting the same field twice used to push
|
|
112
|
+
// the name twice, producing duplicate assignments in the
|
|
113
|
+
// UPDATE SET clause (`SET col = ?, col = ?`) which is a
|
|
114
|
+
// hard error in Postgres and may silently take-last in
|
|
115
|
+
// MySQL/SQLite.
|
|
116
|
+
if (!modelClass.__dirtyFields.includes(modelField)) {
|
|
117
|
+
modelClass.__dirtyFields.push(modelField);
|
|
118
|
+
}
|
|
112
119
|
// ensure this entity is tracked on any modification
|
|
113
120
|
if(modelClass.__context && typeof modelClass.__context.__track === 'function'){
|
|
114
121
|
modelClass.__context.__track(modelClass);
|
package/Migrations/cli.js
CHANGED
|
@@ -42,6 +42,77 @@ function __getMigrationTimestamp(file){
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
// ============================================================================
|
|
46
|
+
// Migration tracking — one row per applied migration filename.
|
|
47
|
+
// Without this, update-database cannot tell which files are pending, so it
|
|
48
|
+
// used to run only the last one and silently skip the rest.
|
|
49
|
+
// ============================================================================
|
|
50
|
+
|
|
51
|
+
const __MIGRATIONS_TABLE__ = '_masterrecord_migrations';
|
|
52
|
+
|
|
53
|
+
async function __ensureMigrationsTable(ctx){
|
|
54
|
+
const engine = ctx._SQLEngine;
|
|
55
|
+
if (ctx.isSQLite) {
|
|
56
|
+
await ctx._execute(`CREATE TABLE IF NOT EXISTS [${__MIGRATIONS_TABLE__}] (
|
|
57
|
+
migration_name TEXT PRIMARY KEY,
|
|
58
|
+
applied_at TEXT NOT NULL
|
|
59
|
+
)`);
|
|
60
|
+
} else if (ctx.isMySQL) {
|
|
61
|
+
await ctx._execute(`CREATE TABLE IF NOT EXISTS \`${__MIGRATIONS_TABLE__}\` (
|
|
62
|
+
migration_name VARCHAR(255) PRIMARY KEY,
|
|
63
|
+
applied_at VARCHAR(64) NOT NULL
|
|
64
|
+
)`);
|
|
65
|
+
} else if (ctx.isPostgres) {
|
|
66
|
+
await ctx._execute(`CREATE TABLE IF NOT EXISTS "${__MIGRATIONS_TABLE__}" (
|
|
67
|
+
migration_name VARCHAR(255) PRIMARY KEY,
|
|
68
|
+
applied_at VARCHAR(64) NOT NULL
|
|
69
|
+
)`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function __getAppliedMigrations(ctx){
|
|
74
|
+
const applied = new Set();
|
|
75
|
+
const engine = ctx._SQLEngine;
|
|
76
|
+
let rows;
|
|
77
|
+
try {
|
|
78
|
+
if (ctx.isSQLite) {
|
|
79
|
+
rows = engine.db.prepare(`SELECT migration_name FROM [${__MIGRATIONS_TABLE__}]`).all();
|
|
80
|
+
} else if (ctx.isMySQL) {
|
|
81
|
+
rows = await engine._runWithParams(`SELECT migration_name FROM \`${__MIGRATIONS_TABLE__}\``, []);
|
|
82
|
+
} else if (ctx.isPostgres) {
|
|
83
|
+
const r = await engine._runWithParams(`SELECT migration_name FROM "${__MIGRATIONS_TABLE__}"`, []);
|
|
84
|
+
rows = r && r.rows ? r.rows : [];
|
|
85
|
+
}
|
|
86
|
+
} catch (_) {
|
|
87
|
+
rows = [];
|
|
88
|
+
}
|
|
89
|
+
for (const r of (rows || [])) {
|
|
90
|
+
if (r && r.migration_name) applied.add(r.migration_name);
|
|
91
|
+
}
|
|
92
|
+
return applied;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function __recordMigrationApplied(ctx, migrationName){
|
|
96
|
+
const appliedAt = new Date().toISOString();
|
|
97
|
+
if (ctx.isSQLite) {
|
|
98
|
+
await ctx._execute(`INSERT INTO [${__MIGRATIONS_TABLE__}] (migration_name, applied_at) VALUES (?, ?)`, [migrationName, appliedAt]);
|
|
99
|
+
} else if (ctx.isMySQL) {
|
|
100
|
+
await ctx._execute(`INSERT INTO \`${__MIGRATIONS_TABLE__}\` (migration_name, applied_at) VALUES (?, ?)`, [migrationName, appliedAt]);
|
|
101
|
+
} else if (ctx.isPostgres) {
|
|
102
|
+
await ctx._execute(`INSERT INTO "${__MIGRATIONS_TABLE__}" (migration_name, applied_at) VALUES ($1, $2)`, [migrationName, appliedAt]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function __removeMigrationApplied(ctx, migrationName){
|
|
107
|
+
if (ctx.isSQLite) {
|
|
108
|
+
await ctx._execute(`DELETE FROM [${__MIGRATIONS_TABLE__}] WHERE migration_name = ?`, [migrationName]);
|
|
109
|
+
} else if (ctx.isMySQL) {
|
|
110
|
+
await ctx._execute(`DELETE FROM \`${__MIGRATIONS_TABLE__}\` WHERE migration_name = ?`, [migrationName]);
|
|
111
|
+
} else if (ctx.isPostgres) {
|
|
112
|
+
await ctx._execute(`DELETE FROM "${__MIGRATIONS_TABLE__}" WHERE migration_name = $1`, [migrationName]);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
45
116
|
// Helper to cleanup context and exit
|
|
46
117
|
async function __cleanupAndExit(contextInstance, exitCode = 0) {
|
|
47
118
|
try {
|
|
@@ -360,23 +431,22 @@ program.option('-V', 'output the version');
|
|
|
360
431
|
await __cleanupAndExit(contextInstance, 1);
|
|
361
432
|
}
|
|
362
433
|
|
|
363
|
-
//
|
|
434
|
+
// Sort by timestamp prefix (filename convention) or file mtime as fallback.
|
|
435
|
+
// Then run EVERY pending migration in order, not just the last one —
|
|
436
|
+
// the old behavior silently skipped earlier pending migrations when a
|
|
437
|
+
// user had stacked multiple add-migration calls before deploying.
|
|
364
438
|
var mFiles = migrationFiles.slice().sort(function(a, b){
|
|
365
439
|
return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
|
|
366
440
|
});
|
|
367
|
-
|
|
368
|
-
console.log(`✓ Found ${mFiles.length} migration file(s), using latest: ${path.basename(mFile)}`);
|
|
441
|
+
console.log(`✓ Found ${mFiles.length} migration file(s)`);
|
|
369
442
|
|
|
370
443
|
console.log(`\n🔍 Loading Context file from: ${contextAbs}`);
|
|
371
|
-
var migrationProjectFile;
|
|
372
444
|
var ContextCtor;
|
|
373
445
|
try{
|
|
374
|
-
migrationProjectFile = await __loadUserModule(mFile);
|
|
375
446
|
ContextCtor = await __loadUserModule(contextAbs);
|
|
376
447
|
}catch(err){
|
|
377
|
-
console.error(`\n❌ Error - Cannot load Context
|
|
448
|
+
console.error(`\n❌ Error - Cannot load Context file`);
|
|
378
449
|
console.error(`\nContext file: ${contextAbs}`);
|
|
379
|
-
console.error(`Migration file: ${mFile}`);
|
|
380
450
|
console.error(`\nDetails: ${err.message}`);
|
|
381
451
|
if(err.stack){
|
|
382
452
|
console.error(`\nStack trace:`);
|
|
@@ -390,6 +460,11 @@ program.option('-V', 'output the version');
|
|
|
390
460
|
|
|
391
461
|
try{
|
|
392
462
|
contextInstance = new ContextCtor();
|
|
463
|
+
// MySQL/Postgres initialize asynchronously — wait for the pool
|
|
464
|
+
// before we can run any migration queries against it.
|
|
465
|
+
if (contextInstance && contextInstance._initPromise) {
|
|
466
|
+
await contextInstance._initPromise;
|
|
467
|
+
}
|
|
393
468
|
}catch(err){
|
|
394
469
|
console.error(`\n❌ Error - Failed to instantiate Context`);
|
|
395
470
|
console.error(`\nContext file: ${contextAbs}`);
|
|
@@ -425,6 +500,8 @@ program.option('-V', 'output the version');
|
|
|
425
500
|
}
|
|
426
501
|
}else if(contextInstance.isMySQL){
|
|
427
502
|
console.log(`\n📊 Database Type: MySQL`);
|
|
503
|
+
}else if(contextInstance.isPostgres){
|
|
504
|
+
console.log(`\n📊 Database Type: PostgreSQL`);
|
|
428
505
|
}
|
|
429
506
|
|
|
430
507
|
console.log(`\n🔍 Loading entities from context...`);
|
|
@@ -439,22 +516,54 @@ program.option('-V', 'output the version');
|
|
|
439
516
|
console.error(` this.dbset(Post, 'Post');`);
|
|
440
517
|
}
|
|
441
518
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
519
|
+
// Build tableObj once from the current entity state. All pending
|
|
520
|
+
// migrations share this object — the old design assumed a single
|
|
521
|
+
// migration, so there's no historical per-migration tableObj.
|
|
522
|
+
// Idempotent helpers (createTable checks tableExists; addColumn
|
|
523
|
+
// no-ops on undefined `table.col`) make this safe in practice.
|
|
524
|
+
var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
|
|
525
|
+
|
|
526
|
+
// Bootstrap the migrations tracking table, then filter out any
|
|
527
|
+
// migrations already applied so re-running update-database is a
|
|
528
|
+
// no-op. Must run before any migration's `up()`.
|
|
529
|
+
try {
|
|
530
|
+
await __ensureMigrationsTable(contextInstance);
|
|
531
|
+
} catch (err) {
|
|
532
|
+
console.error(`\n❌ Error - Could not create migration tracking table`);
|
|
450
533
|
console.error(`\nDetails: ${err.message}`);
|
|
451
|
-
if(err.stack){
|
|
452
|
-
console.error(`\nStack trace:`);
|
|
453
|
-
console.error(err.stack);
|
|
454
|
-
}
|
|
455
534
|
await __cleanupAndExit(contextInstance, 1);
|
|
456
535
|
}
|
|
457
536
|
|
|
537
|
+
const appliedMigrations = await __getAppliedMigrations(contextInstance);
|
|
538
|
+
const pending = mFiles.filter(f => !appliedMigrations.has(path.basename(f)));
|
|
539
|
+
|
|
540
|
+
if (pending.length === 0) {
|
|
541
|
+
console.log(`\n✓ All migrations already applied (${appliedMigrations.size} on record). Database is up to date.`);
|
|
542
|
+
} else {
|
|
543
|
+
console.log(`\n🚀 Running ${pending.length} pending migration(s)...`);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
for (const mFile of pending) {
|
|
547
|
+
const migrationName = path.basename(mFile);
|
|
548
|
+
console.log(`\n → ${migrationName}`);
|
|
549
|
+
try {
|
|
550
|
+
const MigrationCtor = await __loadUserModule(mFile);
|
|
551
|
+
const instance = new MigrationCtor(ContextCtor);
|
|
552
|
+
await instance.up(tableObj);
|
|
553
|
+
await __recordMigrationApplied(contextInstance, migrationName);
|
|
554
|
+
console.log(` ✓ applied`);
|
|
555
|
+
} catch (err) {
|
|
556
|
+
console.error(`\n❌ Error - Migration '${migrationName}' failed during execution`);
|
|
557
|
+
console.error(`\nDetails: ${err.message}`);
|
|
558
|
+
if (err.stack) {
|
|
559
|
+
console.error(`\nStack trace:`);
|
|
560
|
+
console.error(err.stack);
|
|
561
|
+
}
|
|
562
|
+
console.error(`\n💡 Earlier migrations (if any) were already applied and recorded. Fix the failing migration and re-run update-database to resume from this point.`);
|
|
563
|
+
await __cleanupAndExit(contextInstance, 1);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
458
567
|
console.log(`\n💾 Updating snapshot...`);
|
|
459
568
|
var snap = {
|
|
460
569
|
file : contextAbs,
|
|
@@ -525,11 +634,11 @@ program.option('-V', 'output the version');
|
|
|
525
634
|
console.log("Error - Cannot read or find migration file");
|
|
526
635
|
return;
|
|
527
636
|
}
|
|
528
|
-
// Sort
|
|
637
|
+
// Sort so we can find the latest APPLIED migration (not just the
|
|
638
|
+
// latest on disk) using the tracking table.
|
|
529
639
|
var mFiles = migrationFiles.slice().sort(function(a, b){
|
|
530
640
|
return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
|
|
531
641
|
});
|
|
532
|
-
var latestFile = mFiles[mFiles.length - 1];
|
|
533
642
|
|
|
534
643
|
// Prepare context and table object
|
|
535
644
|
let ContextCtor;
|
|
@@ -548,6 +657,9 @@ program.option('-V', 'output the version');
|
|
|
548
657
|
var contextInstance;
|
|
549
658
|
try{
|
|
550
659
|
contextInstance = new ContextCtor();
|
|
660
|
+
if (contextInstance && contextInstance._initPromise) {
|
|
661
|
+
await contextInstance._initPromise;
|
|
662
|
+
}
|
|
551
663
|
}catch(err){
|
|
552
664
|
console.error(`\n❌ Error - Failed to construct Context from '${contextAbs}'`);
|
|
553
665
|
console.error(`\nThis usually happens when:`);
|
|
@@ -565,12 +677,32 @@ program.option('-V', 'output the version');
|
|
|
565
677
|
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
566
678
|
var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
|
|
567
679
|
|
|
568
|
-
|
|
680
|
+
// Roll back the most recently APPLIED migration. Falls back to the
|
|
681
|
+
// latest migration file on disk if the tracking table is missing or
|
|
682
|
+
// empty (preserves legacy behavior for users who haven't run
|
|
683
|
+
// update-database under the tracked system yet).
|
|
684
|
+
await __ensureMigrationsTable(contextInstance);
|
|
685
|
+
const applied = await __getAppliedMigrations(contextInstance);
|
|
686
|
+
let rollbackFile;
|
|
687
|
+
let rollbackName;
|
|
688
|
+
if (applied.size > 0) {
|
|
689
|
+
// Pick the applied file with the latest timestamp prefix.
|
|
690
|
+
const appliedFiles = mFiles.filter(f => applied.has(path.basename(f)));
|
|
691
|
+
rollbackFile = appliedFiles[appliedFiles.length - 1];
|
|
692
|
+
rollbackName = path.basename(rollbackFile || mFiles[mFiles.length - 1]);
|
|
693
|
+
} else {
|
|
694
|
+
rollbackFile = mFiles[mFiles.length - 1];
|
|
695
|
+
rollbackName = path.basename(rollbackFile);
|
|
696
|
+
console.log(`⚠️ No applied-migration record found; falling back to latest file on disk: ${rollbackName}`);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
var MigCtor = await __loadUserModule(rollbackFile);
|
|
569
700
|
var migInstance = new MigCtor(ContextCtor);
|
|
570
701
|
if(typeof migInstance.down === 'function'){
|
|
571
702
|
await migInstance.down(tableObj);
|
|
703
|
+
await __removeMigrationApplied(contextInstance, rollbackName);
|
|
572
704
|
}else{
|
|
573
|
-
console.log(`Warning - Migration '${
|
|
705
|
+
console.log(`Warning - Migration '${rollbackName}' has no down method; skipping.`);
|
|
574
706
|
}
|
|
575
707
|
|
|
576
708
|
// Update snapshot
|
|
@@ -609,7 +609,15 @@ class queryMethods{
|
|
|
609
609
|
}
|
|
610
610
|
}
|
|
611
611
|
|
|
612
|
-
|
|
612
|
+
// Apply fieldDef.set transform if the entity defines one — the
|
|
613
|
+
// tracker-entity setter (entityTrackerModel.build) already does
|
|
614
|
+
// this on UPDATE; we need the same behavior on INSERT so values
|
|
615
|
+
// normalize consistently regardless of which path created them.
|
|
616
|
+
if (fieldDef && typeof fieldDef.set === 'function') {
|
|
617
|
+
this.__proto__["_" + fname] = fieldDef.set(value);
|
|
618
|
+
} else {
|
|
619
|
+
this.__proto__["_" + fname] = value;
|
|
620
|
+
}
|
|
613
621
|
if(!this.__dirtyFields.includes(fname)){
|
|
614
622
|
this.__dirtyFields.push(fname);
|
|
615
623
|
}
|
package/context.js
CHANGED
|
@@ -1699,9 +1699,14 @@ class context {
|
|
|
1699
1699
|
}
|
|
1700
1700
|
|
|
1701
1701
|
if (entities.length === 1) {
|
|
1702
|
-
// Single update - use existing logic
|
|
1702
|
+
// Single update - use existing logic.
|
|
1703
|
+
// NOTE: the second argument must be `__entity` (double underscore).
|
|
1704
|
+
// The previous code used `_entity` which is undefined, so
|
|
1705
|
+
// `removePrimarykeyandVirtual` received `undefined` and stripped
|
|
1706
|
+
// nothing — a latent bug that left primary-key/virtual fields in
|
|
1707
|
+
// the UPDATE SET clause if they ever landed in __dirtyFields.
|
|
1703
1708
|
const currentModel = entities[0];
|
|
1704
|
-
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.
|
|
1709
|
+
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.__entity);
|
|
1705
1710
|
const argu = this._SQLEngine._buildSQLEqualToParameterized(cleanCurrentModel);
|
|
1706
1711
|
|
|
1707
1712
|
if (argu !== -1) {
|
|
@@ -1721,7 +1726,7 @@ class context {
|
|
|
1721
1726
|
const updateQueries = [];
|
|
1722
1727
|
|
|
1723
1728
|
for (const currentModel of entities) {
|
|
1724
|
-
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.
|
|
1729
|
+
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.__entity);
|
|
1725
1730
|
const argu = this._SQLEngine._buildSQLEqualToParameterized(cleanCurrentModel);
|
|
1726
1731
|
|
|
1727
1732
|
if (argu !== -1) {
|
|
@@ -1754,6 +1759,18 @@ class context {
|
|
|
1754
1759
|
await entity.afterSave.call(entity);
|
|
1755
1760
|
}
|
|
1756
1761
|
}
|
|
1762
|
+
|
|
1763
|
+
// Reset tracker state on the entity itself so a subsequent
|
|
1764
|
+
// saveChanges() doesn't re-emit the same UPDATE for the same fields.
|
|
1765
|
+
// Previously only _processBatchInserts did this, not updates —
|
|
1766
|
+
// leaving __state="modified" and __dirtyFields populated. On the
|
|
1767
|
+
// next modification of a different field, the stale fields would
|
|
1768
|
+
// be re-written, silently overwriting any concurrent external
|
|
1769
|
+
// changes to those columns.
|
|
1770
|
+
for (const entity of entities) {
|
|
1771
|
+
entity.__state = 'track';
|
|
1772
|
+
entity.__dirtyFields = [];
|
|
1773
|
+
}
|
|
1757
1774
|
}
|
|
1758
1775
|
|
|
1759
1776
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.003",
|
|
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",
|
package/postgresEngine.js
CHANGED
|
@@ -255,6 +255,21 @@ class postgresEngine {
|
|
|
255
255
|
return await this._runWithParams(query, params);
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Introspection: Check if a table exists in the current Postgres schema.
|
|
260
|
+
* Mirrors the SQLite/MySQL engines so migration code and the CLI's
|
|
261
|
+
* migration tracker can call it uniformly across all three drivers.
|
|
262
|
+
*/
|
|
263
|
+
async tableExists(tableName) {
|
|
264
|
+
try {
|
|
265
|
+
const sql = `SELECT 1 FROM information_schema.tables WHERE table_schema = ANY(current_schemas(false)) AND table_name = $1 LIMIT 1`;
|
|
266
|
+
const result = await this._runWithParams(sql, [tableName]);
|
|
267
|
+
return !!(result && result.rows && result.rows.length > 0);
|
|
268
|
+
} catch (_) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
258
273
|
/**
|
|
259
274
|
* Build complete SELECT query with parameters
|
|
260
275
|
*/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
+
class CreateAuthor extends masterrecord.schema {
|
|
4
|
+
constructor(context){ super(context); }
|
|
5
|
+
async up(table){
|
|
6
|
+
await this.init(table);
|
|
7
|
+
await this.createTable(table.Author);
|
|
8
|
+
}
|
|
9
|
+
async down(table){
|
|
10
|
+
await this.init(table);
|
|
11
|
+
await this.dropTable(table.Author);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default CreateAuthor;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
+
class CreateBook extends masterrecord.schema {
|
|
4
|
+
constructor(context){ super(context); }
|
|
5
|
+
async up(table){
|
|
6
|
+
await this.init(table);
|
|
7
|
+
await this.createTable(table.Book);
|
|
8
|
+
}
|
|
9
|
+
async down(table){
|
|
10
|
+
await this.init(table);
|
|
11
|
+
await this.dropTable(table.Book);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default CreateBook;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
+
class CreateReview extends masterrecord.schema {
|
|
4
|
+
constructor(context){ super(context); }
|
|
5
|
+
async up(table){
|
|
6
|
+
await this.init(table);
|
|
7
|
+
await this.createTable(table.Review);
|
|
8
|
+
}
|
|
9
|
+
async down(table){
|
|
10
|
+
await this.init(table);
|
|
11
|
+
await this.dropTable(table.Review);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default CreateReview;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"contextLocation": "../../multimigctx.js",
|
|
3
|
+
"migrationFolder": ".",
|
|
4
|
+
"snapShotLocation": "multimigctx_contextSnapShot.json",
|
|
5
|
+
"schema": [
|
|
6
|
+
{
|
|
7
|
+
"id": {
|
|
8
|
+
"name": "id",
|
|
9
|
+
"type": "integer",
|
|
10
|
+
"primary": true,
|
|
11
|
+
"nullable": false,
|
|
12
|
+
"unique": true,
|
|
13
|
+
"auto": true,
|
|
14
|
+
"cascadeOnDelete": true,
|
|
15
|
+
"lazyLoading": true,
|
|
16
|
+
"isNavigational": false,
|
|
17
|
+
"skipGetFunction": false,
|
|
18
|
+
"valueConversion": true
|
|
19
|
+
},
|
|
20
|
+
"name": {
|
|
21
|
+
"name": "name",
|
|
22
|
+
"type": "string",
|
|
23
|
+
"nullable": true,
|
|
24
|
+
"unique": false,
|
|
25
|
+
"auto": false,
|
|
26
|
+
"cascadeOnDelete": true,
|
|
27
|
+
"lazyLoading": true,
|
|
28
|
+
"isNavigational": false,
|
|
29
|
+
"skipGetFunction": false,
|
|
30
|
+
"valueConversion": true
|
|
31
|
+
},
|
|
32
|
+
"__compositeIndexes": [],
|
|
33
|
+
"__name": "Author"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"id": {
|
|
37
|
+
"name": "id",
|
|
38
|
+
"type": "integer",
|
|
39
|
+
"primary": true,
|
|
40
|
+
"nullable": false,
|
|
41
|
+
"unique": true,
|
|
42
|
+
"auto": true,
|
|
43
|
+
"cascadeOnDelete": true,
|
|
44
|
+
"lazyLoading": true,
|
|
45
|
+
"isNavigational": false,
|
|
46
|
+
"skipGetFunction": false,
|
|
47
|
+
"valueConversion": true
|
|
48
|
+
},
|
|
49
|
+
"title": {
|
|
50
|
+
"name": "title",
|
|
51
|
+
"type": "string",
|
|
52
|
+
"nullable": true,
|
|
53
|
+
"unique": false,
|
|
54
|
+
"auto": false,
|
|
55
|
+
"cascadeOnDelete": true,
|
|
56
|
+
"lazyLoading": true,
|
|
57
|
+
"isNavigational": false,
|
|
58
|
+
"skipGetFunction": false,
|
|
59
|
+
"valueConversion": true
|
|
60
|
+
},
|
|
61
|
+
"__compositeIndexes": [],
|
|
62
|
+
"__name": "Book"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"id": {
|
|
66
|
+
"name": "id",
|
|
67
|
+
"type": "integer",
|
|
68
|
+
"primary": true,
|
|
69
|
+
"nullable": false,
|
|
70
|
+
"unique": true,
|
|
71
|
+
"auto": true,
|
|
72
|
+
"cascadeOnDelete": true,
|
|
73
|
+
"lazyLoading": true,
|
|
74
|
+
"isNavigational": false,
|
|
75
|
+
"skipGetFunction": false,
|
|
76
|
+
"valueConversion": true
|
|
77
|
+
},
|
|
78
|
+
"body": {
|
|
79
|
+
"name": "body",
|
|
80
|
+
"type": "string",
|
|
81
|
+
"nullable": true,
|
|
82
|
+
"unique": false,
|
|
83
|
+
"auto": false,
|
|
84
|
+
"cascadeOnDelete": true,
|
|
85
|
+
"lazyLoading": true,
|
|
86
|
+
"isNavigational": false,
|
|
87
|
+
"skipGetFunction": false,
|
|
88
|
+
"valueConversion": true
|
|
89
|
+
},
|
|
90
|
+
"__compositeIndexes": [],
|
|
91
|
+
"__name": "Review"
|
|
92
|
+
}
|
|
93
|
+
],
|
|
94
|
+
"seedData": {},
|
|
95
|
+
"seedConfig": {
|
|
96
|
+
"generateDownMigrations": false,
|
|
97
|
+
"downStrategy": "delete",
|
|
98
|
+
"deleteByPrimaryKey": true,
|
|
99
|
+
"onRollbackError": "warn",
|
|
100
|
+
"detectCircularDependencies": true,
|
|
101
|
+
"circularStrategy": "warn",
|
|
102
|
+
"defaultStrategy": "insert"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
import context from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/context.js";
|
|
3
|
+
|
|
4
|
+
class Author {
|
|
5
|
+
id(db) { db.integer().primary().auto(); }
|
|
6
|
+
name(db) { db.string(); }
|
|
7
|
+
}
|
|
8
|
+
class Book {
|
|
9
|
+
id(db) { db.integer().primary().auto(); }
|
|
10
|
+
title(db) { db.string(); }
|
|
11
|
+
}
|
|
12
|
+
class Review {
|
|
13
|
+
id(db) { db.integer().primary().auto(); }
|
|
14
|
+
body(db) { db.string(); }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class multimigctx extends context {
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
this.env("/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-MFc2cl/config/environments");
|
|
21
|
+
this.dbset(Author);
|
|
22
|
+
this.dbset(Book);
|
|
23
|
+
this.dbset(Review);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export default multimigctx;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"multimigctx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-MFc2cl/db/"}}
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"multi-migration-fixture","type":"module"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
+
class CreateAuthor extends masterrecord.schema {
|
|
4
|
+
constructor(context){ super(context); }
|
|
5
|
+
async up(table){
|
|
6
|
+
await this.init(table);
|
|
7
|
+
await this.createTable(table.Author);
|
|
8
|
+
}
|
|
9
|
+
async down(table){
|
|
10
|
+
await this.init(table);
|
|
11
|
+
await this.dropTable(table.Author);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default CreateAuthor;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
+
class CreateBook extends masterrecord.schema {
|
|
4
|
+
constructor(context){ super(context); }
|
|
5
|
+
async up(table){
|
|
6
|
+
await this.init(table);
|
|
7
|
+
await this.createTable(table.Book);
|
|
8
|
+
}
|
|
9
|
+
async down(table){
|
|
10
|
+
await this.init(table);
|
|
11
|
+
await this.dropTable(table.Book);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default CreateBook;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
+
class CreateReview extends masterrecord.schema {
|
|
4
|
+
constructor(context){ super(context); }
|
|
5
|
+
async up(table){
|
|
6
|
+
await this.init(table);
|
|
7
|
+
await this.createTable(table.Review);
|
|
8
|
+
}
|
|
9
|
+
async down(table){
|
|
10
|
+
await this.init(table);
|
|
11
|
+
await this.dropTable(table.Review);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default CreateReview;
|