From 510125b505045f9062435888261f5ed1fec3f5bd Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Nov 2023 11:59:28 +0200 Subject: [PATCH 1/5] - Added "generated"-related stuff on PP - Added hydration for generated columns --- .../ddlProvider/ddlProvider.js | 2 + .../field_level/fieldLevelConfig.json | 826 ++++++++++++++++++ 2 files changed, 828 insertions(+) diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index a89e126..88f4c46 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -670,6 +670,8 @@ module.exports = (baseProvider, options, app) => { timezone, intervalOptions, dbVersion, + generatedColumn: Boolean(jsonSchema.generatedColumn), + columnGenerationExpression: jsonSchema.columnGenerationExpression, }; }, diff --git a/properties_pane/field_level/fieldLevelConfig.json b/properties_pane/field_level/fieldLevelConfig.json index 6e7f56d..ea12cb6 100644 --- a/properties_pane/field_level/fieldLevelConfig.json +++ b/properties_pane/field_level/fieldLevelConfig.json @@ -189,6 +189,65 @@ making sure that you maintain a proper JSON format. ] } }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -732,6 +791,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -1213,6 +1331,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -1737,6 +1914,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -2188,6 +2424,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -2243,6 +2538,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -2719,6 +3073,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -3248,6 +3661,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -3309,6 +3781,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -3765,6 +4296,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -4242,6 +4832,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -4698,6 +5347,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -4792,6 +5500,65 @@ making sure that you maintain a proper JSON format. } ] }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", @@ -4844,6 +5611,65 @@ making sure that you maintain a proper JSON format. "propertyType": "details", "template": "textarea" }, + { + "propertyName": "Generated column", + "propertyKeyword": "generatedColumn", + "propertyTooltip": "A generated column is a special column that is always computed from other columns", + "propertyType": "checkbox", + "dependency": { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + }, + { + "propertyName": "Generation expression", + "propertyKeyword": "columnGenerationExpression", + "propertyTooltip": "An SQL statement for computing values for this column", + "propertyType": "details", + "template": "textarea", + "markdown": false, + "dependency": { + "type": "and", + "values": [ + { + "key": "generatedColumn", + "value": true + }, + { + "type": "not", + "values": { + "type": "or", + "values": [ + { + "level": "model", + "key": "dbVersion", + "value": "v10.x" + }, + { + "level": "model", + "key": "dbVersion", + "value": "v11.x" + } + ] + } + } + ] + } + }, { "propertyName": "Not null", "propertyKeyword": "required", From 81986b32fd06cf03959be5ed83b78acb7368dd1f Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Nov 2023 12:38:29 +0200 Subject: [PATCH 2/5] - Added FE for generated columns --- forward_engineering/ddlProvider/ddlProvider.js | 6 ++++++ forward_engineering/ddlProvider/templates.js | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 88f4c46..80e3a9b 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -258,11 +258,17 @@ module.exports = (baseProvider, options, app) => { const defaultValue = !_.isUndefined(columnDefinition.default) ? ' DEFAULT ' + decorateDefault(type, columnDefinition.default, isArrayType) : ''; + const generatedColumnClause = columnDefinition.generatedColumn && columnDefinition.columnGenerationExpression + ? assignTemplates(templates.generatedColumnClause, { + generationExpression: columnDefinition.columnGenerationExpression, + }) + : ''; return commentIfDeactivated( assignTemplates(templates.columnDefinition, { name: wrapInQuotes(columnDefinition.name), type: decorateType(type, columnDefinition), + generatedColumnClause, notNull, primaryKey, uniqueKey, diff --git a/forward_engineering/ddlProvider/templates.js b/forward_engineering/ddlProvider/templates.js index a3e933c..7b9495d 100644 --- a/forward_engineering/ddlProvider/templates.js +++ b/forward_engineering/ddlProvider/templates.js @@ -16,7 +16,9 @@ module.exports = { '${partitionOf} ${openParenthesis}${keyConstraints}${checkConstraints}${foreignKeyConstraints}\n' + '${closeParenthesis}${options};\n\n${comment}${columnDescriptions}', - columnDefinition: '${name} ${type}${collation}${primaryKey}${uniqueKey}${defaultValue}${notNull}', + generatedColumnClause: ' GENERATED ALWAYS AS (${generationExpression}) STORED', + + columnDefinition: '${name} ${type}${collation}${generatedColumnClause}${primaryKey}${uniqueKey}${defaultValue}${notNull}', checkConstraint: '${name} CHECK (${expression})${noInherit}', From d5df0bc0baa8a007874b8907eb38bd7a95445ff0 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Nov 2023 13:27:50 +0200 Subject: [PATCH 3/5] Added ability to modify generated columns --- .../alterScript/alterScriptFromDeltaHelper.js | 2 +- .../alterScriptHelpers/alterEntityHelper.js | 133 ++++++++++++------ 2 files changed, 94 insertions(+), 41 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index 7c33454..7a1f9e5 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -120,7 +120,7 @@ const getAlterCollectionsScriptDtos = ({ .filter(Boolean) .map(item => Object.values(item.properties)[0]) .filter(collection => !collection.compMod) - .flatMap(getModifyColumnScriptDtos(app)); + .flatMap(getModifyColumnScriptDtos({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions})); return [ ...createCollectionsScriptDtos, diff --git a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js index 87c3ae8..85b2a8b 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js @@ -89,53 +89,63 @@ const getModifyCollectionScriptDtos = (app) => (collection) => { ].filter(Boolean); } +/** + * @return {(collection: Object, predicate: ([name: string, jsonSchema: Object]) => boolean) => AlterScriptDto[]} + * */ +const getAddColumnsByConditionScriptDtos = ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => + (collection, predicate) => { + const _ = app.require('lodash'); + const {getEntityName, getNamePrefixedWithSchemaName} = require('../../utils/general')(_); + const {createColumnDefinitionBySchema} = require('./createColumnDefinition')(app); + const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app); + const {getDefinitionByReference} = app.require('@hackolade/ddl-fe-utils'); + + const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; + const tableName = getEntityName(collectionSchema); + const schemaName = collectionSchema.compMod?.keyspaceName; + const fullName = getNamePrefixedWithSchemaName(tableName, schemaName); + const schemaData = {schemaName, dbVersion}; + + return _.toPairs(collection.properties) + .filter(([name, jsonSchema]) => predicate([name, jsonSchema])) + .map(([name, jsonSchema]) => { + const definitionJsonSchema = getDefinitionByReference({ + propertySchema: jsonSchema, + modelDefinitions, + internalDefinitions, + externalDefinitions, + }); + + return createColumnDefinitionBySchema({ + name, + jsonSchema, + parentJsonSchema: collectionSchema, + ddlProvider, + schemaData, + definitionJsonSchema, + }); + }) + .map(ddlProvider.convertColumnDefinition) + .map(columnDefinition => ddlProvider.addColumn(fullName, columnDefinition)) + .map(addColumnScript => AlterScriptDto.getInstance([addColumnScript], true, false)) + .filter(Boolean); + }; + /** * @return {(collection: Object) => AlterScriptDto[]} * */ const getAddColumnScriptDtos = ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => collection => { - const _ = app.require('lodash'); - const {getEntityName, getNamePrefixedWithSchemaName} = require('../../utils/general')(_); - const {createColumnDefinitionBySchema} = require('./createColumnDefinition')(app); - const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app); - const {getDefinitionByReference} = app.require('@hackolade/ddl-fe-utils'); - - const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; - const tableName = getEntityName(collectionSchema); - const schemaName = collectionSchema.compMod?.keyspaceName; - const fullName = getNamePrefixedWithSchemaName(tableName, schemaName); - const schemaData = {schemaName, dbVersion}; - - return _.toPairs(collection.properties) - .filter(([name, jsonSchema]) => !jsonSchema.compMod) - .map(([name, jsonSchema]) => { - const definitionJsonSchema = getDefinitionByReference({ - propertySchema: jsonSchema, - modelDefinitions, - internalDefinitions, - externalDefinitions, - }); - - return createColumnDefinitionBySchema({ - name, - jsonSchema, - parentJsonSchema: collectionSchema, - ddlProvider, - schemaData, - definitionJsonSchema, - }); - }) - .map(ddlProvider.convertColumnDefinition) - .map(columnDefinition => ddlProvider.addColumn(fullName, columnDefinition)) - .map(addColumnScript => AlterScriptDto.getInstance([addColumnScript], true, false)) - .filter(Boolean); - }; + return getAddColumnsByConditionScriptDtos({ + app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions + })(collection, ([name, jsonSchema]) => !jsonSchema.compMod); + } /** - * @return {(collection: Object) => AlterScriptDto[]} + * @return {(collection: Object, predicate: ([name: string, jsonSchema: Object]) => boolean) => AlterScriptDto[]} * */ -const getDeleteColumnScriptDtos = app => collection => { +const getDeleteColumnsByConditionScriptDtos = app => (collection, predicate) => { const _ = app.require('lodash'); const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app); const {getEntityName, getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../utils/general')(_); @@ -146,7 +156,7 @@ const getDeleteColumnScriptDtos = app => collection => { const fullTableName = getNamePrefixedWithSchemaName(tableName, schemaName); return _.toPairs(collection.properties) - .filter(([name, jsonSchema]) => !jsonSchema.compMod) + .filter(([name, jsonSchema]) => predicate([name, jsonSchema])) .map(([name]) => { const columnNameForDDL = wrapInQuotes(name); return ddlProvider.dropColumn(fullTableName, columnNameForDDL) @@ -158,18 +168,61 @@ const getDeleteColumnScriptDtos = app => collection => { /** * @return {(collection: Object) => AlterScriptDto[]} * */ -const getModifyColumnScriptDtos = app => collection => { +const getDeleteColumnScriptDtos = app => collection => { + return getDeleteColumnsByConditionScriptDtos(app)(collection, ([name, jsonSchema]) => !jsonSchema.compMod) +}; + +const getModifyGeneratedColumnsScriptDtos = ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => + (collection) => { + const _ = app.require('lodash'); + + return _.toPairs(collection.properties) + .filter(([name, jsonSchema]) => { + const oldName = jsonSchema.compMod.oldField.name; + const oldProperty = collection.role.properties[oldName]; + + return oldProperty.generatedColumn !== jsonSchema.generatedColumn + || oldProperty.columnGenerationExpression !== jsonSchema.columnGenerationExpression; + }) + .flatMap(([name, jsonSchema]) => { + const collectionWithJustThisProperty = { + ...collection, + properties: _.fromPairs([ + [name, jsonSchema] + ]), + } + const deleteColumnsScriptDtos = getDeleteColumnsByConditionScriptDtos(app)(collectionWithJustThisProperty, () => true); + const addColumnsScriptDtos = getAddColumnsByConditionScriptDtos({ + app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions + })(collectionWithJustThisProperty, () => true); + + return [ + ...deleteColumnsScriptDtos, + ...addColumnsScriptDtos, + ] + }) + .filter(Boolean); +} + +/** + * @return {(collection: Object) => AlterScriptDto[]} + * */ +const getModifyColumnScriptDtos = ( + {app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions} +) => collection => { const _ = app.require('lodash'); const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app); const renameColumnScriptDtos = getRenameColumnScriptDtos(_, ddlProvider)(collection); const updateTypeScriptDtos = getUpdateTypesScriptDtos(_, ddlProvider)(collection); + const modifyGeneratedColumnsScriptDtos = getModifyGeneratedColumnsScriptDtos({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions})(collection); const modifyNotNullScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)(collection); const modifyCommentScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)(collection); return [ ...renameColumnScriptDtos, ...updateTypeScriptDtos, + ...modifyGeneratedColumnsScriptDtos, ...modifyNotNullScriptDtos, ...modifyCommentScriptDtos, ].filter(Boolean); From cfae34f4aed3f90f02f120aca62c211f20f75293 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Nov 2023 15:10:03 +0200 Subject: [PATCH 4/5] Fixed drop-and-recreate related problems --- .../alterScriptHelpers/alterEntityHelper.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js index 85b2a8b..0811c85 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js @@ -204,6 +204,15 @@ const getModifyGeneratedColumnsScriptDtos = ({app, dbVersion, modelDefinitions, .filter(Boolean); } +const getDropAndRecreateColumnsScriptDtos = ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => + (collection) => { + const modifyGeneratedColumnsScriptDtos = getModifyGeneratedColumnsScriptDtos({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions})(collection); + + return [ + ...modifyGeneratedColumnsScriptDtos, + ] + } + /** * @return {(collection: Object) => AlterScriptDto[]} * */ @@ -214,15 +223,22 @@ const getModifyColumnScriptDtos = ( const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app); const renameColumnScriptDtos = getRenameColumnScriptDtos(_, ddlProvider)(collection); + + const dropAndRecreateScriptDtos = getDropAndRecreateColumnsScriptDtos({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions})(collection); + if (dropAndRecreateScriptDtos.length) { + return [ + ...renameColumnScriptDtos, + ...dropAndRecreateScriptDtos, + ].filter(Boolean); + } + const updateTypeScriptDtos = getUpdateTypesScriptDtos(_, ddlProvider)(collection); - const modifyGeneratedColumnsScriptDtos = getModifyGeneratedColumnsScriptDtos({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions})(collection); const modifyNotNullScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)(collection); const modifyCommentScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)(collection); return [ ...renameColumnScriptDtos, ...updateTypeScriptDtos, - ...modifyGeneratedColumnsScriptDtos, ...modifyNotNullScriptDtos, ...modifyCommentScriptDtos, ].filter(Boolean); From cb2caa9b5c42193bff8302966600643787329049 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Nov 2023 15:13:28 +0200 Subject: [PATCH 5/5] Removed obsolete function --- .../alterScriptHelpers/alterEntityHelper.js | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js index 0811c85..fd85bdf 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js @@ -172,45 +172,41 @@ const getDeleteColumnScriptDtos = app => collection => { return getDeleteColumnsByConditionScriptDtos(app)(collection, ([name, jsonSchema]) => !jsonSchema.compMod) }; -const getModifyGeneratedColumnsScriptDtos = ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => - (collection) => { - const _ = app.require('lodash'); - - return _.toPairs(collection.properties) - .filter(([name, jsonSchema]) => { - const oldName = jsonSchema.compMod.oldField.name; - const oldProperty = collection.role.properties[oldName]; - - return oldProperty.generatedColumn !== jsonSchema.generatedColumn - || oldProperty.columnGenerationExpression !== jsonSchema.columnGenerationExpression; - }) - .flatMap(([name, jsonSchema]) => { - const collectionWithJustThisProperty = { - ...collection, - properties: _.fromPairs([ - [name, jsonSchema] - ]), - } - const deleteColumnsScriptDtos = getDeleteColumnsByConditionScriptDtos(app)(collectionWithJustThisProperty, () => true); - const addColumnsScriptDtos = getAddColumnsByConditionScriptDtos({ - app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions - })(collectionWithJustThisProperty, () => true); - - return [ - ...deleteColumnsScriptDtos, - ...addColumnsScriptDtos, - ] - }) - .filter(Boolean); -} - +/** + * @return {(collection: Object) => Array} + * */ const getDropAndRecreateColumnsScriptDtos = ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => (collection) => { - const modifyGeneratedColumnsScriptDtos = getModifyGeneratedColumnsScriptDtos({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions})(collection); + const _ = app.require('lodash'); - return [ - ...modifyGeneratedColumnsScriptDtos, - ] + return _.toPairs(collection.properties) + .filter(([name, jsonSchema]) => { + const oldName = jsonSchema.compMod.oldField.name; + const oldProperty = collection.role.properties[oldName]; + + const didGeneratedColumnChange = oldProperty.generatedColumn !== jsonSchema.generatedColumn + || oldProperty.columnGenerationExpression !== jsonSchema.columnGenerationExpression; + // all conditions that require drop-and-recreate go here + return didGeneratedColumnChange; + }) + .flatMap(([name, jsonSchema]) => { + const collectionWithJustThisProperty = { + ...collection, + properties: _.fromPairs([ + [name, jsonSchema] + ]), + } + const deleteColumnsScriptDtos = getDeleteColumnsByConditionScriptDtos(app)(collectionWithJustThisProperty, () => true); + const addColumnsScriptDtos = getAddColumnsByConditionScriptDtos({ + app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions + })(collectionWithJustThisProperty, () => true); + + return [ + ...deleteColumnsScriptDtos, + ...addColumnsScriptDtos, + ] + }) + .filter(Boolean); } /**