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 @@ -245,7 +245,7 @@ const getAddColumnsScripts = (definitions, provider) => entity => {
const entityData = { ...entity, ..._.omit(entity.role, ['properties']) };
const { columns } = getColumns(entityData, true, definitions);
const properties = getEntityProperties(entity);
const columnStatement = getColumnsStatement(columns, null, entityData.disableNoValidate);
const columnStatement = getColumnsStatement({ columns });
const fullCollectionName = generateFullEntityName(entity);
const { hydratedAddIndexes, hydratedDropIndexes } = hydrateIndex(entity, properties, definitions);
const modifyScript = generateModifyCollectionScript(entity, definitions, provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
const {
getFullEntityName,
generateFullEntityName,
getEntityProperties,
getContainerName,
getEntityData,
getEntityName,
prepareScript,
hydrateProperty,
} = require('./generalHelper');
const { getFullEntityName } = require('./generalHelper');
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');

const templates = require('./config/templates');
Expand Down Expand Up @@ -69,7 +60,7 @@ const canRelationshipBeAdded = relationship => {
compMod.child?.bucket,
compMod.child?.collection,
compMod.child?.collection?.fkFields?.length,
].every(property => Boolean(property));
].every(Boolean);
};

const getAddForeignKeyScript = provider => relationship => {
Expand Down Expand Up @@ -100,7 +91,7 @@ const canRelationshipBeDeleted = relationship => {
compMod.code?.old || compMod.name?.old || getRelationshipName(relationship),
compMod.child?.bucket,
compMod.child?.collection,
].every(property => Boolean(property));
].every(Boolean);
};

const getDeleteForeignKeyScripts = provider => deletedRelationships => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
const { getTypeByProperty } = require('../columnHelper');
const { commentDeactivatedStatements } = require('../generalHelper');

const postfix = 'check';
const { CONSTRAINT_POSTFIX } = require('../constants');

const didCompositeCheckConstraintsChange = collection => {
const checkConstraintsDto = collection?.role?.compMod?.chkConstr || {};
Expand Down Expand Up @@ -33,7 +32,9 @@ const getDropCompositeCheckConstraintsScripts = ({ collection, provider }) => {
const oldCheckConstraints = checkConstraintsDto.old || [];

return oldCheckConstraints.map(oldCheckConstraint => {
const constraintName = oldCheckConstraint.constraintName || getDefaultConstraintName(collection, postfix);
const constraintName =
oldCheckConstraint.constraintName ||
getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.check });

return provider.assignTemplates(templates.dropConstraint, {
tableName,
Expand All @@ -54,7 +55,9 @@ const getAddCompositeCheckConstraintsScripts = ({ collection, provider }) => {
const newCheckConstraints = checkConstraintsDto.new || [];

return newCheckConstraints.map(newCheckConstraint => {
const constraintName = newCheckConstraint.constraintName || getDefaultConstraintName(collection, postfix);
const constraintName =
newCheckConstraint.constraintName ||
getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.check });
const expression = newCheckConstraint.checkExpression || '';
const enable = newCheckConstraint.enableSpecification ? ` ${newCheckConstraint.enableSpecification}` : '';
const noValidate = newCheckConstraint.noValidateSpecification
Expand Down Expand Up @@ -82,10 +85,10 @@ const getModifyCompositeCheckConstraintsScripts = ({ collection, provider }) =>

const getModifyColumnCheckConstraintsScripts = ({ collection, provider, definitions }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.check });
const isActivated = collection.role.isActivated;

const addCheckConstraintsScript = _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const newField = jsonSchema.compMod.newField;

Expand Down Expand Up @@ -120,8 +123,6 @@ const getModifyColumnCheckConstraintsScripts = ({ collection, provider, definiti

return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
});

return addCheckConstraintsScript;
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
const { getTypeByProperty } = require('../columnHelper');
const { commentDeactivatedStatements } = require('../generalHelper');

const postfix = 'default';
const { CONSTRAINT_POSTFIX } = require('../constants');

const getModifyDefaultValueConstraintsScripts = ({ collection, provider, definitions }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.default });
const isActivated = collection.role.isActivated;

const addDefaultConstraintsScript = _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const newField = jsonSchema.compMod.newField;

Expand Down Expand Up @@ -46,8 +45,6 @@ const getModifyDefaultValueConstraintsScripts = ({ collection, provider, definit

return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
});

return addDefaultConstraintsScript;
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const generateFullEntityName = entity => {
const getEntityProperties = entity => {
const propertiesInRole = _.get(entity, 'role.properties', {});
const propertiesInEntity = _.get(entity, 'properties', {});
return { ...(propertiesInEntity || {}), ...propertiesInRole };
return { ...propertiesInEntity, ...propertiesInRole };
};

const getEntityName = (compMod = {}, type = 'collectionName') => {
Expand All @@ -43,13 +43,14 @@ const isEqualProperty = (compMod, nameProperty) => {
};

const hydrateProperty = (entity, compMod, nameProperty) => {
return !isEqualProperty(compMod, nameProperty) ? entity?.role?.[nameProperty] : null;
return isEqualProperty(compMod, nameProperty) ? null : entity?.role?.[nameProperty];
};

const getDefaultConstraintName = (collection, postfix) => {
const getDefaultConstraintName = ({ collection, column = {}, postfix }) => {
const entityData = collection?.role || {};
const entityName = prepareName(getName(entityData));
return `${entityName}_${postfix}`;
const columnName = prepareName(getName(column));
return [entityName, columnName, postfix].filter(Boolean).join('_');
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
const { getTypeByProperty } = require('../columnHelper');
const { commentDeactivatedStatements } = require('../generalHelper');

const postfix = 'nn';
const { CONSTRAINT_POSTFIX } = require('../constants');

const getModifyNonNullColumnsScripts = ({ collection, provider, definitions }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.notNull });
const isActivated = collection.role.isActivated;

const currentRequiredColumnNames = collection.required || [];
const previousRequiredColumnNames = collection.role.required || [];

const addNotNullConstraintsScript = _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const newField = jsonSchema.compMod.newField;

Expand Down Expand Up @@ -44,8 +43,6 @@ const getModifyNonNullColumnsScripts = ({ collection, provider, definitions }) =

return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
});

return addNotNullConstraintsScript;
};

module.exports = {
Expand Down
23 changes: 12 additions & 11 deletions forward_engineering/helpers/alterScriptHelpers/primaryKeyHelper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const _ = require('lodash');
const { getName, prepareName, commentDeactivatedStatements } = require('../generalHelper');
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');
const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');

const postfix = 'pk';
const { CONSTRAINT_POSTFIX } = require('../constants');

const getPropertyNameByGuid = (collection, guid) => {
const property = _.toPairs(collection?.role?.properties).find(([name, jsonSchema]) => jsonSchema.GUID === guid);
const property = _.toPairs(collection?.role?.properties).find(([, jsonSchema]) => jsonSchema.GUID === guid);
return property?.[0] && prepareName(property[0]);
};

Expand Down Expand Up @@ -40,7 +39,8 @@ const getDropCompositePkScripts = ({ collection, provider }) => {
const oldPrimaryKeys = pkDto.old || [];

return oldPrimaryKeys.map(oldPk => {
const pkConstraintName = oldPk.constraintName || getDefaultConstraintName(collection, postfix);
const pkConstraintName =
oldPk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
const constraintName = prepareName(pkConstraintName);

return provider.assignTemplates(templates.dropConstraint, {
Expand All @@ -65,7 +65,8 @@ const getAddCompositePkScripts = ({ collection, provider }) => {
const compositePrimaryKey = newPk.compositePrimaryKey || [];
const guidsOfColumnsInPk = compositePrimaryKey.map(compositePkEntry => compositePkEntry.keyId);
const columnNames = getPropertiesNamesByGUIDs(collection, guidsOfColumnsInPk);
const pkConstraintName = newPk.constraintName || getDefaultConstraintName(collection, postfix);
const pkConstraintName =
newPk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
const constraintName = prepareName(pkConstraintName);
const noValidate = newPk.noValidateSpecification ? ` ${newPk.noValidateSpecification}` : '';
const rely = newPk.rely ? ` ${newPk.rely}` : '';
Expand All @@ -89,18 +90,18 @@ const getModifyCompositePkScripts = ({ collection, provider }) => {

const getDropPkScripts = ({ collection, provider }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
.filter(([, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const oldJsonSchema = collection.role.properties[oldName];
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;

const isNotAPrimaryKey = !jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
})
.map(([name, jsonSchema]) => {
.map(() => {
return provider.assignTemplates(templates.dropConstraint, {
tableName,
constraintName,
Expand All @@ -110,10 +111,10 @@ const getDropPkScripts = ({ collection, provider }) => {

const getAddPkScripts = ({ collection, provider }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
.filter(([, jsonSchema]) => {
const isRegularPrimaryKey = jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
const oldName = jsonSchema.compMod.oldField.name;
const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey);
Expand Down
21 changes: 11 additions & 10 deletions forward_engineering/helpers/alterScriptHelpers/uniqueKeyHelper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const _ = require('lodash');
const { getName, prepareName, commentDeactivatedStatements } = require('../generalHelper');
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');
const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');

const postfix = 'uk';
const { CONSTRAINT_POSTFIX } = require('../constants');

const getPropertyNameByGuid = (collection, guid) => {
const property = _.toPairs(collection?.role?.properties).find(([name, jsonSchema]) => jsonSchema.GUID === guid);
Expand Down Expand Up @@ -40,7 +39,8 @@ const getDropCompositeUkScripts = ({ collection, provider }) => {
const oldUniqueKeys = pkDto.old || [];

return oldUniqueKeys.map(oldUk => {
const pkConstraintName = oldUk.constraintName || getDefaultConstraintName(collection, postfix);
const pkConstraintName =
oldUk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });
const constraintName = prepareName(pkConstraintName);

return provider.assignTemplates(templates.dropConstraint, {
Expand All @@ -65,7 +65,8 @@ const getAddCompositeUkScripts = ({ collection, provider }) => {
const compositeUniqueKey = newUk.compositeUniqueKey || [];
const guidsOfColumnsInUk = compositeUniqueKey.map(compositeUkEntry => compositeUkEntry.keyId);
const columnNames = getPropertiesNamesByGUIDs(collection, guidsOfColumnsInUk);
const pkConstraintName = newUk.constraintName || getDefaultConstraintName(collection, postfix);
const pkConstraintName =
newUk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });
const constraintName = prepareName(pkConstraintName);
const noValidate = newUk.noValidateSpecification ? ` ${newUk.noValidateSpecification}` : '';
const rely = newUk.rely ? ` ${newUk.rely}` : '';
Expand All @@ -89,18 +90,18 @@ const getModifyCompositeUkScripts = ({ collection, provider }) => {

const getDropUkScripts = ({ collection, provider }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
.filter(([, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const oldJsonSchema = collection.role.properties[oldName];
const wasTheFieldARegularUniqueKey = oldJsonSchema?.unique && !oldJsonSchema?.compositeUniqueKey;

const isNotAUniqueKey = !jsonSchema.unique && !jsonSchema.compositeUniqueKey;
return wasTheFieldARegularUniqueKey && isNotAUniqueKey;
})
.map(([name, jsonSchema]) => {
.map(() => {
return provider.assignTemplates(templates.dropConstraint, {
tableName,
constraintName,
Expand All @@ -110,10 +111,10 @@ const getDropUkScripts = ({ collection, provider }) => {

const getAddUkScripts = ({ collection, provider }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName(collection, postfix);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
.filter(([, jsonSchema]) => {
const isRegularUniqueKey = jsonSchema.unique && !jsonSchema.compositeUniqueKey;
const oldName = jsonSchema.compMod.oldField.name;
const wasTheFieldAUniqueKey = Boolean(collection.role.properties[oldName]?.unique);
Expand Down
Loading