mirror of
https://github.com/Fabio286/antares.git
synced 2025-02-02 18:36:52 +01:00
fix: field apparently loses index or foreign key on rename in table editor
This commit is contained in:
parent
2584c9b9c2
commit
7d2ace9456
2
.github/FUNDING.yml
vendored
2
.github/FUNDING.yml
vendored
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -68,6 +68,7 @@
|
||||
@remove-field="removeField"
|
||||
@add-new-index="addNewIndex"
|
||||
@add-to-index="addToIndex"
|
||||
@rename-field="renameField"
|
||||
/>
|
||||
</div>
|
||||
<WorkspacePropsOptionsModal
|
||||
@ -457,6 +458,20 @@ export default {
|
||||
scrollable.scrollTop = scrollable.scrollHeight + 30;
|
||||
}, 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) {
|
||||
this.localFields = this.localFields.filter(field => field._id !== uid);
|
||||
},
|
||||
|
@ -115,6 +115,7 @@
|
||||
:data-types="dataTypes"
|
||||
:customizations="customizations"
|
||||
@contextmenu="contextMenu"
|
||||
@rename-field="$emit('rename-field', $event)"
|
||||
/>
|
||||
</draggable>
|
||||
</div>
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user