fix: field apparently loses index or foreign key on rename in table editor

This commit is contained in:
Fabio Di Stasio 2021-04-16 17:42:16 +02:00
parent 2584c9b9c2
commit 7d2ace9456
5 changed files with 35 additions and 19 deletions

2
.github/FUNDING.yml vendored
View File

@ -1,7 +1,7 @@
# These are supported funding model platforms # These are supported funding model platforms
github: [fabio286] github: [fabio286]
patreon: fabio286 patreon: #fabio286
open_collective: # Replace with a single Open Collective username open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel

View File

@ -1017,6 +1017,9 @@ export class PostgreSQLClient extends AntaresCore {
options options
} = params; } = params;
if (this._schema !== 'public')
this.use(this._schema);
let sql = ''; let sql = '';
const alterColumns = []; const alterColumns = [];
const renameColumns = []; const renameColumns = [];
@ -1056,7 +1059,7 @@ export class PostgreSQLClient extends AntaresCore {
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 else
manageIndexes.push(`CREATE INDEX ${addition.name} ON ${table}(${fields})`); manageIndexes.push(`CREATE INDEX ${addition.name} ON ${this._schema}.${table}(${fields})`);
}); });
// ADD FOREIGN KEYS // ADD FOREIGN KEYS
@ -1084,13 +1087,13 @@ export class PostgreSQLClient extends AntaresCore {
localType = change.type.toLowerCase(); 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.name}" TYPE ${localType}${length ? `(${length})` : ''}${change.isArray ? '[]' : ''} USING "${change.name}"::${localType}`);
alterColumns.push(`ALTER COLUMN "${change.orgName}" ${change.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'}`); alterColumns.push(`ALTER COLUMN "${change.name}" ${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}" ${change.default ? `SET DEFAULT ${change.default}` : 'DROP DEFAULT'}`);
if (['SERIAL', 'SMALLSERIAL', 'BIGSERIAL'].includes(change.type)) { if (['SERIAL', 'SMALLSERIAL', 'BIGSERIAL'].includes(change.type)) {
const sequenceName = `${table}_${change.name}_seq`.replace(' ', '_'); const sequenceName = `${table}_${change.name}_seq`.replace(' ', '_');
createSequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequenceName} OWNED BY "${table}"."${change.orgName}"`); createSequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequenceName} OWNED BY "${table}"."${change.name}"`);
alterColumns.push(`ALTER COLUMN "${change.orgName}" SET DEFAULT nextval('${sequenceName}')`); alterColumns.push(`ALTER COLUMN "${change.name}" SET DEFAULT nextval('${sequenceName}')`);
} }
if (change.orgName !== change.name) if (change.orgName !== change.name)
@ -1099,9 +1102,7 @@ export class PostgreSQLClient extends AntaresCore {
// CHANGE INDEX // CHANGE INDEX
indexChanges.changes.forEach(change => { indexChanges.changes.forEach(change => {
if (change.oldType === 'PRIMARY') if (['PRIMARY', 'UNIQUE'].includes(change.oldType))
alterColumns.push('DROP PRIMARY KEY');
else if (change.oldType === 'UNIQUE')
alterColumns.push(`DROP CONSTRAINT ${change.oldName}`); alterColumns.push(`DROP CONSTRAINT ${change.oldName}`);
else else
manageIndexes.push(`DROP INDEX ${change.oldName}`); manageIndexes.push(`DROP INDEX ${change.oldName}`);
@ -1114,7 +1115,7 @@ export class PostgreSQLClient extends AntaresCore {
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 else
manageIndexes.push(`CREATE INDEX ${change.name} ON ${table}(${fields})`); manageIndexes.push(`CREATE INDEX ${change.name} ON ${this._schema}.${table}(${fields})`);
}); });
// CHANGE FOREIGN KEYS // CHANGE FOREIGN KEYS
@ -1142,13 +1143,13 @@ export class PostgreSQLClient extends AntaresCore {
}); });
if (alterColumns.length) sql += `ALTER TABLE "${this._schema}"."${table}" ${alterColumns.join(', ')}; `; 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 (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`; if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`;
if (options.name) sql += `ALTER TABLE "${this._schema}"."${table}" RENAME TO "${options.name}"; `; 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); return await this.raw(sql);
} }

View File

@ -68,6 +68,7 @@
@remove-field="removeField" @remove-field="removeField"
@add-new-index="addNewIndex" @add-new-index="addNewIndex"
@add-to-index="addToIndex" @add-to-index="addToIndex"
@rename-field="renameField"
/> />
</div> </div>
<WorkspacePropsOptionsModal <WorkspacePropsOptionsModal
@ -457,6 +458,20 @@ export default {
scrollable.scrollTop = scrollable.scrollHeight + 30; scrollable.scrollTop = scrollable.scrollHeight + 30;
}, 20); }, 20);
}, },
renameField (payload) {
this.localIndexes = this.localIndexes.map(index => {
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) { removeField (uid) {
this.localFields = this.localFields.filter(field => field._id !== uid); this.localFields = this.localFields.filter(field => field._id !== uid);
}, },

View File

@ -115,6 +115,7 @@
:data-types="dataTypes" :data-types="dataTypes"
:customizations="customizations" :customizations="customizations"
@contextmenu="contextMenu" @contextmenu="contextMenu"
@rename-field="$emit('rename-field', $event)"
/> />
</draggable> </draggable>
</div> </div>

View File

@ -440,9 +440,6 @@ export default {
this.defaultValue.expression = this.localRow.default; this.defaultValue.expression = this.localRow.default;
} }
}, },
updateRow () {
this.$emit('input', this.localRow);
},
editON (event, content, field) { editON (event, content, field) {
if (field === 'length') { if (field === 'length') {
if (['integer', 'float', 'binary', 'spatial'].includes(this.fieldType.group)) this.editingField = 'numLength'; if (['integer', 'float', 'binary', 'spatial'].includes(this.fieldType.group)) this.editingField = 'numLength';
@ -469,6 +466,9 @@ export default {
} }
}, },
editOFF () { editOFF () {
if (this.editingField === 'name')
this.$emit('rename-field', { old: this.localRow[this.editingField], new: this.editingContent });
this.localRow[this.editingField] = this.editingContent; this.localRow[this.editingField] = this.editingContent;
if (this.editingField === 'type' && this.editingContent !== this.originalContent) { if (this.editingField === 'type' && this.editingContent !== this.originalContent) {
@ -491,8 +491,7 @@ export default {
if (!this.fieldType.zerofill) if (!this.fieldType.zerofill)
this.localRow.zerofill = false; this.localRow.zerofill = false;
} }
else if (this.editingField === 'default') {
if (this.editingField === 'default') {
switch (this.defaultValue.type) { switch (this.defaultValue.type) {
case 'autoincrement': case 'autoincrement':
this.localRow.autoIncrement = true; this.localRow.autoIncrement = true;