From 7d2ace94562f8da307b15b83c89d919727d800c8 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Fri, 16 Apr 2021 17:42:16 +0200 Subject: [PATCH] fix: field apparently loses index or foreign key on rename in table editor --- .github/FUNDING.yml | 2 +- src/main/libs/clients/PostgreSQLClient.js | 27 ++++++++++--------- src/renderer/components/WorkspacePropsTab.vue | 15 +++++++++++ .../components/WorkspacePropsTable.vue | 1 + .../components/WorkspacePropsTableRow.vue | 9 +++---- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index c8a50db0..87bcd77c 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,7 +1,7 @@ # These are supported funding model platforms github: [fabio286] -patreon: fabio286 +patreon: #fabio286 open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index ca3c88c1..f19af90f 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -1017,6 +1017,9 @@ export class PostgreSQLClient extends AntaresCore { options } = params; + if (this._schema !== 'public') + this.use(this._schema); + let sql = ''; const alterColumns = []; const renameColumns = []; @@ -1056,7 +1059,7 @@ export class PostgreSQLClient extends AntaresCore { else if (type === 'UNIQUE') alterColumns.push(`ADD CONSTRAINT ${addition.name} UNIQUE (${fields})`); else - manageIndexes.push(`CREATE INDEX ${addition.name} ON ${table}(${fields})`); + manageIndexes.push(`CREATE INDEX ${addition.name} ON ${this._schema}.${table}(${fields})`); }); // ADD FOREIGN KEYS @@ -1084,13 +1087,13 @@ export class PostgreSQLClient extends AntaresCore { localType = change.type.toLowerCase(); } - alterColumns.push(`ALTER COLUMN "${change.orgName}" TYPE ${localType}${length ? `(${length})` : ''}${change.isArray ? '[]' : ''} USING "${change.orgName}"::${localType}`); - alterColumns.push(`ALTER COLUMN "${change.orgName}" ${change.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'}`); - alterColumns.push(`ALTER COLUMN "${change.orgName}" ${change.default ? `SET DEFAULT ${change.default}` : 'DROP DEFAULT'}`); + alterColumns.push(`ALTER COLUMN "${change.name}" TYPE ${localType}${length ? `(${length})` : ''}${change.isArray ? '[]' : ''} USING "${change.name}"::${localType}`); + alterColumns.push(`ALTER COLUMN "${change.name}" ${change.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'}`); + alterColumns.push(`ALTER COLUMN "${change.name}" ${change.default ? `SET DEFAULT ${change.default}` : 'DROP DEFAULT'}`); if (['SERIAL', 'SMALLSERIAL', 'BIGSERIAL'].includes(change.type)) { const sequenceName = `${table}_${change.name}_seq`.replace(' ', '_'); - createSequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequenceName} OWNED BY "${table}"."${change.orgName}"`); - alterColumns.push(`ALTER COLUMN "${change.orgName}" SET DEFAULT nextval('${sequenceName}')`); + createSequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequenceName} OWNED BY "${table}"."${change.name}"`); + alterColumns.push(`ALTER COLUMN "${change.name}" SET DEFAULT nextval('${sequenceName}')`); } if (change.orgName !== change.name) @@ -1099,9 +1102,7 @@ export class PostgreSQLClient extends AntaresCore { // CHANGE INDEX indexChanges.changes.forEach(change => { - if (change.oldType === 'PRIMARY') - alterColumns.push('DROP PRIMARY KEY'); - else if (change.oldType === 'UNIQUE') + if (['PRIMARY', 'UNIQUE'].includes(change.oldType)) alterColumns.push(`DROP CONSTRAINT ${change.oldName}`); else manageIndexes.push(`DROP INDEX ${change.oldName}`); @@ -1114,7 +1115,7 @@ export class PostgreSQLClient extends AntaresCore { else if (type === 'UNIQUE') alterColumns.push(`ADD CONSTRAINT ${change.name} UNIQUE (${fields})`); else - manageIndexes.push(`CREATE INDEX ${change.name} ON ${table}(${fields})`); + manageIndexes.push(`CREATE INDEX ${change.name} ON ${this._schema}.${table}(${fields})`); }); // CHANGE FOREIGN KEYS @@ -1142,13 +1143,13 @@ export class PostgreSQLClient extends AntaresCore { }); if (alterColumns.length) sql += `ALTER TABLE "${this._schema}"."${table}" ${alterColumns.join(', ')}; `; - - // RENAME - if (renameColumns.length) sql += `${renameColumns.join(';')}; `; if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`; if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`; if (options.name) sql += `ALTER TABLE "${this._schema}"."${table}" RENAME TO "${options.name}"; `; + // RENAME + if (renameColumns.length) sql = `${renameColumns.join(';')}; ${sql}`; + return await this.raw(sql); } diff --git a/src/renderer/components/WorkspacePropsTab.vue b/src/renderer/components/WorkspacePropsTab.vue index c510cb18..ae098166 100644 --- a/src/renderer/components/WorkspacePropsTab.vue +++ b/src/renderer/components/WorkspacePropsTab.vue @@ -68,6 +68,7 @@ @remove-field="removeField" @add-new-index="addNewIndex" @add-to-index="addToIndex" + @rename-field="renameField" /> { + const fi = index.fields.findIndex(field => field === payload.old); + if (fi !== -1) + index.fields[fi] = payload.new; + return index; + }); + + this.localKeyUsage = this.localKeyUsage.map(key => { + if (key.field === payload.old) + key.field = payload.new; + return key; + }); + }, removeField (uid) { this.localFields = this.localFields.filter(field => field._id !== uid); }, diff --git a/src/renderer/components/WorkspacePropsTable.vue b/src/renderer/components/WorkspacePropsTable.vue index 49d7a1db..703551ea 100644 --- a/src/renderer/components/WorkspacePropsTable.vue +++ b/src/renderer/components/WorkspacePropsTable.vue @@ -115,6 +115,7 @@ :data-types="dataTypes" :customizations="customizations" @contextmenu="contextMenu" + @rename-field="$emit('rename-field', $event)" /> diff --git a/src/renderer/components/WorkspacePropsTableRow.vue b/src/renderer/components/WorkspacePropsTableRow.vue index b1dbddab..6fe5de69 100644 --- a/src/renderer/components/WorkspacePropsTableRow.vue +++ b/src/renderer/components/WorkspacePropsTableRow.vue @@ -440,9 +440,6 @@ export default { this.defaultValue.expression = this.localRow.default; } }, - updateRow () { - this.$emit('input', this.localRow); - }, editON (event, content, field) { if (field === 'length') { if (['integer', 'float', 'binary', 'spatial'].includes(this.fieldType.group)) this.editingField = 'numLength'; @@ -469,6 +466,9 @@ export default { } }, editOFF () { + if (this.editingField === 'name') + this.$emit('rename-field', { old: this.localRow[this.editingField], new: this.editingContent }); + this.localRow[this.editingField] = this.editingContent; if (this.editingField === 'type' && this.editingContent !== this.originalContent) { @@ -491,8 +491,7 @@ export default { if (!this.fieldType.zerofill) this.localRow.zerofill = false; } - - if (this.editingField === 'default') { + else if (this.editingField === 'default') { switch (this.defaultValue.type) { case 'autoincrement': this.localRow.autoIncrement = true;