Fix autoincrement for Oracle with schema (#4594)

This commit is contained in:
Igor Savin 2021-07-29 17:07:22 +03:00 committed by GitHub
parent b71f0afe91
commit 124107e0ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 10 deletions

View File

@ -5,9 +5,11 @@ function createAutoIncrementTriggerAndSequence(columnCompiler) {
// TODO Add warning that sequence etc is created
columnCompiler.pushAdditional(function () {
const tableName = this.tableCompiler.tableNameRaw;
const schemaName = this.tableCompiler.schemaNameRaw;
const createTriggerSQL = Trigger.createAutoIncrementTrigger(
this.client.logger,
tableName
tableName,
schemaName
);
this.pushQuery(createTriggerSQL);
});

View File

@ -43,34 +43,43 @@ const trigger = {
);
},
createAutoIncrementTrigger: function (logger, tableName) {
createAutoIncrementTrigger: function (logger, tableName, schemaName) {
const tableQuoted = `"${tableName}"`;
const tableUnquoted = tableName;
const schemaQuoted = schemaName ? `"${schemaName}".` : '';
const constraintOwner = schemaName ? `'${schemaName}'` : 'cols.owner';
const triggerName = utils.generateCombinedName(
logger,
'autoinc_trg',
tableName
);
const sequenceName = utils.generateCombinedName(logger, 'seq', tableName);
const sequenceNameUnquoted = utils.generateCombinedName(
logger,
'seq',
tableName
);
const sequenceNameQuoted = `"${sequenceNameUnquoted}"`;
return (
`DECLARE ` +
`PK_NAME VARCHAR(200); ` +
`BEGIN` +
` EXECUTE IMMEDIATE ('CREATE SEQUENCE "${sequenceName}"');` +
` EXECUTE IMMEDIATE ('CREATE SEQUENCE ${schemaQuoted}${sequenceNameQuoted}');` +
` 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 = '${tableName}';` +
` execute immediate ('create or replace trigger "${triggerName}"` +
` BEFORE INSERT on "${tableName}"` +
` AND cons.owner = ${constraintOwner}` +
` AND cols.table_name = '${tableUnquoted}';` +
` execute immediate ('create or replace trigger ${schemaQuoted}"${triggerName}"` +
` BEFORE INSERT on ${schemaQuoted}${tableQuoted}` +
` for each row` +
` declare` +
` checking number := 1;` +
` begin` +
` if (:new."' || PK_NAME || '" is null) then` +
` while checking >= 1 loop` +
` select "${sequenceName}".nextval into :new."' || PK_NAME || '" from dual;` +
` select count("' || PK_NAME || '") into checking from "${tableName}"` +
` select ${schemaQuoted}${sequenceNameQuoted}.nextval into :new."' || PK_NAME || '" from dual;` +
` select count("' || PK_NAME || '") into checking from ${schemaQuoted}${tableQuoted}` +
` where "' || PK_NAME || '" = :new."' || PK_NAME || '";` +
` end loop;` +
` end if;` +

View File

@ -10,6 +10,23 @@ describe('Oracle SchemaBuilder', function () {
let tableSql;
const equal = require('assert').equal;
it('test increments supports withSchema', function () {
tableSql = client
.schemaBuilder()
.withSchema('scm')
.createTable('users', function (table) {
table.increments('id');
table.string('email');
});
equal(2, tableSql.toSQL().length);
expect(tableSql.toSQL()[0].sql).to.equal(
'create table "scm"."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 "scm"."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 = \'scm\' AND cols.table_name = \'users\'; execute immediate (\'create or replace trigger "scm"."users_autoinc_trg" BEFORE INSERT on "scm"."users" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "scm"."users_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "scm"."users" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'); END;'
);
});
it('test basic create table with charset and collate', function () {
tableSql = client.schemaBuilder().createTable('users', function (table) {
table.increments('id');