feat(PostgreSQL): unique keys management

This commit is contained in:
Fabio Di Stasio 2021-03-29 20:18:44 +02:00
parent feef5e30ee
commit 614e0d3275
2 changed files with 13 additions and 21 deletions

View File

@ -1,5 +1,5 @@
module.exports = [ module.exports = [
'PRIMARY', 'PRIMARY',
'KEY', 'INDEX',
'UNIQUE' 'UNIQUE'
]; ];

View File

@ -301,7 +301,7 @@ export class PostgreSQLClient extends AntaresCore {
SELECT pg_index.indexrelid, pg_class.oid SELECT pg_index.indexrelid, pg_class.oid
FROM pg_index, pg_class FROM pg_index, pg_class
WHERE pg_class.relname = '${table}' AND pg_class.oid = pg_index.indrelid), ndx_cols AS ( WHERE pg_class.relname = '${table}' AND pg_class.oid = pg_index.indrelid), ndx_cols AS (
SELECT pg_class.relname, UNNEST(i.indkey) AS col_ndx, CASE i.indisprimary WHEN TRUE THEN 'PRIMARY' ELSE CASE i.indisunique WHEN TRUE THEN 'UNIQUE' ELSE 'KEY' END END AS CONSTRAINT_TYPE, pg_class.oid SELECT pg_class.relname, UNNEST(i.indkey) AS col_ndx, CASE i.indisprimary WHEN TRUE THEN 'PRIMARY' ELSE CASE i.indisunique WHEN TRUE THEN 'UNIQUE' ELSE 'INDEX' END END AS CONSTRAINT_TYPE, pg_class.oid
FROM pg_class FROM pg_class
JOIN pg_index i ON (pg_class.oid = i.indexrelid) JOIN pg_index i ON (pg_class.oid = i.indexrelid)
JOIN ndx_list ON (pg_class.oid = ndx_list.indexrelid) JOIN ndx_list ON (pg_class.oid = ndx_list.indexrelid)
@ -1052,17 +1052,13 @@ export class PostgreSQLClient extends AntaresCore {
// ADD INDEX // ADD INDEX
indexChanges.additions.forEach(addition => { indexChanges.additions.forEach(addition => {
const fields = addition.fields.map(field => `\`${field}\``).join(','); const fields = addition.fields.map(field => `${field}`).join(',');
let type = addition.type; const type = addition.type;
if (type === 'PRIMARY') if (type === 'PRIMARY')
alterColumns.push(`ADD PRIMARY KEY (${fields})`); alterColumns.push(`ADD PRIMARY KEY (${fields})`);
else { else if (type === 'UNIQUE')
if (type === 'UNIQUE') alterColumns.push(`ADD CONSTRAINT ${addition.name} UNIQUE (${fields})`);
type = 'UNIQUE INDEX';
alterColumns.push(`ADD ${type} \`${addition.name}\` (${fields})`);
}
}); });
// ADD FOREIGN KEYS // ADD FOREIGN KEYS
@ -1108,19 +1104,15 @@ export class PostgreSQLClient extends AntaresCore {
if (change.oldType === 'PRIMARY') if (change.oldType === 'PRIMARY')
alterColumns.push('DROP PRIMARY KEY'); alterColumns.push('DROP PRIMARY KEY');
else else
alterColumns.push(`DROP INDEX \`${change.oldName}\``); alterColumns.push(`DROP CONSTRAINT ${change.oldName}`);
const fields = change.fields.map(field => `\`${field}\``).join(','); const fields = change.fields.map(field => `${field}`).join(',');
let type = change.type; const type = change.type;
if (type === 'PRIMARY') if (type === 'PRIMARY')
alterColumns.push(`ADD PRIMARY KEY (${fields})`); alterColumns.push(`ADD PRIMARY KEY (${fields})`);
else { else if (type === 'UNIQUE')
if (type === 'UNIQUE') alterColumns.push(`ADD CONSTRAINT ${change.name} UNIQUE (${fields})`);
type = 'UNIQUE INDEX';
alterColumns.push(`ADD ${type} \`${change.name}\` (${fields})`);
}
}); });
// CHANGE FOREIGN KEYS // CHANGE FOREIGN KEYS
@ -1139,12 +1131,12 @@ export class PostgreSQLClient extends AntaresCore {
if (deletion.type === 'PRIMARY') if (deletion.type === 'PRIMARY')
alterColumns.push('DROP PRIMARY KEY'); alterColumns.push('DROP PRIMARY KEY');
else else
alterColumns.push(`DROP INDEX \`${deletion.name}\``); alterColumns.push(`DROP CONSTRAINT ${deletion.name}`);
}); });
// DROP FOREIGN KEYS // DROP FOREIGN KEYS
foreignChanges.deletions.forEach(deletion => { foreignChanges.deletions.forEach(deletion => {
alterColumns.push(`DROP FOREIGN KEY \`${deletion.constraintName}\``); alterColumns.push(`DROP FOREIGN KEY ${deletion.constraintName}`);
}); });
if (alterColumns.length) sql += `ALTER TABLE "${table}" ${alterColumns.join(', ')}; `; if (alterColumns.length) sql += `ALTER TABLE "${table}" ${alterColumns.join(', ')}; `;