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
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression for the "update-database only runs the latest migration file"
|
|
3
|
+
* bug. Spawns the real CLI against SQLite and asserts that:
|
|
4
|
+
* 1. With THREE pending migrations, all three are applied.
|
|
5
|
+
* 2. A second `update-database` run is a no-op (the tracking table filters
|
|
6
|
+
* out already-applied migrations).
|
|
7
|
+
* 3. `update-database-down` rolls back only the MOST RECENTLY APPLIED
|
|
8
|
+
* migration, not just the latest file on disk.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { test } from 'node:test';
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
import { spawnSync } from 'node:child_process';
|
|
17
|
+
import Database from 'better-sqlite3';
|
|
18
|
+
|
|
19
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
const masterRecordRoot = path.resolve(__dirname, '..');
|
|
21
|
+
const cliEntry = path.join(masterRecordRoot, 'Migrations', 'cli.js');
|
|
22
|
+
|
|
23
|
+
// Each test sets up an isolated project directory with:
|
|
24
|
+
// - package.json (ESM)
|
|
25
|
+
// - config/environments/env.multimigctx.json
|
|
26
|
+
// - app/models/multimigctx.js (context)
|
|
27
|
+
// - app/models/db/migrations/<ts>_<name>_migration.js (3 files)
|
|
28
|
+
// - app/models/db/migrations/multimigctx_contextSnapShot.json
|
|
29
|
+
// - db/ (destination for SQLite file)
|
|
30
|
+
|
|
31
|
+
function makeProject() {
|
|
32
|
+
const projectDir = fs.mkdtempSync(path.join(masterRecordRoot, 'test', 'fixtures', 'multi-migration-'));
|
|
33
|
+
const envDir = path.join(projectDir, 'config', 'environments');
|
|
34
|
+
const modelsDir = path.join(projectDir, 'app', 'models');
|
|
35
|
+
const migrationsDir = path.join(modelsDir, 'db', 'migrations');
|
|
36
|
+
const dbDir = path.join(projectDir, 'db');
|
|
37
|
+
|
|
38
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
39
|
+
fs.mkdirSync(modelsDir, { recursive: true });
|
|
40
|
+
fs.mkdirSync(migrationsDir, { recursive: true });
|
|
41
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
42
|
+
|
|
43
|
+
fs.writeFileSync(
|
|
44
|
+
path.join(projectDir, 'package.json'),
|
|
45
|
+
JSON.stringify({ name: 'multi-migration-fixture', type: 'module' })
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
fs.writeFileSync(
|
|
49
|
+
path.join(envDir, 'env.multimig.json'),
|
|
50
|
+
JSON.stringify({
|
|
51
|
+
multimigctx: {
|
|
52
|
+
type: 'better-sqlite3',
|
|
53
|
+
connection: dbDir + path.sep,
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// Context file — 3 entities: Author, Book, Review
|
|
59
|
+
const mrPath = pathToImportSpecifier(path.join(masterRecordRoot, 'MasterRecord.js'));
|
|
60
|
+
const ctxPath = pathToImportSpecifier(path.join(masterRecordRoot, 'context.js'));
|
|
61
|
+
fs.writeFileSync(
|
|
62
|
+
path.join(modelsDir, 'multimigctx.js'),
|
|
63
|
+
`
|
|
64
|
+
import context from ${JSON.stringify(ctxPath)};
|
|
65
|
+
|
|
66
|
+
class Author {
|
|
67
|
+
id(db) { db.integer().primary().auto(); }
|
|
68
|
+
name(db) { db.string(); }
|
|
69
|
+
}
|
|
70
|
+
class Book {
|
|
71
|
+
id(db) { db.integer().primary().auto(); }
|
|
72
|
+
title(db) { db.string(); }
|
|
73
|
+
}
|
|
74
|
+
class Review {
|
|
75
|
+
id(db) { db.integer().primary().auto(); }
|
|
76
|
+
body(db) { db.string(); }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class multimigctx extends context {
|
|
80
|
+
constructor() {
|
|
81
|
+
super();
|
|
82
|
+
this.env(${JSON.stringify(envDir)});
|
|
83
|
+
this.dbset(Author);
|
|
84
|
+
this.dbset(Book);
|
|
85
|
+
this.dbset(Review);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export default multimigctx;
|
|
89
|
+
`
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Snapshot — lists all three entities as the current schema.
|
|
93
|
+
// The `schema` field must match what buildUpObject expects. Since this
|
|
94
|
+
// is an integration smoke test we construct it manually to reflect the
|
|
95
|
+
// current entity shape the context produces.
|
|
96
|
+
const entityShape = (name, cols) => {
|
|
97
|
+
const obj = { __name: name, __compositeIndexes: [] };
|
|
98
|
+
for (const [cname, def] of Object.entries(cols)) {
|
|
99
|
+
obj[cname] = { ...def, name: cname };
|
|
100
|
+
}
|
|
101
|
+
return obj;
|
|
102
|
+
};
|
|
103
|
+
const schema = [
|
|
104
|
+
entityShape('Author', {
|
|
105
|
+
id: { type: 'integer', primary: true, auto: true, nullable: false, unique: true },
|
|
106
|
+
name: { type: 'string', nullable: true },
|
|
107
|
+
}),
|
|
108
|
+
entityShape('Book', {
|
|
109
|
+
id: { type: 'integer', primary: true, auto: true, nullable: false, unique: true },
|
|
110
|
+
title: { type: 'string', nullable: true },
|
|
111
|
+
}),
|
|
112
|
+
entityShape('Review', {
|
|
113
|
+
id: { type: 'integer', primary: true, auto: true, nullable: false, unique: true },
|
|
114
|
+
body: { type: 'string', nullable: true },
|
|
115
|
+
}),
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
fs.writeFileSync(
|
|
119
|
+
path.join(migrationsDir, 'multimigctx_contextSnapShot.json'),
|
|
120
|
+
JSON.stringify({
|
|
121
|
+
contextLocation: path.relative(migrationsDir, path.join(modelsDir, 'multimigctx.js')),
|
|
122
|
+
migrationFolder: '.',
|
|
123
|
+
snapShotLocation: 'multimigctx_contextSnapShot.json',
|
|
124
|
+
schema,
|
|
125
|
+
seedData: {},
|
|
126
|
+
seedConfig: {},
|
|
127
|
+
}, null, 2)
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// Three migration files with ascending timestamps. Each creates one table.
|
|
131
|
+
// createTable is idempotent so running them all in sequence on a fresh DB
|
|
132
|
+
// produces all three tables.
|
|
133
|
+
const migFor = (ts, className, entityKey) => `
|
|
134
|
+
import masterrecord from ${JSON.stringify(mrPath)};
|
|
135
|
+
class ${className} extends masterrecord.schema {
|
|
136
|
+
constructor(context){ super(context); }
|
|
137
|
+
async up(table){
|
|
138
|
+
await this.init(table);
|
|
139
|
+
await this.createTable(table.${entityKey});
|
|
140
|
+
}
|
|
141
|
+
async down(table){
|
|
142
|
+
await this.init(table);
|
|
143
|
+
await this.dropTable(table.${entityKey});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export default ${className};
|
|
147
|
+
`;
|
|
148
|
+
const ts = [1700000001000, 1700000002000, 1700000003000];
|
|
149
|
+
fs.writeFileSync(path.join(migrationsDir, `${ts[0]}_CreateAuthor_migration.js`), migFor(ts[0], 'CreateAuthor', 'Author'));
|
|
150
|
+
fs.writeFileSync(path.join(migrationsDir, `${ts[1]}_CreateBook_migration.js`), migFor(ts[1], 'CreateBook', 'Book'));
|
|
151
|
+
fs.writeFileSync(path.join(migrationsDir, `${ts[2]}_CreateReview_migration.js`), migFor(ts[2], 'CreateReview', 'Review'));
|
|
152
|
+
|
|
153
|
+
return { projectDir, dbDir, migrationsDir, envDir };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function pathToImportSpecifier(absPath) {
|
|
157
|
+
// Import via absolute file URL so the fixture does not need node_modules.
|
|
158
|
+
return 'file://' + absPath;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function runCli(args, cwd) {
|
|
162
|
+
return spawnSync(process.execPath, [cliEntry, ...args], {
|
|
163
|
+
cwd,
|
|
164
|
+
env: { ...process.env, master: 'multimig' },
|
|
165
|
+
encoding: 'utf8',
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function dbPath(dbDir) {
|
|
170
|
+
return path.join(dbDir, 'multimigctx.sqlite');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function tableNames(dbFile) {
|
|
174
|
+
const db = new Database(dbFile);
|
|
175
|
+
const rows = db.prepare(`SELECT name FROM sqlite_master WHERE type='table' ORDER BY name`).all();
|
|
176
|
+
db.close();
|
|
177
|
+
return rows.map(r => r.name);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function appliedMigrationRows(dbFile) {
|
|
181
|
+
const db = new Database(dbFile);
|
|
182
|
+
const rows = db.prepare(`SELECT migration_name FROM _masterrecord_migrations ORDER BY migration_name`).all();
|
|
183
|
+
db.close();
|
|
184
|
+
return rows.map(r => r.migration_name);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
test('update-database applies ALL pending migrations, not just the latest', () => {
|
|
188
|
+
const { projectDir, dbDir } = makeProject();
|
|
189
|
+
const result = runCli(['update-database', 'multimigctx'], projectDir);
|
|
190
|
+
|
|
191
|
+
assert.equal(result.status, 0, `CLI exited non-zero. stderr:\n${result.stderr}\nstdout:\n${result.stdout}`);
|
|
192
|
+
|
|
193
|
+
const file = dbPath(dbDir);
|
|
194
|
+
assert.ok(fs.existsSync(file), 'SQLite DB file should be created');
|
|
195
|
+
|
|
196
|
+
const tables = tableNames(file);
|
|
197
|
+
assert.ok(tables.includes('Author'), 'Author table should exist (from migration 1)');
|
|
198
|
+
assert.ok(tables.includes('Book'), 'Book table should exist (from migration 2)');
|
|
199
|
+
assert.ok(tables.includes('Review'), 'Review table should exist (from migration 3)');
|
|
200
|
+
assert.ok(tables.includes('_masterrecord_migrations'), 'migration tracking table should exist');
|
|
201
|
+
|
|
202
|
+
const applied = appliedMigrationRows(file);
|
|
203
|
+
assert.equal(applied.length, 3, `all three migrations should be recorded; got ${applied.length}: ${applied.join(', ')}`);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('update-database is a no-op on the second run (tracking table filters)', () => {
|
|
207
|
+
const { projectDir, dbDir } = makeProject();
|
|
208
|
+
const first = runCli(['update-database', 'multimigctx'], projectDir);
|
|
209
|
+
assert.equal(first.status, 0, `first run failed. stderr:\n${first.stderr}`);
|
|
210
|
+
|
|
211
|
+
const second = runCli(['update-database', 'multimigctx'], projectDir);
|
|
212
|
+
assert.equal(second.status, 0, `second run failed. stderr:\n${second.stderr}`);
|
|
213
|
+
assert.match(second.stdout, /already applied|up to date/i, `expected no-op message on second run; got:\n${second.stdout}`);
|
|
214
|
+
|
|
215
|
+
const applied = appliedMigrationRows(dbPath(dbDir));
|
|
216
|
+
assert.equal(applied.length, 3, 'still exactly 3 applied rows after second run');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('update-database-down rolls back the MOST RECENTLY APPLIED migration', () => {
|
|
220
|
+
const { projectDir, dbDir } = makeProject();
|
|
221
|
+
const up = runCli(['update-database', 'multimigctx'], projectDir);
|
|
222
|
+
assert.equal(up.status, 0, `up failed. stderr:\n${up.stderr}`);
|
|
223
|
+
|
|
224
|
+
assert.deepEqual(tableNames(dbPath(dbDir)).filter(n => !n.startsWith('_') && !n.startsWith('sqlite_')).sort(),
|
|
225
|
+
['Author', 'Book', 'Review']);
|
|
226
|
+
|
|
227
|
+
const down = runCli(['update-database-down', 'multimigctx'], projectDir);
|
|
228
|
+
assert.equal(down.status, 0, `down failed. stderr:\n${down.stderr}`);
|
|
229
|
+
|
|
230
|
+
// Review was the latest migration; after down, Review should be gone,
|
|
231
|
+
// Author and Book should remain.
|
|
232
|
+
const tables = tableNames(dbPath(dbDir)).filter(n => !n.startsWith('_') && !n.startsWith('sqlite_'));
|
|
233
|
+
assert.ok(tables.includes('Author'), 'Author should still exist');
|
|
234
|
+
assert.ok(tables.includes('Book'), 'Book should still exist');
|
|
235
|
+
assert.ok(!tables.includes('Review'), 'Review should have been dropped by down()');
|
|
236
|
+
|
|
237
|
+
const applied = appliedMigrationRows(dbPath(dbDir));
|
|
238
|
+
assert.equal(applied.length, 2, `after down, 2 migrations should remain applied; got ${applied.join(', ')}`);
|
|
239
|
+
});
|