Merge pull request #256 from johanneslumpe/0.6.0-alpha

Made migration tests pass
This commit is contained in:
Tim Griesser 2014-04-27 18:45:39 -04:00
commit 26835a9a4f
5 changed files with 35 additions and 30 deletions

View File

@ -107,6 +107,7 @@ Knex.initialize = function(config) {
_.each(['make', 'latest', 'rollback', 'currentVersion'], function(method) {
migrate[method] = function(config) {
if (!client.Migrator) client.initMigrator();
config.knex = knex;
var migrator = new client.Migrator(config);
return migrator[method].apply(migrator, arguments);
};

View File

@ -19,6 +19,8 @@ function Migrator(config) {
directory: process.cwd() + '/migrations'
});
this.knex = config.knex;
// Resolve to the correct directory when running globally.
if (this.config.directory.indexOf('./') === 0) {
this.config.directory = path.resolve(process.cwd(), this.config.directory);
@ -29,9 +31,9 @@ function Migrator(config) {
// dependent on the migration config settings.
Migrator.prototype.ensureTable = function(config) {
var migration = this;
return this.knex.schema.hasTable(config.tableName)
return this.knex.schema.hasTable(this.config.tableName)
.then(function(exists) {
if (!exists) return migration.createMigrationTable(config.tableName);
if (!exists) return migration.createMigrationTable(migration.config.tableName);
});
};
@ -56,9 +58,9 @@ Migrator.prototype.createMigrationTable = function(tableName) {
// Migrators to the latest configuration.
Migrator.prototype.latest = function(config) {
return this.init(config)
.then(this.migrationData)
return this.migrationData()
.tap(validateMigrationList)
.bind(this)
.spread(function(all, completed) {
return this.runBatch(_.difference(all, completed), 'up');
})
@ -67,9 +69,9 @@ Migrator.prototype.latest = function(config) {
// Rollback the last "batch" of migrations that were run.
Migrator.prototype.rollback = function(config) {
return this.init(config)
.then(this.migrationData)
return this.migrationData()
.tap(validateMigrationList)
.bind(this)
.then(this.getLastBatch)
.then(function(migrations) {
return this.runBatch(_.pluck(migrations, 'name'), 'down');
@ -102,32 +104,31 @@ Migrator.prototype.currentVersion = function(config) {
};
// Creates a new migration, with a given name.
Migrator.prototype.make = function(name, config) {
if (!name) Promise.rejected(new Error('A name must be specified for the generated migration'));
return this.ensureFolder()
// .then()
Migrator.prototype.make = function(config) {
if (!config.name) Promise.rejected(new Error('A name must be specified for the generated migration'));
return this.ensureFolder(config)
.bind(this)
.then(this.generateStubTemplate)
.then(this.writeNewMigration(name));
.then(this.writeNewMigration(config.name));
};
// Lists all available migration versions, as a sorted array.
Migrator.prototype.listAll = function(config) {
return this.init(config)
.then(function() {
return Promise.promisify(fs.readdir, fs)(this.config.directory);
})
.then(function(migrations) {
var ext = this.config.extension;
return _.filter(migrations, function (value) {
return value.indexOf(ext, value.length - ext.length) !== -1;
}).sort();
});
return Promise.promisify(fs.readdir, fs)(this.config.directory)
.bind(this)
.then(function(migrations) {
var ext = this.config.extension;
return _.filter(migrations, function (value) {
return value.indexOf(ext, value.length - ext.length) !== -1;
}).sort();
});
};
// Lists all migrations that have been completed for the current db, as an array.
Migrator.prototype.listCompleted = function(config) {
return this.init(config)
.then(function() {
return this.ensureTable(this.config.tableName)
.bind(this)
.then(function () {
return this.knex(this.config.tableName).orderBy('id').select('name');
})
.then(function(migrations) {
@ -195,10 +196,12 @@ Migrator.prototype.waterfallBatch = function(migrations, direction) {
var tableName = this.config.tableName;
var current = Promise.fulfilled().bind({failed: false, failedOn: 0});
var log = [];
var directory = this.config.directory;
return function(batchNo) {
_.each(migrations, function(migration, i) {
var name = migration[0];
migration = migration[1];
var name = migration;
migration = require(directory + '/' + name);
// We're going to run each of the migrations in the current "up"
current = current.then(function() {

View File

@ -44,7 +44,7 @@ module.exports = function(knex) {
return knex('datatype_test').columnInfo().testSql(function(tester) {
tester('mysql',
'select column_name, data_type, character_maximum_length from information_schema.columns where table_name = ? and table_schema = ?',
['datatype_test','db_test'], {
['datatype_test','knex_test'], {
"enum_value": {
"length": 1,
"type": "enum"
@ -55,7 +55,7 @@ module.exports = function(knex) {
}
});
tester('postgresql', 'select column_name, data_type, character_maximum_length from information_schema.columns where table_name = ? and table_catalog = ?',
['datatype_test','db_test'], {
['datatype_test','knex_test'], {
"enum_value": {
"length": null,
"type": "text"

View File

@ -6,7 +6,8 @@ var Promise = require('../../../lib/promise');
module.exports = function(knex) {
var migrationConfig = {
directory: path.join(__dirname, './migration/')
directory: path.join(__dirname, './migration/'),
name: 'test'
};
require('rimraf').sync(path.join(__dirname, './migration'));
@ -14,7 +15,7 @@ module.exports = function(knex) {
describe('knex.migrate', function () {
it('should create a new migration file with the create method', function() {
return knex.migrate.make('test', migrationConfig).then(function(name) {
return knex.migrate.make(migrationConfig).then(function(name) {
expect(name.split('_')[0]).to.have.length(14);
expect(name.split('_')[1].split('.')[0]).to.equal('test');
});

View File

@ -5,7 +5,7 @@ module.exports = function(knex) {
this.dialect = knex.client.dialect;
require('./schema')(knex);
// require('./migrate')(knex);
require('./migrate')(knex);
require('./builder/inserts')(knex);
require('./builder/selects')(knex);
require('./builder/unions')(knex);