From d3a607535eb2dfc1639b7a7461bfa639dcabea63 Mon Sep 17 00:00:00 2001 From: rikinsk Date: Thu, 28 Nov 2019 17:35:04 +0530 Subject: [PATCH] fix column edit migrations --- .../Data/TableModify/ModifyActions.js | 201 ++++++------------ 1 file changed, 63 insertions(+), 138 deletions(-) diff --git a/console/src/components/Services/Data/TableModify/ModifyActions.js b/console/src/components/Services/Data/TableModify/ModifyActions.js index 30a727d1bb2b6..14bd9a024b974 100644 --- a/console/src/components/Services/Data/TableModify/ModifyActions.js +++ b/console/src/components/Services/Data/TableModify/ModifyActions.js @@ -493,14 +493,14 @@ const saveForeignKeys = (index, tableSchema, columns) => { alter table "${schemaName}"."${tableName}" drop constraint "${generatedConstraintName}", add constraint "${constraintName}" foreign key (${Object.keys(oldConstraint.column_mapping) - .map(lc => `"${lc}"`) - .join(', ')}) + .map(lc => `"${lc}"`) + .join(', ')}) references "${oldConstraint.ref_table_table_schema}"."${ - oldConstraint.ref_table - }" + oldConstraint.ref_table +}" (${Object.values(oldConstraint.column_mapping) - .map(rc => `"${rc}"`) - .join(', ')}) + .map(rc => `"${rc}"`) + .join(', ')}) on update ${pgConfTypes[oldConstraint.on_update]} on delete ${pgConfTypes[oldConstraint.on_delete]}; `; @@ -748,8 +748,8 @@ const deleteTrigger = (trigger, table) => { downMigrationSql += `CREATE TRIGGER "${triggerName}" ${trigger.action_timing} ${ - trigger.event_manipulation - } ON "${tableSchema}"."${tableName}" + trigger.event_manipulation +} ON "${tableSchema}"."${tableName}" FOR EACH ${trigger.action_orientation} ${trigger.action_statement};`; if (trigger.comment) { @@ -1549,27 +1549,19 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { const colType = columnEdit.type; const nullable = columnEdit.isNullable; const unique = columnEdit.isUnique; - const def = columnEdit.default || ''; - const comment = columnEdit.comment || ''; - const newName = columnEdit.name; + const colDefault = (columnEdit.default || '').trim(); + const comment = (columnEdit.comment || '').trim(); + const newName = columnEdit.name.trim(); const currentSchema = columnEdit.schemaName; - const customFieldName = columnEdit.customFieldName; - const checkIfFunctionFormat = isPostgresFunction(def); - // ALTER TABLE ALTER COLUMN TYPE ; - let defWithQuotes; - if (colType === 'text' && !checkIfFunctionFormat) { - defWithQuotes = `'${def}'`; - } else { - defWithQuotes = def; - } + const customFieldName = (columnEdit.customFieldName || '').trim(); const tableDef = generateTableDef(tableName, currentSchema); const table = findTable(getState().tables.allSchemas, tableDef); // check if column type has changed before making it part of the migration const originalColType = column.data_type; // "value" - const originalColDefault = column.column_default; // null or "value" - const originalColComment = column.comment; // null or "value" + const originalColDefault = column.column_default || ''; // null or "value" + const originalColComment = column.comment || ''; // null or "value" const originalColNullable = column.is_nullable; // "YES" or "NO" const originalColUnique = isColumnUnique(table, colName); @@ -1609,24 +1601,24 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { const schemaChangesUp = originalColType !== colType ? [ - { - type: 'run_sql', - args: { - sql: columnChangesUpQuery, - }, + { + type: 'run_sql', + args: { + sql: columnChangesUpQuery, }, - ] + }, + ] : []; const schemaChangesDown = originalColType !== colType ? [ - { - type: 'run_sql', - args: { - sql: columnChangesDownQuery, - }, + { + type: 'run_sql', + args: { + sql: columnChangesDownQuery, }, - ] + }, + ] : []; /* column custom field up/down migration*/ @@ -1638,7 +1630,7 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { if (customFieldName) { if (customFieldName !== existingCustomColumnNames[colName]) { isCustomFieldNameChanged = true; - newCustomColumnNames[colName] = customFieldName.trim(); + newCustomColumnNames[colName] = customFieldName; } } else { if (existingCustomColumnNames[colName]) { @@ -1664,10 +1656,15 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { } } + const colDefaultWithQuotes = (colType === 'text' && !isPostgresFunction(colDefault)) ? `'${colDefault}'` : colDefault; + const originalColDefaultWithQuotes = (colType === 'text' && !isPostgresFunction(originalColDefault)) ? `'${originalColDefault}'` : originalColDefault; + /* column default up/down migration */ - if (def.trim() !== '') { + let columnDefaultUpQuery; + let columnDefaultDownQuery; + if (colDefault !== '') { // ALTER TABLE ONLY
ALTER COLUMN SET DEFAULT ; - const columnDefaultUpQuery = + columnDefaultUpQuery = 'ALTER TABLE ONLY ' + '"' + currentSchema + @@ -1681,10 +1678,12 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { colName + '"' + ' SET DEFAULT ' + - defWithQuotes + + colDefaultWithQuotes + ';'; - let columnDefaultDownQuery = - 'ALTER TABLE ONLY ' + + } else { + // ALTER TABLE
ALTER COLUMN DROP DEFAULT; + columnDefaultUpQuery = + 'ALTER TABLE ' + '"' + currentSchema + '"' + @@ -1695,36 +1694,12 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { ' ALTER COLUMN ' + '"' + colName + + '"' + ' DROP DEFAULT;'; + } - // form migration queries - if ( - column.column_default !== '' && - column.column_default === def.trim() - ) { - // default value unchanged - columnDefaultDownQuery = - 'ALTER TABLE ONLY ' + - '"' + - currentSchema + - '"' + - '.' + - '"' + - tableName + - '"' + - ' ALTER COLUMN ' + - '"' + - colName + - '"' + - ' SET DEFAULT ' + - defWithQuotes + - ';'; - } else if ( - column.column_default !== '' && - column.column_default !== def.trim() - ) { - // default value has changed - columnDefaultDownQuery = + if (originalColDefault !== '') { + columnDefaultDownQuery = 'ALTER TABLE ONLY ' + '"' + currentSchema + @@ -1738,11 +1713,11 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { colName + '"' + ' SET DEFAULT ' + - defWithQuotes + + originalColDefaultWithQuotes + ';'; - } else { - // there was no default value originally. so drop default. - columnDefaultDownQuery = + } else { + // there was no default value originally. so drop default. + columnDefaultDownQuery = 'ALTER TABLE ONLY ' + '"' + currentSchema + @@ -1756,72 +1731,22 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { colName + '"' + ' DROP DEFAULT;'; - } - - // check if default is unchanged and then do a drop. if not skip - if (originalColDefault !== def.trim()) { - schemaChangesUp.push({ - type: 'run_sql', - args: { - sql: columnDefaultUpQuery, - }, - }); - schemaChangesDown.push({ - type: 'run_sql', - args: { - sql: columnDefaultDownQuery, - }, - }); - } - } else { - // ALTER TABLE
ALTER COLUMN DROP DEFAULT; - const columnDefaultUpQuery = - 'ALTER TABLE ' + - '"' + - currentSchema + - '"' + - '.' + - '"' + - tableName + - '"' + - ' ALTER COLUMN ' + - '"' + - colName + - '"' + - ' DROP DEFAULT;'; - if (column.column_default !== null) { - const columnDefaultDownQuery = - 'ALTER TABLE ' + - '"' + - currentSchema + - '"' + - '.' + - '"' + - tableName + - '"' + - ' ALTER COLUMN ' + - '"' + - colName + - '"' + - ' SET DEFAULT ' + - column.column_default + - ';'; - schemaChangesDown.push({ - type: 'run_sql', - args: { - sql: columnDefaultDownQuery, - }, - }); - } + } - if (originalColDefault !== def.trim() && originalColDefault !== null) { - schemaChangesUp.push({ - type: 'run_sql', - args: { - sql: columnDefaultUpQuery, - }, - }); - } + // check if default is unchanged and then do a drop. if not skip + if (originalColDefault !== colDefault) { + schemaChangesUp.push({ + type: 'run_sql', + args: { + sql: columnDefaultUpQuery, + }, + }); + schemaChangesDown.push({ + type: 'run_sql', + args: { + sql: columnDefaultDownQuery, + }, + }); } /* column nullable up/down migration */ @@ -2054,7 +1979,7 @@ const saveColumnChangesSql = (colName, column, onSuccess) => { sqlEscapeText(originalColComment); // check if comment is unchanged and then do an update. if not skip - if (originalColComment !== comment.trim()) { + if (originalColComment !== comment) { schemaChangesUp.push({ type: 'run_sql', args: {