mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31: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) {
|
|
||||||
suffix = 1;
|
|
||||||
continue;
|
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) {
|
||||||
@ -318,7 +319,7 @@ const saveForeignKeys = (index, tableSchema, columns) => {
|
|||||||
.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(', ')})
|
||||||
|
Loading…
Reference in New Issue
Block a user