Skip to content

Commit

Permalink
fix(oracle): clean constraints (#16694)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Bardasano <cbardasano@alaudaingenieria.es>
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 3, 2023
1 parent b204b5f commit 6c03176
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/dialects/oracle/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ export class OracleQueryGenerator extends AbstractQueryGenerator {
addConstraintQuery(tableName, options) {
options = options || {};

if (options.onUpdate) {
// Oracle does not support ON UPDATE, remove it.
delete options.onUpdate;
}

if (options.onDelete && options.onDelete.toUpperCase() === 'NO ACTION') {
// 'ON DELETE NO ACTION' is the default option in Oracle, but it is not supported if defined
delete options.onDelete;
}

const constraintSnippet = this.getConstraintSnippet(tableName, options);

tableName = this.quoteTable(tableName);
Expand Down
27 changes: 21 additions & 6 deletions test/unit/sql/add-constraint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ if (current.dialect.supports.constraints.addConstraint) {
});
});

// The Oracle dialect doesn't support onUpdate cascade
(current.dialect.name !== 'oracle' ? it : it.skip)('supports composite keys', () => {
it('supports composite keys', () => {
expectsql(
sql.addConstraintQuery('myTable', {
type: 'foreign key',
Expand All @@ -171,13 +170,14 @@ if (current.dialect.supports.constraints.addConstraint) {
}),
{
db2: 'ALTER TABLE "myTable" ADD CONSTRAINT "myTable_myColumn_anotherColumn_myOtherTable_fk" FOREIGN KEY ("myColumn", "anotherColumn") REFERENCES "myOtherTable" ("id1", "id2") ON DELETE CASCADE;',
oracle: 'ALTER TABLE "myTable" ADD CONSTRAINT "myTable_myColumn_anotherColumn_myOtherTable_fk" FOREIGN KEY ("myColumn", "anotherColumn") REFERENCES "myOtherTable" ("id1", "id2") ON DELETE CASCADE;',
default:
'ALTER TABLE [myTable] ADD CONSTRAINT [myTable_myColumn_anotherColumn_myOtherTable_fk] FOREIGN KEY ([myColumn], [anotherColumn]) REFERENCES [myOtherTable] ([id1], [id2]) ON UPDATE CASCADE ON DELETE CASCADE;'
}
);
});
// The Oracle dialect doesn't support onUpdate cascade
(current.dialect.name !== 'oracle' ? it : it.skip)('uses onDelete, onUpdate', () => {

it('uses onDelete, onUpdate', () => {
expectsql(sql.addConstraintQuery('myTable', {
type: 'foreign key',
fields: ['myColumn'],
Expand All @@ -189,18 +189,33 @@ if (current.dialect.supports.constraints.addConstraint) {
onDelete: 'cascade'
}), {
db2: 'ALTER TABLE "myTable" ADD CONSTRAINT "myTable_myColumn_myOtherTable_fk" FOREIGN KEY ("myColumn") REFERENCES "myOtherTable" ("id") ON DELETE CASCADE;',
oracle: 'ALTER TABLE "myTable" ADD CONSTRAINT "myTable_myColumn_myOtherTable_fk" FOREIGN KEY ("myColumn") REFERENCES "myOtherTable" ("id") ON DELETE CASCADE;',
default: 'ALTER TABLE [myTable] ADD CONSTRAINT [myTable_myColumn_myOtherTable_fk] FOREIGN KEY ([myColumn]) REFERENCES [myOtherTable] ([id]) ON UPDATE CASCADE ON DELETE CASCADE;'
});
});

it('uses onDelete: \'no action\'', () => {
expectsql(sql.addConstraintQuery('myTable', {
type: 'foreign key',
fields: ['myColumn'],
references: {
table: 'myOtherTable',
field: 'id'
},
onUpdate: 'cascade',
onDelete: 'no action'
}), {
oracle: 'ALTER TABLE "myTable" ADD CONSTRAINT "myTable_myColumn_myOtherTable_fk" FOREIGN KEY ("myColumn") REFERENCES "myOtherTable" ("id");',
default: 'ALTER TABLE [myTable] ADD CONSTRAINT [myTable_myColumn_myOtherTable_fk] FOREIGN KEY ([myColumn]) REFERENCES [myOtherTable] ([id]) ON UPDATE CASCADE ON DELETE NO ACTION;'
});
});

it('errors if references object is not passed', () => {
expect(sql.addConstraintQuery.bind(sql, 'myTable', {
type: 'foreign key',
fields: ['myColumn']
})).to.throw('references object with table and field must be specified');
});


});

describe('validation', () => {
Expand Down

0 comments on commit 6c03176

Please sign in to comment.