diff --git a/forward_engineering/configs/templates.js b/forward_engineering/configs/templates.js index edff7e32..31532327 100644 --- a/forward_engineering/configs/templates.js +++ b/forward_engineering/configs/templates.js @@ -23,7 +23,7 @@ module.exports = { spatialIndex: 'CREATE SPATIAL INDEX ${name} ON ${table} (${column})${using}\n${options}${terminator}\n', - checkConstraint: 'CONSTRAINT [${name}] CHECK${notForReplication} (${expression})', + checkConstraint: 'CONSTRAINT [${name}] ${check}${notForReplication} (${expression})', createForeignKeyConstraint: 'CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey}) ${onDelete}${onUpdate}', diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index 6b303860..15720a7f 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -258,6 +258,7 @@ module.exports = (baseProvider, options, app) => { createCheckConstraint(checkConstraint) { return assignTemplates(templates.checkConstraint, { name: checkConstraint.name, + check: checkConstraint.check ? 'CHECK' : 'NO CHECK', notForReplication: checkConstraint.enforceForReplication ? '' : ' NOT FOR REPLICATION', expression: _.trim(checkConstraint.expression).replace(/^\(([\s\S]*)\)$/, '$1'), terminator, @@ -533,6 +534,7 @@ module.exports = (baseProvider, options, app) => { hydrateCheckConstraint(checkConstraint) { return { name: checkConstraint.chkConstrName, + check: checkConstraint.constrCheck, expression: checkConstraint.constrExpression, existingData: checkConstraint.constrCheck, enforceForUpserts: checkConstraint.constrEnforceUpserts, diff --git a/forward_engineering/helpers/alterScriptHelpers/columnHelpers/defaultValueColumnHelper.js b/forward_engineering/helpers/alterScriptHelpers/columnHelpers/defaultValueColumnHelper.js index 09a87aea..ab473a37 100644 --- a/forward_engineering/helpers/alterScriptHelpers/columnHelpers/defaultValueColumnHelper.js +++ b/forward_engineering/helpers/alterScriptHelpers/columnHelpers/defaultValueColumnHelper.js @@ -1,6 +1,8 @@ module.exports = (app, ddlProvider) => { + const _ = app.require('lodash'); const { AlterScriptDto } = require('../types/AlterScriptDto'); - const { sanitizeConstraintName } = require('../../../helpers/general')(app); + const { sanitizeConstraintName, hasType, getTableName } = require('../../../helpers/general')(app); + const { decorateDefault } = require('../../columnDefinitionHelper')(app); const getDefaultValueChangeDto = (collection, fullName) => { const scripts = []; @@ -8,6 +10,10 @@ module.exports = (app, ddlProvider) => { const getDefaultConstraintName = columnName => sanitizeConstraintName(`DF_${fullName}_${columnName}`); Object.entries(collection?.properties ?? []).forEach(([columnName, collectionSchema]) => { + const type = hasType(collectionSchema.type) + ? _.toUpper(collectionSchema.type) + : getTableName(collectionSchema.type, collectionSchema.schemaName); + const newDefaultValue = collectionSchema.default; const newConstraintName = collectionSchema.defaultConstraintName; const oldDefaultValue = collection.role.properties[columnName]?.default; @@ -24,6 +30,7 @@ module.exports = (app, ddlProvider) => { !!oldConstraintName && !!newConstraintName && oldConstraintName !== newConstraintName; + const decoratedValue = decorateDefault(type, newDefaultValue); switch (true) { case defaultValueWasRemoved: { @@ -40,7 +47,7 @@ module.exports = (app, ddlProvider) => { { constraintName, columnName, - value: newDefaultValue, + value: decoratedValue, }, fullName, ); @@ -58,7 +65,7 @@ module.exports = (app, ddlProvider) => { { constraintName, columnName, - value: newDefaultValue, + value: decoratedValue, }, fullName, ); @@ -72,7 +79,7 @@ module.exports = (app, ddlProvider) => { { constraintName: newConstraintName, columnName, - value: newDefaultValue, + value: decoratedValue, }, fullName, ); diff --git a/forward_engineering/helpers/columnDefinitionHelper.js b/forward_engineering/helpers/columnDefinitionHelper.js index 0eefc7d4..3d170bdc 100644 --- a/forward_engineering/helpers/columnDefinitionHelper.js +++ b/forward_engineering/helpers/columnDefinitionHelper.js @@ -63,6 +63,11 @@ module.exports = app => { const isString = type => ['CHAR', 'VARCHAR', 'NCHAR', 'NVARCHAR', 'TEXT', 'NTEXT'].includes(_.toUpper(type)); + /** + * Escape only inner single quotes. + * @param {string} str + * @returns {string} + */ const escapeQuotes = str => _.trim(str).replace(/(\')+/g, "'$1"); const decorateDefault = (type, defaultValue) => {