Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ const getModifyNonNullColumnsScriptDtos = ({ scriptFormat, collection }) => {
const scripts = [];

if (isOldRequired && (!isNewRequired || isNameChanged)) {
const template = oldConstraintName ? templates.dropConstraint : templates.alterNullableConstraint;
scripts.push(
AlterScriptDto.getInstance(
assignTemplates(template, { ...scriptParams, constraintName: prepareName(oldConstraintName) }),
assignTemplates(templates.alterNullableConstraint, scriptParams),
true,
Boolean(templates.dropConstraint),
false,
SCRIPT_TYPE.alterEntity,
),
);
Expand All @@ -74,33 +73,12 @@ const getModifyNonNullColumnsScriptDtos = ({ scriptFormat, collection }) => {
return addNotNullConstraintsScript;
};

const createNotNullConstraintScript = ({ scriptFormat, constraintName, columnName }) => {
const prepareName = prepareNameForScriptFormat(scriptFormat);
return assignTemplates(templates.notNullConstraint, {
constraintName: prepareName(constraintName),
columnName: prepareName(columnName),
});
};

/**
* Get named NOT NULL constraints data
* @param {object} jsonSchema
* @param {string} scriptFormat
* Column-level named NOT NULL constraints are emitted inline on the column definition.
* @returns {Array<{ statement: string, isActivated: boolean }>}
*/
const getNotNullConstraints = (jsonSchema, scriptFormat) => {
return _.toPairs(jsonSchema.properties)
.filter(
([name, columnSchema]) => jsonSchema.required?.includes(name) && columnSchema.notNullConstraintName?.trim(),
)
.map(([name, columnSchema]) => ({
statement: createNotNullConstraintScript({
scriptFormat,
constraintName: columnSchema.notNullConstraintName,
columnName: name,
}),
isActivated: columnSchema.isActivated,
}));
const getNotNullConstraints = () => {
return [];
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,34 @@ module.exports = ({ assignTemplates, templates, commentIfDeactivated, wrapCommen
.value();
};

const getColumnConstraints = ({ nullable, unique, primaryKey, primaryKeyOptions, uniqueKeyOptions }) => {
const getNotNullString = ({ nullable, primaryKey, notNullConstraintName }) => {
if (nullable || primaryKey) {
return '';
}

if (notNullConstraintName?.trim()) {
const { constraintString } = getOptionsString({ constraintName: notNullConstraintName });
return ` ${constraintString}NOT NULL`;
}

return ' NOT NULL';
};

const getColumnConstraints = ({
nullable,
unique,
primaryKey,
primaryKeyOptions,
uniqueKeyOptions,
notNullConstraintName,
}) => {
const { constraintString, statement } = getOptionsString(
getOptions({ primaryKey, unique, primaryKeyOptions, uniqueKeyOptions }),
);
const primaryKeyString = primaryKey ? ` PRIMARY KEY` : '';
const uniqueKeyString = unique ? ` UNIQUE` : '';
const nullableString = nullable || primaryKey ? '' : ' NOT NULL';
return `${nullableString}${constraintString}${primaryKeyString}${uniqueKeyString}${statement}`;
const notNullString = getNotNullString({ nullable, primaryKey, notNullConstraintName });
return `${notNullString}${constraintString}${primaryKeyString}${uniqueKeyString}${statement}`;
};

const getOptions = ({ primaryKey, unique, primaryKeyOptions, uniqueKeyOptions }) => {
Expand Down
17 changes: 9 additions & 8 deletions forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ const {
wrapComment,
getColumnsList,
prepareNameForScriptFormat,
toArray,
} = require('../utils/general');
const { getAnnotationsString } = require('../utils/getAnnotationsString');
const { assignTemplates } = require('../utils/assignTemplates');
const { decorateType } = require('./ddlHelpers/columnDefinitionHelpers/decorateType');
const { getNotNullConstraints } = require('../alterScript/alterScriptHelpers/columnHelpers/nonNullConstraintHelper');
const keyHelper = require('./ddlHelpers/keyHelper')(clean);

/**
* @param dbVersion {string} DB version in "21ai" format
Expand All @@ -38,15 +40,11 @@ const shouldUseTryCatchIfNotExistsWrapper = dbVersion => {
return dbVersionAsNumber < DbVersion.IF_NOT_EXISTS_SINCE;
};

module.exports = (baseProvider, options, app) => {
const toArray = val => (_.isArray(val) ? val : [val]);

const ddlProvider = (baseProvider, options, app) => {
const scriptFormat = options?.targetScriptOptions?.keyword;
const prepareName = prepareNameForScriptFormat(scriptFormat);
const getNamePrefixedWithSchemaName = getNamePrefixedWithSchemaNameForScriptFormat(scriptFormat);

const keyHelper = require('./ddlHelpers/keyHelper')(clean);

const { getColumnComments, getColumnConstraints, replaceTypeByVersion, getColumnDefault, getColumnEncrypt } =
require('./ddlHelpers/columnDefinitionHelper.js')({
wrap,
Expand Down Expand Up @@ -193,7 +191,8 @@ module.exports = (baseProvider, options, app) => {
primaryKeyOptions: jsonSchema.primaryKeyOptions,
unique: keyHelper.isInlineUnique(jsonSchema),
uniqueKeyOptions: jsonSchema.uniqueKeyOptions,
nullable: columnDefinition.nullable || Boolean(jsonSchema.notNullConstraintName?.trim()),
nullable: columnDefinition.nullable,
notNullConstraintName: jsonSchema.notNullConstraintName,
default: columnDefinition.default,
comment: jsonSchema.refDescription || jsonSchema.description || definitionJsonSchema.description,
isActivated: columnDefinition.isActivated,
Expand Down Expand Up @@ -354,7 +353,7 @@ module.exports = (baseProvider, options, app) => {

/**
* @param tableName {string}
* @param fkConstraintName {string}
* @param constraintName {string}
* @return string
* */
dropForeignKey(tableName, constraintName) {
Expand All @@ -375,7 +374,7 @@ module.exports = (baseProvider, options, app) => {
keyConstraints: keyHelper.getTableKeyConstraints(jsonSchema),
notNullConstraints: getNotNullConstraints(jsonSchema, scriptFormat),
selectStatement: _.trim(detailsTab.selectStatement),
partitioning: _.assign({}, partitioning, { compositePartitionKey }),
partitioning: { ...partitioning, compositePartitionKey },
..._.pick(
detailsTab,
'blockchain_table_clauses',
Expand Down Expand Up @@ -792,3 +791,5 @@ module.exports = (baseProvider, options, app) => {
},
};
};

module.exports = ddlProvider;
3 changes: 1 addition & 2 deletions forward_engineering/ddlProvider/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ module.exports = {

alterNotNullConstraint: 'ALTER TABLE ${tableName} MODIFY ${columnName} NOT NULL;',

alterNamedNotNullConstraint:
'ALTER TABLE ${tableName} ADD CONSTRAINT ${constraintName} CHECK (${columnName} IS NOT NULL);',
alterNamedNotNullConstraint: 'ALTER TABLE ${tableName} MODIFY ${columnName} CONSTRAINT ${constraintName} NOT NULL;',

alterNullableConstraint: 'ALTER TABLE ${tableName} MODIFY ${columnName} NULL;',

Expand Down
3 changes: 3 additions & 0 deletions forward_engineering/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ const getId = entity => entity.id || entity.role.id;

const normalizeLineEndings = str => str.replaceAll('\r\n', '\n');

const toArray = val => (Array.isArray(val) ? val : [val]);

module.exports = {
getDbName,
getBucketName,
Expand Down Expand Up @@ -268,4 +270,5 @@ module.exports = {
isParentContainerActivated,
getId,
normalizeLineEndings,
toArray,
};
Loading