@momentumcms/migrations 0.5.10 → 0.5.11
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/package.json +1 -1
- package/src/cli/generate.cjs +20 -1
- package/src/cli/generate.js +21 -2
- package/src/index.cjs +23 -0
- package/src/index.d.ts +1 -1
- package/src/index.js +22 -1
- package/src/lib/loader/snapshot-manager.d.ts +13 -0
package/package.json
CHANGED
package/src/cli/generate.cjs
CHANGED
|
@@ -1378,7 +1378,11 @@ function serializeOperationsMeta(operations) {
|
|
|
1378
1378
|
var import_node_fs = require("node:fs");
|
|
1379
1379
|
var import_node_path = require("node:path");
|
|
1380
1380
|
var SNAPSHOT_FILENAME = ".snapshot.json";
|
|
1381
|
+
var PER_MIGRATION_SNAPSHOT_PATTERN = /^\d{14}_.+\.snapshot\.json$/;
|
|
1381
1382
|
function readSnapshot(directory) {
|
|
1383
|
+
const perMigration = findLatestPerMigrationSnapshot(directory);
|
|
1384
|
+
if (perMigration)
|
|
1385
|
+
return perMigration;
|
|
1382
1386
|
const filePath = (0, import_node_path.join)(directory, SNAPSHOT_FILENAME);
|
|
1383
1387
|
if (!(0, import_node_fs.existsSync)(filePath))
|
|
1384
1388
|
return null;
|
|
@@ -1390,6 +1394,21 @@ function writeSnapshot(directory, snapshot) {
|
|
|
1390
1394
|
const filePath = (0, import_node_path.join)(directory, SNAPSHOT_FILENAME);
|
|
1391
1395
|
(0, import_node_fs.writeFileSync)(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
1392
1396
|
}
|
|
1397
|
+
function writePerMigrationSnapshot(directory, migrationName, snapshot) {
|
|
1398
|
+
(0, import_node_fs.mkdirSync)(directory, { recursive: true });
|
|
1399
|
+
const filePath = (0, import_node_path.join)(directory, `${migrationName}.snapshot.json`);
|
|
1400
|
+
(0, import_node_fs.writeFileSync)(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
1401
|
+
}
|
|
1402
|
+
function findLatestPerMigrationSnapshot(directory) {
|
|
1403
|
+
if (!(0, import_node_fs.existsSync)(directory))
|
|
1404
|
+
return null;
|
|
1405
|
+
const snapshotFiles = (0, import_node_fs.readdirSync)(directory).filter((f) => PER_MIGRATION_SNAPSHOT_PATTERN.test(f)).sort();
|
|
1406
|
+
if (snapshotFiles.length === 0)
|
|
1407
|
+
return null;
|
|
1408
|
+
const latestFile = snapshotFiles[snapshotFiles.length - 1];
|
|
1409
|
+
const json2 = (0, import_node_fs.readFileSync)((0, import_node_path.join)(directory, latestFile), "utf-8");
|
|
1410
|
+
return deserializeSnapshot(json2);
|
|
1411
|
+
}
|
|
1393
1412
|
|
|
1394
1413
|
// libs/migrations/src/cli/shared.ts
|
|
1395
1414
|
var import_node_url = require("node:url");
|
|
@@ -1754,7 +1773,7 @@ Would write: ${(0, import_node_path2.join)(directory, `${timestampedName}.ts`)}`
|
|
|
1754
1773
|
(0, import_node_fs2.mkdirSync)(directory, { recursive: true });
|
|
1755
1774
|
const filePath = (0, import_node_path2.join)(directory, `${timestampedName}.ts`);
|
|
1756
1775
|
(0, import_node_fs2.writeFileSync)(filePath, fileContent, "utf-8");
|
|
1757
|
-
|
|
1776
|
+
writePerMigrationSnapshot(directory, timestampedName, desired);
|
|
1758
1777
|
console.warn(`
|
|
1759
1778
|
Generated migration: ${filePath}`);
|
|
1760
1779
|
console.warn(`Operations: ${diff.operations.length}`);
|
package/src/cli/generate.js
CHANGED
|
@@ -1373,10 +1373,14 @@ function serializeOperationsMeta(operations) {
|
|
|
1373
1373
|
}
|
|
1374
1374
|
|
|
1375
1375
|
// libs/migrations/src/lib/loader/snapshot-manager.ts
|
|
1376
|
-
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
|
|
1376
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync } from "node:fs";
|
|
1377
1377
|
import { join } from "node:path";
|
|
1378
1378
|
var SNAPSHOT_FILENAME = ".snapshot.json";
|
|
1379
|
+
var PER_MIGRATION_SNAPSHOT_PATTERN = /^\d{14}_.+\.snapshot\.json$/;
|
|
1379
1380
|
function readSnapshot(directory) {
|
|
1381
|
+
const perMigration = findLatestPerMigrationSnapshot(directory);
|
|
1382
|
+
if (perMigration)
|
|
1383
|
+
return perMigration;
|
|
1380
1384
|
const filePath = join(directory, SNAPSHOT_FILENAME);
|
|
1381
1385
|
if (!existsSync(filePath))
|
|
1382
1386
|
return null;
|
|
@@ -1388,6 +1392,21 @@ function writeSnapshot(directory, snapshot) {
|
|
|
1388
1392
|
const filePath = join(directory, SNAPSHOT_FILENAME);
|
|
1389
1393
|
writeFileSync(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
1390
1394
|
}
|
|
1395
|
+
function writePerMigrationSnapshot(directory, migrationName, snapshot) {
|
|
1396
|
+
mkdirSync(directory, { recursive: true });
|
|
1397
|
+
const filePath = join(directory, `${migrationName}.snapshot.json`);
|
|
1398
|
+
writeFileSync(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
1399
|
+
}
|
|
1400
|
+
function findLatestPerMigrationSnapshot(directory) {
|
|
1401
|
+
if (!existsSync(directory))
|
|
1402
|
+
return null;
|
|
1403
|
+
const snapshotFiles = readdirSync(directory).filter((f) => PER_MIGRATION_SNAPSHOT_PATTERN.test(f)).sort();
|
|
1404
|
+
if (snapshotFiles.length === 0)
|
|
1405
|
+
return null;
|
|
1406
|
+
const latestFile = snapshotFiles[snapshotFiles.length - 1];
|
|
1407
|
+
const json2 = readFileSync(join(directory, latestFile), "utf-8");
|
|
1408
|
+
return deserializeSnapshot(json2);
|
|
1409
|
+
}
|
|
1391
1410
|
|
|
1392
1411
|
// libs/migrations/src/cli/shared.ts
|
|
1393
1412
|
import { pathToFileURL } from "node:url";
|
|
@@ -1752,7 +1771,7 @@ Would write: ${join2(directory, `${timestampedName}.ts`)}`);
|
|
|
1752
1771
|
mkdirSync2(directory, { recursive: true });
|
|
1753
1772
|
const filePath = join2(directory, `${timestampedName}.ts`);
|
|
1754
1773
|
writeFileSync2(filePath, fileContent, "utf-8");
|
|
1755
|
-
|
|
1774
|
+
writePerMigrationSnapshot(directory, timestampedName, desired);
|
|
1756
1775
|
console.warn(`
|
|
1757
1776
|
Generated migration: ${filePath}`);
|
|
1758
1777
|
console.warn(`Operations: ${diff.operations.length}`);
|
package/src/index.cjs
CHANGED
|
@@ -41,6 +41,7 @@ __export(src_exports, {
|
|
|
41
41
|
fieldToColumnType: () => fieldToColumnType,
|
|
42
42
|
fieldToPostgresType: () => fieldToPostgresType,
|
|
43
43
|
fieldToSqliteType: () => fieldToSqliteType,
|
|
44
|
+
findLatestPerMigrationSnapshot: () => findLatestPerMigrationSnapshot,
|
|
44
45
|
generateMigrationFileContent: () => generateMigrationFileContent,
|
|
45
46
|
generateMigrationName: () => generateMigrationName,
|
|
46
47
|
getAppliedMigrations: () => getAppliedMigrations,
|
|
@@ -67,6 +68,7 @@ __export(src_exports, {
|
|
|
67
68
|
runMigrations: () => runMigrations,
|
|
68
69
|
runPush: () => runPush,
|
|
69
70
|
serializeSnapshot: () => serializeSnapshot,
|
|
71
|
+
writePerMigrationSnapshot: () => writePerMigrationSnapshot,
|
|
70
72
|
writeSnapshot: () => writeSnapshot
|
|
71
73
|
});
|
|
72
74
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -2394,7 +2396,11 @@ async function loadMigrationsFromDisk(directory) {
|
|
|
2394
2396
|
var import_node_fs2 = require("node:fs");
|
|
2395
2397
|
var import_node_path2 = require("node:path");
|
|
2396
2398
|
var SNAPSHOT_FILENAME = ".snapshot.json";
|
|
2399
|
+
var PER_MIGRATION_SNAPSHOT_PATTERN = /^\d{14}_.+\.snapshot\.json$/;
|
|
2397
2400
|
function readSnapshot(directory) {
|
|
2401
|
+
const perMigration = findLatestPerMigrationSnapshot(directory);
|
|
2402
|
+
if (perMigration)
|
|
2403
|
+
return perMigration;
|
|
2398
2404
|
const filePath = (0, import_node_path2.join)(directory, SNAPSHOT_FILENAME);
|
|
2399
2405
|
if (!(0, import_node_fs2.existsSync)(filePath))
|
|
2400
2406
|
return null;
|
|
@@ -2406,6 +2412,21 @@ function writeSnapshot(directory, snapshot) {
|
|
|
2406
2412
|
const filePath = (0, import_node_path2.join)(directory, SNAPSHOT_FILENAME);
|
|
2407
2413
|
(0, import_node_fs2.writeFileSync)(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
2408
2414
|
}
|
|
2415
|
+
function writePerMigrationSnapshot(directory, migrationName, snapshot) {
|
|
2416
|
+
(0, import_node_fs2.mkdirSync)(directory, { recursive: true });
|
|
2417
|
+
const filePath = (0, import_node_path2.join)(directory, `${migrationName}.snapshot.json`);
|
|
2418
|
+
(0, import_node_fs2.writeFileSync)(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
2419
|
+
}
|
|
2420
|
+
function findLatestPerMigrationSnapshot(directory) {
|
|
2421
|
+
if (!(0, import_node_fs2.existsSync)(directory))
|
|
2422
|
+
return null;
|
|
2423
|
+
const snapshotFiles = (0, import_node_fs2.readdirSync)(directory).filter((f) => PER_MIGRATION_SNAPSHOT_PATTERN.test(f)).sort();
|
|
2424
|
+
if (snapshotFiles.length === 0)
|
|
2425
|
+
return null;
|
|
2426
|
+
const latestFile = snapshotFiles[snapshotFiles.length - 1];
|
|
2427
|
+
const json2 = (0, import_node_fs2.readFileSync)((0, import_node_path2.join)(directory, latestFile), "utf-8");
|
|
2428
|
+
return deserializeSnapshot(json2);
|
|
2429
|
+
}
|
|
2409
2430
|
function getSnapshotPath(directory) {
|
|
2410
2431
|
return (0, import_node_path2.join)(directory, SNAPSHOT_FILENAME);
|
|
2411
2432
|
}
|
|
@@ -2545,6 +2566,7 @@ function parseMigrationArgs(args) {
|
|
|
2545
2566
|
fieldToColumnType,
|
|
2546
2567
|
fieldToPostgresType,
|
|
2547
2568
|
fieldToSqliteType,
|
|
2569
|
+
findLatestPerMigrationSnapshot,
|
|
2548
2570
|
generateMigrationFileContent,
|
|
2549
2571
|
generateMigrationName,
|
|
2550
2572
|
getAppliedMigrations,
|
|
@@ -2571,5 +2593,6 @@ function parseMigrationArgs(args) {
|
|
|
2571
2593
|
runMigrations,
|
|
2572
2594
|
runPush,
|
|
2573
2595
|
serializeSnapshot,
|
|
2596
|
+
writePerMigrationSnapshot,
|
|
2574
2597
|
writeSnapshot
|
|
2575
2598
|
});
|
package/src/index.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ export type { CloneCapableDb, CloneTestApplyOptions, CloneTestApplyResult, } fro
|
|
|
30
30
|
export { createDataHelpers } from './lib/helpers/data-helpers';
|
|
31
31
|
export type { DataHelperDb } from './lib/helpers/data-helpers';
|
|
32
32
|
export { loadMigrationsFromDisk } from './lib/loader/migration-loader';
|
|
33
|
-
export { readSnapshot, writeSnapshot, getSnapshotPath } from './lib/loader/snapshot-manager';
|
|
33
|
+
export { readSnapshot, writeSnapshot, writePerMigrationSnapshot, findLatestPerMigrationSnapshot, getSnapshotPath, } from './lib/loader/snapshot-manager';
|
|
34
34
|
export { resolveDialect, buildTrackerFromAdapter, buildContextFromAdapter, buildPushDbFromAdapter, buildCloneDbFromAdapter, buildIntrospector, parseMigrationArgs, } from './cli/shared';
|
|
35
35
|
export type { MigrationCliArgs } from './cli/shared';
|
|
36
36
|
export * from './lib/migration.types';
|
package/src/index.js
CHANGED
|
@@ -2326,10 +2326,14 @@ async function loadMigrationsFromDisk(directory) {
|
|
|
2326
2326
|
}
|
|
2327
2327
|
|
|
2328
2328
|
// libs/migrations/src/lib/loader/snapshot-manager.ts
|
|
2329
|
-
import { readFileSync, writeFileSync, existsSync as existsSync2, mkdirSync } from "node:fs";
|
|
2329
|
+
import { readFileSync, writeFileSync, existsSync as existsSync2, mkdirSync, readdirSync as readdirSync2 } from "node:fs";
|
|
2330
2330
|
import { join as join2 } from "node:path";
|
|
2331
2331
|
var SNAPSHOT_FILENAME = ".snapshot.json";
|
|
2332
|
+
var PER_MIGRATION_SNAPSHOT_PATTERN = /^\d{14}_.+\.snapshot\.json$/;
|
|
2332
2333
|
function readSnapshot(directory) {
|
|
2334
|
+
const perMigration = findLatestPerMigrationSnapshot(directory);
|
|
2335
|
+
if (perMigration)
|
|
2336
|
+
return perMigration;
|
|
2333
2337
|
const filePath = join2(directory, SNAPSHOT_FILENAME);
|
|
2334
2338
|
if (!existsSync2(filePath))
|
|
2335
2339
|
return null;
|
|
@@ -2341,6 +2345,21 @@ function writeSnapshot(directory, snapshot) {
|
|
|
2341
2345
|
const filePath = join2(directory, SNAPSHOT_FILENAME);
|
|
2342
2346
|
writeFileSync(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
2343
2347
|
}
|
|
2348
|
+
function writePerMigrationSnapshot(directory, migrationName, snapshot) {
|
|
2349
|
+
mkdirSync(directory, { recursive: true });
|
|
2350
|
+
const filePath = join2(directory, `${migrationName}.snapshot.json`);
|
|
2351
|
+
writeFileSync(filePath, serializeSnapshot(snapshot), "utf-8");
|
|
2352
|
+
}
|
|
2353
|
+
function findLatestPerMigrationSnapshot(directory) {
|
|
2354
|
+
if (!existsSync2(directory))
|
|
2355
|
+
return null;
|
|
2356
|
+
const snapshotFiles = readdirSync2(directory).filter((f) => PER_MIGRATION_SNAPSHOT_PATTERN.test(f)).sort();
|
|
2357
|
+
if (snapshotFiles.length === 0)
|
|
2358
|
+
return null;
|
|
2359
|
+
const latestFile = snapshotFiles[snapshotFiles.length - 1];
|
|
2360
|
+
const json2 = readFileSync(join2(directory, latestFile), "utf-8");
|
|
2361
|
+
return deserializeSnapshot(json2);
|
|
2362
|
+
}
|
|
2344
2363
|
function getSnapshotPath(directory) {
|
|
2345
2364
|
return join2(directory, SNAPSHOT_FILENAME);
|
|
2346
2365
|
}
|
|
@@ -2479,6 +2498,7 @@ export {
|
|
|
2479
2498
|
fieldToColumnType,
|
|
2480
2499
|
fieldToPostgresType,
|
|
2481
2500
|
fieldToSqliteType,
|
|
2501
|
+
findLatestPerMigrationSnapshot,
|
|
2482
2502
|
generateMigrationFileContent,
|
|
2483
2503
|
generateMigrationName,
|
|
2484
2504
|
getAppliedMigrations,
|
|
@@ -2505,5 +2525,6 @@ export {
|
|
|
2505
2525
|
runMigrations,
|
|
2506
2526
|
runPush,
|
|
2507
2527
|
serializeSnapshot,
|
|
2528
|
+
writePerMigrationSnapshot,
|
|
2508
2529
|
writeSnapshot
|
|
2509
2530
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { DatabaseSchemaSnapshot } from '../schema/schema-snapshot';
|
|
2
2
|
/**
|
|
3
3
|
* Read the schema snapshot from the migrations directory.
|
|
4
|
+
* Prefers per-migration snapshots (latest by timestamp) over the legacy .snapshot.json.
|
|
4
5
|
* Returns null if no snapshot file exists.
|
|
5
6
|
*/
|
|
6
7
|
export declare function readSnapshot(directory: string): DatabaseSchemaSnapshot | null;
|
|
@@ -9,6 +10,18 @@ export declare function readSnapshot(directory: string): DatabaseSchemaSnapshot
|
|
|
9
10
|
* Creates the directory if it doesn't exist.
|
|
10
11
|
*/
|
|
11
12
|
export declare function writeSnapshot(directory: string, snapshot: DatabaseSchemaSnapshot): void;
|
|
13
|
+
/**
|
|
14
|
+
* Write a per-migration snapshot alongside its migration file.
|
|
15
|
+
* Creates a file named `{migrationName}.snapshot.json` in the directory.
|
|
16
|
+
*/
|
|
17
|
+
export declare function writePerMigrationSnapshot(directory: string, migrationName: string, snapshot: DatabaseSchemaSnapshot): void;
|
|
18
|
+
/**
|
|
19
|
+
* Find the latest per-migration snapshot in the directory.
|
|
20
|
+
* Scans for files matching YYYYMMDDHHMMSS_name.snapshot.json,
|
|
21
|
+
* sorts lexicographically (timestamp prefix ensures correct order),
|
|
22
|
+
* and returns the last one.
|
|
23
|
+
*/
|
|
24
|
+
export declare function findLatestPerMigrationSnapshot(directory: string): DatabaseSchemaSnapshot | null;
|
|
12
25
|
/**
|
|
13
26
|
* Get the full path to the snapshot file.
|
|
14
27
|
*/
|