2016-05-19 22:21:34 +02:00
'use strict' ;
2020-03-08 19:48:23 -04:00
const { expect } = require ( 'chai' ) ;
2018-10-15 22:29:53 -04:00
const sinon = require ( 'sinon' ) ;
2019-07-10 22:48:43 +01:00
const Oracle _Client = require ( '../../../lib/dialects/oracle' ) ;
2018-10-15 22:29:53 -04:00
const client = new Oracle _Client ( { client : 'oracledb' } ) ;
2016-05-19 22:21:34 +02:00
2020-04-19 00:40:23 +02:00
describe ( 'Oracle SchemaBuilder' , function ( ) {
2018-10-15 22:29:53 -04:00
let tableSql ;
const equal = require ( 'assert' ) . equal ;
2016-05-19 22:21:34 +02:00
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 ) {
2016-05-19 22:21:34 +02:00
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} ) ;
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . toSQL ( ) . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql . toSQL ( ) [ 0 ] . sql ) . to . equal (
'create table "users" ("id" integer not null primary key, "email" varchar2(255))'
) ;
expect ( tableSql . toSQL ( ) [ 1 ] . sql ) . to . equal (
'DECLARE PK_NAME VARCHAR(200); BEGIN EXECUTE IMMEDIATE (\'CREATE SEQUENCE "users_seq"\'); SELECT cols.column_name INTO PK_NAME FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = \'P\' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cols.table_name = \'users\'; execute immediate (\'create or replace trigger "users_autoinc_trg" BEFORE INSERT on "users" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "users_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "users" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'); END;'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test basic create table if not exists' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. createTableIfNotExists ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04:00
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} ) ;
2016-05-19 22:21:34 +02:00
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . toSQL ( ) . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql . toSQL ( ) [ 0 ] . sql ) . to . equal (
'begin execute immediate \'create table "users" ("id" integer not null primary key, "email" varchar2(255))\'; exception when others then if sqlcode != -955 then raise; end if; end;'
) ;
expect ( tableSql . toSQL ( ) [ 1 ] . sql ) . to . equal (
'DECLARE PK_NAME VARCHAR(200); BEGIN EXECUTE IMMEDIATE (\'CREATE SEQUENCE "users_seq"\'); SELECT cols.column_name INTO PK_NAME FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = \'P\' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cols.table_name = \'users\'; execute immediate (\'create or replace trigger "users_autoinc_trg" BEFORE INSERT on "users" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "users_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "users" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'); END;'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test drop table' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTable ( 'users' ) . toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop table "users"' ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 1 ] . sql ) . to . equal (
'begin execute immediate \'drop sequence "users_seq"\'; exception when others then if sqlcode != -2289 then raise; end if; end;'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test drop table if exists' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTableIfExists ( 'users' ) . toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 2 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'begin execute immediate \'drop table "users"\'; exception when others then if sqlcode != -942 then raise; end if; end;'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'begin execute immediate \'drop sequence "users_seq"\'; exception when others then if sqlcode != -2289 then raise; end if; end;'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("foo")' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("foo", "bar")' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("foo", "bar")' ) ;
} ) ;
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
. 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' ) ;
} )
. 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" varchar2(255)'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'alter table "users" modify "foo" varchar2(255)'
) ;
2017-02-16 20:34:59 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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_pkey"'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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_unique"'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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"'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop index "users_foo_index"' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop index "foo"' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02: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"'
) ;
2016-05-19 22:21:34 +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 ( ) ;
2016-05-19 22:21:34 +02: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"'
) ;
2016-05-19 22:21:34 +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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" drop ("created_at", "updated_at")'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'rename table' , function ( ) {
tableSql = client . schemaBuilder ( ) . renameTable ( 'users' , 'foo' ) . toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'DECLARE PK_NAME VARCHAR(200); IS_AUTOINC NUMBER := 0; BEGIN EXECUTE IMMEDIATE (\'RENAME "users" TO "foo"\'); SELECT COUNT(*) INTO IS_AUTOINC from "USER_TRIGGERS" where trigger_name = \'users_autoinc_trg\'; IF (IS_AUTOINC > 0) THEN EXECUTE IMMEDIATE (\'DROP TRIGGER "users_autoinc_trg"\'); EXECUTE IMMEDIATE (\'RENAME "users_seq" TO "foo_seq"\'); SELECT cols.column_name INTO PK_NAME FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = \'P\' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cols.table_name = \'foo\'; EXECUTE IMMEDIATE (\'create or replace trigger "foo_autoinc_trg" BEFORE INSERT on "foo" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "foo_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "foo" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'); end if;END;'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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")'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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" unique ("foo")'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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")'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
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 ( ) ;
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 ( )
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 ( ) ;
2017-02-17 00:35:43 +02:00
equal ( 2 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo_id" integer'
) ;
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
} ) ;
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 ( ) ;
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 ( )
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 ( ) ;
2017-02-17 00:35:43 +02:00
equal ( 2 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo_id" integer'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'alter table "users" add constraint "fk_foo" foreign key ("foo_id") references "orders" ("id")'
) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 3 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
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'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "id" integer not null primary key'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'DECLARE PK_NAME VARCHAR(200); BEGIN EXECUTE IMMEDIATE (\'CREATE SEQUENCE "users_seq"\'); SELECT cols.column_name INTO PK_NAME FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = \'P\' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cols.table_name = \'users\'; execute immediate (\'create or replace trigger "users_autoinc_trg" BEFORE INSERT on "users" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "users_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "users" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'); END;'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "id" number(20, 0) not null primary key'
) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'DECLARE PK_NAME VARCHAR(200); BEGIN EXECUTE IMMEDIATE (\'CREATE SEQUENCE "users_seq"\'); SELECT cols.column_name INTO PK_NAME FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = \'P\' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cols.table_name = \'users\'; execute immediate (\'create or replace trigger "users_autoinc_trg" BEFORE INSERT on "users" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "users_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "users" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'); END;'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test rename 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 . renameColumn ( 'foo' , 'bar' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'DECLARE PK_NAME VARCHAR(200); IS_AUTOINC NUMBER := 0; BEGIN EXECUTE IMMEDIATE (\'ALTER TABLE "users" RENAME COLUMN "foo" TO "bar"\'); SELECT COUNT(*) INTO IS_AUTOINC from "USER_TRIGGERS" where trigger_name = \'users_autoinc_trg\'; IF (IS_AUTOINC > 0) THEN SELECT cols.column_name INTO PK_NAME FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = \'P\' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cols.table_name = \'users\'; IF (\'bar\' = PK_NAME) THEN EXECUTE IMMEDIATE (\'DROP TRIGGER "users_autoinc_trg"\'); EXECUTE IMMEDIATE (\'create or replace trigger "users_autoinc_trg" BEFORE INSERT on "users" for each row declare checking number := 1; begin if (:new."bar" is null) then while checking >= 1 loop select "users_seq".nextval into :new."bar" from dual; select count("bar") into checking from "users" where "bar" = :new."bar"; end loop; end if; end;\'); end if; end if;END;'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" varchar2(255)'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" varchar2(100)'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" varchar2(100) default \'bar\' not null'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" varchar2(100) default CURRENT TIMESTAMP null'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" clob' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" number(20, 0)'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" integer' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" integer' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" smallint' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" smallint' ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding default 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' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" float' ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding float with 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 . float ( 'foo' , 5 ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" float(5)' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" number(8, 2)'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" number(15, 8)'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02: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)'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'adding decimal, variable precision' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. schemaBuilder ( )
2020-04-19 00:40:23 +02:00
. table ( 'users' , function ( table ) {
2018-07-09 08:10:34 -04:00
table . decimal ( 'foo' , null ) ;
} )
. toSQL ( ) ;
2017-11-30 15:05:39 -06:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" decimal' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" number(1, 0) check ("foo" in (\'0\', \'1\'))'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" varchar2(3) check ("foo" in (\'bar\', \'baz\'))'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding date' , 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 . date ( 'foo' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" date' ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding date time' , 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 . dateTime ( 'foo' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" timestamp with time zone'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding date time without time zone' , 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 . dateTime ( 'foo' , true ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp' ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding time' , 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 . time ( 'foo' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
// oracle does not support time
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" timestamp with time zone'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding time stamp' , 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 . timestamp ( 'foo' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add "foo" timestamp with time zone'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'test adding time stamp without time zone' , 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 . timestamp ( 'foo' , true ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp' ) ;
} ) ;
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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "users" add ("created_at" timestamp with time zone, "updated_at" timestamp with time zone)'
) ;
2016-05-19 22:21:34 +02: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 ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" blob' ) ;
} ) ;
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-05-19 22:21:34 +02: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)'
) ;
2016-05-19 22:21:34 +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 ( ) ;
2017-06-10 19:47:51 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'comment on table "users" is \'Custom comment\''
) ;
2017-06-10 19:47:51 +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 ( ) ;
2017-06-10 19:47:51 +02:00
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'comment on table "users" is \'\'' ) ;
} ) ;
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 ) ;
2017-10-31 23:22:07 +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 ) ;
2017-10-31 23:22:07 +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
. 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' ) ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'create table "default_raw_test" ("created_at" timestamp with time zone default CURRENT_TIMESTAMP)'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'allows dropping a unique compound index with too long generated name' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. 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' ] ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "composite_key_test" drop constraint "zYmMt0VQwlLZ20XnrMicXZ0ufZk"'
) ;
2016-05-19 22:21:34 +02:00
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'allows dropping a unique compound index with specified name' , function ( ) {
2018-07-09 08:10:34 -04:00
tableSql = client
. 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' ] , 'ckt_unique' ) ;
} )
. toSQL ( ) ;
2016-05-19 22:21:34 +02:00
equal ( 1 , tableSql . length ) ;
2018-07-09 08:10:34 -04:00
expect ( tableSql [ 0 ] . sql ) . to . equal (
'alter table "composite_key_test" drop constraint "ckt_unique"'
) ;
2016-05-19 22:21:34 +02: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 constraint "testconstraintname" primary key ("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 ( ) ;
expect ( tableSql [ 1 ] . sql ) . to . equal (
'alter table "users" add constraint "testconstraintname" primary key ("test")'
) ;
} ) ;
2020-04-19 00:40:23 +02:00
describe ( 'queryContext' , function ( ) {
2018-02-01 23:41:01 +01:00
let spy ;
let originalWrapIdentifier ;
2020-04-19 00:40:23 +02:00
before ( function ( ) {
2018-02-01 23:41:01 +01:00
spy = sinon . spy ( ) ;
originalWrapIdentifier = client . config . wrapIdentifier ;
2020-04-19 00:40:23 +02:00
client . config . wrapIdentifier = function ( value , wrap , queryContext ) {
2018-02-01 23:41:01 +01:00
spy ( value , queryContext ) ;
return wrap ( value ) ;
} ;
} ) ;
2020-04-19 00:40:23 +02:00
beforeEach ( function ( ) {
2018-07-07 09:47:51 +02:00
spy . resetHistory ( ) ;
2018-02-01 23:41:01 +01:00
} ) ;
2020-04-19 00:40:23 +02:00
after ( function ( ) {
2018-02-01 23:41:01 +01:00
client . config . wrapIdentifier = originalWrapIdentifier ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'SchemaCompiler passes queryContext to wrapIdentifier via TableCompiler' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
. queryContext ( 'table context' )
2020-04-19 00:40:23 +02: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' ] ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'TableCompiler passes queryContext to wrapIdentifier' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
2020-04-19 00:40:23 +02: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 ] ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'TableCompiler allows overwriting queryContext from SchemaCompiler' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2020-04-19 00:40:23 +02: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' ] ) ;
} ) ;
2020-04-19 00:40:23 +02:00
it ( 'ColumnCompiler allows overwriting queryContext from TableCompiler' , function ( ) {
2018-02-01 23:41:01 +01:00
client
. schemaBuilder ( )
. queryContext ( 'schema context' )
2020-04-19 00:40:23 +02: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' ] ) ;
} ) ;
} ) ;
2020-10-05 15:35:22 -03:00
it ( 'test converting a sql wrapped with catch to string, #4045' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTableIfExists ( 'book' ) ;
expect ( tableSql . toQuery ( ) ) . to . equal (
'begin execute immediate \'drop table "book"\'; exception when others then if sqlcode != -942 then raise; end if; end;\nbegin execute immediate \'drop sequence "book_seq"\'; exception when others then if sqlcode != -2289 then raise; end if; end;'
) ;
} ) ;
2016-05-19 22:21:34 +02:00
} ) ;