Skip to content

Commit

Permalink
Merge pull request #57 from ArzamastsevVladyslav/fix/HCK-3442-type-co…
Browse files Browse the repository at this point in the history
…nversion

fix/HCK-3443
  • Loading branch information
VitaliiBedletskyi authored Apr 21, 2023
2 parents 7f6bd99 + 4522e90 commit 1c8a1d7
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,27 +236,21 @@ const getDeleteColumnScript = app => collection => {
.map(([name]) => `ALTER TABLE IF EXISTS ${fullName} DROP COLUMN IF EXISTS ${wrapInQuotes(name)};`);
};

const extractNewPropertyByName = (collection, fieldName) => {
return collection.role.compMod?.newProperties?.find(newProperty => newProperty.name === fieldName);
}

const hasLengthChanged = (collection, newFieldName, oldFieldName) => {
const hasLengthChanged = (collection, oldFieldName, currentJsonSchema) => {
const oldProperty = collection.role.properties[oldFieldName];
const newProperty = extractNewPropertyByName(collection, newFieldName);

const previousLength = oldProperty?.length;
const newLength = newProperty?.length;
const newLength = currentJsonSchema?.length;
return previousLength !== newLength;
}

const hasPrecisionOrScaleChanged = (collection, newFieldName, oldFieldName) => {
const hasPrecisionOrScaleChanged = (collection, oldFieldName, currentJsonSchema) => {
const oldProperty = collection.role.properties[oldFieldName];
const newProperty = extractNewPropertyByName(collection, newFieldName);

const previousPrecision = oldProperty?.precision;
const newPrecision = newProperty?.precision;
const newPrecision = currentJsonSchema?.precision;
const previousScale = oldProperty?.scale;
const newScale = newProperty?.scale;
const newScale = currentJsonSchema?.scale;

return previousPrecision !== newPrecision || previousScale !== newScale;
}
Expand All @@ -270,8 +264,8 @@ const getUpdateTypesScripts = (_, ddlProvider) => (collection) => {
const hasTypeChanged = checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']);
if (!hasTypeChanged) {
const oldName = jsonSchema.compMod.oldField.name;
const isNewLength = hasLengthChanged(collection, name, oldName);
const isNewPrecisionOrScale = hasPrecisionOrScaleChanged(collection, name, oldName);
const isNewLength = hasLengthChanged(collection, oldName, jsonSchema);
const isNewPrecisionOrScale = hasPrecisionOrScaleChanged(collection, oldName, jsonSchema);
return isNewLength || isNewPrecisionOrScale;
}
return hasTypeChanged;
Expand All @@ -280,8 +274,7 @@ const getUpdateTypesScripts = (_, ddlProvider) => (collection) => {
([name, jsonSchema]) => {
const typeName = jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type;
const columnName = wrapInQuotes(name);
const newProperty = extractNewPropertyByName(collection, name);
const typeConfig = _.pick(newProperty, ['length', 'precision', 'scale']);
const typeConfig = _.pick(jsonSchema, ['length', 'precision', 'scale']);
return ddlProvider.alterColumnType(fullTableName, columnName, typeName, typeConfig);
}
);
Expand Down

0 comments on commit 1c8a1d7

Please sign in to comment.