2016-05-19 22:21:34 +02:00
/*global it, describe, expect*/
'use strict' ;
var Oracle _Client = require ( '../../../lib/dialects/oracle' ) ;
var client = new Oracle _Client ( { } )
describe ( "Oracle SchemaBuilder" , function ( ) {
var tableSql ;
var equal = require ( 'assert' ) . equal ;
it ( 'test basic create table with charset and collate' , function ( ) {
tableSql = client . schemaBuilder ( ) . createTable ( 'users' , function ( table ) {
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} ) ;
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . toSQL ( ) . length ) ;
2016-05-19 22:21:34 +02:00
expect ( tableSql . toSQL ( ) [ 0 ] . sql ) . to . equal ( 'create table "users" ("id" integer not null primary key, "email" varchar2(255))' ) ;
2016-10-14 17:00:39 +02:00
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
} ) ;
it ( 'test basic create table if not exists' , function ( ) {
tableSql = client . schemaBuilder ( ) . createTableIfNotExists ( 'users' , function ( table ) {
table . increments ( 'id' ) ;
table . string ( 'email' ) ;
} ) ;
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . toSQL ( ) . length ) ;
2016-05-19 22:21:34 +02: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;" ) ;
2016-10-14 17:00:39 +02:00
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
} ) ;
it ( 'test drop table' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTable ( 'users' ) . toSQL ( ) ;
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop table "users"' ) ;
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;" ) ;
} ) ;
it ( 'test drop table if exists' , function ( ) {
tableSql = client . schemaBuilder ( ) . dropTableIfExists ( 'users' ) . toSQL ( ) ;
equal ( 2 , tableSql . length ) ;
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;" ) ;
} ) ;
it ( 'test drop column' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropColumn ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("foo")' ) ;
} ) ;
it ( 'drops multiple columns with an array' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropColumn ( [ 'foo' , 'bar' ] ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("foo", "bar")' ) ;
} ) ;
it ( 'drops multiple columns as multiple arguments' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropColumn ( 'foo' , 'bar' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("foo", "bar")' ) ;
} ) ;
2017-02-16 20:34:59 +02:00
it ( 'should alter columns with the alter flag' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . string ( 'foo' ) . alter ( ) ;
this . string ( 'bar' ) ;
} ) . toSQL ( ) ;
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "bar" varchar2(255)' ) ;
expect ( tableSql [ 1 ] . sql ) . to . equal ( 'alter table "users" modify "foo" varchar2(255)' ) ;
} ) ;
2016-05-19 22:21:34 +02:00
it ( 'test drop primary' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropPrimary ( ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop constraint "users_pkey"' ) ;
} ) ;
it ( 'test drop unique' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropUnique ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop constraint "users_foo_unique"' ) ;
} ) ;
it ( 'test drop unique, custom' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropUnique ( null , 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop constraint "foo"' ) ;
} ) ;
it ( 'test drop index' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropIndex ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop index "users_foo_index"' ) ;
} ) ;
it ( 'test drop index, custom' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropIndex ( null , 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'drop index "foo"' ) ;
} ) ;
it ( 'test drop foreign' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropForeign ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop constraint "users_foo_foreign"' ) ;
} ) ;
it ( 'test drop foreign, custom' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropForeign ( null , 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop constraint "foo"' ) ;
} ) ;
it ( 'test drop timestamps' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dropTimestamps ( ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" drop ("created_at", "updated_at")' ) ;
} ) ;
it ( "rename table" , function ( ) {
tableSql = client . schemaBuilder ( ) . renameTable ( 'users' , 'foo' ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
2016-10-14 17:00:39 +02: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
} ) ;
it ( 'test adding primary key' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . primary ( 'foo' , 'bar' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add constraint "bar" primary key ("foo")' ) ;
} ) ;
it ( 'test adding unique key' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . unique ( 'foo' , 'bar' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add constraint "bar" unique ("foo")' ) ;
} ) ;
it ( 'test adding index' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . index ( [ 'foo' , 'bar' ] , 'baz' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'create index "baz" on "users" ("foo", "bar")' ) ;
} ) ;
it ( 'test adding foreign key' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . foreign ( 'foo_id' ) . references ( 'id' ) . on ( 'orders' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . 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
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . integer ( 'foo_id' ) . references ( 'id' ) . on ( 'orders' ) ;
} ) . toSQL ( ) ;
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo_id" integer' ) ;
expect ( tableSql [ 1 ] . sql ) . to . equal ( 'alter table "users" add constraint "users_foo_id_foreign" foreign key ("foo_id") references "orders" ("id")' ) ;
} ) ;
it ( 'adding foreign key with specific identifier' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . foreign ( 'foo_id' , 'fk_foo' ) . references ( 'id' ) . on ( 'orders' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add constraint "fk_foo" foreign key ("foo_id") references "orders" ("id")' ) ;
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . integer ( 'foo_id' ) . references ( 'id' ) . on ( 'orders' ) . withKeyName ( 'fk_foo' ) ;
} ) . toSQL ( ) ;
equal ( 2 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo_id" integer' ) ;
expect ( tableSql [ 1 ] . sql ) . to . equal ( 'alter table "users" add constraint "fk_foo" foreign key ("foo_id") references "orders" ("id")' ) ;
2016-05-19 22:21:34 +02:00
} ) ;
it ( "adds foreign key with onUpdate and onDelete" , function ( ) {
tableSql = client . schemaBuilder ( ) . createTable ( 'person' , function ( table ) {
table . integer ( 'user_id' ) . notNull ( ) . references ( 'users.id' ) . onDelete ( 'SET NULL' ) ;
table . integer ( 'account_id' ) . notNull ( ) . references ( 'id' ) . inTable ( 'accounts' ) . onUpdate ( 'cascade' ) ;
} ) . toSQL ( ) ;
equal ( 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' ) ;
} ) ;
it ( 'test adding incrementing id' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . increments ( 'id' ) ;
} ) . toSQL ( ) ;
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . length ) ;
2016-05-19 22:21:34 +02:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "id" integer not null primary key' ) ;
2016-10-14 17:00:39 +02:00
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
} ) ;
it ( 'test adding big incrementing id' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . bigIncrements ( 'id' ) ;
} ) . toSQL ( ) ;
2016-10-14 17:00:39 +02:00
equal ( 2 , tableSql . length ) ;
2016-05-19 22:21:34 +02:00
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "id" number(20, 0) not null primary key' ) ;
2016-10-14 17:00:39 +02:00
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
} ) ;
it ( 'test rename column' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . renameColumn ( 'foo' , 'bar' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
2016-10-14 17:00:39 +02: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
} ) ;
it ( 'test adding string' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . string ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" varchar2(255)' ) ;
} ) ;
it ( 'uses the varchar column constraint' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . string ( 'foo' , 100 ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" varchar2(100)' ) ;
} ) ;
it ( 'chains notNull and defaultTo' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . string ( 'foo' , 100 ) . notNull ( ) . defaultTo ( 'bar' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" varchar2(100) default \'bar\' not null' ) ;
} ) ;
it ( 'allows for raw values in the default field' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . string ( 'foo' , 100 ) . nullable ( ) . defaultTo ( client . raw ( 'CURRENT TIMESTAMP' ) ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" varchar2(100) default CURRENT TIMESTAMP null' ) ;
} ) ;
it ( 'test adding text' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . text ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" clob' ) ;
} ) ;
it ( 'test adding big integer' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . bigInteger ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" number(20, 0)' ) ;
} ) ;
it ( 'test adding integer' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . integer ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" integer' ) ;
} ) ;
it ( 'test adding medium integer' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . mediumint ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" integer' ) ;
} ) ;
it ( 'test adding small integer' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . smallint ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" smallint' ) ;
} ) ;
it ( 'test adding tiny integer' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . tinyint ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" smallint' ) ;
} ) ;
it ( 'test adding default float' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . float ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" float' ) ;
} ) ;
it ( 'test adding float with precision' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . float ( 'foo' , 5 ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" float(5)' ) ;
} ) ;
it ( 'test adding double' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . double ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" number(8, 2)' ) ;
} ) ;
it ( 'test adding double specifying precision' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . double ( 'foo' , 15 , 8 ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" number(15, 8)' ) ;
} ) ;
it ( 'test adding decimal' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . decimal ( 'foo' , 5 , 2 ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" decimal(5, 2)' ) ;
} ) ;
2017-11-30 15:05:39 -06:00
it ( "adding decimal, variable precision" , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( table ) {
table . decimal ( 'foo' , null ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" decimal' ) ;
} ) ;
2016-05-19 22:21:34 +02:00
it ( 'test adding boolean' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . boolean ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" number(1, 0) check ("foo" in (\'0\', \'1\'))' ) ;
} ) ;
it ( 'test adding enum' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . enum ( 'foo' , [ 'bar' , 'baz' ] ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" varchar2(3) check ("foo" in (\'bar\', \'baz\'))' ) ;
} ) ;
it ( 'test adding date' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . date ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" date' ) ;
} ) ;
it ( 'test adding date time' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dateTime ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp with time zone' ) ;
} ) ;
it ( 'test adding date time without time zone' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . dateTime ( 'foo' , true ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp' ) ;
} ) ;
it ( 'test adding time' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . time ( 'foo' ) ;
} ) . toSQL ( ) ;
// oracle does not support time
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp with time zone' ) ;
} ) ;
it ( 'test adding time stamp' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . timestamp ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp with time zone' ) ;
} ) ;
it ( 'test adding time stamp without time zone' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . timestamp ( 'foo' , true ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" timestamp' ) ;
} ) ;
it ( 'test adding time stamps' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . timestamps ( ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
2017-06-10 20:01:57 +02: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
} ) ;
it ( 'test adding binary' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . binary ( 'foo' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" blob' ) ;
} ) ;
it ( 'test adding decimal' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( ) {
this . decimal ( 'foo' , 2 , 6 ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add "foo" decimal(2, 6)' ) ;
} ) ;
2017-06-10 19:47:51 +02:00
it ( 'test set comment' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( t ) {
t . comment ( 'Custom comment' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'comment on table "users" is \'Custom comment\'' ) ;
} ) ;
it ( 'test set empty comment' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( t ) {
t . comment ( '' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'comment on table "users" is \'\'' ) ;
} ) ;
2017-10-31 23:22:07 +01:00
it ( 'set comment to undefined' , function ( ) {
expect ( function ( ) {
client . schemaBuilder ( ) . table ( 'user' , function ( t ) {
t . comment ( ) ;
} ) . toSQL ( ) ;
} )
. to . throw ( TypeError ) ;
} ) ;
it ( 'set comment to null' , function ( ) {
expect ( function ( ) {
client . schemaBuilder ( ) . table ( 'user' , function ( t ) {
t . comment ( null ) ;
} ) . toSQL ( ) ;
} )
. to . throw ( TypeError ) ;
} ) ;
2016-05-19 22:21:34 +02:00
it ( 'is possible to set raw statements in defaultTo, #146' , function ( ) {
tableSql = client . schemaBuilder ( ) . createTable ( 'default_raw_test' , function ( t ) {
t . timestamp ( 'created_at' ) . defaultTo ( client . raw ( 'CURRENT_TIMESTAMP' ) ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'create table "default_raw_test" ("created_at" timestamp with time zone default CURRENT_TIMESTAMP)' ) ;
} ) ;
it ( 'allows dropping a unique compound index with too long generated name' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'composite_key_test' , function ( t ) {
t . dropUnique ( [ 'column_a' , 'column_b' ] ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "composite_key_test" drop constraint "zYmMt0VQwlLZ20XnrMicXZ0ufZk"' ) ;
} ) ;
it ( 'allows dropping a unique compound index with specified name' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'composite_key_test' , function ( t ) {
t . dropUnique ( [ 'column_a' , 'column_b' ] , 'ckt_unique' ) ;
} ) . toSQL ( ) ;
equal ( 1 , tableSql . length ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "composite_key_test" drop constraint "ckt_unique"' ) ;
} ) ;
it ( '#1430 - .primary & .dropPrimary takes columns and constraintName' , function ( ) {
tableSql = client . schemaBuilder ( ) . table ( 'users' , function ( t ) {
t . primary ( [ 'test1' , 'test2' ] , 'testconstraintname' ) ;
} ) . toSQL ( ) ;
expect ( tableSql [ 0 ] . sql ) . to . equal ( 'alter table "users" add constraint "testconstraintname" primary key ("test1", "test2")' ) ;
tableSql = client . schemaBuilder ( ) . createTable ( 'users' , function ( t ) {
t . string ( 'test' ) . primary ( 'testconstraintname' ) ;
} ) . toSQL ( ) ;
expect ( tableSql [ 1 ] . sql ) . to . equal ( 'alter table "users" add constraint "testconstraintname" primary key ("test")' ) ;
} ) ;
} ) ;