mirror of
https://github.com/knex/knex.git
synced 2025-11-11 15:23:57 +00:00
Merge pull request #256 from johanneslumpe/0.6.0-alpha
Made migration tests pass
This commit is contained in:
commit
26835a9a4f
1
knex.js
1
knex.js
@ -107,6 +107,7 @@ Knex.initialize = function(config) {
|
|||||||
_.each(['make', 'latest', 'rollback', 'currentVersion'], function(method) {
|
_.each(['make', 'latest', 'rollback', 'currentVersion'], function(method) {
|
||||||
migrate[method] = function(config) {
|
migrate[method] = function(config) {
|
||||||
if (!client.Migrator) client.initMigrator();
|
if (!client.Migrator) client.initMigrator();
|
||||||
|
config.knex = knex;
|
||||||
var migrator = new client.Migrator(config);
|
var migrator = new client.Migrator(config);
|
||||||
return migrator[method].apply(migrator, arguments);
|
return migrator[method].apply(migrator, arguments);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -19,6 +19,8 @@ function Migrator(config) {
|
|||||||
directory: process.cwd() + '/migrations'
|
directory: process.cwd() + '/migrations'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.knex = config.knex;
|
||||||
|
|
||||||
// Resolve to the correct directory when running globally.
|
// Resolve to the correct directory when running globally.
|
||||||
if (this.config.directory.indexOf('./') === 0) {
|
if (this.config.directory.indexOf('./') === 0) {
|
||||||
this.config.directory = path.resolve(process.cwd(), this.config.directory);
|
this.config.directory = path.resolve(process.cwd(), this.config.directory);
|
||||||
@ -29,9 +31,9 @@ function Migrator(config) {
|
|||||||
// dependent on the migration config settings.
|
// dependent on the migration config settings.
|
||||||
Migrator.prototype.ensureTable = function(config) {
|
Migrator.prototype.ensureTable = function(config) {
|
||||||
var migration = this;
|
var migration = this;
|
||||||
return this.knex.schema.hasTable(config.tableName)
|
return this.knex.schema.hasTable(this.config.tableName)
|
||||||
.then(function(exists) {
|
.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.
|
// Migrators to the latest configuration.
|
||||||
Migrator.prototype.latest = function(config) {
|
Migrator.prototype.latest = function(config) {
|
||||||
return this.init(config)
|
return this.migrationData()
|
||||||
.then(this.migrationData)
|
|
||||||
.tap(validateMigrationList)
|
.tap(validateMigrationList)
|
||||||
|
.bind(this)
|
||||||
.spread(function(all, completed) {
|
.spread(function(all, completed) {
|
||||||
return this.runBatch(_.difference(all, completed), 'up');
|
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.
|
// Rollback the last "batch" of migrations that were run.
|
||||||
Migrator.prototype.rollback = function(config) {
|
Migrator.prototype.rollback = function(config) {
|
||||||
return this.init(config)
|
return this.migrationData()
|
||||||
.then(this.migrationData)
|
|
||||||
.tap(validateMigrationList)
|
.tap(validateMigrationList)
|
||||||
|
.bind(this)
|
||||||
.then(this.getLastBatch)
|
.then(this.getLastBatch)
|
||||||
.then(function(migrations) {
|
.then(function(migrations) {
|
||||||
return this.runBatch(_.pluck(migrations, 'name'), 'down');
|
return this.runBatch(_.pluck(migrations, 'name'), 'down');
|
||||||
@ -102,20 +104,18 @@ Migrator.prototype.currentVersion = function(config) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Creates a new migration, with a given name.
|
// Creates a new migration, with a given name.
|
||||||
Migrator.prototype.make = function(name, config) {
|
Migrator.prototype.make = function(config) {
|
||||||
if (!name) Promise.rejected(new Error('A name must be specified for the generated migration'));
|
if (!config.name) Promise.rejected(new Error('A name must be specified for the generated migration'));
|
||||||
return this.ensureFolder()
|
return this.ensureFolder(config)
|
||||||
// .then()
|
.bind(this)
|
||||||
.then(this.generateStubTemplate)
|
.then(this.generateStubTemplate)
|
||||||
.then(this.writeNewMigration(name));
|
.then(this.writeNewMigration(config.name));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Lists all available migration versions, as a sorted array.
|
// Lists all available migration versions, as a sorted array.
|
||||||
Migrator.prototype.listAll = function(config) {
|
Migrator.prototype.listAll = function(config) {
|
||||||
return this.init(config)
|
return Promise.promisify(fs.readdir, fs)(this.config.directory)
|
||||||
.then(function() {
|
.bind(this)
|
||||||
return Promise.promisify(fs.readdir, fs)(this.config.directory);
|
|
||||||
})
|
|
||||||
.then(function(migrations) {
|
.then(function(migrations) {
|
||||||
var ext = this.config.extension;
|
var ext = this.config.extension;
|
||||||
return _.filter(migrations, function (value) {
|
return _.filter(migrations, function (value) {
|
||||||
@ -126,8 +126,9 @@ Migrator.prototype.listAll = function(config) {
|
|||||||
|
|
||||||
// Lists all migrations that have been completed for the current db, as an array.
|
// Lists all migrations that have been completed for the current db, as an array.
|
||||||
Migrator.prototype.listCompleted = function(config) {
|
Migrator.prototype.listCompleted = function(config) {
|
||||||
return this.init(config)
|
return this.ensureTable(this.config.tableName)
|
||||||
.then(function() {
|
.bind(this)
|
||||||
|
.then(function () {
|
||||||
return this.knex(this.config.tableName).orderBy('id').select('name');
|
return this.knex(this.config.tableName).orderBy('id').select('name');
|
||||||
})
|
})
|
||||||
.then(function(migrations) {
|
.then(function(migrations) {
|
||||||
@ -195,10 +196,12 @@ Migrator.prototype.waterfallBatch = function(migrations, direction) {
|
|||||||
var tableName = this.config.tableName;
|
var tableName = this.config.tableName;
|
||||||
var current = Promise.fulfilled().bind({failed: false, failedOn: 0});
|
var current = Promise.fulfilled().bind({failed: false, failedOn: 0});
|
||||||
var log = [];
|
var log = [];
|
||||||
|
var directory = this.config.directory;
|
||||||
|
|
||||||
return function(batchNo) {
|
return function(batchNo) {
|
||||||
_.each(migrations, function(migration, i) {
|
_.each(migrations, function(migration, i) {
|
||||||
var name = migration[0];
|
var name = migration;
|
||||||
migration = migration[1];
|
migration = require(directory + '/' + name);
|
||||||
|
|
||||||
// We're going to run each of the migrations in the current "up"
|
// We're going to run each of the migrations in the current "up"
|
||||||
current = current.then(function() {
|
current = current.then(function() {
|
||||||
|
|||||||
@ -44,7 +44,7 @@ module.exports = function(knex) {
|
|||||||
return knex('datatype_test').columnInfo().testSql(function(tester) {
|
return knex('datatype_test').columnInfo().testSql(function(tester) {
|
||||||
tester('mysql',
|
tester('mysql',
|
||||||
'select column_name, data_type, character_maximum_length from information_schema.columns where table_name = ? and table_schema = ?',
|
'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": {
|
"enum_value": {
|
||||||
"length": 1,
|
"length": 1,
|
||||||
"type": "enum"
|
"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 = ?',
|
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": {
|
"enum_value": {
|
||||||
"length": null,
|
"length": null,
|
||||||
"type": "text"
|
"type": "text"
|
||||||
|
|||||||
@ -6,7 +6,8 @@ var Promise = require('../../../lib/promise');
|
|||||||
module.exports = function(knex) {
|
module.exports = function(knex) {
|
||||||
|
|
||||||
var migrationConfig = {
|
var migrationConfig = {
|
||||||
directory: path.join(__dirname, './migration/')
|
directory: path.join(__dirname, './migration/'),
|
||||||
|
name: 'test'
|
||||||
};
|
};
|
||||||
|
|
||||||
require('rimraf').sync(path.join(__dirname, './migration'));
|
require('rimraf').sync(path.join(__dirname, './migration'));
|
||||||
@ -14,7 +15,7 @@ module.exports = function(knex) {
|
|||||||
describe('knex.migrate', function () {
|
describe('knex.migrate', function () {
|
||||||
|
|
||||||
it('should create a new migration file with the create method', 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('_')[0]).to.have.length(14);
|
||||||
expect(name.split('_')[1].split('.')[0]).to.equal('test');
|
expect(name.split('_')[1].split('.')[0]).to.equal('test');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,7 +5,7 @@ module.exports = function(knex) {
|
|||||||
this.dialect = knex.client.dialect;
|
this.dialect = knex.client.dialect;
|
||||||
|
|
||||||
require('./schema')(knex);
|
require('./schema')(knex);
|
||||||
// require('./migrate')(knex);
|
require('./migrate')(knex);
|
||||||
require('./builder/inserts')(knex);
|
require('./builder/inserts')(knex);
|
||||||
require('./builder/selects')(knex);
|
require('./builder/selects')(knex);
|
||||||
require('./builder/unions')(knex);
|
require('./builder/unions')(knex);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user