Skip to content

Commit

Permalink
Regenerate bundle.json if newer than SQL files, fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jul 4, 2024
1 parent 6b2a177 commit b81f7ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/bundler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
43 changes: 42 additions & 1 deletion test/bundler.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});

/**
Expand Down

0 comments on commit b81f7ce

Please sign in to comment.