Skip to content

Commit

Permalink
Migrated to support latest updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Bautista committed Sep 11, 2017
1 parent a73b933 commit 55f8b65
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 113 deletions.
57 changes: 36 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = class Umzug extends EventEmitter {
params: [],
path: path.resolve(process.cwd(), 'migrations'),
pattern: /^\d+[\w-]+\.js$/,
traverseDirectories: false,
wrap: fun => fun,
...this.options.migrations,
};
Expand Down Expand Up @@ -436,33 +437,47 @@ module.exports = class Umzug extends EventEmitter {
* @returns {Promise.<Migration[]>}
* @private
*/
_findMigrations () {
_findMigrations (migrationPath) {
let isRoot = !migrationPath;
if (isRoot) {
migrationPath = this.options.migrations.path;
}
return Bluebird
.promisify(fs.readdir)(this.options.migrations.path)
.promisify(fs.readdir)(migrationPath)
.bind(this)
.filter(function (file) {
if (!this.options.migrations.pattern.test(file)) {
this.log('File: ' + file + ' does not match pattern: ' + this.options.migrations.pattern);
return false;
}
return true;
})
.map(function (file) {
return path.resolve(this.options.migrations.path, file);
let filePath = path.resolve(migrationPath, file);
if (this.options.migrations.traverseDirectories) {
if (fs.lstatSync(filePath).isDirectory()) {
return this._findMigrations(filePath)
.then(function (migrations) {
return migrations;
});
}
}
if (this.options.migrations.pattern.test(file)) {
return new Migration(filePath, this.options);
}
this.log('File: ' + file + ' does not match pattern: ' + this.options.migrations.pattern);
return file;
})
.map(function (path) {
return new Migration(path, this.options);
.reduce(function (a, b) { return a.concat(b); }, []) // flatten the result to an array
.filter(function (file) {
return file instanceof Migration; // only care about Migration
})
.then(function (migrations) {
return migrations.sort(function (a, b) {
if (a.file > b.file) {
return 1;
} else if (a.file < b.file) {
return -1;
} else {
return 0;
}
});
if (isRoot) { // only sort if its root
return migrations.sort(function (a, b) {
if (a.file > b.file) {
return 1;
} else if (a.file < b.file) {
return -1;
} else {
return 0;
}
});
}
return migrations;
});
}

Expand Down
79 changes: 53 additions & 26 deletions test/Umzug/down.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { expect } from 'chai';
import {expect} from 'chai';
import helper from '../helper';
import Umzug from '../../src/index';
import {join} from 'path';

describe('down', function () {
beforeEach(function () {
helper.clearTmp();
return helper
.prepareMigrations(3)
.then((migrationNames) => {
this.migrationNames = migrationNames;
this.umzug = new Umzug({
migrations: { path: join(__dirname, '/../tmp/') },
storageOptions: { path: join(__dirname, '/../tmp/umzug.json') },
});
});
});

let downTestSuite = function downTestSuite () {
describe('when no migrations has been executed yet', function () {
beforeEach(function () {
return this.umzug.down().then((migrations) => {
Expand All @@ -36,7 +23,7 @@ describe('down', function () {
describe('when a migration has been executed already', function () {
beforeEach(function () {
return this.umzug.execute({
migrations: [ this.migrationNames[0] ],
migrations: [this.migrationNames[0]],
method: 'up',
}).then(() => {
return this.umzug.executed();
Expand Down Expand Up @@ -155,7 +142,7 @@ describe('down', function () {

describe('that does not match a migration', function () {
it('rejects the promise', function () {
return this.umzug.down({ to: '123-asdasd' }).then(() => {
return this.umzug.down({to: '123-asdasd'}).then(() => {
return Promise.reject(new Error('We should not end up here...'));
}, (err) => {
expect(err.message).to.equal('Unable to find migration: 123-asdasd');
Expand All @@ -166,9 +153,9 @@ describe('down', function () {
describe('that does not match an executed migration', function () {
it('rejects the promise', function () {
return this.umzug
.execute({ migrations: this.migrationNames, method: 'down' })
.execute({migrations: this.migrationNames, method: 'down'})
.then(() => {
return this.umzug.down({ to: this.migrationNames[1] });
return this.umzug.down({to: this.migrationNames[1]});
})
.then(() => {
return Promise.reject(new Error('We should not end up here...'));
Expand All @@ -191,7 +178,9 @@ describe('down', function () {
describe('that matches an executed migration', function () {
beforeEach(function () {
return this.umzug.down(this.migrationNames[1])
.then((migrations) => { this.migrations = migrations; });
.then((migrations) => {
this.migrations = migrations;
});
});

it('returns only 1 migrations', function () {
Expand Down Expand Up @@ -220,7 +209,7 @@ describe('down', function () {
describe('that does not match an executed migration', function () {
it('rejects the promise', function () {
return this.umzug
.execute({ migrations: this.migrationNames, method: 'down' })
.execute({migrations: this.migrationNames, method: 'down'})
.then(() => {
return this.umzug.down(this.migrationNames[1]);
})
Expand All @@ -244,7 +233,9 @@ describe('down', function () {
describe('that matches an executed migration', function () {
beforeEach(function () {
return this.umzug.down([this.migrationNames[1]])
.then((migrations) => { this.migrations = migrations; });
.then((migrations) => {
this.migrations = migrations;
});
});

it('returns only 1 migrations', function () {
Expand All @@ -263,7 +254,9 @@ describe('down', function () {
describe('that matches multiple pending migration', function () {
beforeEach(function () {
return this.umzug.down(this.migrationNames.slice(1))
.then((migrations) => { this.migrations = migrations; });
.then((migrations) => {
this.migrations = migrations;
});
});

it('returns only 2 migrations', function () {
Expand Down Expand Up @@ -291,7 +284,7 @@ describe('down', function () {
describe('that does not match an executed migration', function () {
it('rejects the promise', function () {
return this.umzug
.execute({ migrations: this.migrationNames, method: 'down' })
.execute({migrations: this.migrationNames, method: 'down'})
.then(() => {
return this.umzug.down([this.migrationNames[1]]);
})
Expand All @@ -306,7 +299,7 @@ describe('down', function () {
describe('that does partially not match an executed migration', function () {
it('rejects the promise', function () {
return this.umzug
.execute({ migrations: this.migrationNames.slice(0, 2), method: 'down' })
.execute({migrations: this.migrationNames.slice(0, 2), method: 'down'})
.then(() => {
return this.umzug.down(this.migrationNames.slice(1));
})
Expand All @@ -323,7 +316,7 @@ describe('down', function () {
beforeEach(function () {
// a migration has been executed already...
return this.umzug.execute({
migrations: [ this.migrationNames[0] ],
migrations: [this.migrationNames[0]],
method: 'up',
}).then(() => {
return this.umzug.executed();
Expand All @@ -350,4 +343,38 @@ describe('down', function () {
});
});
});
};

describe('down', function () {
beforeEach(function () {
helper.clearTmp();
return helper
.prepareMigrations(3)
.then((migrationNames) => {
this.migrationNames = migrationNames;
this.umzug = new Umzug({
migrations: {path: join(__dirname, '/../tmp/')},
storageOptions: {path: join(__dirname, '/../tmp/umzug.json')},
});
});
});

downTestSuite();
});

describe('down-directories', function () {
beforeEach(function () {
helper.clearTmp();
return helper
.prepareMigrations(3, {directories: [['1', '2'], ['1', '2'], ['1', '3', '4', '5']]})
.then((migrationNames) => {
this.migrationNames = migrationNames;
this.umzug = new Umzug({
migrations: {path: join(__dirname, '/../tmp/'), traverseDirectories: true},
storageOptions: {path: join(__dirname, '/../tmp/umzug.json')},
});
});
});

downTestSuite();
});
55 changes: 38 additions & 17 deletions test/Umzug/executed.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { expect } from 'chai';
import {expect} from 'chai';
import helper from '../helper';
import Umzug from '../../src/index';
import {join} from 'path';

describe('executed', function () {
beforeEach(function () {
helper.clearTmp();
return helper
.prepareMigrations(3)
.then((migrationNames) => {
this.migrationNames = migrationNames;
this.umzug = new Umzug({
migrations: { path: join(__dirname, '/../tmp/') },
storageOptions: {path: join(__dirname, '/../tmp/umzug.json')},
});
});
});

let executedTestSuite = function executedTestSuite () {
describe('when no migrations has been executed yet', function () {
beforeEach(function () {
return this.umzug.executed()
Expand All @@ -37,7 +24,7 @@ describe('executed', function () {
describe('when one migration has been executed yet', function () {
beforeEach(function () {
return this.umzug.execute({
migrations: [ this.migrationNames[0] ],
migrations: [this.migrationNames[0]],
method: 'up',
}).then(() => {
return this.umzug.executed();
Expand Down Expand Up @@ -84,7 +71,7 @@ describe('executed', function () {
beforeEach(function () {
// migration has been executed already
return this.umzug.execute({
migrations: [ this.migrationNames[0] ],
migrations: [this.migrationNames[0]],
method: 'up',
}).then(() => {
this.umzug.storage = helper.wrapStorageAsCustomThenable(this.umzug.storage);
Expand All @@ -103,4 +90,38 @@ describe('executed', function () {
expect(this.migrations[0].file).to.equal(this.migrationNames[0] + '.js');
});
});
};

describe('executed', function () {
beforeEach(function () {
helper.clearTmp();
return helper
.prepareMigrations(3)
.then((migrationNames) => {
this.migrationNames = migrationNames;
this.umzug = new Umzug({
migrations: {path: join(__dirname, '/../tmp/')},
storageOptions: {path: join(__dirname, '/../tmp/umzug.json')},
});
});
});

executedTestSuite();
});

describe('executed-directories', function () {
beforeEach(function () {
helper.clearTmp();
return helper
.prepareMigrations(3, {directories: [['1', '2'], ['1', '2'], ['1', '3', '4', '5']]})
.then((migrationNames) => {
this.migrationNames = migrationNames;
this.umzug = new Umzug({
migrations: {path: join(__dirname, '/../tmp/'), traverseDirectories: true},
storageOptions: {path: join(__dirname, '/../tmp/umzug.json')},
});
});
});

executedTestSuite();
});
Loading

0 comments on commit 55f8b65

Please sign in to comment.