oracle - fixed create if not exists

This commit is contained in:
Vincent Schoettke 2014-08-16 14:20:44 +02:00
parent f14e894748
commit 9a0e7abdaa
2 changed files with 15 additions and 2 deletions

View File

@ -42,9 +42,10 @@ TableCompiler_Oracle.prototype.compileAdd = function(builder) {
// Adds the "create" query to the query sequence.
TableCompiler_Oracle.prototype.createQuery = function(columns, ifNot) {
var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
var sql = 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')';
this.pushQuery({
sql: createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')',
// catch "name is already used by an existing object" for workaround for "if not exists"
sql: ifNot ? utils.wrapSqlWithCatch(sql, -955) : sql,
bindings: columns.bindings
});
if (this.single.comment) this.comment(this.single.comment);

View File

@ -27,6 +27,18 @@ module.exports = function(client) {
expect(tableSql.toSQL()[2].sql).to.equal("create or replace trigger \"users_id_trg\" before insert on \"users\" for each row when (new.\"id\" is null) begin select \"users_seq\".nextval into :new.\"id\" from dual; end;");
});
it('test basic create table if not exists', function() {
tableSql = new SchemaBuilder().createTableIfNotExists('users', function(table) {
table.increments('id');
table.string('email');
});
equal(3, tableSql.toSQL().length);
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("begin execute immediate 'create sequence \"users_seq\"'; exception when others then if sqlcode != -955 then raise; end if; end;");
expect(tableSql.toSQL()[2].sql).to.equal("create or replace trigger \"users_id_trg\" before insert on \"users\" for each row when (new.\"id\" is null) begin select \"users_seq\".nextval into :new.\"id\" from dual; end;");
});
it('test drop table', function() {
tableSql = new SchemaBuilder().dropTable('users').toSQL();