diff --git a/.changeset/fruity-guests-take.md b/.changeset/fruity-guests-take.md new file mode 100644 index 0000000..cd2a121 --- /dev/null +++ b/.changeset/fruity-guests-take.md @@ -0,0 +1,6 @@ +--- +"graphile-migrate": patch +--- + +Fix a bug when commit fails: restore the original migration fully (don't drop +`Message` header) diff --git a/__tests__/commit.test.ts b/__tests__/commit.test.ts index 5bf2321..3eb11dd 100644 --- a/__tests__/commit.test.ts +++ b/__tests__/commit.test.ts @@ -29,6 +29,20 @@ it("aborts if current.sql is empty", async () => { ); }); +it("restores migration headers after a failed commit", async () => { + const original = "--! Message: test-migration\n\nselect 1 + ;\n"; + mockFs({ + "migrations/current.sql": original, + }); + + await expect(commit(settings)).rejects.toThrow(); + + expect(await fsp.readFile("migrations/current.sql", "utf8")).toEqual( + original, + ); + expect(await fsp.readdir("migrations/committed")).toEqual([]); +}); + describe.each([[undefined], ["My Commit Message"]])( "commit message '%s'", (commitMessage) => { diff --git a/src/commands/commit.ts b/src/commands/commit.ts index f7a5752..4e98f61 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -119,7 +119,7 @@ export async function _commit( logDbError(parsedSettings, e); parsedSettings.logger.error("ABORTING..."); - await writeCurrentMigration(parsedSettings, currentLocation, body); + await writeCurrentMigration(parsedSettings, currentLocation, contents); await fsp.unlink(newMigrationFilepath); parsedSettings.logger.error("ABORTED AND ROLLED BACK"); throw e;