From b81f7cee06b478de5c11a7a1b015501ee9656be3 Mon Sep 17 00:00:00 2001 From: Viktor Podzigun Date: Thu, 4 Jul 2024 18:34:36 +0200 Subject: [PATCH] Regenerate bundle.json if newer than SQL files, fixed #2 --- src/bundler.mjs | 7 ++++++- test/bundler.test.mjs | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/bundler.mjs b/src/bundler.mjs index 6ee21ec..662cf32 100644 --- a/src/bundler.mjs +++ b/src/bundler.mjs @@ -44,7 +44,7 @@ export async function createBundle(args) { const migrationsBundle = path.join(migrationsDir, bundleFileName); const bundleStats = getFileStats(migrationsBundle); - if (!bundleStats || bundleStats.mtimeMs < lastModified) { + if (!bundleStats || bundleStats.mtimeMs !== lastModified) { /** @type {MigrationBundle} */ const bundleObj = sqlFiles.map((file) => { return { @@ -58,6 +58,11 @@ export async function createBundle(args) { JSON.stringify(bundleObj, undefined, 2), { encoding: "utf8" } ); + fs.utimesSync( + migrationsBundle, + fs.lstatSync(migrationsBundle).atimeMs / 1000, + lastModified / 1000 + ); console.log(`Generated SQL bundle file: ${migrationsBundle}`); return; } diff --git a/test/bundler.test.mjs b/test/bundler.test.mjs index 7ea5372..d5947a3 100644 --- a/test/bundler.test.mjs +++ b/test/bundler.test.mjs @@ -149,7 +149,7 @@ describe("bundler.test.mjs", () => { assertBundleFile(migrationsBundle); }); - it("should re-generate bundle file if it's outdated", async () => { + it("should re-generate bundle file if it's older", async () => { //given if (fs.existsSync(migrationsBundle)) { fs.unlinkSync(migrationsBundle); @@ -189,6 +189,47 @@ describe("bundler.test.mjs", () => { ]); assertBundleFile(migrationsBundle); }); + + it("should re-generate bundle file if it's newer", async () => { + //given + if (fs.existsSync(migrationsBundle)) { + fs.unlinkSync(migrationsBundle); + } + const logs = /** @type {string[]} */ ([]); + const logMock = mockFunction((msg) => { + logs.push(msg); + }); + const savedLog = console.log; + console.log = logMock; + + //when & then + await createBundle([migrationsDir]); + assert.deepEqual(logMock.times, 1); + assert.deepEqual(logs, [`Generated SQL bundle file: ${migrationsBundle}`]); + assertBundleFile(migrationsBundle); + + const sqlStats = fs.lstatSync( + path.join(migrationsDir, "V001__initial_db_structure.sql") + ); + const bundleStats = fs.lstatSync(migrationsBundle); + fs.utimesSync( + migrationsBundle, + bundleStats.atimeMs / 1000, + sqlStats.mtimeMs / 1000 + 60 // set bundle time to plus 60 sec. + ); + + //when + await createBundle([migrationsDir]); + + //then + console.log = savedLog; + assert.deepEqual(logMock.times, 2); + assert.deepEqual(logs, [ + `Generated SQL bundle file: ${migrationsBundle}`, + `Generated SQL bundle file: ${migrationsBundle}`, + ]); + assertBundleFile(migrationsBundle); + }); }); /**