Skip to content

Commit

Permalink
Take migration title from filename, not exported object, since the fi…
Browse files Browse the repository at this point in the history
…lesystem already guarantees order and uniqueness
  • Loading branch information
rbayliss committed Dec 31, 2019
1 parent f90a729 commit cec9803
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
1 change: 0 additions & 1 deletion fixtures/dup-ok1.js → fixtures/duplicate/ok1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

module.exports = {
title: 'OK1',
up: function(client) {

},
Expand Down
1 change: 0 additions & 1 deletion fixtures/ok1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

module.exports = {
title: 'OK1',
up: function(client) {

},
Expand Down
1 change: 0 additions & 1 deletion fixtures/ok2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

module.exports = {
title: 'OK2',
up: function(client) {

},
Expand Down
12 changes: 6 additions & 6 deletions src/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ describe('Loader', function() {
it('Should resolve an OK migration', async function() {
const loader = new Loader(fixtures, 'ok1.js');
await expect(loader.load()).resolves.toEqual([
expect.objectContaining({title: 'OK1'})
expect.objectContaining({title: 'ok1'})
]);
});
it('Should sort migrations by title', async function() {
const loader = new Loader(fixtures, ['ok2.js', 'ok1.js']);
await expect(loader.load()).resolves.toEqual([
expect.objectContaining({title: 'OK1'}),
expect.objectContaining({title: 'OK2'})
expect.objectContaining({title: 'ok1'}),
expect.objectContaining({title: 'ok2'})
]);
});
it('Should require a unique name for each migration', async function() {
const loader = new Loader(fixtures, ['ok1.js', 'dup-ok1.js']);
await expect(loader.load()).rejects.toThrowError('Duplicate migration detected: OK1');
})
const loader = new Loader(fixtures, ['ok1.js', 'duplicate/ok1.js']);
await expect(loader.load()).rejects.toThrowError('Duplicate migration detected: ok1');
});
})
37 changes: 19 additions & 18 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Migration, MigrationSet} from './migration'
import glob from 'glob';
import {promisify} from "util";
import {join} from 'path'
import {join, basename} from 'path'
import Maybe = jest.Maybe;

const globp = promisify(glob);
Expand Down Expand Up @@ -31,29 +31,15 @@ export default class Loader {
throw new Error(`Unable to load migration from ${filename} - this does not look like a JS file.`);
}
const prospect = require(join(this.cwd, filename))
if(!this.isMigration(prospect)) {

if(!isMigrationStub(prospect)) {
throw new Error(`Unable to load migration from ${filename} - this file needs to export an object with a title, up, and down.`);
}
return prospect
return Object.assign({}, prospect, {title: basename(filename, '.js')})
}
private sort(a: Migration, b: Migration): number {
return a.title > b.title ? 1 : -1
}
private isMigration(prospect: Maybe<Migration>): prospect is Migration {
if(typeof prospect !== 'object' || prospect === null) {
return false
}
if(!('title' in prospect) || !prospect.title.length) {
return false
}
if(!('up' in prospect) || typeof prospect.up !== 'function') {
return false;
}
if(!('down' in prospect) || typeof prospect.down !== 'function') {
return false;
}
return true
}
private assertUnique(migrations: MigrationSet) {
const seen: {[k: string]: boolean} = {}
migrations.forEach(function(migration) {
Expand All @@ -66,3 +52,18 @@ export default class Loader {
})
}
}

type MigrationStub = Pick<Migration, 'up'|'down'>

function isMigrationStub(prospect: Maybe<MigrationStub>): prospect is MigrationStub {
if(typeof prospect !== 'object' || prospect === null) {
return false
}
if(!('up' in prospect) || typeof prospect.up !== 'function') {
return false;
}
if(!('down' in prospect) || typeof prospect.down !== 'function') {
return false;
}
return true
}
2 changes: 1 addition & 1 deletion src/migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Client as ElasticsearchClient} from "@elastic/elasticsearch";

export interface Migration {
export type Migration = {
title: string
up: (client: ElasticsearchClient) => Promise<void>
down: (client: ElasticsearchClient) => Promise<void>
Expand Down

0 comments on commit cec9803

Please sign in to comment.