Skip to content

Commit

Permalink
fix(migrations): always ensure the migrations folder exists
Browse files Browse the repository at this point in the history
Closes #907
  • Loading branch information
B4nan committed Oct 8, 2020
1 parent e6101a2 commit a1e0703
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/migrations/src/Migrator.ts
Expand Up @@ -39,6 +39,8 @@ export class Migrator {
}

async createMigration(path?: string, blank = false, initial = false): Promise<MigrationResult> {
await ensureDir(Utils.normalizePath(this.options.path!));

if (initial) {
await this.validateInitialMigration();
}
Expand All @@ -63,7 +65,6 @@ export class Migrator {
}

async validateInitialMigration() {
await ensureDir(Utils.normalizePath(this.options.path!));
const executed = await this.getExecutedMigrations();
const pending = await this.getPendingMigrations();

Expand All @@ -73,11 +74,13 @@ export class Migrator {
}

async getExecutedMigrations(): Promise<MigrationRow[]> {
await ensureDir(Utils.normalizePath(this.options.path!));
await this.storage.ensureTable();
return this.storage.getExecutedMigrations();
}

async getPendingMigrations(): Promise<UmzugMigration[]> {
await ensureDir(Utils.normalizePath(this.options.path!));
await this.storage.ensureTable();
return this.umzug.pending();
}
Expand Down Expand Up @@ -162,6 +165,7 @@ export class Migrator {
}

private async runMigrations(method: 'up' | 'down', options?: string | string[] | MigrateOptions) {
await ensureDir(Utils.normalizePath(this.options.path!));
await this.storage.ensureTable();

if (!this.options.transactional || !this.options.allOrNothing) {
Expand Down
6 changes: 6 additions & 0 deletions tests/Migrator.test.ts
Expand Up @@ -138,6 +138,12 @@ describe('Migrator', () => {
upMock.mockRestore();
});

test('run schema migration without existing migrations folder (GH #907)', async () => {
await remove(process.cwd() + '/temp/migrations');
const migrator = new Migrator(orm.em);
await migrator.up();
});

test('ensureTable and list executed migrations', async () => {
await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!);
const migrator = new Migrator(orm.em);
Expand Down

0 comments on commit a1e0703

Please sign in to comment.