knex/test/unit/schema/mysql.js

465 lines
16 KiB
JavaScript
Raw Normal View History

2013-12-27 14:44:21 -05:00
module.exports = function(client) {
var knex = require('../../../knex');
client.initSchema();
describe("MySQL SchemaBuilder", function() {
client.initSchema();
var tableSql;
var SchemaBuilder = client.SchemaBuilder;
var equal = require('assert').equal;
it('test basic create table with charset and collate', function() {
tableSql = new SchemaBuilder().createTable('users', function(table) {
table.increments('id');
table.string('email');
table.charset('utf8');
table.collate('utf8_unicode_ci');
});
equal(1, tableSql.toSQL().length);
expect(tableSql.toSQL()[0].sql).to.equal('create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255)) default character set utf8 collate utf8_unicode_ci');
expect(tableSql.toQuery()).to.equal('create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255)) default character set utf8 collate utf8_unicode_ci');
2013-12-27 14:44:21 -05:00
});
it('basic create table without charset or collate', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.increments('id');
this.string('email');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `id` int unsigned not null auto_increment primary key, add `email` varchar(255)');
});
it('test drop table', function() {
tableSql = new SchemaBuilder().dropTable('users').toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('drop table `users`');
});
it('test drop table if exists', function() {
tableSql = new SchemaBuilder().dropTableIfExists('users').toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('drop table if exists `users`');
});
it('test drop column', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropColumn('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop `foo`');
});
it('drops multiple columns with an array', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropColumn(['foo', 'bar']);
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop `foo`, drop `bar`');
});
it('drops multiple columns as multiple arguments', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropColumn('foo', 'bar');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop `foo`, drop `bar`');
});
it('test drop primary', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropPrimary();
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop primary key');
});
it('test drop unique', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropUnique('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop index users_foo_unique');
});
it('test drop unique, custom', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropUnique(null, 'foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop index foo');
});
it('test drop index', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropIndex('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop index users_foo_index');
});
it('test drop index, custom', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropIndex(null, 'foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop index foo');
});
it('test drop foreign', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropForeign('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop foreign key users_foo_foreign');
});
it('test drop foreign, custom', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropForeign(null, 'foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop foreign key foo');
});
it('test drop timestamps', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dropTimestamps();
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` drop `created_at`, drop `updated_at`');
});
it('test rename table', function() {
tableSql = new SchemaBuilder().renameTable('users', 'foo').toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('rename table `users` to `foo`');
});
it('test adding primary key', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.primary('foo', 'bar');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add primary key bar(`foo`)');
});
it('test adding unique key', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.unique('foo', 'bar');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add unique bar(`foo`)');
});
it('test adding index', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.index(['foo', 'bar'], 'baz');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add index baz(`foo`, `bar`)');
});
it('test adding foreign key', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.foreign('foo_id').references('id').on('orders');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add constraint users_foo_id_foreign foreign key (`foo_id`) references `orders` (`id`)');
});
it("adds foreign key with onUpdate and onDelete", function() {
tableSql = new SchemaBuilder().createTable('person', function(table) {
2014-06-12 14:07:33 -04:00
table.integer('user_id').notNull().references('users.id').onDelete('SET NULL');
table.integer('account_id').notNull().references('id').inTable('accounts').onUpdate('cascade');
}).toSQL();
equal(3, tableSql.length);
expect(tableSql[1].sql).to.equal('alter table `person` add constraint person_user_id_foreign foreign key (`user_id`) references `users` (`id`) on delete SET NULL');
expect(tableSql[2].sql).to.equal('alter table `person` add constraint person_account_id_foreign foreign key (`account_id`) references `accounts` (`id`) on update cascade');
});
2013-12-27 14:44:21 -05:00
it('test adding incrementing id', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.increments('id');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `id` int unsigned not null auto_increment primary key');
});
it('test adding big incrementing id', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.bigIncrements('id');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `id` bigint unsigned not null auto_increment primary key');
});
it('test adding column after another column', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('name').after('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `name` varchar(255) after `foo`');
});
2014-07-30 10:54:29 +02:00
it('test adding column on the first place', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('first_name').first();
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `first_name` varchar(255) first');
});
2013-12-27 14:44:21 -05:00
it('test adding string', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` varchar(255)');
});
it('uses the varchar column constraint', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('foo', 100);
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` varchar(100)');
});
it('chains notNull and defaultTo', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('foo', 100).notNull().defaultTo('bar');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` varchar(100) not null default \'bar\'');
2013-12-27 14:44:21 -05:00
});
it('allows for raw values in the default field', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('foo', 100).nullable().defaultTo(knex.raw('CURRENT TIMESTAMP'));
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` varchar(100) null default CURRENT TIMESTAMP');
});
it('test adding text', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.text('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` text');
});
it('test adding big integer', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.bigInteger('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` bigint');
});
it('test adding integer', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.integer('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` int');
});
it('test adding medium integer', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.mediumint('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` mediumint');
});
it('test adding small integer', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.smallint('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` smallint');
});
it('test adding tiny integer', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.tinyint('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` tinyint');
});
it('test adding float', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.float('foo', 5, 2);
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` float(5, 2)');
});
it('test adding double', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.double('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` double');
});
it('test adding double specifying precision', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.double('foo', 15, 8);
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` double(15, 8)');
});
it('test adding decimal', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.decimal('foo', 5, 2);
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` decimal(5, 2)');
});
it('test adding boolean', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.boolean('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` boolean');
});
it('test adding enum', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.enum('foo', ['bar', 'baz']);
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` enum(\'bar\', \'baz\')');
});
it('test adding date', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.date('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` date');
});
it('test adding date time', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.dateTime('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` datetime');
});
it('test adding time', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.time('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` time');
});
it('test adding time stamp', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.timestamp('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` timestamp');
});
it('test adding time stamps', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.timestamps();
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `created_at` datetime, add `updated_at` datetime');
});
it('test adding binary', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.binary('foo');
}).toSQL();
2013-12-27 14:44:21 -05:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` blob');
});
2014-06-09 21:28:49 -04:00
it('test adding decimal', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.decimal('foo', 2, 6);
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` decimal(2, 6)');
});
2013-12-27 14:44:21 -05:00
it('is possible to set raw statements in defaultTo, #146', function() {
tableSql = new SchemaBuilder().createTable('default_raw_test', function(t) {
t.timestamp('created_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('create table `default_raw_test` (`created_at` timestamp default CURRENT_TIMESTAMP)');
});
it('allows dropping a unique compound index', function() {
tableSql = new SchemaBuilder().table('composite_key_test', function(t) {
t.dropUnique(['column_a', 'column_b']);
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `composite_key_test` drop index composite_key_test_column_a_column_b_unique');
});
});
2013-12-27 14:44:21 -05:00
};