Skip to content

Commit 5b7c801

Browse files
hjamil-24WikiRik
andauthored
fix(oracle): fix changeColumn SQL for BLOB to avoid implicit conversion (#17719)
Co-authored-by: Rik Smale <[email protected]>
1 parent 5623e2d commit 5b7c801

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/dialects/oracle/query-generator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ export class OracleQueryGenerator extends AbstractQueryGenerator {
504504
* @param {string} attributeName The name of the attribute which would get altered
505505
*/
506506
_modifyQuery(definition, table, attributeName) {
507+
definition = definition.startsWith('BLOB') ? definition.replace('BLOB ', '') : definition;
507508
const query = Utils.joinSQLFragments([
508509
'ALTER TABLE',
509510
this.quoteTable(table),

test/unit/dialects/oracle/query-generator.test.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const Support = require('../../support'),
55
DataTypes = require('sequelize/lib/data-types'),
66
expectsql = Support.expectsql,
77
current = Support.sequelize,
8-
sql = current.dialect.queryGenerator;
8+
sql = current.dialect.queryGenerator,
9+
sinon = require('sinon');
910

1011
if (current.dialect.name === 'oracle') {
1112
describe('createTable', () => {
@@ -28,4 +29,36 @@ if (current.dialect.name === 'oracle') {
2829
});
2930
});
3031
});
31-
}
32+
33+
// Alter table for blob tries implicit type conversion if datatype is mentioned in SQL, which leads to failure.
34+
describe('changeColumn', () => {
35+
const Model = current.define('items', {
36+
blobCol: {
37+
type: DataTypes.BLOB
38+
}
39+
}, { timestamps: false });
40+
41+
before(function() {
42+
this.stub = sinon.stub(current, 'query').resolvesArg(0);
43+
});
44+
45+
beforeEach(function() {
46+
this.stub.resetHistory();
47+
});
48+
49+
after(function() {
50+
this.stub.restore();
51+
});
52+
53+
it('properly generate alter queries for BLOB', () => {
54+
return current.getQueryInterface().changeColumn(Model.getTableName(), 'blobCol', {
55+
type: DataTypes.BLOB,
56+
allowNull: false
57+
}).then(sql => {
58+
expectsql(sql, {
59+
oracle: 'DECLARE CONS_NAME VARCHAR2(200); BEGIN BEGIN EXECUTE IMMEDIATE \'ALTER TABLE "items" MODIFY "blobCol" NOT NULL\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE = -1442 OR SQLCODE = -1451 THEN EXECUTE IMMEDIATE \'ALTER TABLE "items" MODIFY "blobCol" \'; ELSE RAISE; END IF; END; END;'
60+
});
61+
});
62+
});
63+
});
64+
}

0 commit comments

Comments
 (0)