Skip to content

Commit

Permalink
fix: verify pending correctly for custom templates (#565)
Browse files Browse the repository at this point in the history
Co-authored-by: Misha Kaletsky <mmkal@users.noreply.github.com>
  • Loading branch information
mmkal and mmkal committed Aug 13, 2022
1 parent 101e81a commit 92add1c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ const umzug = new Umzug({
})
```

The create command includes some safety checks to make sure migrations aren't created with ambiguous ordering, and that they will be picked up by umzug when applying migrations.
The create command includes some safety checks to make sure migrations aren't created with ambiguous ordering, and that they will be picked up by umzug when applying migrations. The first pair is expected to be the "up" migration file, and to be picked up by the `pending` command.

Use `node migrator create --help` for more options:

Expand Down
6 changes: 4 additions & 2 deletions src/umzug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,12 @@ export class Umzug<Ctx extends object = object> extends emittery<UmzugEvents<Ctx
});

if (!options.skipVerify) {
const [firstFilePath] = toWrite[0];
const pending = await this._pending(context);
if (!pending.some(p => p.path && path.resolve(p.path) === path.resolve(filepath))) {
if (!pending.some(p => p.path && path.resolve(p.path) === path.resolve(firstFilePath))) {
const paths = pending.map(p => p.path).join(', ');
throw new Error(
`Expected ${filepath} to be a pending migration but it wasn't! You should investigate this. Use skipVerify to bypass this error.`
`Expected ${firstFilePath} to be a pending migration but it wasn't! Pending migration paths: ${paths}. You should investigate this. Use skipVerify to bypass this error.`
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('create migration file', () => {
);

await expect(runCLI(['create', '--name', 'm4.txt', '--allow-extension', '.txt'])).rejects.toThrowError(
/Expected .*2000.01.06T00.00.00.m4.txt to be a pending migration but it wasn't! You should investigate this./
/Expected .*2000.01.06T00.00.00.m4.txt to be a pending migration but it wasn't! Pending migration paths: (.*). You should investigate this./
);

await expect(runCLI(['create', '--name', 'm4.txt', '--allow-extension', '.txt', '--skip-verify'])).resolves
Expand Down
21 changes: 21 additions & 0 deletions test/umzug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ describe('custom context', () => {
expect(spy).toHaveBeenCalledTimes(1);
});

test(`create with custom template extension doesn't cause bogus warning`, async () => {
const syncer = fsSyncer(path.join(__dirname, 'generated/create-custom-template'), {});
syncer.sync();

const umzug = new Umzug({
migrations: {
glob: ['*.js', { cwd: syncer.baseDir }],
},
logger: undefined,
create: {
folder: syncer.baseDir,
template: filepath => [[`${filepath}.x.js`, `/* custom template */`]],
},
});

await umzug.create({ name: 'test' });
const pending = names(await umzug.pending());
expect(pending).toHaveLength(1);
expect(pending[0]).toContain('test.x.js');
});

describe(`resolve asynchronous context getter before the migrations run`, () => {
const sleep = async (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const getContext = async () => {
Expand Down

0 comments on commit 92add1c

Please sign in to comment.