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;");
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;");
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;");
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;");
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;');
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;');
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;');
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;');