2013-09-08 15:57:32 -04:00
|
|
|
var when = require('when');
|
|
|
|
|
|
|
|
module.exports = function(knex) {
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('Schema', function() {
|
|
|
|
|
|
|
|
it('has a dropTableIfExists method', function() {
|
|
|
|
return when.all([
|
|
|
|
knex.schema.dropTableIfExists('test_foreign_table_two').logMe('sql'),
|
|
|
|
knex.schema.dropTableIfExists('test_table_one').logMe('sql'),
|
|
|
|
knex.schema.dropTableIfExists('test_table_two'),
|
|
|
|
knex.schema.dropTableIfExists('test_table_three'),
|
|
|
|
knex.schema.dropTableIfExists('datatype_test'),
|
|
|
|
knex.schema.dropTableIfExists('accounts'),
|
|
|
|
knex.schema.dropTableIfExists('test_default_table')
|
|
|
|
]);
|
|
|
|
});
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('createTable', function() {
|
|
|
|
|
|
|
|
it('accepts the table name, and a "container" function', function() {
|
|
|
|
return knex.schema.createTable('test_table_one', function(table) {
|
|
|
|
table.engine('InnoDB');
|
|
|
|
table.comment('A table comment.');
|
|
|
|
table.bigIncrements('id');
|
|
|
|
table.string('first_name');
|
|
|
|
table.string('last_name');
|
|
|
|
table.string('email').unique().nullable();
|
|
|
|
table.integer('logins').defaultTo(1).index().comment();
|
|
|
|
table.text('about').comment('A comment.');
|
|
|
|
table.timestamps();
|
|
|
|
}).logMe('sql');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('is possible to set the db engine with the table.engine', function() {
|
|
|
|
return knex.schema.createTable('test_table_two', function(table) {
|
|
|
|
table.engine('InnoDB');
|
|
|
|
table.increments();
|
|
|
|
table.integer('account_id');
|
|
|
|
table.text('details');
|
|
|
|
table.tinyint('status');
|
|
|
|
}).logMe('sql');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('sets default values with defaultTo', function() {
|
|
|
|
return knex.schema.createTable('test_table_three', function(table) {
|
|
|
|
table.engine('InnoDB');
|
|
|
|
table.integer('main').primary();
|
|
|
|
table.text('paragraph').defaultTo('Lorem ipsum Qui quis qui in.');
|
|
|
|
}).logMe('sql');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('supports the enum and uuid columns', function() {
|
|
|
|
return knex.schema.createTable('datatype_test', function(table) {
|
|
|
|
table.enum('enum_value', ['a', 'b', 'c']);
|
|
|
|
table.uuid('uuid');
|
|
|
|
}).logMe('sql');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('allows for setting foreign keys on schema creation', function() {
|
|
|
|
return knex.schema.createTable('test_foreign_table_two', function(table) {
|
|
|
|
table.increments();
|
|
|
|
table.integer('fkey_two')
|
|
|
|
.unsigned()
|
|
|
|
.references('id')
|
|
|
|
.inTable('test_table_two');
|
|
|
|
}).logMe('sql');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('table', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('allows adding a field', function (done) {
|
|
|
|
return knex.schema.table('test_table_two', function(t) {
|
|
|
|
t.json('json_data').nullable();
|
2013-09-13 18:11:50 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('allows changing a field', function() {
|
|
|
|
return knex.schema.table('test_table_one', function(t) {
|
|
|
|
t.string('phone').nullable();
|
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('hasTable', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('checks whether a table exists', function() {
|
2013-09-13 18:11:50 -04:00
|
|
|
return knex.schema.hasTable('test_table_two').then(function(resp) {
|
2013-09-12 13:30:47 -04:00
|
|
|
expect(resp).to.be.true;
|
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
|
2013-09-23 23:07:38 -04:00
|
|
|
it('should be false if a table does not exists', function() {
|
|
|
|
return knex.schema.hasTable('this_table_is_fake').debug().then(function(resp) {
|
|
|
|
expect(resp).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('renameTable', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('renames the table from one to another', function () {
|
|
|
|
return knex.schema.renameTable('test_table_one', 'accounts');
|
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('dropTable', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('should drop a table', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
return knex.schema.dropTable('test_table_three').then(function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
// Drop this here so we don't have foreign key constraints...
|
|
|
|
return knex.schema.dropTable('test_foreign_table_two');
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('hasColumn', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
it('checks whether a column exists, resolving with a boolean', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
return knex.schema.hasColumn('accounts', 'first_name');
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
2013-09-08 15:57:32 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
};
|