Skip to content

Commit

Permalink
feat: table fields addition
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Nov 13, 2020
1 parent 249926b commit 0765403
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/main/libs/clients/MySQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,31 @@ export class MySQLClient extends AntaresCore {
async alterTable (params) {
const {
table,
// additions,
// deletions,
additions,
deletions,
changes
} = params;

let sql = `ALTER TABLE \`${table}\` `;
const alterColumns = [];

// ADD
additions.forEach(addition => {
const length = addition.numLength || addition.charLength || addition.datePrecision;

alterColumns.push(`ADD COLUMN \`${addition.name}\`
${addition.type.toUpperCase()}${length ? `(${length})` : ''}
${addition.unsigned ? 'UNSIGNED' : ''}
${addition.nullable ? 'NULL' : 'NOT NULL'}
${addition.autoIncrement ? 'AUTO_INCREMENT' : ''}
${addition.default ? `DEFAULT ${addition.default}` : ''}
${addition.comment ? `COMMENT '${addition.comment}'` : ''}
${addition.collation ? `COLLATE ${addition.collation}` : ''}
${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}
${addition.after ? `AFTER \`${addition.after}\`` : 'FIRST'}`);
});

// CHANGE
changes.forEach(change => {
const length = change.numLength || change.charLength || change.datePrecision;

Expand All @@ -299,6 +316,11 @@ export class MySQLClient extends AntaresCore {
${change.after ? `AFTER \`${change.after}\`` : 'FIRST'}`);
});

// DROP
deletions.forEach(deletion => {
alterColumns.push(`DROP COLUMN \`${deletion.name}\``);
});

sql += alterColumns.join(', ');

return await this.raw(sql);
Expand Down
43 changes: 41 additions & 2 deletions src/renderer/components/WorkspacePropsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

<div class="divider-vert py-3" />

<button class="btn btn-dark btn-sm" :title="$t('message.addNewField')">
<button
class="btn btn-dark btn-sm"
:title="$t('message.addNewField')"
@click="addField"
>
<span>{{ $t('word.add') }}</span>
<i class="mdi mdi-24px mdi-playlist-plus ml-1" />
</button>
Expand Down Expand Up @@ -168,7 +172,15 @@ export default {
const originalIDs = this.originalFields.reduce((acc, curr) => [...acc, curr._id], []);
const localIDs = this.localFields.reduce((acc, curr) => [...acc, curr._id], []);
const additions = this.localFields.filter(field => !originalIDs.includes(field._id));
// Additions
const additions = this.localFields.filter((field, i) => !originalIDs.includes(field._id)).map(field => {
const lI = this.localFields.findIndex(localField => localField._id === field._id);
const after = lI > 0 ? this.localFields[lI - 1].name : false;
return { ...field, after };
});
// Deletions
const deletions = this.originalFields.filter(field => !localIDs.includes(field._id));
// Changes
Expand Down Expand Up @@ -211,6 +223,33 @@ export default {
this.localFields = JSON.parse(JSON.stringify(this.originalFields));
this.localKeyUsage = JSON.parse(JSON.stringify(this.originalKeyUsage));
},
addField () {
this.localFields.push({
_id: uidGen(),
name: '',
key: '',
type: 'int',
schema: this.schema,
table: this.table,
numPrecision: null,
numLength: null,
datePrecision: null,
charLength: null,
nullable: false,
unsigned: false,
zerofill: false,
order: this.localFields.length + 1,
default: null,
charset: null,
collation: null,
autoIncrement: false,
onUpdate: '',
comment: ''
});
},
removeField (uid) {
this.localFields = this.localFields.filter(field => field._id !== uid);
},
showAddModal () {
this.isAddModal = true;
},
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/store/modules/workspaces.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default {
dataTypes = require('common/data-types/mysql');
break;
}
commit('ADD_CONNECTED', { // TODO: add data types
commit('ADD_CONNECTED', {
uid: connection.uid,
client: connection.client,
dataTypes,
Expand Down

0 comments on commit 0765403

Please sign in to comment.