|
| 1 | +jest.mock("child_process"); |
1 | 2 | import "./helpers"; // Has side-effects; must come first |
2 | 3 |
|
| 4 | +import { exec } from "child_process"; |
3 | 5 | import mockFs from "mock-fs"; |
4 | 6 |
|
5 | 7 | import { current } from "../src"; |
6 | 8 | import { withClient } from "../src/pg"; |
7 | | -import { ParsedSettings, parseSettings } from "../src/settings"; |
| 9 | +import { ParsedSettings, parseSettings, Settings } from "../src/settings"; |
8 | 10 | import { makeMigrations, resetDb, settings } from "./helpers"; |
9 | 11 |
|
10 | 12 | beforeEach(resetDb); |
@@ -134,3 +136,69 @@ it("runs migrations", async () => { |
134 | 136 | expect(newTables).toEqual(tables); |
135 | 137 | expect(newEnums).toEqual(enums); |
136 | 138 | }); |
| 139 | + |
| 140 | +it("runs actions when forceActions is set", async () => { |
| 141 | + const ACTIONS = { |
| 142 | + initial: { |
| 143 | + forceActions: false, |
| 144 | + currentSql: "", |
| 145 | + expectedActions: [ |
| 146 | + "beforeAllMigrations", |
| 147 | + "afterAllMigrations", |
| 148 | + "beforeCurrent", |
| 149 | + "afterCurrent", |
| 150 | + ], |
| 151 | + }, |
| 152 | + currentChange: { |
| 153 | + forceActions: false, |
| 154 | + currentSql: MIGRATION_NOTRX_TEXT, |
| 155 | + expectedActions: ["beforeCurrent", "afterCurrent"], |
| 156 | + }, |
| 157 | + noop: { |
| 158 | + forceActions: false, |
| 159 | + currentSql: MIGRATION_NOTRX_TEXT, |
| 160 | + expectedActions: [], |
| 161 | + }, |
| 162 | + forceActions: { |
| 163 | + forceActions: true, |
| 164 | + currentSql: MIGRATION_NOTRX_TEXT, |
| 165 | + expectedActions: [ |
| 166 | + "beforeAllMigrations", |
| 167 | + "afterAllMigrations", |
| 168 | + "beforeCurrent", |
| 169 | + "afterCurrent", |
| 170 | + ], |
| 171 | + }, |
| 172 | + } as const; |
| 173 | + const settingsWithHooks: Settings = { |
| 174 | + ...settings, |
| 175 | + beforeAllMigrations: [ |
| 176 | + { _: "command", command: "echo did_beforeAllMigrations" }, |
| 177 | + ], |
| 178 | + afterAllMigrations: [ |
| 179 | + { _: "command", command: "echo did_afterAllMigrations" }, |
| 180 | + ], |
| 181 | + beforeCurrent: [{ _: "command", command: "echo did_beforeCurrent" }], |
| 182 | + afterCurrent: [{ _: "command", command: "echo did_afterCurrent" }], |
| 183 | + }; |
| 184 | + for (const mode of Object.keys(ACTIONS) as Array<keyof typeof ACTIONS>) { |
| 185 | + const { forceActions, currentSql, expectedActions } = ACTIONS[mode]; |
| 186 | + |
| 187 | + mockFs({ |
| 188 | + [`migrations/committed/000001.sql`]: MIGRATION_1_COMMITTED, |
| 189 | + [`migrations/committed/000002.sql`]: MIGRATION_ENUM_COMMITTED, // Creates enum with 1 value |
| 190 | + "migrations/current.sql": currentSql, |
| 191 | + }); |
| 192 | + |
| 193 | + const mockedExec: jest.Mock<typeof exec> = exec as any; |
| 194 | + mockedExec.mockClear(); |
| 195 | + mockedExec.mockImplementation((_cmd, _options, callback) => |
| 196 | + callback(null, { stdout: "", stderr: "" }), |
| 197 | + ); |
| 198 | + await current(settingsWithHooks, { forceActions }); |
| 199 | + const calledActions = mockedExec.mock.calls.map((c) => |
| 200 | + c[0].substring("echo did_".length), |
| 201 | + ); |
| 202 | + expect(calledActions).toEqual(expectedActions); |
| 203 | + } |
| 204 | +}); |
0 commit comments