Skip to content

Commit

Permalink
fix(oracle): add missing default and not null condition to addColumn (#…
Browse files Browse the repository at this point in the history
…16619)

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
hjamil-24 and WikiRik authored Nov 3, 2023
1 parent b284d37 commit b204b5f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/dialects/oracle/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,11 +854,20 @@ export class OracleQueryGenerator extends AbstractQueryGenerator {

let template;

template = attribute.type.toSql ? attribute.type.toSql() : '';
if (attribute.type instanceof DataTypes.JSON) {
template += ` CHECK (${this.quoteIdentifier(options.attributeName)} IS JSON)`;
return template;
}
if (Utils.defaultValueSchemable(attribute.defaultValue)) {
template += ` DEFAULT ${this.escape(attribute.defaultValue)}`;
}
if (attribute.allowNull === false) {
template += ' NOT NULL';
}
if (attribute.type instanceof DataTypes.ENUM) {
if (attribute.type.values && !attribute.values) attribute.values = attribute.type.values;

// enums are a special case
template = attribute.type.toSql();
template +=
` CHECK (${this.quoteIdentifier(options.attributeName)} IN(${
_.map(attribute.values, value => {
Expand All @@ -867,13 +876,7 @@ export class OracleQueryGenerator extends AbstractQueryGenerator {
}))`;
return template;
}
if (attribute.type instanceof DataTypes.JSON) {
template = attribute.type.toSql();
template += ` CHECK (${this.quoteIdentifier(options.attributeName)} IS JSON)`;
return template;
}
if (attribute.type instanceof DataTypes.BOOLEAN) {
template = attribute.type.toSql();
template +=
` CHECK (${this.quoteIdentifier(options.attributeName)} IN('1', '0'))`;
return template;
Expand Down
34 changes: 34 additions & 0 deletions test/unit/sql/add-column.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
}

it('DEFAULT VALUE FOR BOOLEAN', () => {
return expectsql(sql.addColumnQuery(User.getTableName(), 'bool_col', current.normalizeAttribute({
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
})), {
oracle: 'ALTER TABLE "Users" ADD "bool_col" CHAR(1) DEFAULT 1 NOT NULL CHECK ("bool_col" IN(\'1\', \'0\'))',
mysql: 'ALTER TABLE `Users` ADD `bool_col` TINYINT(1) NOT NULL DEFAULT true;',
mariadb: 'ALTER TABLE `Users` ADD `bool_col` TINYINT(1) NOT NULL DEFAULT true;',
mssql: 'ALTER TABLE [Users] ADD [bool_col] BIT NOT NULL DEFAULT 1;',
postgres: 'ALTER TABLE "public"."Users" ADD COLUMN "bool_col" BOOLEAN NOT NULL DEFAULT true;',
db2: 'ALTER TABLE "Users" ADD "bool_col" BOOLEAN NOT NULL DEFAULT true;',
snowflake: 'ALTER TABLE "Users" ADD "bool_col" BOOLEAN NOT NULL DEFAULT true;',
sqlite: 'ALTER TABLE `Users` ADD `bool_col` TINYINT(1) NOT NULL DEFAULT 1;'
});
});

it('DEFAULT VALUE FOR ENUM', () => {
return expectsql(sql.addColumnQuery(User.getTableName(), 'enum_col', current.normalizeAttribute({
type: DataTypes.ENUM('happy', 'sad'),
allowNull: false,
defaultValue: 'happy'
})), {
oracle: 'ALTER TABLE "Users" ADD "enum_col" VARCHAR2(512) DEFAULT \'happy\' NOT NULL CHECK ("enum_col" IN(\'happy\', \'sad\'))',
mysql: 'ALTER TABLE `Users` ADD `enum_col` ENUM(\'happy\', \'sad\') NOT NULL DEFAULT \'happy\';',
mariadb: 'ALTER TABLE `Users` ADD `enum_col` ENUM(\'happy\', \'sad\') NOT NULL DEFAULT \'happy\';',
mssql: 'ALTER TABLE [Users] ADD [enum_col] VARCHAR(255) CHECK ([enum_col] IN(N\'happy\', N\'sad\'));',
postgres: 'DO \'BEGIN CREATE TYPE "public"."enum_Users_enum_col" AS ENUM(\'\'happy\'\', \'\'sad\'\'); EXCEPTION WHEN duplicate_object THEN null; END\';ALTER TABLE "public"."Users" ADD COLUMN "enum_col" "public"."enum_Users_enum_col" NOT NULL DEFAULT \'happy\';',
db2: 'ALTER TABLE "Users" ADD "enum_col" VARCHAR(255) CHECK ("enum_col" IN(\'happy\', \'sad\')) NOT NULL DEFAULT \'happy\';',
snowflake: 'ALTER TABLE "Users" ADD "enum_col" ENUM NOT NULL DEFAULT \'happy\';',
sqlite: 'ALTER TABLE `Users` ADD `enum_col` TEXT NOT NULL DEFAULT \'happy\';'
});
});

it('defaults the schema to the one set in the Sequelize options', () => {
return expectsql(customSql.addColumnQuery(User.getTableName(), 'level_id', customSequelize.normalizeAttribute({
type: DataTypes.FLOAT,
Expand Down

0 comments on commit b204b5f

Please sign in to comment.