feat(PostgreSQL): indexes management

This commit is contained in:
Fabio Di Stasio 2021-03-30 19:07:04 +02:00
parent affb7288b0
commit 9ca03f4625
1 changed files with 13 additions and 5 deletions

View File

@ -1000,7 +1000,7 @@ export class PostgreSQLClient extends AntaresCore {
name name
} = params; } = params;
const sql = `CREATE TABLE ${name} (${name}_id INTEGER NULL)`; const sql = `CREATE TABLE ${name} (${name}_id INTEGER NULL); ALTER TABLE ${name} DROP COLUMN ${name}_id`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -1026,6 +1026,7 @@ export class PostgreSQLClient extends AntaresCore {
const alterColumns = []; const alterColumns = [];
const renameColumns = []; const renameColumns = [];
const createSequences = []; const createSequences = [];
const manageIndexes = [];
// OPTIONS // OPTIONS
if ('comment' in options) alterColumns.push(`COMMENT='${options.comment}'`); if ('comment' in options) alterColumns.push(`COMMENT='${options.comment}'`);
@ -1059,6 +1060,8 @@ export class PostgreSQLClient extends AntaresCore {
alterColumns.push(`ADD PRIMARY KEY (${fields})`); alterColumns.push(`ADD PRIMARY KEY (${fields})`);
else if (type === 'UNIQUE') else if (type === 'UNIQUE')
alterColumns.push(`ADD CONSTRAINT ${addition.name} UNIQUE (${fields})`); alterColumns.push(`ADD CONSTRAINT ${addition.name} UNIQUE (${fields})`);
else
manageIndexes.push(`CREATE INDEX ${addition.name} ON ${table}(${fields})`);
}); });
// ADD FOREIGN KEYS // ADD FOREIGN KEYS
@ -1103,8 +1106,10 @@ export class PostgreSQLClient extends AntaresCore {
indexChanges.changes.forEach(change => { indexChanges.changes.forEach(change => {
if (change.oldType === 'PRIMARY') if (change.oldType === 'PRIMARY')
alterColumns.push('DROP PRIMARY KEY'); alterColumns.push('DROP PRIMARY KEY');
else else if (change.oldType === 'UNIQUE')
alterColumns.push(`DROP CONSTRAINT ${change.oldName}`); alterColumns.push(`DROP CONSTRAINT ${change.oldName}`);
else
manageIndexes.push(`DROP INDEX ${change.oldName}`);
const fields = change.fields.map(field => `${field}`).join(','); const fields = change.fields.map(field => `${field}`).join(',');
const type = change.type; const type = change.type;
@ -1113,6 +1118,8 @@ export class PostgreSQLClient extends AntaresCore {
alterColumns.push(`ADD PRIMARY KEY (${fields})`); alterColumns.push(`ADD PRIMARY KEY (${fields})`);
else if (type === 'UNIQUE') else if (type === 'UNIQUE')
alterColumns.push(`ADD CONSTRAINT ${change.name} UNIQUE (${fields})`); alterColumns.push(`ADD CONSTRAINT ${change.name} UNIQUE (${fields})`);
else
manageIndexes.push(`CREATE INDEX ${change.name} ON ${table}(${fields})`);
}); });
// CHANGE FOREIGN KEYS // CHANGE FOREIGN KEYS
@ -1128,10 +1135,10 @@ export class PostgreSQLClient extends AntaresCore {
// DROP INDEX // DROP INDEX
indexChanges.deletions.forEach(deletion => { indexChanges.deletions.forEach(deletion => {
if (deletion.type === 'PRIMARY') if (['PRIMARY', 'UNIQUE'].includes(deletion.type))
alterColumns.push('DROP PRIMARY KEY');
else
alterColumns.push(`DROP CONSTRAINT ${deletion.name}`); alterColumns.push(`DROP CONSTRAINT ${deletion.name}`);
else
manageIndexes.push(`DROP INDEX ${deletion.name}`);
}); });
// DROP FOREIGN KEYS // DROP FOREIGN KEYS
@ -1144,6 +1151,7 @@ export class PostgreSQLClient extends AntaresCore {
// RENAME // RENAME
if (renameColumns.length) sql += `${renameColumns.join(';')}; `; if (renameColumns.length) sql += `${renameColumns.join(';')}; `;
if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`; if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`;
if (options.name) sql += `ALTER TABLE "${table}" RENAME TO "${options.name}"; `; if (options.name) sql += `ALTER TABLE "${table}" RENAME TO "${options.name}"; `;
return await this.raw(sql); return await this.raw(sql);