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 = (
|
export const generateFKConstraintName = (
|
||||||
tableName,
|
tableName,
|
||||||
lCols,
|
lCols,
|
||||||
existingConstraints
|
existingConstraints,
|
||||||
|
ignoreConstraints = []
|
||||||
) => {
|
) => {
|
||||||
const expectedNamePrefix = `${tableName}_${lCols
|
const expectedName = `${tableName}_${lCols
|
||||||
.map(lc => lc.replace(/"/g, ''))
|
.map(lc => lc.replace(/"/g, ''))
|
||||||
.join('_')}_fkey`.substring(0, 60);
|
.join('_')}_fkey`.substring(0, 60);
|
||||||
const prefixLength = expectedNamePrefix.length;
|
|
||||||
let suffix;
|
let maxSuffix;
|
||||||
for (let i = existingConstraints.length - 1; i >= 0; i--) {
|
for (let i = existingConstraints.length - 1; i >= 0; i--) {
|
||||||
const existingConstraintName = existingConstraints[i].constraint_name;
|
const existingConstraintName = existingConstraints[i].constraint_name;
|
||||||
if (existingConstraintName.indexOf(expectedNamePrefix) === 0) {
|
|
||||||
if (existingConstraintName === expectedNamePrefix) {
|
if (ignoreConstraints.includes(existingConstraintName)) {
|
||||||
if (!suffix) {
|
continue;
|
||||||
suffix = 1;
|
}
|
||||||
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),
|
if (!isNaN(currSuffix) && (!maxSuffix || currSuffix >= maxSuffix)) {
|
||||||
10
|
maxSuffix = currSuffix;
|
||||||
);
|
|
||||||
if (!isNaN(intSuffix) && (!suffix || (suffix && intSuffix >= suffix))) {
|
|
||||||
suffix = intSuffix;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (suffix === undefined) {
|
|
||||||
return expectedNamePrefix;
|
return maxSuffix === undefined
|
||||||
}
|
? expectedName
|
||||||
return `${expectedNamePrefix}${suffix + 1}`;
|
: `${expectedName}${maxSuffix + 1}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUniqueConstraintName = (tableName, columns) => {
|
export const getUniqueConstraintName = (tableName, columns) => {
|
||||||
|
@ -268,7 +268,8 @@ const saveForeignKeys = (index, tableSchema, columns) => {
|
|||||||
const generatedConstraintName = generateFKConstraintName(
|
const generatedConstraintName = generateFKConstraintName(
|
||||||
tableName,
|
tableName,
|
||||||
lcols,
|
lcols,
|
||||||
tableSchema.foreign_key_constraints
|
tableSchema.foreign_key_constraints,
|
||||||
|
[constraintName]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (constraintName) {
|
if (constraintName) {
|
||||||
@ -314,14 +315,14 @@ const saveForeignKeys = (index, tableSchema, columns) => {
|
|||||||
alter table "${schemaName}"."${tableName}" drop constraint "${generatedConstraintName}",
|
alter table "${schemaName}"."${tableName}" drop constraint "${generatedConstraintName}",
|
||||||
add constraint "${constraintName}"
|
add constraint "${constraintName}"
|
||||||
foreign key (${Object.keys(oldConstraint.column_mapping)
|
foreign key (${Object.keys(oldConstraint.column_mapping)
|
||||||
.map(lc => `"${lc}"`)
|
.map(lc => `"${lc}"`)
|
||||||
.join(', ')})
|
.join(', ')})
|
||||||
references "${oldConstraint.ref_table_table_schema}"."${
|
references "${oldConstraint.ref_table_table_schema}"."${
|
||||||
oldConstraint.ref_table
|
oldConstraint.ref_table
|
||||||
}"
|
}"
|
||||||
(${Object.values(oldConstraint.column_mapping)
|
(${Object.values(oldConstraint.column_mapping)
|
||||||
.map(rc => `"${rc}"`)
|
.map(rc => `"${rc}"`)
|
||||||
.join(', ')})
|
.join(', ')})
|
||||||
on update ${pgConfTypes[oldConstraint.on_update]}
|
on update ${pgConfTypes[oldConstraint.on_update]}
|
||||||
on delete ${pgConfTypes[oldConstraint.on_delete]};
|
on delete ${pgConfTypes[oldConstraint.on_delete]};
|
||||||
`;
|
`;
|
||||||
@ -1205,24 +1206,24 @@ const saveColumnChangesSql = (colName, column) => {
|
|||||||
const schemaChangesUp =
|
const schemaChangesUp =
|
||||||
originalColType !== colType
|
originalColType !== colType
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
type: 'run_sql',
|
type: 'run_sql',
|
||||||
args: {
|
args: {
|
||||||
sql: columnChangesUpQuery,
|
sql: columnChangesUpQuery,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
]
|
||||||
]
|
|
||||||
: [];
|
: [];
|
||||||
const schemaChangesDown =
|
const schemaChangesDown =
|
||||||
originalColType !== colType
|
originalColType !== colType
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
type: 'run_sql',
|
type: 'run_sql',
|
||||||
args: {
|
args: {
|
||||||
sql: columnChangesDownQuery,
|
sql: columnChangesDownQuery,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
]
|
||||||
]
|
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
/* column default up/down migration */
|
/* column default up/down migration */
|
||||||
|
Loading…
Reference in New Issue
Block a user