2020-03-08 19:48:23 -04:00
const { expect } = require ( 'chai' ) ;
2014-09-01 17:18:45 +02:00
2018-10-15 22:29:53 -04:00
const sinon = require ( 'sinon' ) ;
2019-07-10 22:48:43 +01:00
const MySQL _Client = require ( '../../../lib/dialects/mysql' ) ;
const MySQL2 _Client = require ( '../../../lib/dialects/mysql2' ) ;
2021-10-20 22:23:29 +02:00
const knex = require ( '../../../knex' ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
module . exports = function ( dialect ) {
describe ( dialect + ' SchemaBuilder' , function ( ) {
2018-10-15 22:29:53 -04:00
let client ;
2018-07-09 08:10:34 -04:00
switch ( dialect ) {
case 'mysql' :
2018-08-29 17:13:16 +02:00
client = new MySQL _Client ( { client : 'mysql' } ) ;
2018-07-09 08:10:34 -04:00
break ;
case 'mysql2' :
2018-08-29 17:13:16 +02:00
client = new MySQL2 _Client ( { client : 'mysql2' } ) ;
2018-07-09 08:10:34 -04:00
break ;
}
2018-10-15 22:29:53 -04:00
let tableSql ;
const equal = require ( 'assert' ) . equal ;
2018-07-09 08:10:34 -04:00
2020-04-19 00:40:23 +02:00
it ( 'basic create table with column collate' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04:00
table . increments ( 'id' ) ;
table . string ( 'email' ) . collate ( 'utf8_unicode_ci' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) collate 'utf8_unicode_ci')"
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2021-10-15 15:57:46 +02:00
it ( 'create table like another' , function ( ) {
tableSql = client
. schemaBuilder ( )
. createTableLike ( 'users_like' , 'users' )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'create table `users_like` like `users`'
) ;
} ) ;
2021-02-03 13:47:32 +01:00
it ( 'test basic create table with incrementing without primary key' , function ( ) {
tableSql = client
. schemaBuilder ( )
. createTable ( 'users' , function ( table ) {
table . increments ( 'id' ) ;
table . increments ( 'other_id' , { primaryKey : false } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'create table `users` (`id` int unsigned not null auto_increment primary key, `other_id` int unsigned not null auto_increment)'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test basic create table with charset and collate' , function ( ) {
tableSql = client . schemaBuilder ( ) . createTable ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04:00
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'
) ;
2018-05-30 17:26:03 +03:00
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'basic create table without charset or collate' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . increments ( 'id' ) ;
this . string ( 'email' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04: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)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2021-10-20 22:23:29 +02:00
describe ( 'views' , function ( ) {
let knexMysql ;
before ( function ( ) {
knexMysql = knex ( {
client : 'mysql2' ,
connection : { } ,
} ) ;
} ) ;
it ( 'basic create view' , async function ( ) {
const viewSql = client
. schemaBuilder ( )
. createView ( 'adults' , function ( view ) {
view . columns ( [ 'name' ] ) ;
view . as ( knexMysql ( 'users' ) . select ( 'name' ) . where ( 'age' , '>' , '18' ) ) ;
} )
. toSQL ( ) ;
equal ( 1 , viewSql . length ) ;
expect ( viewSql [ 0 ] . sql ) . to . equal (
"create view `adults` (`name`) as select `name` from `users` where `age` > '18'"
) ;
} ) ;
it ( 'create view or replace' , async function ( ) {
const viewSql = client
. schemaBuilder ( )
. createViewOrReplace ( 'adults' , function ( view ) {
view . columns ( [ 'name' ] ) ;
view . as ( knexMysql ( 'users' ) . select ( 'name' ) . where ( 'age' , '>' , '18' ) ) ;
} )
. toSQL ( ) ;
equal ( 1 , viewSql . length ) ;
expect ( viewSql [ 0 ] . sql ) . to . equal (
"create view or replace `adults` (`name`) as select `name` from `users` where `age` > '18'"
) ;
} ) ;
it ( 'create view with check options' , async function ( ) {
const viewSqlLocalCheck = client
. schemaBuilder ( )
. createView ( 'adults' , function ( view ) {
view . columns ( [ 'name' ] ) ;
view . as ( knexMysql ( 'users' ) . select ( 'name' ) . where ( 'age' , '>' , '18' ) ) ;
view . localCheckOption ( ) ;
} )
. toSQL ( ) ;
equal ( 1 , viewSqlLocalCheck . length ) ;
expect ( viewSqlLocalCheck [ 0 ] . sql ) . to . equal (
"create view `adults` (`name`) as select `name` from `users` where `age` > '18' with local check option"
) ;
const viewSqlCascadedCheck = client
. schemaBuilder ( )
. createView ( 'adults' , function ( view ) {
view . columns ( [ 'name' ] ) ;
view . as ( knexMysql ( 'users' ) . select ( 'name' ) . where ( 'age' , '>' , '18' ) ) ;
view . cascadedCheckOption ( ) ;
} )
. toSQL ( ) ;
equal ( 1 , viewSqlCascadedCheck . length ) ;
expect ( viewSqlCascadedCheck [ 0 ] . sql ) . to . equal (
"create view `adults` (`name`) as select `name` from `users` where `age` > '18' with cascaded check option"
) ;
} ) ;
it ( 'drop view' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropView ( 'users' ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop view `users`' ) ;
} ) ;
it ( 'drop view with schema' , function ( ) {
tableSql = client
. schemaBuilder ( )
. withSchema ( 'myschema' )
. dropView ( 'users' )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop view `myschema`.`users`' ) ;
} ) ;
it ( 'rename and change default of column of view' , function ( ) {
expect ( ( ) => {
tableSql = client
. schemaBuilder ( )
. view ( 'users' , function ( view ) {
view . column ( 'oldName' ) . rename ( 'newName' ) . defaultTo ( '10' ) ;
} )
. toSQL ( ) ;
} ) . to . throw ( 'rename column of views is not supported by this dialect.' ) ;
} ) ;
it ( 'rename view' , function ( ) {
tableSql = client
. schemaBuilder ( )
. renameView ( 'old_view' , 'new_view' )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'rename table `old_view` to `new_view`'
) ;
} ) ;
it ( 'create materialized view' , function ( ) {
expect ( ( ) => {
tableSql = client
. schemaBuilder ( )
. createMaterializedView ( 'mat_view' , function ( view ) {
view . columns ( [ 'name' ] ) ;
view . as (
knexMysql ( 'users' ) . select ( 'name' ) . where ( 'age' , '>' , '18' )
) ;
} )
. toSQL ( ) ;
} ) . to . throw ( 'materialized views are not supported by this dialect.' ) ;
} ) ;
it ( 'refresh view' , function ( ) {
expect ( ( ) => {
tableSql = client
. schemaBuilder ( )
. refreshMaterializedView ( 'view_to_refresh' )
. toSQL ( ) ;
} ) . to . throw ( 'materialized views are not supported by this dialect.' ) ;
} ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'adding json' , function ( ) {
2019-08-14 17:11:01 +02:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'user' , function ( t ) {
2019-08-14 17:11:01 +02:00
t . json ( 'preferences' ) ;
} )
. toSQL ( ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `user` add `preferences` json'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'adding jsonb' , function ( ) {
2019-08-14 17:11:01 +02:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'user' , function ( t ) {
2019-08-14 17:11:01 +02:00
t . jsonb ( 'preferences' ) ;
} )
. toSQL ( ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `user` add `preferences` json'
) ;
} ) ;
2018-07-09 08:10:34 -04:00
2020-04-19 00:40:23 +02:00
it ( 'test drop table' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTable ( 'users' ) . toSQL ( ) ;
2014-06-04 11:29:21 -04:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop table `users`' ) ;
} ) ;
2014-06-04 11:29:21 -04:00
2020-04-19 00:40:23 +02:00
it ( 'test drop table if exists' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTableIfExists ( 'users' ) . toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop table if exists `users`' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test drop column' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropColumn ( 'foo' ) ;
} )
. toSQL ( ) ;
2014-06-04 11:29:21 -04:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` drop `foo`' ) ;
} ) ;
2014-06-04 11:29:21 -04:00
2020-04-19 00:40:23 +02:00
it ( 'drops multiple columns with an array' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropColumn ( [ 'foo' , 'bar' ] ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop `foo`, drop `bar`'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'drops multiple columns as multiple arguments' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropColumn ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop `foo`, drop `bar`'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test drop primary' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropPrimary ( ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` drop primary key' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test drop unique' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropUnique ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop index `users_foo_unique`'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test drop unique, custom' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropUnique ( null , 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` drop index `foo`' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test drop index' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropIndex ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop index `users_foo_index`'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test drop index, custom' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropIndex ( null , 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` drop index `foo`' ) ;
} ) ;
2017-02-17 00:35:43 +02:00
2020-04-19 00:40:23 +02:00
it ( 'test drop foreign' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropForeign ( 'foo' ) ;
} )
. toSQL ( ) ;
2017-02-17 00:35:43 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop foreign key `users_foo_foreign`'
) ;
} ) ;
2017-02-17 00:35:43 +02:00
2020-04-19 00:40:23 +02:00
it ( 'test drop foreign, custom' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropForeign ( null , 'foo' ) ;
} )
. toSQL ( ) ;
2017-02-17 00:35:43 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop foreign key `foo`'
) ;
} ) ;
2017-02-17 00:35:43 +02:00
2020-04-19 00:40:23 +02:00
it ( 'test drop timestamps' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . dropTimestamps ( ) ;
} )
. toSQL ( ) ;
2017-02-17 00:35:43 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` drop `created_at`, drop `updated_at`'
) ;
} ) ;
2014-06-12 13:57:01 -04:00
2020-04-19 00:40:23 +02:00
it ( 'test rename table' , function ( ) {
tableSql = client . schemaBuilder ( ) . renameTable ( 'users' , 'foo' ) . toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'rename table `users` to `foo`' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding primary key' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . primary ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add primary key `bar`(`foo`)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding unique key' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . unique ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add unique `bar`(`foo`)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding index' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . index ( [ 'foo' , 'bar' ] , 'baz' ) ;
} )
. toSQL ( ) ;
2014-07-30 10:54:29 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add index `baz`(`foo`, `bar`)'
) ;
} ) ;
2017-05-15 19:16:15 +09:00
2020-04-19 00:40:23 +02:00
it ( 'test adding index with an index type' , function ( ) {
2018-11-10 13:36:10 -02:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-11-10 13:36:10 -02:00
this . index ( [ 'foo' , 'bar' ] , 'baz' , 'FULLTEXT' ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add FULLTEXT index `baz`(`foo`, `bar`)'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding foreign key' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
this . foreign ( 'foo_id' ) . references ( 'id' ) . on ( 'orders' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2017-05-15 19:16:15 +09:00
2018-07-09 08:10:34 -04: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`)'
) ;
2014-07-30 10:54:29 +02:00
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
this . integer ( 'foo_id' ) . references ( 'id' ) . on ( 'orders' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
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`)'
) ;
} ) ;
2017-05-15 19:16:15 +09:00
2020-04-19 00:40:23 +02:00
it ( 'adding foreign key with specific identifier' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
this . foreign ( 'foo_id' , 'fk_foo' ) . references ( 'id' ) . on ( 'orders' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2017-05-15 19:16:15 +09:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add constraint `fk_foo` foreign key (`foo_id`) references `orders` (`id`)'
) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . integer ( 'foo_id' )
. references ( 'id' )
. on ( 'orders' )
. withKeyName ( 'fk_foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
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`)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2021-07-25 17:23:17 +10:00
it ( 'adds foreign key with deferred throw error ' , function ( ) {
const addDeferredConstraint = ( ) => {
client
. schemaBuilder ( )
. createTable ( 'person' , function ( table ) {
table
. integer ( 'user_id' )
. notNull ( )
. references ( 'users.id' )
. deferrable ( 'immediate' ) ;
} )
. toSQL ( ) ;
} ;
expect ( addDeferredConstraint ) . to . throw (
'mysql does not support deferrable'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'adds foreign key with onUpdate and onDelete' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02: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 ( 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
2020-04-19 00:40:23 +02:00
it ( 'test adding incrementing id' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . increments ( 'id' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `id` int unsigned not null auto_increment primary key'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding big incrementing id' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . bigIncrements ( 'id' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `id` bigint unsigned not null auto_increment primary key'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2021-02-03 13:47:32 +01:00
it ( 'test adding big incrementing id without primary key' , function ( ) {
tableSql = client
. schemaBuilder ( )
. table ( 'users' , function ( ) {
this . bigIncrements ( 'id' , { primaryKey : false } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `id` bigint unsigned not null auto_increment'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding column after another column' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'name' ) . after ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `name` varchar(255) after `foo`'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding column after another column with comment' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
this . string ( 'name' ) . after ( 'foo' ) . comment ( 'bar' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"alter table `users` add `name` varchar(255) comment 'bar' after `foo`"
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding column on the first place' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'first_name' ) . first ( ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
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
2020-04-19 00:40:23 +02:00
it ( 'test adding column on the first place with comment' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
this . string ( 'first_name' ) . first ( ) . comment ( 'bar' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"alter table `users` add `first_name` varchar(255) comment 'bar' first"
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding string' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` varchar(255)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'uses the varchar column constraint' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' , 100 ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` varchar(100)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'chains notNull and defaultTo' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
this . string ( 'foo' , 100 ) . notNull ( ) . defaultTo ( 'bar' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
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
2020-04-19 00:40:23 +02:00
it ( 'allows for raw values in the default field' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' , 100 )
. nullable ( )
. defaultTo ( client . raw ( 'CURRENT TIMESTAMP' ) ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` varchar(100) null default CURRENT TIMESTAMP'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding text' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . text ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` text' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding big integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . bigInteger ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` bigint' ) ;
} ) ;
2018-02-01 23:41:01 +01:00
2020-04-19 00:40:23 +02:00
it ( 'test adding integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . integer ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` int' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding medium integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . mediumint ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` mediumint'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding small integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . smallint ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` smallint'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding tiny integer' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . tinyint ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` tinyint' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding float' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . float ( 'foo' , 5 , 2 ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` float(5, 2)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding double' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . double ( 'foo' ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` double' ) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding double specifying precision' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . double ( 'foo' , 15 , 8 ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` double(15, 8)'
) ;
} ) ;
2013-12-27 14:44:21 -05:00
2020-04-19 00:40:23 +02:00
it ( 'test adding decimal' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . decimal ( 'foo' , 5 , 2 ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` decimal(5, 2)'
) ;
} ) ;
2018-04-18 21:14:29 +01:00
2020-04-19 00:40:23 +02:00
it ( 'test adding decimal, no precision' , function ( ) {
2018-07-09 08:10:34 -04:00
expect ( ( ) => {
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . decimal ( 'foo' , null ) ;
} )
. toSQL ( ) ;
} ) . to . throw (
'Specifying no precision on decimal columns is not supported'
) ;
} ) ;
2018-04-18 21:14:29 +01:00
2020-04-19 00:40:23 +02:00
it ( 'test adding boolean' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . boolean ( 'foo' ) ;
} )
. toSQL ( ) ;
2018-04-18 21:14:29 +01:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` boolean' ) ;
} ) ;
2018-04-18 21:14:29 +01:00
2020-04-19 00:40:23 +02:00
it ( 'test adding enum' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . enum ( 'foo' , [ 'bar' , 'baz' ] ) ;
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"alter table `users` add `foo` enum('bar', 'baz')"
) ;
} ) ;
2014-06-09 21:28:49 -04:00
2018-11-26 10:22:27 +01:00
it ( 'test adding date' , ( ) => {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2018-11-26 10:22:27 +01:00
. table ( 'users' , ( table ) => {
table . date ( 'foo' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2013-12-27 14:44:21 -05:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` date' ) ;
} ) ;
2014-02-21 20:37:24 -05:00
2018-11-26 10:22:27 +01:00
it ( 'test adding date time' , ( ) => {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2018-11-26 10:22:27 +01:00
. table ( 'users' , ( table ) => {
table . dateTime ( 'foo' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2017-06-10 19:47:51 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` datetime'
) ;
} ) ;
2017-06-10 19:47:51 +02:00
2018-11-26 10:22:27 +01:00
it ( 'test adding date time with options object' , ( ) => {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2018-11-26 10:22:27 +01:00
. table ( 'users' , ( table ) => {
table . dateTime ( 'foo' , { precision : 3 } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` datetime(3)'
) ;
} ) ;
it ( 'test adding time' , ( ) => {
tableSql = client
. schemaBuilder ( )
. table ( 'users' , ( table ) => {
table . time ( 'foo' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2017-06-10 19:47:51 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` time' ) ;
} ) ;
2017-06-10 19:47:51 +02:00
2018-11-26 10:22:27 +01:00
it ( 'test adding time with options object' , ( ) => {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2018-11-26 10:22:27 +01:00
. table ( 'users' , ( table ) => {
table . time ( 'foo' , { precision : 3 } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` time(3)' ) ;
} ) ;
it ( 'test adding time stamp' , ( ) => {
tableSql = client
. schemaBuilder ( )
. table ( 'users' , ( table ) => {
table . timestamp ( 'foo' ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
2017-10-31 23:22:07 +01:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` timestamp'
) ;
} ) ;
2017-10-31 23:22:07 +01:00
2018-11-26 10:22:27 +01:00
it ( 'test adding time stamp with options object' , ( ) => {
tableSql = client
. schemaBuilder ( )
. table ( 'users' , ( table ) => {
table . timestamp ( 'foo' , { precision : 3 } ) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` timestamp(3)'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding time stamps' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . timestamps ( ) ;
} )
. toSQL ( ) ;
2017-02-16 20:34:59 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `created_at` datetime, add `updated_at` datetime'
) ;
} ) ;
2017-02-16 20:34:59 +02:00
2020-04-19 00:40:23 +02:00
it ( 'test adding precise timestamp' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-19 10:46:23 -04:00
this . timestamp ( 'foo' , 6 ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` timestamp(6)'
) ;
} ) ;
2014-06-04 11:29:21 -04:00
2020-04-19 00:40:23 +02:00
it ( 'test adding precise datetime' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-19 10:46:23 -04:00
this . datetime ( 'foo' , 6 ) ;
2018-07-09 08:10:34 -04:00
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
2018-07-19 10:46:23 -04:00
'alter table `users` add `foo` datetime(6)'
2018-07-09 08:10:34 -04:00
) ;
} ) ;
2014-09-02 10:52:38 -04:00
2020-04-19 00:40:23 +02:00
it ( 'test adding binary' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . binary ( 'foo' ) ;
} )
. toSQL ( ) ;
2014-09-02 10:52:38 -04:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table `users` add `foo` blob' ) ;
} ) ;
2015-04-19 16:31:52 -04:00
2020-04-19 00:40:23 +02:00
it ( 'test adding decimal' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . decimal ( 'foo' , 2 , 6 ) ;
} )
. toSQL ( ) ;
2016-03-15 16:31:07 -07:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `foo` decimal(2, 6)'
) ;
} ) ;
2016-05-19 21:30:17 +02:00
2020-04-19 00:40:23 +02:00
it ( 'test set comment' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . comment ( 'Custom comment' ) ;
} )
. toSQL ( ) ;
2016-05-19 21:30:17 +02:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"alter table `users` comment = 'Custom comment'"
) ;
} ) ;
2016-05-19 21:30:17 +02:00
2020-04-19 00:40:23 +02:00
it ( 'test set empty comment' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . comment ( '' ) ;
} )
. toSQL ( ) ;
2018-02-01 23:41:01 +01:00
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( "alter table `users` comment = ''" ) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-10-27 10:05:21 +02:00
it ( 'test column comment with quotes' , function ( ) {
tableSql = client
. schemaBuilder ( )
. createTable ( 'test' , ( t ) => {
t . text ( 'column1' ) . comment (
"The table's first column and it's escaped"
) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"create table `test` (`column1` text comment 'The table\\'s first column and it\\'s escaped')"
) ;
} ) ;
it ( 'test column comment with pre-escaped quotes' , function ( ) {
tableSql = client
. schemaBuilder ( )
. createTable ( 'test' , ( t ) => {
t . text ( 'column1' ) . comment (
"The table\\'s first column and it\\'s escaped"
) ;
} )
. toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
"create table `test` (`column1` text comment 'The table\\'s first column and it\\'s escaped')"
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'set comment to undefined' , function ( ) {
expect ( function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'user' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . comment ( ) ;
} )
. toSQL ( ) ;
} ) . to . throw ( TypeError ) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'set comment to null' , function ( ) {
expect ( function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'user' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . comment ( null ) ;
} )
. toSQL ( ) ;
} ) . to . throw ( TypeError ) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'should alter columns with the alter flag' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
2018-02-01 23:41:01 +01:00
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( ) {
2018-07-09 08:10:34 -04:00
this . string ( 'foo' ) . alter ( ) ;
this . string ( 'bar' ) ;
2018-02-01 23:41:01 +01:00
} )
. toSQL ( ) ;
2018-07-09 08:10:34 -04:00
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table `users` add `bar` varchar(255)'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'alter table `users` modify `foo` varchar(255)'
) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'is possible to set raw statements in defaultTo, #146' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
2018-02-01 23:41:01 +01:00
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTable ( 'default_raw_test' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . timestamp ( 'created_at' ) . defaultTo ( client . raw ( 'CURRENT_TIMESTAMP' ) ) ;
2018-02-01 23:41:01 +01:00
} )
. toSQL ( ) ;
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'create table `default_raw_test` (`created_at` timestamp default CURRENT_TIMESTAMP)'
) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'allows dropping a unique compound index' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
2018-02-01 23:41:01 +01:00
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'composite_key_test' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . dropUnique ( [ 'column_a' , 'column_b' ] ) ;
2018-02-01 23:41:01 +01:00
} )
. toSQL ( ) ;
2018-07-09 08:10:34 -04:00
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`'
) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'allows default as alias for defaultTo' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
2018-02-01 23:41:01 +01:00
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTable ( 'default_raw_test' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . timestamp ( 'created_at' ) . default ( client . raw ( 'CURRENT_TIMESTAMP' ) ) ;
2018-02-01 23:41:01 +01:00
} )
. toSQL ( ) ;
2018-07-09 08:10:34 -04:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'create table `default_raw_test` (`created_at` timestamp default CURRENT_TIMESTAMP)'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'sets myISAM engine' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . string ( 'username' ) ;
t . engine ( 'myISAM' ) ;
} )
. toSQL ( ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal (
'create table `users` (`username` varchar(255)) engine = myISAM'
) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( '#1430 - .primary & .dropPrimary takes columns and constraintName' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02: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 primary key `testconstraintname`(`test1`, `test2`)'
) ;
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( t ) {
2018-07-09 08:10:34 -04:00
t . string ( 'test' ) . primary ( 'testconstraintname' ) ;
} )
. toSQL ( ) ;
2015-04-22 16:19:59 -04:00
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 1 ] . sql ) . to . equal (
'alter table `users` add primary key `testconstraintname`(`test`)'
) ;
} ) ;
2015-04-22 16:19:59 -04:00
2020-04-19 00:40:23 +02:00
describe ( 'queryContext' , function ( ) {
2018-07-09 08:10:34 -04:00
let spy ;
let originalWrapIdentifier ;
2020-04-19 00:40:23 +02:00
before ( function ( ) {
2018-07-09 08:10:34 -04:00
spy = sinon . spy ( ) ;
originalWrapIdentifier = client . config . wrapIdentifier ;
2020-04-19 00:40:23 +02:00
client . config . wrapIdentifier = function ( value , wrap , queryContext ) {
2018-07-09 08:10:34 -04:00
spy ( value , queryContext ) ;
return wrap ( value ) ;
} ;
} ) ;
2020-04-19 00:40:23 +02:00
beforeEach ( function ( ) {
2018-07-09 08:10:34 -04:00
spy . resetHistory ( ) ;
} ) ;
2020-04-19 00:40:23 +02:00
after ( function ( ) {
2018-07-09 08:10:34 -04:00
client . config . wrapIdentifier = originalWrapIdentifier ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'SchemaCompiler passes queryContext to wrapIdentifier via TableCompiler' , function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04:00
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} )
. toSQL ( ) ;
expect ( spy . callCount ) . to . equal ( 3 ) ;
expect ( spy . firstCall . args ) . to . deep . equal ( [ 'id' , 'schema context' ] ) ;
expect ( spy . secondCall . args ) . to . deep . equal ( [ 'email' , 'schema context' ] ) ;
expect ( spy . thirdCall . args ) . to . deep . equal ( [ 'users' , 'schema context' ] ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'TableCompiler passes queryContext to wrapIdentifier' , function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04: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 ] ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'TableCompiler allows overwriting queryContext from SchemaCompiler' , function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04: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' ] ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'ColumnCompiler allows overwriting queryContext from TableCompiler' , function ( ) {
2018-07-09 08:10:34 -04:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2020-04-19 00:40:23 +02:00
. createTable ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04: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' ] ) ;
} ) ;
} ) ;
} ) ;
} ;