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
4 changes: 2 additions & 2 deletions forward_engineering/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@
},
"separate": {
"default": false,
"disabled": false,
"disabledLabel": ""
"disabled": true,
"disabledLabel": "N/A"
},
"ignore": {
"default": false,
Expand Down
6 changes: 4 additions & 2 deletions forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ module.exports = {

createKeyConstraint: '${constraintName}${keyType}${clustered}${columns}${options}${partition}',

createDefaultConstraint:
columnDefaultConstraint: 'CONSTRAINT [${constraintName}] DEFAULT (${default})',

alterDefaultConstraint:
'ALTER TABLE ${tableName} ADD CONSTRAINT [${constraintName}] DEFAULT (${default}) FOR [${columnName}]${terminator}\n',

ifNotExistSchema:
Expand Down Expand Up @@ -63,7 +65,7 @@ module.exports = {

addColumn: 'ADD ${script}',

alterColumn: 'ALTER COLUMN [${name}] ${type}${collation}${not_null}',
alterColumn: 'ALTER COLUMN [${name}] ${type}${collation}',

renameColumn: "EXEC sp_rename '${fullTableName}.${oldColumnName}', '${newColumnName}', 'COLUMN';${terminator}",

Expand Down
47 changes: 31 additions & 16 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,16 @@ const provider = (baseProvider, options, app) => {
const tableName = getTableName(setPersistenceSpecificName(persistence, name), schemaData.schemaName);

const dividedKeysConstraints = divideIntoActivatedAndDeactivated(
keyConstraints.map(createKeyConstraint(templates, tableTerminator, isActivated)),
keyConstraints.map(
createKeyConstraint({ terminator: tableTerminator, isParentActivated: isActivated }),
),
key => key.statement,
);

const keyConstraintsString = generateConstraintsString(dividedKeysConstraints, isActivated);

const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' });

const tableStatement = assignTemplates(templates.createTable, {
name: tableName,
external: persistence === 'external' ? ' EXTERNAL' : '',
Expand All @@ -122,11 +127,8 @@ const provider = (baseProvider, options, app) => {
: '',
terminator: tableTerminator,
});
const defaultConstraintsStatements = defaultConstraints
.map(data => createDefaultConstraint(templates, tableTerminator)(data, tableName))
.join('\n');

const fullTableStatement = [tableStatement, defaultConstraintsStatements].filter(Boolean).join('\n\n');
const fullTableStatement = [tableStatement].filter(Boolean).join('\n\n');

return ifNotExist
? wrapIfNotExistTable({
Expand All @@ -144,9 +146,19 @@ const provider = (baseProvider, options, app) => {
: getTableName(columnDefinition.type, columnDefinition.schemaName);
const notNull = columnDefinition.nullable ? '' : ' NOT NULL';
const primaryKey = columnDefinition.primaryKey ? ' PRIMARY KEY NONCLUSTERED NOT ENFORCED' : '';
const defaultValue = _.isUndefined(columnDefinition.default)
? ''
: ' DEFAULT ' + decorateDefault(type, columnDefinition.default);

const isInline = options?.scriptGenerationOptions?.feActiveOptions?.columnDefaultValues === 'inline';

let defaultValue;

if (isInline) {
if (!_.isUndefined(columnDefinition.default)) {
defaultValue = ' DEFAULT ' + decorateDefault(type, columnDefinition.default);
} else if (columnDefinition.defaultConstraint.name) {
defaultValue = ` ${createDefaultConstraint({ constraint: columnDefinition.defaultConstraint })}`;
}
}

const sparse = columnDefinition.sparse ? ' SPARSE' : '';
const maskedWithFunction = columnDefinition.maskedWithFunction
? ` MASKED WITH (FUNCTION='${columnDefinition.maskedWithFunction}')`
Expand Down Expand Up @@ -298,7 +310,7 @@ const provider = (baseProvider, options, app) => {

return {
...columnDefinition,
default: jsonSchema.defaultConstraintName ? '' : columnDefinition.default,
default: jsonSchema.defaultConstraintName ? undefined : columnDefinition.default,
defaultConstraint: {
name: jsonSchema.defaultConstraintName,
value: columnDefinition.default,
Expand Down Expand Up @@ -493,24 +505,18 @@ const provider = (baseProvider, options, app) => {
});
},

alterColumn({ fullTableName, columnDefinition, alterType = true, alterNullable = true }) {
alterColumn({ fullTableName, columnDefinition, alterType = true }) {
let type = '';
let notNull = '';

if (alterType) {
type = hasType(columnDefinition.type)
? _.toUpper(columnDefinition.type)
: getTableName(columnDefinition.type, columnDefinition.schemaName);
}

if (alterNullable) {
notNull = columnDefinition.nullable ? 'NULL' : 'NOT NULL';
}

const command = assignTemplates(templates.alterColumn, {
name: columnDefinition.name,
type,
not_null: type ? ` ${notNull}` : notNull,
});

return assignTemplates(templates.alterTable, {
Expand All @@ -520,6 +526,15 @@ const provider = (baseProvider, options, app) => {
});
},

alterColumnDefault({ fullTableName, constraint, columnName }) {
return assignTemplates(templates.alterDefaultConstraint, {
tableName: fullTableName,
constraintName: constraint.name,
default: constraint.value,
columnName,
});
},

dropView(fullViewName) {
return assignTemplates(templates.dropView, {
name: fullViewName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash');
const { getTableName } = require('../general');
const { getEntityName } = require('../../utils/general');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition');
const { checkFieldPropertiesChanged, modifyGroupItems, setIndexKeys, checkRequiredChanged } = require('./common');
const { checkFieldPropertiesChanged, modifyGroupItems, setIndexKeys } = require('./common');
const { getModifyPkScripts } = require('./entityHelper/primaryKeyHelper');
const { getModifyUkScripts } = require('./entityHelper/uniqueKeyHelper');

Expand Down Expand Up @@ -135,9 +135,10 @@ const alterEntityHelper = (app, options) => {
),
);

const alterColumnScripts = _.toPairs(collection.properties).reduce((acc, [name, jsonSchema]) => {
const pairs = _.toPairs(collection.properties);

const alterColumnScripts = pairs.reduce((acc, [name, jsonSchema]) => {
const fieldTypeChanged = checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']);
const fieldRequiredChanged = checkRequiredChanged(collection, name);

const columnDefinition = createColumnDefinitionBySchema({
name,
Expand All @@ -147,21 +148,34 @@ const alterEntityHelper = (app, options) => {
schemaData,
});

if (fieldTypeChanged || fieldRequiredChanged) {
if (fieldTypeChanged) {
acc.push(
ddlProvider.alterColumn({
fullTableName,
columnDefinition,
alterType: fieldTypeChanged,
alterNullable: fieldRequiredChanged,
}),
);
}

return acc;
}, []);

return [...renameColumnScripts, ...alterColumnScripts];
const alterDefaultScripts = pairs
.filter(
([, jsonSchema]) =>
options?.scriptGenerationOptions?.feActiveOptions?.columnDefaultValues === 'separate' &&
jsonSchema.defaultConstraintName,
)
.map(([name, jsonSchema]) => {
return ddlProvider.alterColumnDefault({
fullTableName,
columnName: name,
constraint: { name: jsonSchema.defaultConstraintName, value: jsonSchema.default },
});
});

return [...renameColumnScripts, ...alterColumnScripts, ...alterDefaultScripts];
};

const getModifyCollectionKeysScript = collection => {
Expand Down
13 changes: 1 addition & 12 deletions forward_engineering/helpers/alterScriptHelpers/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { isEqual, difference } = require('lodash');
const { isEqual } = require('lodash');

const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => {
return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]);
Expand Down Expand Up @@ -82,21 +82,10 @@ const setIndexKeys = (idToNameHashTable, idToActivatedHashTable, index) => {
};
};

const checkRequiredChanged = (collection, propertyName) => {
const currentRequiredColumnNames = collection.required || [];
const previousRequiredColumnNames = collection.role.required || [];

const isRequired = currentRequiredColumnNames.includes(propertyName);
const wasRequired = previousRequiredColumnNames.includes(propertyName);

return isRequired !== wasRequired;
};

module.exports = {
checkFieldPropertiesChanged,
getCompMod,
modifyGroupItems,
checkCompModEqual,
setIndexKeys,
checkRequiredChanged,
};
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ const getAddRegularKeyScripts = (collection, config, options) => {
const terminator = getTerminator(options);
const collectionSchema = { ...collection, ..._.omit(collection?.role, 'properties') };
const tableName = getEntityName(collectionSchema);
const schemaName = collection.compMod?.keyspaceName;
const schemaName = collectionSchema.compMod?.keyspaceName;
const fullName = getTableName(tableName, schemaName);

const isTableActivated = _.get(collectionSchema, 'isActivated', true);
Expand Down
62 changes: 31 additions & 31 deletions forward_engineering/helpers/constraintsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ const { commentIfDeactivated } = require('./commentIfDeactivated');
const { trimBraces } = require('./general');
const { checkAllKeysDeactivated, divideIntoActivatedAndDeactivated } = require('../utils/general');
const { assignTemplates } = require('../utils/assignTemplates');
const templates = require('../configs/templates');

const createKeyConstraint = (templates, terminator, isParentActivated) => keyData => {
const partition = keyData.partition ? ` ON [${keyData.partition}]` : '';
const columnMapToString = ({ name }) => `[${name}]`.trim();
const createKeyConstraint =
({ terminator, isParentActivated }) =>
keyData => {
const partition = keyData.partition ? ` ON [${keyData.partition}]` : '';
const columnMapToString = ({ name }) => `[${name}]`.trim();

const isAllColumnsDeactivated = checkAllKeysDeactivated(keyData.columns);
const isAllColumnsDeactivated = checkAllKeysDeactivated(keyData.columns);

const dividedColumns = divideIntoActivatedAndDeactivated(keyData.columns, columnMapToString);
const deactivatedColumnsAsString = dividedColumns.deactivatedItems.length
? commentIfDeactivated(dividedColumns.deactivatedItems.join(', '), { isActivated: false }, true)
: '';
const dividedColumns = divideIntoActivatedAndDeactivated(keyData.columns, columnMapToString);
const deactivatedColumnsAsString = dividedColumns.deactivatedItems.length
? commentIfDeactivated(dividedColumns.deactivatedItems.join(', '), { isActivated: false }, true)
: '';

const columns =
!isAllColumnsDeactivated && isParentActivated
? ' (' + dividedColumns.activatedItems.join(', ') + deactivatedColumnsAsString + ')'
: ' (' + keyData.columns.map(columnMapToString).join(', ') + ')';
const columns =
!isAllColumnsDeactivated && isParentActivated
? ' (' + dividedColumns.activatedItems.join(', ') + deactivatedColumnsAsString + ')'
: ' (' + keyData.columns.map(columnMapToString).join(', ') + ')';

return {
statement: assignTemplates(templates.createKeyConstraint, {
constraintName: keyData.name ? `CONSTRAINT [${keyData.name}] ` : '',
keyType: keyData.keyType,
clustered: ' NONCLUSTERED',
columns,
options: ' NOT ENFORCED',
partition,
terminator,
}),
isActivated: !isAllColumnsDeactivated,
return {
statement: assignTemplates(templates.createKeyConstraint, {
constraintName: keyData.name ? `CONSTRAINT [${keyData.name}] ` : '',
keyType: keyData.keyType,
clustered: ' NONCLUSTERED',
columns,
options: ' NOT ENFORCED',
partition,
terminator,
}),
isActivated: !isAllColumnsDeactivated,
};
};
};

const createDefaultConstraint = (templates, terminator) => (constraintData, tableName) => {
return assignTemplates(templates.createDefaultConstraint, {
tableName,
constraintName: constraintData.constraintName,
columnName: constraintData.columnName,
default: trimBraces(constraintData.value),
terminator,
const createDefaultConstraint = ({ constraint }) => {
return assignTemplates(templates.columnDefaultConstraint, {
constraintName: constraint.name,
default: trimBraces(constraint.value),
});
};

Expand Down