mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
fix fk name generation (#2320)
This commit is contained in:
parent
31cf7314e2
commit
d1f0cf02a9
@ -60,35 +60,40 @@ export const getExistingFKConstraints = (tableSchema, orderedColumns) => {
|
||||
export const generateFKConstraintName = (
|
||||
tableName,
|
||||
lCols,
|
||||
existingConstraints
|
||||
existingConstraints,
|
||||
ignoreConstraints = []
|
||||
) => {
|
||||
const expectedNamePrefix = `${tableName}_${lCols
|
||||
const expectedName = `${tableName}_${lCols
|
||||
.map(lc => lc.replace(/"/g, ''))
|
||||
.join('_')}_fkey`.substring(0, 60);
|
||||
const prefixLength = expectedNamePrefix.length;
|
||||
let suffix;
|
||||
|
||||
let maxSuffix;
|
||||
for (let i = existingConstraints.length - 1; i >= 0; i--) {
|
||||
const existingConstraintName = existingConstraints[i].constraint_name;
|
||||
if (existingConstraintName.indexOf(expectedNamePrefix) === 0) {
|
||||
if (existingConstraintName === expectedNamePrefix) {
|
||||
if (!suffix) {
|
||||
suffix = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ignoreConstraints.includes(existingConstraintName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (existingConstraintName.startsWith(expectedName)) {
|
||||
let currSuffix;
|
||||
|
||||
if (existingConstraintName === expectedName) {
|
||||
currSuffix = 1;
|
||||
} else {
|
||||
const prefixLength = expectedName.length;
|
||||
currSuffix = parseInt(existingConstraintName.slice(prefixLength), 10);
|
||||
}
|
||||
const intSuffix = parseInt(
|
||||
existingConstraintName.slice(prefixLength),
|
||||
10
|
||||
);
|
||||
if (!isNaN(intSuffix) && (!suffix || (suffix && intSuffix >= suffix))) {
|
||||
suffix = intSuffix;
|
||||
|
||||
if (!isNaN(currSuffix) && (!maxSuffix || currSuffix >= maxSuffix)) {
|
||||
maxSuffix = currSuffix;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (suffix === undefined) {
|
||||
return expectedNamePrefix;
|
||||
}
|
||||
return `${expectedNamePrefix}${suffix + 1}`;
|
||||
|
||||
return maxSuffix === undefined
|
||||
? expectedName
|
||||
: `${expectedName}${maxSuffix + 1}`;
|
||||
};
|
||||
|
||||
export const getUniqueConstraintName = (tableName, columns) => {
|
||||
|
@ -268,7 +268,8 @@ const saveForeignKeys = (index, tableSchema, columns) => {
|
||||
const generatedConstraintName = generateFKConstraintName(
|
||||
tableName,
|
||||
lcols,
|
||||
tableSchema.foreign_key_constraints
|
||||
tableSchema.foreign_key_constraints,
|
||||
[constraintName]
|
||||
);
|
||||
|
||||
if (constraintName) {
|
||||
@ -314,14 +315,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]};
|
||||
`;
|
||||
@ -1205,24 +1206,24 @@ const saveColumnChangesSql = (colName, column) => {
|
||||
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 default up/down migration */
|
||||
|
Loading…
Reference in New Issue
Block a user