Browse Source

fix(gui): block table deletion if there is a relation exist

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1886/head
Pranav C 2 years ago
parent
commit
0611c26e0e
  1. 108
      packages/nc-gui/components/project/table.vue

108
packages/nc-gui/components/project/table.vue

@ -14,11 +14,12 @@
> >
<template v-if="_isUIAllowed('smartSheet')"> <template v-if="_isUIAllowed('smartSheet')">
<v-tab v-show="relationTabs && relationTabs.length" class=""> <v-tab v-show="relationTabs && relationTabs.length" class="">
<v-icon small> mdi-table-edit </v-icon>&nbsp;<span <v-icon small>
mdi-table-edit
</v-icon>&nbsp;<span
class="caption text-capitalize font-weight-bold" class="caption text-capitalize font-weight-bold"
> >
{{ nodes.title }}</span {{ nodes.title }}</span>
>
</v-tab> </v-tab>
<v-tab-item style="height: 100%"> <v-tab-item style="height: 100%">
<rows-xc-data-table <rows-xc-data-table
@ -49,15 +50,16 @@
</template> </template>
<script> <script>
import { mapActions } from "vuex"; import { mapActions } from 'vuex'
import dlgLabelSubmitCancel from "../utils/dlgLabelSubmitCancel"; import { UITypes } from 'nocodb-sdk'
import { isMetaTable } from "@/helpers/xutils"; import dlgLabelSubmitCancel from '../utils/dlgLabelSubmitCancel'
import RowsXcDataTable from "@/components/project/spreadsheet/rowsXcDataTable"; import { isMetaTable } from '@/helpers/xutils'
import RowsXcDataTable from '@/components/project/spreadsheet/rowsXcDataTable'
export default { export default {
components: { components: {
RowsXcDataTable, RowsXcDataTable,
dlgLabelSubmitCancel, dlgLabelSubmitCancel
}, },
data() { data() {
return { return {
@ -72,87 +74,101 @@ export default {
loadRows: false, loadRows: false,
loadColumnsMock: false, loadColumnsMock: false,
relationTabs: [], relationTabs: [],
deleteId: null, deleteId: null
}; }
}, },
methods: { methods: {
async handleKeyDown(event) { async handleKeyDown(event) {
const activeTabEleKey = `tabs${this.active}`; const activeTabEleKey = `tabs${this.active}`
if ( if (
this.$refs[activeTabEleKey] && this.$refs[activeTabEleKey] &&
this.$refs[activeTabEleKey].handleKeyDown this.$refs[activeTabEleKey].handleKeyDown
) { ) {
await this.$refs[activeTabEleKey].handleKeyDown(event); await this.$refs[activeTabEleKey].handleKeyDown(event)
} }
}, },
...mapActions({ ...mapActions({
removeTableTab: "tabs/removeTableTab", removeTableTab: 'tabs/removeTableTab',
loadTablesFromParentTreeNode: "project/loadTablesFromParentTreeNode", loadTablesFromParentTreeNode: 'project/loadTablesFromParentTreeNode'
}), }),
mtdNewTableUpdate(value) { mtdNewTableUpdate(value) {
this.newTableCopy = value; this.newTableCopy = value
}, },
async deleteTable(action = "", id) { async deleteTable(action = '', id) {
if (id) { if (id) {
this.deleteId = id; this.deleteId = id
} }
if (action === "showDialog") { if (action === 'showDialog') {
this.dialogShow = true; this.dialogShow = true
} else if (action === "hideDialog") { } else if (action === 'hideDialog') {
this.dialogShow = false; this.dialogShow = false
} else { } else {
// todo : check relations and triggers
try { try {
await this.$api.dbTable.delete(this.deleteId); const meta = await this.$store.dispatch('meta/ActLoadMeta', { id: this.deleteId })
const relationColumns = meta.columns.filter(c => c.uidt === UITypes.LinkToAnotherRecord)
if (relationColumns.length) {
const referredTables = await Promise.all(relationColumns.map(async(c) => {
const refMeta = await this.$store.dispatch('meta/ActLoadMeta', { id: c.colOptions.fk_related_model_id })
return (refMeta && refMeta.title) || c.title
}))
this.$toast.info('Table can\'t be deleted since Table is being referred in following tables : ' + referredTables.join(', ') +
'. Delete relation columns and try again.').goAway(10000)
this.dialogShow = false
return
}
await this.$api.dbTable.delete(this.deleteId)
this.removeTableTab({ this.removeTableTab({
env: this.nodes.env, env: this.nodes.env,
dbAlias: this.nodes.dbAlias, dbAlias: this.nodes.dbAlias,
table_name: this.nodes.table_name, table_name: this.nodes.table_name
}); })
await this.loadTablesFromParentTreeNode({ await this.loadTablesFromParentTreeNode({
_nodes: { _nodes: {
...this.nodes, ...this.nodes
}, }
}); })
this.$store.commit("meta/MutMeta", { this.$store.commit('meta/MutMeta', {
key: this.nodes.table_name, key: this.nodes.table_name,
value: null, value: null
}); })
this.$store.commit("meta/MutMeta", { this.$store.commit('meta/MutMeta', {
key: this.deleteId, key: this.deleteId,
value: null, value: null
}); })
} catch (e) { } catch (e) {
const msg = await this._extractSdkResponseErrorMsg(e); const msg = await this._extractSdkResponseErrorMsg(e)
this.$toast.error(msg).goAway(3000); this.$toast.error(msg).goAway(3000)
} }
this.dialogShow = false; this.dialogShow = false
this.$e("a:table:delete"); this.$e('a:table:delete')
} }
}, },
onTabChange() { onTabChange() {
this.$emit("update:hideLogWindows", this.active === 2); this.$emit('update:hideLogWindows', this.active === 2)
}, }
}, },
computed: { computed: {
isMetaTable() { isMetaTable() {
return isMetaTable(this.nodes.table_name); return isMetaTable(this.nodes.table_name)
}, }
}, },
mounted() { mounted() {
this.onTabChange(); this.onTabChange()
}, },
props: { props: {
nodes: Object, nodes: Object,
hideLogWindows: Boolean, hideLogWindows: Boolean,
tabId: String, tabId: String,
isActive: Boolean, isActive: Boolean,
isView: Boolean, isView: Boolean
}, }
}; }
</script> </script>
<style scoped> <style scoped>

Loading…
Cancel
Save