knex/test/unit/schema/mssql.js

551 lines
19 KiB
JavaScript
Raw Normal View History

2015-12-08 11:37:31 -06:00
/*global describe, expect, it*/
'use strict';
var MSSQL_Client = require('../../../lib/dialects/mssql');
var client = new MSSQL_Client();
describe("MSSQL SchemaBuilder", function() {
var tableSql;
var equal = require('assert').equal;
it('test basic create table with charset and collate', function() {
tableSql = client.schemaBuilder().createTable('users', function(table) {
table.increments('id');
table.string('email');
table.charset('utf8');
table.collate('utf8_unicode_ci');
});
equal(1, tableSql.toSQL().length);
2015-12-14 09:56:53 -06:00
expect(tableSql.toSQL()[0].sql).to.equal('CREATE TABLE [users] ([id] int identity(1,1) not null primary key, [email] nvarchar(255))');
expect(tableSql.toQuery()).to.equal('CREATE TABLE [users] ([id] int identity(1,1) not null primary key, [email] nvarchar(255))');
2015-12-08 11:37:31 -06:00
});
it('basic create table without charset or collate', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.increments('id');
this.string('email');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [id] int identity(1,1) not null primary key, [email] nvarchar(255)');
2015-12-08 11:37:31 -06:00
});
it('test drop table', function() {
tableSql = client.schemaBuilder().dropTable('users').toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('DROP TABLE [users]');
2015-12-08 11:37:31 -06:00
});
it('test drop table if exists', function() {
tableSql = client.schemaBuilder().dropTableIfExists('users').toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal("if object_id('[users]', 'U') is not null DROP TABLE [users]");
2015-12-08 11:37:31 -06:00
});
it('test drop column', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropColumn('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP COLUMN [foo]');
2015-12-08 11:37:31 -06:00
});
it('drops multiple columns with an array', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropColumn(['foo', 'bar']);
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP COLUMN [foo], [bar]');
2015-12-08 11:37:31 -06:00
});
it('drops multiple columns as multiple arguments', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropColumn('foo', 'bar');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP COLUMN [foo], [bar]');
2015-12-08 11:37:31 -06:00
});
it('test drop primary', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropPrimary('testconstraintname');
2015-12-08 11:37:31 -06:00
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT [testconstraintname]');
2015-12-08 11:37:31 -06:00
});
it('test drop unique', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropUnique('foo');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT [users_foo_unique]');
2015-12-08 11:37:31 -06:00
});
it('should alter columns with the alter flag', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.string('foo').alter();
this.string('bar');
}).toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [bar] nvarchar(255)');
expect(tableSql[1].sql).to.equal('ALTER TABLE [users] alter column [foo] nvarchar(255)');
});
2015-12-08 11:37:31 -06:00
it('test drop unique, custom', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropUnique(null, 'foo');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT [foo]');
2015-12-08 11:37:31 -06:00
});
it('test drop index', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropIndex('foo');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('DROP INDEX [users_foo_index] ON [users]');
2015-12-08 11:37:31 -06:00
});
it('test drop index, custom', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropIndex(null, 'foo');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('DROP INDEX [foo] ON [users]');
2015-12-08 11:37:31 -06:00
});
it('test drop foreign', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropForeign('foo');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT [users_foo_foreign]');
2015-12-08 11:37:31 -06:00
});
it('test drop foreign, custom', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropForeign(null, 'foo');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT [foo]');
2015-12-08 11:37:31 -06:00
});
it('test drop timestamps', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropTimestamps();
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP COLUMN [created_at], [updated_at]');
2015-12-08 11:37:31 -06:00
});
it('test rename table', function() {
tableSql = client.schemaBuilder().renameTable('users', 'foo').toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('exec sp_rename ?, ?');
expect(tableSql[0].bindings[0]).to.equal('users');
expect(tableSql[0].bindings[1]).to.equal('foo');
2015-12-08 11:37:31 -06:00
});
it('test adding primary key', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.primary('foo', 'bar');
}).toSQL();
equal(1, tableSql.length);
2016-05-19 21:36:30 +02:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD CONSTRAINT [bar] PRIMARY KEY ([foo])');
2015-12-08 11:37:31 -06:00
});
it('test adding unique key', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.unique('foo', 'bar');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('CREATE UNIQUE INDEX [bar] ON [users] ([foo])');
2015-12-08 11:37:31 -06:00
});
it('test adding index', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.index(['foo', 'bar'], 'baz');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('CREATE INDEX [baz] ON [users] ([foo], [bar])');
2015-12-08 11:37:31 -06:00
});
it('test adding foreign key', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.foreign('foo_id').references('id').on('orders');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD CONSTRAINT [users_foo_id_foreign] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])');
tableSql = client.schemaBuilder().table('users', function() {
this.integer('foo_id').references('id').on('orders');
}).toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo_id] int');
expect(tableSql[1].sql).to.equal('ALTER TABLE [users] ADD CONSTRAINT [users_foo_id_foreign] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])');
});
it('adding foreign key with specific identifier', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.foreign('foo_id', 'fk_foo').references('id').on('orders');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD CONSTRAINT [fk_foo] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])');
tableSql = client.schemaBuilder().table('users', function() {
this.integer('foo_id').references('id').on('orders').withKeyName('fk_foo');
}).toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo_id] int');
expect(tableSql[1].sql).to.equal('ALTER TABLE [users] ADD CONSTRAINT [fk_foo] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])');
2015-12-08 11:37:31 -06:00
});
it("adds foreign key with onUpdate and onDelete", function() {
tableSql = client.schemaBuilder().createTable('person', function(table) {
table.integer('user_id').notNull().references('users.id').onDelete('SET NULL');
table.integer('account_id').notNull().references('id').inTable('accounts').onUpdate('cascade');
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('CREATE TABLE [person] ([user_id] int not null, [account_id] int not null, CONSTRAINT [person_user_id_foreign] FOREIGN KEY ([user_id]) REFERENCES [users] ([id]) ON DELETE SET NULL, CONSTRAINT [person_account_id_foreign] FOREIGN KEY ([account_id]) REFERENCES [accounts] ([id]) ON UPDATE cascade)');
2015-12-08 11:37:31 -06:00
});
it('test adding incrementing id', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.increments('id');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [id] int identity(1,1) not null primary key');
2015-12-08 11:37:31 -06:00
});
it('test adding big incrementing id', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.bigIncrements('id');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [id] bigint identity(1,1) not null primary key');
2015-12-08 11:37:31 -06:00
});
it('test adding column after another column', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.string('name').after('foo');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [name] nvarchar(255)');
2015-12-08 11:37:31 -06:00
});
it('test adding column on the first place', function() {
tableSql = client.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] nvarchar(255)');
2015-12-08 11:37:31 -06:00
});
it('test adding string', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.string('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] nvarchar(255)');
2015-12-08 11:37:31 -06:00
});
it('uses the varchar column constraint', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.string('foo', 100);
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] nvarchar(100)');
2015-12-08 11:37:31 -06:00
});
it('chains notNull and defaultTo', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.string('foo', 100).notNull().defaultTo('bar');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] nvarchar(100) not null default \'bar\'');
2015-12-08 11:37:31 -06:00
});
it('allows for raw values in the default field', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.string('foo', 100).nullable().defaultTo(client.raw('CURRENT TIMESTAMP'));
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] nvarchar(100) null default CURRENT TIMESTAMP');
2015-12-08 11:37:31 -06:00
});
it('test adding text', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.text('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] nvarchar(max)');
2015-12-08 11:37:31 -06:00
});
it('test adding big integer', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.bigInteger('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] bigint');
2015-12-08 11:37:31 -06:00
});
it('test adding integer', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.integer('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] int');
2015-12-08 11:37:31 -06:00
});
it('test adding medium integer', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.mediumint('foo');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] int');
2015-12-08 11:37:31 -06:00
});
it('test adding small integer', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.smallint('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] smallint');
2015-12-08 11:37:31 -06:00
});
it('test adding tiny integer', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.tinyint('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] tinyint');
2015-12-08 11:37:31 -06:00
});
it('test adding float', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.float('foo', 5, 2);
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal(5, 2)');
2015-12-08 11:37:31 -06:00
});
it('test adding double', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.double('foo');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal');
2015-12-08 11:37:31 -06:00
});
it('test adding double specifying precision', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.double('foo', 15, 8);
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal(15, 8)');
2015-12-08 11:37:31 -06:00
});
it('test adding decimal', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.decimal('foo', 5, 2);
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal(5, 2)');
2015-12-08 11:37:31 -06:00
});
it('set comment to undefined', function() {
expect(function() {
client.schemaBuilder().table('user', function(t) {
t.comment();
}).toSQL();
})
.to.throw(TypeError);
});
it('set comment to null', function() {
expect(function() {
client.schemaBuilder().table('user', function(t) {
t.comment(null);
}).toSQL();
})
.to.throw(TypeError);
});
2015-12-08 11:37:31 -06:00
it('test adding boolean', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.boolean('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] bit');
2015-12-08 11:37:31 -06:00
});
it('test adding enum', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.enum('foo', ['bar', 'baz']);
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] nvarchar(100)');
});
2015-12-08 11:37:31 -06:00
it('test adding date', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.date('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] date');
2015-12-08 11:37:31 -06:00
});
it('test adding date time', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dateTime('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] datetime');
2015-12-08 11:37:31 -06:00
});
it('test adding time', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.time('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] time');
2015-12-08 11:37:31 -06:00
});
it('test adding time stamp', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.timestamp('foo');
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] datetime');
2015-12-08 11:37:31 -06:00
});
it('test adding time stamps', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.timestamps();
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [created_at] datetime, [updated_at] datetime');
2015-12-08 11:37:31 -06:00
});
it('test adding binary', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.binary('foo');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] varbinary(max)');
2015-12-08 11:37:31 -06:00
});
it('test adding decimal', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.decimal('foo', 2, 6);
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal(2, 6)');
2015-12-08 11:37:31 -06:00
});
it('test adding multiple columns, #1348', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.integer('foo');
this.integer('baa');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] int, [baa] int');
});
2015-12-08 11:37:31 -06:00
it('is possible to set raw statements in defaultTo, #146', function() {
tableSql = client.schemaBuilder().createTable('default_raw_test', function(t) {
t.timestamp('created_at').defaultTo(client.raw('GETDATE()'));
2015-12-08 11:37:31 -06:00
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('CREATE TABLE [default_raw_test] ([created_at] datetime default GETDATE())');
2015-12-08 11:37:31 -06:00
});
it('allows dropping a unique compound index', function() {
tableSql = client.schemaBuilder().table('composite_key_test', function(t) {
t.dropUnique(['column_a', 'column_b']);
}).toSQL();
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [composite_key_test] DROP CONSTRAINT [composite_key_test_column_a_column_b_unique]');
2015-12-08 11:37:31 -06:00
});
it('allows default as alias for defaultTo', function() {
tableSql = client.schemaBuilder().createTable('default_raw_test', function(t) {
t.timestamp('created_at').default(client.raw('GETDATE()'));
2015-12-08 11:37:31 -06:00
}).toSQL();
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('CREATE TABLE [default_raw_test] ([created_at] datetime default GETDATE())');
2015-12-08 11:37:31 -06:00
});
it('#1430 - .primary & .dropPrimary takes columns and constraintName', function() {
tableSql = client.schemaBuilder().table('users', function(t) {
t.primary(['test1', 'test2'], 'testconstraintname');
}).toSQL();
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD CONSTRAINT [testconstraintname] PRIMARY KEY ([test1], [test2])');
tableSql = client.schemaBuilder().createTable('users', function(t) {
t.string('test').primary('testconstraintname');
}).toSQL();
expect(tableSql[0].sql).to.equal('CREATE TABLE [users] ([test] nvarchar(255), CONSTRAINT [testconstraintname] PRIMARY KEY ([test]))');
});
2015-12-08 11:37:31 -06:00
});