这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
161 changes: 43 additions & 118 deletions console/src/components/Services/Data/TableModify/ModifyActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1550,27 +1550,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 <table> ALTER COLUMN <column> TYPE <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);

Expand Down Expand Up @@ -1639,7 +1631,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]) {
Expand All @@ -1665,10 +1657,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 <table> ALTER COLUMN <column> SET DEFAULT <default>;
const columnDefaultUpQuery =
columnDefaultUpQuery =
'ALTER TABLE ONLY ' +
'"' +
currentSchema +
Expand All @@ -1682,10 +1679,12 @@ const saveColumnChangesSql = (colName, column, onSuccess) => {
colName +
'"' +
' SET DEFAULT ' +
defWithQuotes +
colDefaultWithQuotes +
';';
let columnDefaultDownQuery =
'ALTER TABLE ONLY ' +
} else {
// ALTER TABLE <table> ALTER COLUMN <column> DROP DEFAULT;
columnDefaultUpQuery =
'ALTER TABLE ' +
'"' +
currentSchema +
'"' +
Expand All @@ -1696,15 +1695,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 =
if (originalColDefault !== '') {
columnDefaultDownQuery =
'ALTER TABLE ONLY ' +
'"' +
currentSchema +
Expand All @@ -1718,32 +1714,11 @@ const saveColumnChangesSql = (colName, column, onSuccess) => {
colName +
'"' +
' SET DEFAULT ' +
defWithQuotes +
originalColDefaultWithQuotes +
';';
} else if (
column.column_default !== '' &&
column.column_default !== def.trim()
) {
// default value has changed
columnDefaultDownQuery =
'ALTER TABLE ONLY ' +
'"' +
currentSchema +
'"' +
'.' +
'"' +
tableName +
'"' +
' ALTER COLUMN ' +
'"' +
colName +
'"' +
' SET DEFAULT ' +
defWithQuotes +
';';
} 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 +
Expand All @@ -1757,72 +1732,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 <table> ALTER COLUMN <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 */
Expand Down Expand Up @@ -2055,7 +1980,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: {
Expand Down