2015-12-08 11:37:31 -06:00
'use strict' ;
2019-10-06 20:21:32 +02:00
const expect = require ( 'chai' ) . expect ;
2018-10-15 22:29:53 -04:00
const sinon = require ( 'sinon' ) ;
2019-07-10 22:48:43 +01:00
const MSSQL _Client = require ( '../../../lib/dialects/mssql' ) ;
2018-10-15 22:29:53 -04:00
const client = new MSSQL _Client ( { client : 'mssql' } ) ;
2015-12-08 11:37:31 -06:00
2021-02-27 23:08:15 +00:00
describe ( 'MSSQL SchemaBuilder' , function ( ) {
2018-10-15 22:29:53 -04:00
let tableSql ;
const equal = require ( 'assert' ) . equal ;
2015-12-08 11:37:31 -06:00
2021-02-27 23:08:15 +00:00
it ( 'throws when charset and collate are specified' , function ( ) {
2019-11-21 19:52:29 +05:30
expect ( ( ) => {
2019-12-29 20:28:40 +01:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. createTable ( 'users' , function ( table ) {
2019-12-29 20:28:40 +01:00
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
table . charset ( 'utf8' ) ;
table . collate ( 'utf8_unicode_ci' ) ;
} )
. toSQL ( ) ;
2019-11-21 19:52:29 +05:30
} ) . to . throw ( 'Knex only supports charset statement with mysql' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'basic create table' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . increments ( 'id' ) ;
this . string ( 'email' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test basic create table with incrementing without primary key' , function ( ) {
tableSql = client . schemaBuilder ( ) . createTable ( 'users' , function ( table ) {
2021-02-03 13:47:32 +01:00
table . increments ( 'id' , { primaryKey : false } ) ;
} ) ;
equal ( 1 , tableSql . toSQL ( ) . length ) ;
expect ( tableSql . toSQL ( ) [ 0 ] . sql ) . to . equal (
'CREATE TABLE [users] ([id] int identity(1,1) not null)'
) ;
expect ( tableSql . toQuery ( ) ) . to . equal (
'CREATE TABLE [users] ([id] int identity(1,1) not null)'
) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop table' , function ( ) {
tableSql = client
. schemaBuilder ( )
. dropTable ( 'users' )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop table if exists' , function ( ) {
tableSql = client
. schemaBuilder ( )
. dropTableIfExists ( 'users' )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04: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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop column' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropColumn ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
2020-05-15 11:28:34 +02:00
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal ( 'ALTER TABLE [users] DROP COLUMN [foo]' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'drops multiple columns with an array' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropColumn ( [ 'foo' , 'bar' ] ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
2020-05-15 11:28:34 +02:00
equal ( 3 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 1 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 2 ] . sql ) . to . equal (
2018-07-09 08:10:34 -04:00
'ALTER TABLE [users] DROP COLUMN [foo], [bar]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'drops multiple columns as multiple arguments' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropColumn ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
2020-05-15 11:28:34 +02:00
equal ( 3 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 1 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 2 ] . sql ) . to . equal (
2018-07-09 08:10:34 -04:00
'ALTER TABLE [users] DROP COLUMN [foo], [bar]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop primary' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropPrimary ( 'testconstraintname' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] DROP CONSTRAINT [testconstraintname]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop unique' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropUnique ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'DROP INDEX [users_foo_unique] ON [users]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'should alter columns with the alter flag' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' ) . alter ( ) ;
this . string ( 'bar' ) ;
} )
. toSQL ( ) ;
2017-02-16 20:34:59 +02:00
equal ( 2 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [bar] nvarchar(255)'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
2021-02-25 17:34:22 +00:00
'ALTER TABLE [users] ALTER COLUMN [foo] nvarchar(255)'
) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'should alter multiple columns over multiple queries' , function ( ) {
2021-02-25 17:34:22 +00:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2021-02-25 17:34:22 +00:00
this . string ( 'foo' ) . alter ( ) ;
this . string ( 'bar' ) . alter ( ) ;
} )
. toSQL ( ) ;
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ALTER COLUMN [foo] nvarchar(255)'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'ALTER TABLE [users] ALTER COLUMN [bar] nvarchar(255)'
2018-07-09 08:10:34 -04:00
) ;
2017-02-16 20:34:59 +02:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop unique, custom' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropUnique ( null , 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-06-29 10:47:06 +03:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'DROP INDEX [foo] ON [users]' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop index' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropIndex ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop index, custom' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropIndex ( null , 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop foreign' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropForeign ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] DROP CONSTRAINT [users_foo_foreign]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop foreign, custom' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropForeign ( null , 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] DROP CONSTRAINT [foo]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test drop timestamps' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropTimestamps ( ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
2020-05-15 11:28:34 +02:00
equal ( 3 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 1 ] . sql ) . to . includes (
` IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint) `
) ;
expect ( tableSql [ 2 ] . sql ) . to . equal (
2018-07-09 08:10:34 -04:00
'ALTER TABLE [users] DROP COLUMN [created_at], [updated_at]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test rename table' , function ( ) {
tableSql = client
. schemaBuilder ( )
. renameTable ( 'users' , 'foo' )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2015-12-09 17:53:53 -06:00
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
} ) ;
2018-07-04 11:28:47 +02:00
2021-02-27 23:08:15 +00:00
it ( 'test has table' , function ( ) {
tableSql = client
. schemaBuilder ( )
. hasTable ( 'users' )
. toSQL ( ) ;
2018-07-04 11:28:47 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'select object_id from sys.tables where object_id = object_id(?)'
) ;
expect ( tableSql [ 0 ] . bindings [ 0 ] ) . to . equal ( '[users]' ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test has table with schema' , function ( ) {
2018-07-04 11:28:47 +02:00
tableSql = client
. schemaBuilder ( )
. withSchema ( 'schema' )
. hasTable ( 'users' )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'select object_id from sys.tables where object_id = object_id(?)'
) ;
expect ( tableSql [ 0 ] . bindings [ 0 ] ) . to . equal ( '[schema].[users]' ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test rename table with schema' , function ( ) {
2018-07-04 11:28:47 +02:00
tableSql = client
. schemaBuilder ( )
. withSchema ( 'schema' )
. renameTable ( 'users' , 'foo' )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'exec sp_rename ?, ?' ) ;
expect ( tableSql [ 0 ] . bindings [ 0 ] ) . to . equal ( 'schema.users' ) ;
expect ( tableSql [ 0 ] . bindings [ 1 ] ) . to . equal ( 'foo' ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test has column' , function ( ) {
tableSql = client
. schemaBuilder ( )
. hasColumn ( 'users' , 'foo' )
. toSQL ( ) ;
2018-07-04 11:28:47 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'select object_id from sys.columns where name = ? and object_id = object_id(?)'
) ;
expect ( tableSql [ 0 ] . bindings [ 0 ] ) . to . equal ( 'foo' ) ;
expect ( tableSql [ 0 ] . bindings [ 1 ] ) . to . equal ( '[users]' ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test has column with schema' , function ( ) {
2018-07-04 11:28:47 +02:00
tableSql = client
. schemaBuilder ( )
. withSchema ( 'schema' )
. hasColumn ( 'users' , 'foo' )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'select object_id from sys.columns where name = ? and object_id = object_id(?)'
) ;
expect ( tableSql [ 0 ] . bindings [ 0 ] ) . to . equal ( 'foo' ) ;
expect ( tableSql [ 0 ] . bindings [ 1 ] ) . to . equal ( '[schema].[users]' ) ;
} ) ;
2015-12-08 11:37:31 -06:00
2021-02-27 23:08:15 +00:00
it ( 'test adding primary key' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . primary ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD CONSTRAINT [bar] PRIMARY KEY ([foo])'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding unique key' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . unique ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'CREATE UNIQUE INDEX [bar] ON [users] ([foo]) WHERE [foo] IS NOT NULL'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding index' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . index ( [ 'foo' , 'bar' ] , 'baz' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'CREATE INDEX [baz] ON [users] ([foo], [bar])'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding foreign key' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
this . foreign ( 'foo_id' )
. references ( 'id' )
. on ( 'orders' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
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])'
) ;
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
this . integer ( 'foo_id' )
. references ( 'id' )
. on ( 'orders' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2017-02-17 00:35:43 +02:00
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo_id] int' ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 1 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD CONSTRAINT [users_foo_id_foreign] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])'
) ;
2017-02-17 00:35:43 +02:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'adding foreign key with specific identifier' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
this . foreign ( 'foo_id' , 'fk_foo' )
. references ( 'id' )
. on ( 'orders' ) ;
2018-07-09 08:10:34 -04:00
} )
. 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 ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . integer ( 'foo_id' )
. references ( 'id' )
. on ( 'orders' )
. withKeyName ( 'fk_foo' ) ;
} )
. toSQL ( ) ;
2017-02-17 00:35:43 +02:00
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo_id] int' ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 1 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD CONSTRAINT [fk_foo] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])'
) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'adds foreign key with onUpdate and onDelete' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. createTable ( 'person' , function ( table ) {
2018-07-09 08:10:34 -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 ( 1 , tableSql . length ) ;
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding incrementing id' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . increments ( 'id' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04: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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding big incrementing id' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . bigIncrements ( 'id' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04: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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding big incrementing id without primary key' , function ( ) {
2021-02-03 13:47:32 +01:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2021-02-03 13:47:32 +01:00
this . bigIncrements ( 'id' , { primaryKey : false } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [id] bigint identity(1,1) not null'
) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding column after another column' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'name' ) . after ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [name] nvarchar(255)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding column on the first place' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'first_name' ) . first ( ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [first_name] nvarchar(255)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding string' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] nvarchar(255)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'uses the varchar column constraint' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' , 100 ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] nvarchar(100)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'chains notNull and defaultTo' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
this . string ( 'foo' , 100 )
. notNull ( )
. defaultTo ( 'bar' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
2021-02-27 23:08:15 +00:00
"ALTER TABLE [users] ADD [foo] nvarchar(100) not null CONSTRAINT [users_foo_default] DEFAULT 'bar'"
2018-07-09 08:10:34 -04:00
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'allows for raw values in the default field' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' , 100 )
. nullable ( )
. defaultTo ( client . raw ( 'CURRENT TIMESTAMP' ) ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
2021-02-27 23:08:15 +00:00
'ALTER TABLE [users] ADD [foo] nvarchar(100) null CONSTRAINT [users_foo_default] DEFAULT CURRENT TIMESTAMP'
) ;
} ) ;
it ( 'allows custom named default constraints' , function ( ) {
tableSql = client
. schemaBuilder ( )
. table ( 'users' , function ( ) {
this . integer ( 'foo' ) . defaultTo ( 'test' , { constraintName : 'DF_foo' } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"ALTER TABLE [users] ADD [foo] int CONSTRAINT [DF_foo] DEFAULT 'test'"
) ;
} ) ;
it ( "doesn't name constraints when opt-out" , function ( ) {
tableSql = client
. schemaBuilder ( )
. table ( 'users' , function ( ) {
this . integer ( 'foo' ) . defaultTo ( 'test' , { constraintName : '' } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"ALTER TABLE [users] ADD [foo] int DEFAULT 'test'"
2018-07-09 08:10:34 -04:00
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding text' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . text ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] nvarchar(max)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding big integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . bigInteger ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . integer ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding medium integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . mediumint ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2016-07-20 04:22:17 +03:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo] int' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding small integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . smallint ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding tiny integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . tinyint ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding float' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . float ( 'foo' , 5 , 2 ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-06-29 10:47:06 +03:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo] float' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding double' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . double ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-06-29 10:47:06 +03:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo] float' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding double specifying precision' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . double ( 'foo' , 15 , 8 ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-06-29 10:47:06 +03:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo] float' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding decimal' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . decimal ( 'foo' , 5 , 2 ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] decimal(5, 2)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding decimal, no precision' , function ( ) {
2017-11-30 15:05:39 -06:00
expect ( ( ) => {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . decimal ( 'foo' , null ) ;
} )
. toSQL ( ) ;
2017-11-30 15:05:39 -06:00
} ) . to . throw ( 'Specifying no precision on decimal columns is not supported' ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'set comment to undefined' , function ( ) {
expect ( function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'user' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . comment ( ) ;
} )
. toSQL ( ) ;
} ) . to . throw ( TypeError ) ;
2017-10-31 23:22:07 +01:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'set comment to null' , function ( ) {
expect ( function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'user' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . comment ( null ) ;
} )
. toSQL ( ) ;
} ) . to . throw ( TypeError ) ;
2017-10-31 23:22:07 +01:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding boolean' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . boolean ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding enum' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . enum ( 'foo' , [ 'bar' , 'baz' ] ) ;
} )
. toSQL ( ) ;
2015-12-09 17:53:53 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] nvarchar(100)'
) ;
2015-12-09 17:53:53 -06:00
} ) ;
2015-12-08 11:37:31 -06:00
2021-02-27 23:08:15 +00:00
it ( 'test adding date' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . date ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding date time' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dateTime ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-08-13 21:33:37 +01:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo] datetime2' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding time' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . time ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
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
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding time stamp' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . timestamp ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-08-13 21:33:37 +01:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'ALTER TABLE [users] ADD [foo] datetime2' ) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding time stamp with timezone' , function ( ) {
2018-08-23 07:10:19 -03:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-08-23 07:10:19 -03:00
this . timestamp ( 'foo' , {
useTz : true ,
} ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] datetimeoffset'
) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding time stamps' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . timestamps ( ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
2018-08-13 21:33:37 +01:00
'ALTER TABLE [users] ADD [created_at] datetime2, [updated_at] datetime2'
2018-07-09 08:10:34 -04:00
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding binary' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . binary ( 'foo' ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] varbinary(max)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding decimal' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . decimal ( 'foo' , 2 , 6 ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] decimal(2, 6)'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'test adding multiple columns, #1348' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . integer ( 'foo' ) ;
this . integer ( 'baa' ) ;
} )
. toSQL ( ) ;
2016-05-12 10:24:29 +01:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'ALTER TABLE [users] ADD [foo] int, [baa] int'
) ;
2016-05-12 10:24:29 +01:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'is possible to set raw statements in defaultTo, #146' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. createTable ( 'default_raw_test' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . timestamp ( 'created_at' ) . defaultTo ( client . raw ( 'GETDATE()' ) ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
2021-02-27 23:08:15 +00:00
'CREATE TABLE [default_raw_test] ([created_at] datetime2 CONSTRAINT [default_raw_test_created_at_default] DEFAULT GETDATE())'
2018-07-09 08:10:34 -04:00
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'allows dropping a unique compound index' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'composite_key_test' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . dropUnique ( [ 'column_a' , 'column_b' ] ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'DROP INDEX [composite_key_test_column_a_column_b_unique] ON [composite_key_test]'
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'allows default as alias for defaultTo' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. createTable ( 'default_raw_test' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . timestamp ( 'created_at' ) . default ( client . raw ( 'GETDATE()' ) ) ;
} )
. toSQL ( ) ;
2015-12-08 11:37:31 -06:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
2021-02-27 23:08:15 +00:00
'CREATE TABLE [default_raw_test] ([created_at] datetime2 CONSTRAINT [default_raw_test_created_at_default] DEFAULT GETDATE())'
2018-07-09 08:10:34 -04:00
) ;
2015-12-08 11:37:31 -06:00
} ) ;
2021-02-27 23:08:15 +00:00
it ( '#1430 - .primary & .dropPrimary takes columns and constraintName' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. table ( 'users' , function ( t ) {
2018-07-09 08:10:34 -04:00
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 ( )
2021-02-27 23:08:15 +00:00
. createTable ( 'users' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . string ( 'test' ) . primary ( 'testconstraintname' ) ;
} )
. toSQL ( ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'CREATE TABLE [users] ([test] nvarchar(255), CONSTRAINT [testconstraintname] PRIMARY KEY ([test]))'
) ;
} ) ;
2021-02-27 23:08:15 +00:00
describe ( 'queryContext' , function ( ) {
2018-02-01 23:41:01 +01:00
let spy ;
let originalWrapIdentifier ;
2021-02-27 23:08:15 +00:00
before ( function ( ) {
2018-02-01 23:41:01 +01:00
spy = sinon . spy ( ) ;
originalWrapIdentifier = client . config . wrapIdentifier ;
2021-02-27 23:08:15 +00:00
client . config . wrapIdentifier = function ( value , wrap , queryContext ) {
2018-02-01 23:41:01 +01:00
spy ( value , queryContext ) ;
return wrap ( value ) ;
} ;
} ) ;
2021-02-27 23:08:15 +00:00
beforeEach ( function ( ) {
2018-07-07 09:47:51 +02:00
spy . resetHistory ( ) ;
2018-02-01 23:41:01 +01:00
} ) ;
2021-02-27 23:08:15 +00:00
after ( function ( ) {
2018-02-01 23:41:01 +01:00
client . config . wrapIdentifier = originalWrapIdentifier ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'SchemaCompiler passes queryContext to wrapIdentifier via TableCompiler' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
. queryContext ( 'table context' )
2021-02-27 23:08:15 +00:00
. createTable ( 'users' , function ( table ) {
2018-02-01 23:41:01 +01:00
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} )
. toSQL ( ) ;
expect ( spy . callCount ) . to . equal ( 3 ) ;
expect ( spy . firstCall . args ) . to . deep . equal ( [ 'id' , 'table context' ] ) ;
expect ( spy . secondCall . args ) . to . deep . equal ( [ 'email' , 'table context' ] ) ;
expect ( spy . thirdCall . args ) . to . deep . equal ( [ 'users' , 'table context' ] ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'TableCompiler passes queryContext to wrapIdentifier' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
2021-02-27 23:08:15 +00:00
. createTable ( 'users' , function ( table ) {
2018-02-01 23:41:01 +01:00
table . increments ( 'id' ) . queryContext ( 'id context' ) ;
table . string ( 'email' ) . queryContext ( 'email context' ) ;
} )
. toSQL ( ) ;
expect ( spy . callCount ) . to . equal ( 3 ) ;
expect ( spy . firstCall . args ) . to . deep . equal ( [ 'id' , 'id context' ] ) ;
expect ( spy . secondCall . args ) . to . deep . equal ( [ 'email' , 'email context' ] ) ;
expect ( spy . thirdCall . args ) . to . deep . equal ( [ 'users' , undefined ] ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'TableCompiler allows overwriting queryContext from SchemaCompiler' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2021-02-27 23:08:15 +00:00
. createTable ( 'users' , function ( table ) {
2018-02-01 23:41:01 +01:00
table . queryContext ( 'table context' ) ;
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} )
. toSQL ( ) ;
expect ( spy . callCount ) . to . equal ( 3 ) ;
expect ( spy . firstCall . args ) . to . deep . equal ( [ 'id' , 'table context' ] ) ;
expect ( spy . secondCall . args ) . to . deep . equal ( [ 'email' , 'table context' ] ) ;
expect ( spy . thirdCall . args ) . to . deep . equal ( [ 'users' , 'table context' ] ) ;
} ) ;
2021-02-27 23:08:15 +00:00
it ( 'ColumnCompiler allows overwriting queryContext from TableCompiler' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2021-02-27 23:08:15 +00:00
. createTable ( 'users' , function ( table ) {
2018-02-01 23:41:01 +01:00
table . queryContext ( 'table context' ) ;
table . increments ( 'id' ) . queryContext ( 'id context' ) ;
table . string ( 'email' ) . queryContext ( 'email context' ) ;
} )
. toSQL ( ) ;
expect ( spy . callCount ) . to . equal ( 3 ) ;
expect ( spy . firstCall . args ) . to . deep . equal ( [ 'id' , 'id context' ] ) ;
expect ( spy . secondCall . args ) . to . deep . equal ( [ 'email' , 'email context' ] ) ;
expect ( spy . thirdCall . args ) . to . deep . equal ( [ 'users' , 'table context' ] ) ;
} ) ;
} ) ;
2015-12-08 11:37:31 -06:00
} ) ;