124 lines
4.5 KiB
JavaScript
Raw Normal View History

/*global after, before, describe, expect, it*/
'use strict';
var equal = require('assert').equal;
var path = require('path');
var rimraf = require('rimraf');
2013-12-27 14:44:21 -05:00
var Promise = require('../../../lib/promise');
2013-10-24 21:54:35 -04:00
module.exports = function(knex) {
require('rimraf').sync(path.join(__dirname, './migration'));
describe('knex.migrate', function () {
it('should create a new migration file with the create method', function() {
return knex.migrate.make('test').then(function(name) {
name = path.basename(name);
2013-10-24 21:54:35 -04:00
expect(name.split('_')[0]).to.have.length(14);
expect(name.split('_')[1].split('.')[0]).to.equal('test');
2013-10-24 21:54:35 -04:00
});
});
it('should list the current migration state with the currentVersion method', function() {
return knex.migrate.currentVersion().then(function(version) {
2013-10-24 21:54:35 -04:00
equal(version, 'none');
});
});
var tables = ['migration_test_1', 'migration_test_2', 'migration_test_2_1'];
describe('knex.migrate.latest', function() {
before(function() {
2015-05-20 11:08:27 -04:00
return knex.migrate.latest({directory: 'test/integration/migrate/test'}).catch(function() {});
});
it('should run all migration files in the specified directory', function() {
return knex('knex_migrations').select('*').then(function(data) {
expect(data.length).to.equal(2);
});
});
it('should run the migrations from oldest to newest', function() {
2014-08-11 12:25:39 +02:00
if (knex.client.dialect === 'oracle') {
return knex('knex_migrations').orderBy('migration_time', 'asc').select('*').then(function(data) {
expect(path.basename(data[0].name)).to.equal('20131019235242_migration_1.js');
expect(path.basename(data[1].name)).to.equal('20131019235306_migration_2.js');
});
} else {
return knex('knex_migrations').orderBy('id', 'asc').select('*').then(function(data) {
expect(path.basename(data[0].name)).to.equal('20131019235242_migration_1.js');
expect(path.basename(data[1].name)).to.equal('20131019235306_migration_2.js');
});
}
});
it('should create all specified tables and columns', function() {
// Map the table names to promises that evaluate chai expectations to
// confirm that the table exists and the 'id' and 'name' columns exist
// within the table
return Promise.map(tables, function(table) {
return knex.schema.hasTable(table).then(function(exists) {
2014-09-02 22:03:14 +02:00
expect(exists).to.equal(true);
if (exists) {
return Promise.all([
knex.schema.hasColumn(table, 'id').then(function(exists) {
2014-09-02 22:03:14 +02:00
expect(exists).to.equal(true);
}),
knex.schema.hasColumn(table, 'name').then(function(exists) {
2014-09-02 22:03:14 +02:00
expect(exists).to.equal(true);
})
]);
}
});
});
});
2015-01-09 14:57:37 +01:00
it('should not create column for invalid migration', function() {
knex.schema.hasColumn('migration_test_1', 'transaction').then(function(exists) {
// MySQL / Oracle commit transactions implicit for most common
2015-01-09 14:57:37 +01:00
// migration statements (e.g. CREATE TABLE, ALTER TABLE, DROP TABLE),
// so we need to check for dialect
2015-04-29 15:14:41 -04:00
if (knex.client.dialect === 'mysql' || knex.client.dialect === 'mariadb' || knex.client.dialect === 'oracle') {
2015-01-09 14:57:37 +01:00
expect(exists).to.equal(true);
} else {
expect(exists).to.equal(false);
}
});
});
it('should not proceed after invalid migration', function() {
return knex.schema.hasTable('should_not_be_run').then(function(exists) {
expect(exists).to.equal(false);
});
});
});
describe('knex.migrate.rollback', function() {
it('should delete the most recent batch from the migration log', function() {
return knex.migrate.rollback({directory: 'test/integration/migrate/test'}).then(function() {
return knex('knex_migrations').select('*').then(function(data) {
expect(data.length).to.equal(0);
});
});
});
it('should drop tables as specified in the batch', function() {
return Promise.map(tables, function(table) {
return knex.schema.hasTable(table).then(function(exists) {
2014-09-02 22:03:14 +02:00
expect(!!exists).to.equal(false);
});
});
});
});
2013-10-24 21:54:35 -04:00
after(function() {
rimraf.sync(path.join(__dirname, './migration'));
});
});
};