229 lines
12 KiB
JavaScript
Raw Normal View History

var Promise = testPromise;
module.exports = function(knex) {
2013-09-12 13:30:47 -04:00
describe('Schema', function() {
2013-12-27 14:44:21 -05:00
describe('dropTable', function() {
it('has a dropTableIfExists method', function() {
return Promise.all([
knex.schema.dropTableIfExists('test_foreign_table_two').testSql(function(tester) {
tester(['sqlite3', 'postgresql'], ['drop table if exists "test_foreign_table_two"']);
tester('mysql', ['drop table if exists `test_foreign_table_two`']);
}),
2014-04-16 01:23:50 -04:00
knex.schema.dropTableIfExists('test_table_one')
.dropTableIfExists('catch_test')
.dropTableIfExists('test_table_two')
.dropTableIfExists('test_table_three')
.dropTableIfExists('datatype_test')
.dropTableIfExists('composite_key_test')
.dropTableIfExists('charset_collate_test')
.dropTableIfExists('accounts')
.dropTableIfExists('migration_test_1')
.dropTableIfExists('migration_test_2')
.dropTableIfExists('migration_test_2_1')
.dropTableIfExists('test_default_table')
.dropTableIfExists('knex_migrations')
2013-12-27 14:44:21 -05:00
]);
});
2013-09-12 13:30:47 -04:00
});
2013-09-12 13:30:47 -04:00
describe('createTable', function() {
it('is possible to chain .catch', function() {
2013-12-27 14:44:21 -05:00
return knex.schema
.createTable('catch_test', function(t) {
t.increments();
}).catch(function(e) {
throw e;
});
});
2014-04-16 02:50:19 -04:00
it('flag');
2013-09-12 13:30:47 -04:00
it('accepts the table name, and a "container" function', function() {
2013-12-27 14:44:21 -05:00
return knex.schema
.createTable('test_table_one', function(table) {
table.engine('InnoDB');
table.comment('A table comment.');
table.bigIncrements('id');
table.string('first_name').index();
table.string('last_name');
table.string('email').unique().nullable();
table.integer('logins').defaultTo(1).index().comment();
table.text('about').comment('A comment.');
table.timestamps();
}).testSql(function(tester) {
2014-04-16 01:23:50 -04:00
// tester('mysql', ['create table `test_table_one` (`id` bigint unsigned not null auto_increment primary key, `first_name` varchar(255), `last_name` varchar(255), `email` varchar(255) null, `logins` int default \'1\', `about` text comment \'A comment.\', `created_at` datetime, `updated_at` datetime) default character set utf8 engine = InnoDB comment = \'A table comment.\'','alter table `test_table_one` add index test_table_one_first_name_index(`first_name`)','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)']);
// tester('postgresql', ['create table "test_table_one" ("id" bigserial primary key, "first_name" varchar(255), "last_name" varchar(255), "email" varchar(255) null, "logins" integer default \'1\', "about" text, "created_at" timestamp, "updated_at" timestamp)',"comment on column \"test_table_one\".\"logins\" is NULL",'comment on column "test_table_one"."about" is \'A comment.\'','create index test_table_one_first_name_index on "test_table_one" ("first_name")','alter table "test_table_one" add constraint test_table_one_email_unique unique ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")','comment on table "test_table_one" is \'A table comment.\'']);
// tester('sqlite3', ['create table "test_table_one" ("id" integer not null primary key autoincrement, "first_name" varchar(255), "last_name" varchar(255), "email" varchar(255) null, "logins" integer default \'1\', "about" text, "created_at" datetime, "updated_at" datetime)','create index test_table_one_first_name_index on "test_table_one" ("first_name")','create unique index test_table_one_email_unique on "test_table_one" ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")']);
2013-12-27 14:44:21 -05:00
});
});
2013-09-12 13:30:47 -04:00
it('is possible to set the db engine with the table.engine', function() {
2013-12-27 14:44:21 -05:00
return knex.schema
.createTable('test_table_two', function(table) {
table.engine('InnoDB');
table.increments();
table.integer('account_id');
table.text('details');
table.tinyint('status');
}).testSql(function(tester){
tester('mysql', ['create table `test_table_two` (`id` int unsigned not null auto_increment primary key, `account_id` int, `details` text, `status` tinyint) default character set utf8 engine = InnoDB']);
});
});
2013-09-12 13:30:47 -04:00
it('sets default values with defaultTo', function() {
2013-12-27 14:44:21 -05:00
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.');
}).testSql(function(tester) {
tester('mysql', ['create table `test_table_three` (`main` int, `paragraph` text) default character set utf8 engine = InnoDB','alter table `test_table_three` add primary key test_table_three_main_primary(`main`)']);
tester('postgresql', ['create table "test_table_three" ("main" integer, "paragraph" text default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add primary key ("main")']);
tester('sqlite3', ['create table "test_table_three" ("main" integer, "paragraph" text default \'Lorem ipsum Qui quis qui in.\', primary key ("main"))']);
});
});
2013-09-12 13:30:47 -04:00
it('supports the enum and uuid columns', function() {
2013-12-27 14:44:21 -05:00
return knex.schema
.createTable('datatype_test', function(table) {
table.enum('enum_value', ['a', 'b', 'c']);
table.uuid('uuid').notNull();
2013-12-27 14:44:21 -05:00
}).testSql(function(tester) {
tester('mysql', ['create table `datatype_test` (`enum_value` enum(\'a\', \'b\', \'c\'), `uuid` char(36) not null) default character set utf8']);
tester('postgresql', ['create table "datatype_test" ("enum_value" text check (enum_value in (\'a\', \'b\', \'c\')), "uuid" uuid not null)']);
tester('sqlite3', ['create table "datatype_test" ("enum_value" varchar, "uuid" char(36) not null)']);
2013-12-27 14:44:21 -05: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');
2013-12-27 14:44:21 -05:00
}).testSql(function(tester) {
tester('mysql', ['create table `test_foreign_table_two` (`id` int unsigned not null auto_increment primary key, `fkey_two` int unsigned) default character set utf8','alter table `test_foreign_table_two` add constraint test_foreign_table_two_fkey_two_foreign foreign key (`fkey_two`) references `test_table_two` (`id`)']);
tester('postgresql', ['create table "test_foreign_table_two" ("id" serial primary key, "fkey_two" integer)','alter table "test_foreign_table_two" add constraint test_foreign_table_two_fkey_two_foreign foreign key ("fkey_two") references "test_table_two" ("id")']);
tester('sqlite3', ['create table "test_foreign_table_two" ("id" integer not null primary key autoincrement, "fkey_two" integer, foreign key("fkey_two") references "test_table_two"("id"))']);
});
});
2013-10-10 11:21:32 -04:00
it('allows for composite keys', function() {
2013-12-27 14:44:21 -05:00
return knex.schema
.createTable('composite_key_test', function(table) {
table.integer('column_a');
table.integer('column_b');
2014-04-16 02:50:19 -04:00
table.text('details');
table.tinyint('status');
2013-12-27 14:44:21 -05:00
table.unique(['column_a', 'column_b']);
}).testSql(function(tester) {
2014-04-16 02:50:19 -04:00
tester('mysql', ['create table `composite_key_test` (`column_a` int, `column_b` int, `details` text, `status` tinyint) default character set utf8','alter table `composite_key_test` add unique composite_key_test_column_a_column_b_unique(`column_a`, `column_b`)']);
tester('postgresql', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" smallint)','alter table "composite_key_test" add constraint composite_key_test_column_a_column_b_unique unique ("column_a", "column_b")']);
tester('sqlite3', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" tinyint)','create unique index composite_key_test_column_a_column_b_unique on "composite_key_test" ("column_a", "column_b")']);
}).then(function() {
return knex('composite_key_test').insert([{
column_a: 1,
column_b: 1,
details: 'One, One, One',
status: 1
}, {
column_a: 1,
column_b: 2,
details: 'One, Two, Zero',
status: 0
}, {
column_a: 1,
column_b: 3,
details: 'One, Three, Zero',
status: 0
}]);
2013-12-27 14:44:21 -05:00
});
2013-10-10 11:21:32 -04:00
});
it('is possible to set the table collation with table.charset and table.collate', function() {
2013-12-27 14:44:21 -05:00
return knex.schema
.createTable('charset_collate_test', function(table) {
table.charset('latin1');
table.collate('latin1_general_ci');
table.engine('InnoDB');
table.increments();
table.integer('account_id');
table.text('details');
table.tinyint('status');
}).testSql(function(tester) {
tester('mysql', ['create table `charset_collate_test` (`id` int unsigned not null auto_increment primary key, `account_id` int, `details` text, `status` tinyint) default character set latin1 collate latin1_general_ci engine = InnoDB']);
tester('postgresql', ['create table "charset_collate_test" ("id" serial primary key, "account_id" integer, "details" text, "status" smallint)']);
tester('sqlite3', ['create table "charset_collate_test" ("id" integer not null primary key autoincrement, "account_id" integer, "details" text, "status" tinyint)']);
});
});
2013-09-12 13:30:47 -04:00
});
2013-09-12 13:30:47 -04:00
describe('table', function() {
it('allows adding a field', function () {
2013-09-12 13:30:47 -04:00
return knex.schema.table('test_table_two', function(t) {
t.json('json_data').nullable();
2013-09-13 18:11:50 -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-12 13:30:47 -04:00
});
2013-09-12 13:30:47 -04:00
describe('hasTable', function() {
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-12-27 14:44:21 -05:00
expect(resp).to.equal(true);
2013-09-12 13:30:47 -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').then(function(resp) {
2013-12-27 14:44:21 -05:00
expect(resp).to.equal(false);
2013-09-23 23:07:38 -04:00
});
});
});
2013-09-12 13:30:47 -04:00
describe('renameTable', function() {
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-12 13:30:47 -04:00
describe('dropTable', function() {
it('should drop a table', function() {
return knex.schema.dropTable('test_table_three').then(function() {
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-12 13:30:47 -04:00
describe('hasColumn', function() {
it('checks whether a column exists, resolving with a boolean', function() {
return knex.schema.hasColumn('accounts', 'first_name').then(function(exists) {
expect(exists).to.be.true;
});
2013-09-12 13:30:47 -04:00
});
});
});
2014-04-16 02:50:19 -04:00
};