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
2 changes: 1 addition & 1 deletion forward_engineering/alterScript/alterScriptBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const generateAlterScript = (data, callback, app) => {
modelData: data.modelData,
ignoreRelationshipIDs,
});
const script = [...containersScripts, ...collectionsScripts, ...viewScripts, ...relationships]
const script = [...containersScripts, ...collectionsScripts, ...relationships, ...viewScripts]
.filter(Boolean)
.join('\n\n');

Expand Down
39 changes: 13 additions & 26 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,19 @@ const getAlterRelationshipsScript = ({ collection, app, modelData, ignoreRelatio
modelData,
});

const addedRelationships = []
.concat(collection.properties?.relationships?.properties?.added?.items)
.filter(Boolean)
.map(item => Object.values(item.properties)[0])
.filter(
relationship =>
relationship?.role?.compMod?.created && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);

const deletedRelationships = []
.concat(collection.properties?.relationships?.properties?.deleted?.items)
.filter(Boolean)
.map(item => Object.values(item.properties)[0])
.filter(
relationship =>
relationship?.role?.compMod?.deleted && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);

const modifiedRelationships = []
.concat(collection.properties?.relationships?.properties?.modified?.items)
.filter(Boolean)
.map(item => Object.values(item.properties)[0])
.filter(
relationship =>
relationship?.role?.compMod?.modified && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);
const relationshipData = collection.properties?.relationships?.properties;
const addedRelationships = getItemProperties(relationshipData.added).filter(
relationship => relationship?.role?.compMod?.created && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);

const deletedRelationships = getItemProperties(relationshipData.deleted).filter(
relationship => relationship?.role?.compMod?.deleted && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);

const modifiedRelationships = getItemProperties(relationshipData.modified).filter(
relationship =>
relationship?.role?.compMod?.modified && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);

const deleteFkScriptDtos = getDeleteForeignKeyScripts(deletedRelationships);
const addFkScriptDtos = getAddForeignKeyScripts(addedRelationships);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getFullName } = require('../../helpers/utils');
const { getFullName, getName } = require('../../helpers/utils');
const { getModifiedDefaultColumnValueScripts } = require('./columnHelpers/defaultConstraintHelper');
const { getModifyColumnNameScript } = require('./columnHelpers/nameHelper');
const { getModifiedColumnNotNullScripts } = require('./columnHelpers/notNullHelper');
Expand All @@ -10,7 +10,6 @@ const { getCompMod, checkCompModEqual, setEntityKeys } = require('./common');

module.exports = (app, options) => {
const _ = app.require('lodash');
const { getEntityName } = app.require('@hackolade/ddl-fe-utils').general;
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(_);
const ddlProvider = require('../../ddlProvider')(null, options, app);
const { generateIdToNameHashTable, generateIdToActivatedHashTable } = app.require('@hackolade/ddl-fe-utils');
Expand All @@ -20,12 +19,12 @@ module.exports = (app, options) => {
const dbData = { databaseName, projectId: _.first(modelData)?.projectId };
const table = {
..._.omit(collection, 'timeUnitpartitionKey', 'clusteringKey', 'rangePartitionKey'),
...(collection?.role || {}),
...collection?.role,
};
const idToNameHashTable = generateIdToNameHashTable(table);
const idToActivatedHashTable = generateIdToActivatedHashTable(table);
const jsonSchema = setEntityKeys({ idToActivatedHashTable, idToNameHashTable, entity: table });
const tableName = getEntityName(jsonSchema);
const tableName = getName(jsonSchema);
const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) =>
createColumnDefinitionBySchema({
jsonSchema: column,
Expand Down Expand Up @@ -54,8 +53,8 @@ module.exports = (app, options) => {
};

const getDeleteCollectionScript = modelData => collection => {
const jsonSchema = { ...collection, ...(collection?.role || {}) };
const tableName = getEntityName(jsonSchema);
const jsonSchema = { ...collection, ...collection?.role };
const tableName = getName(jsonSchema);
const databaseName = collection.compMod.keyspaceName;
const projectId = _.first(modelData)?.projectId;

Expand All @@ -65,15 +64,15 @@ module.exports = (app, options) => {
const getModifyCollectionScript = modelData => collection => {
const table = {
..._.omit(collection, 'timeUnitpartitionKey', 'clusteringKey', 'rangePartitionKey'),
...(collection?.role || {}),
...collection?.role,
};

const databaseName = table.compMod.keyspaceName;
const dbData = { databaseName, projectId: _.first(modelData)?.projectId };
const idToNameHashTable = generateIdToNameHashTable(table);
const idToActivatedHashTable = generateIdToActivatedHashTable(table);
const jsonSchema = setEntityKeys({ idToActivatedHashTable, idToNameHashTable, entity: table });
const tableName = getEntityName(jsonSchema);
const tableName = getName(jsonSchema);
const fullTableName = getFullName(dbData.projectId, dbData.databaseName, collection.role?.name || tableName);

const tableData = {
Expand Down Expand Up @@ -131,8 +130,8 @@ module.exports = (app, options) => {
};

const getAddColumnScript = modelData => collection => {
const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const tableName = collectionSchema?.code || collectionSchema?.collectionName || collectionSchema?.name;
const collectionSchema = { ...collection, ..._.omit(collection?.role, 'properties') };
const tableName = getName(collectionSchema);
const databaseName = collectionSchema.compMod?.keyspaceName;
const dbData = { databaseName, projectId: _.first(modelData)?.projectId };

Expand All @@ -153,7 +152,7 @@ module.exports = (app, options) => {

const getDeleteColumnScript = modelData => collection => {
const collectionSchema = { ...collection, ..._.omit(collection?.role, 'properties') };
const tableName = collectionSchema?.code || collectionSchema?.collectionName || collectionSchema?.name;
const tableName = getName(collectionSchema);
const databaseName = collectionSchema.compMod?.keyspaceName;
const dbData = { databaseName, projectId: _.first(modelData)?.projectId };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const getModifiedColumnOptionScripts = ({ collection, app, tableData }) => {

return assignTemplates(templates.alterColumnOptions, {
tableName: tableData.name,
columnName: wrapByBackticks(oldName),
columnName: wrapByBackticks(name),
options: tab(optionsToUpdate.join(',\n')),
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ const setTypeChangeAction = (oldField, newField, changeState) => {
return;
}

const newPrecision = newField.precision ?? 0;
const oldPrecision = oldField.precision ?? 0;
const newPrecision = newField.precision;
const oldPrecision = oldField.precision;

const newScale = newField.scale ?? 0;
const oldScale = oldField.scale ?? 0;
const newScale = newField.scale;
const oldScale = oldField.scale;

const isPrecisionChanged = newPrecision > oldPrecision;
const isPrecisionChanged = _.isFinite(newPrecision) && _.isFinite(oldPrecision) && newPrecision > oldPrecision;

if (isPrecisionChanged && newScale >= oldScale) {
if (isPrecisionChanged && (newScale === oldScale || newScale > oldScale)) {
changeState.action = TYPE_CHANGE.update;
return;
}
const isScaleChanged = newScale > oldScale;
const isScaleChanged = _.isFinite(newScale) && _.isFinite(oldScale) && newScale > oldScale;

if (isScaleChanged && newPrecision >= oldPrecision) {
changeState.action = TYPE_CHANGE.update;
Expand Down
2 changes: 1 addition & 1 deletion forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = {

renameTable: 'ALTER TABLE IF EXISTS ${oldTableName} RENAME TO ${newTableName};',

alterPkConstraint: 'ALTER TABLE ${tableName}\nADD CONSTRAINT PRIMARY KEY (${columns}) NOT ENFORCED;',
alterPkConstraint: 'ALTER TABLE ${tableName}\nADD PRIMARY KEY (${columns}) NOT ENFORCED;',

dropPk: 'ALTER TABLE ${tableName} DROP PRIMARY KEY IF EXISTS;',

Expand Down
3 changes: 2 additions & 1 deletion forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
prepareConstraintName,
wrapByBackticks,
getFullName,
getName,
} = require('./helpers/utils');

module.exports = (baseProvider, options, app) => {
Expand Down Expand Up @@ -341,7 +342,7 @@ module.exports = (baseProvider, options, app) => {

return {
...tableData,
name: data.code || data.collectionName,
name: getName(jsonSchema) || getName(data),
friendlyName: jsonSchema.title && jsonSchema.title !== data.collectionName ? jsonSchema.title : '',
description: data.description,
orReplace: data.orReplace,
Expand Down
10 changes: 10 additions & 0 deletions forward_engineering/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ const getFullName = (...names) => {
return wrapByBackticks(fullName);
};

const getName = entity =>
entity.compMod?.code?.new ||
entity.code ||
entity.compMod?.collectionName?.new ||
entity.collectionName ||
entity.compMod?.name?.new ||
entity.name ||
'';

module.exports = {
isActivatedPartition,
getTablePartitioning,
Expand All @@ -393,4 +402,5 @@ module.exports = {
decorateType,
getFullName,
addParameters,
getName,
};