console: fix alter primary key for cockroachdb

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6292
GitOrigin-RevId: 0f449e08837c174627fb69ee611411b44ba88b62
This commit is contained in:
Luca Restagno 2022-10-11 18:07:51 +02:00 committed by hasura-bot
parent 1ffc19367d
commit 950836849d

View File

@ -790,24 +790,27 @@ export const getCreatePkSql = ({
}) => `alter table "${schemaName}"."${tableName}"
add constraint "${constraintName}"
primary key (${selectedPkColumns.map(pkc => `"${pkc}"`).join(', ')});`;
type GetAlterPkSql = {
schemaName: string;
tableName: string;
selectedPkColumns: string[];
constraintName: string;
};
export const getAlterPkSql = ({
schemaName,
tableName,
selectedPkColumns,
constraintName,
}: {
schemaName: string;
tableName: string;
selectedPkColumns: string[];
constraintName: string; // compulsory for PG
}) => `BEGIN TRANSACTION;
ALTER TABLE "${schemaName}"."${tableName}" DROP CONSTRAINT "${constraintName}";
}: GetAlterPkSql) => {
const underscoreColumnNames = selectedPkColumns.join('_');
const newPkConstraintName = `${tableName}_pkey_${underscoreColumnNames}`;
const columnsSqlList = selectedPkColumns.map(pkc => `"${pkc}"`).join(', ');
ALTER TABLE "${schemaName}"."${tableName}"
ADD CONSTRAINT "${constraintName}" PRIMARY KEY (${selectedPkColumns
.map(pkc => `"${pkc}"`)
.join(', ')});
COMMIT TRANSACTION;`;
return `
ALTER TABLE "${schemaName}"."${tableName}" DROP CONSTRAINT "${constraintName}";
ALTER TABLE "${schemaName}"."${tableName}" ADD CONSTRAINT "${newPkConstraintName}" PRIMARY KEY (${columnsSqlList});`;
};
const trackableFunctionsWhere = `
AND has_variadic = FALSE