Browse Source

fix: ignore user created associative table, remove schema and swagger json backup

re #496

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/498/head
Pranav C 3 years ago
parent
commit
24e98ed7d0
  1. 2
      packages/nc-gui/components/project/spreadsheet/components/headerCell.vue
  2. 2
      packages/nc-gui/components/project/spreadsheet/components/virtualHeaderCell.vue
  3. 10
      packages/nc-gui/components/project/tableTabs/logicGrpc.vue
  4. 10
      packages/nc-gui/components/project/tableTabs/logicRest.vue
  5. 2
      packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts
  6. 84
      packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts
  7. 14
      packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts
  8. 36
      packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts

2
packages/nc-gui/components/project/spreadsheet/components/headerCell.vue

@ -115,7 +115,7 @@
<v-divider /> <v-divider />
<v-card-text class="mt-4 title"> <v-card-text class="mt-4 title">
Do you want to delete <span class="font-weight-bold">'{{ Do you want to delete <span class="font-weight-bold">'{{
column.cn column._cn
}}'</span> column ? }}'</span> column ?
</v-card-text> </v-card-text>
<v-divider /> <v-divider />

2
packages/nc-gui/components/project/spreadsheet/components/virtualHeaderCell.vue

@ -82,7 +82,7 @@
<v-divider /> <v-divider />
<v-card-text class="mt-4 title"> <v-card-text class="mt-4 title">
Do you want to delete <span class="font-weight-bold">'{{ Do you want to delete <span class="font-weight-bold">'{{
column.cn column._cn
}}'</span> column ? }}'</span> column ?
</v-card-text> </v-card-text>
<v-divider /> <v-divider />

10
packages/nc-gui/components/project/tableTabs/logicGrpc.vue

@ -256,11 +256,11 @@ export default {
}]) }])
this.messages = tableMeta.messages this.messages = tableMeta.messages
this.services = tableMeta.services this.services = tableMeta.services
if (tableMeta.schema_previous) { // if (tableMeta.schema_previous) {
this.schemaHistory = JSON.parse(tableMeta.schema_previous).reverse() // this.schemaHistory = JSON.parse(tableMeta.schema_previous).reverse()
} else { // } else {
this.schemaHistory = [] // this.schemaHistory = []
} // }
}, },
async loadRpcs() { async loadRpcs() {
this.rpcServices = (await this.$store.dispatch('sqlMgr/ActSqlOp', [{ this.rpcServices = (await this.$store.dispatch('sqlMgr/ActSqlOp', [{

10
packages/nc-gui/components/project/tableTabs/logicRest.vue

@ -322,11 +322,11 @@ export default {
tn: this.nodes.tn || this.nodes.view_name tn: this.nodes.tn || this.nodes.view_name
}]) }])
this.swaggerDoc = JSON.stringify(JSON.parse(tableMeta.schema), 0, 2) this.swaggerDoc = JSON.stringify(JSON.parse(tableMeta.schema), 0, 2)
if (tableMeta.schema_previous) { // if (tableMeta.schema_previous) {
this.swaggerDocHistory = JSON.parse(tableMeta.schema_previous).reverse().map(o => JSON.stringify(o, null, 2)) // this.swaggerDocHistory = JSON.parse(tableMeta.schema_previous).reverse().map(o => JSON.stringify(o, null, 2))
} else { // } else {
this.swaggerDocHistory = [] // this.swaggerDocHistory = []
} // }
}, },
async saveSwaggerDoc() { async saveSwaggerDoc() {
this.edited = false this.edited = false

2
packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts

@ -1389,7 +1389,7 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
continue; continue;
} }
const tableMetaA = this.metas[meta.belongsTo[0].rtn]; const tableMetaA = this. metas[meta.belongsTo[0].rtn];
const tableMetaB = this.metas[meta.belongsTo[1].rtn]; const tableMetaB = this.metas[meta.belongsTo[1].rtn];
/* // remove hasmany relation with associate table from tables /* // remove hasmany relation with associate table from tables

84
packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts

@ -112,7 +112,9 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
} }
}); });
for (const {meta, id, title, schema_previous} of metas) { for (const {meta, id, title,
// schema_previous
} of metas) {
const metaObj = JSON.parse(meta); const metaObj = JSON.parse(meta);
/* filter relation where this table is present */ /* filter relation where this table is present */
const hasMany = metaObj.hasMany.filter(({enabled}) => enabled) const hasMany = metaObj.hasMany.filter(({enabled}) => enabled)
@ -127,15 +129,15 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
const newSchema = this.schemas[title] = GqlXcSchemaFactory.create(this.connectionConfig, this.generateRendererArgs(ctx)).getString(); const newSchema = this.schemas[title] = GqlXcSchemaFactory.create(this.connectionConfig, this.generateRendererArgs(ctx)).getString();
if (oldSchema !== this.schemas[title]) { if (oldSchema !== this.schemas[title]) {
// keep upto 5 schema backup on table update // keep upto 5 schema backup on table update
let previousSchemas = [oldSchema] // const previousSchemas = [oldSchema]
if (schema_previous) { // if (schema_previous) {
previousSchemas = [...JSON.parse(schema_previous), oldSchema].slice(-5); // previousSchemas = [...JSON.parse(schema_previous), oldSchema].slice(-5);
} // }
this.log(`onToggleModelRelation : Updating and taking backup of schema for '%s' table`, title) this.log(`onToggleModelRelation : Updating and taking backup of schema for '%s' table`, title)
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: newSchema, schema: newSchema,
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
id id
}); });
@ -1523,19 +1525,19 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
if (oldSchema !== this.schemas[tn]) { if (oldSchema !== this.schemas[tn]) {
this.log(`onTableUpdate : Updating and taking backup of schema - '%s' table`, changeObj.tn); this.log(`onTableUpdate : Updating and taking backup of schema - '%s' table`, changeObj.tn);
const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', { // const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', {
title: tn // title: tn
}); // });
// keep upto 5 schema backup on table update // keep upto 5 schema backup on table update
let previousSchemas = [oldSchema] // let previousSchemas = [oldSchema]
if (oldModel.schema_previous) { // if (oldModel.schema_previous) {
previousSchemas = [...JSON.parse(oldModel.schema_previous), oldSchema].slice(-5); // previousSchemas = [...JSON.parse(oldModel.schema_previous), oldSchema].slice(-5);
} // }
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: this.schemas[tn], schema: this.schemas[tn],
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: tn title: tn
}); });
@ -1567,19 +1569,19 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
this.log(`onViewUpdate : Updating and taking backup of schema - '%s' view`, viewName); this.log(`onViewUpdate : Updating and taking backup of schema - '%s' view`, viewName);
const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', { // const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', {
title: viewName // title: viewName
}); // });
// keep upto 5 schema backup on table update // // keep upto 5 schema backup on table update
let previousSchemas = [oldSchema] // let previousSchemas = [oldSchema]
if (oldModel.schema_previous) { // if (oldModel.schema_previous) {
previousSchemas = [...JSON.parse(oldModel.schema_previous), oldSchema].slice(-5); // previousSchemas = [...JSON.parse(oldModel.schema_previous), oldSchema].slice(-5);
} // }
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: meta.schema, schema: meta.schema,
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: viewName title: viewName
}); });
@ -1641,19 +1643,19 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
const newSchemaa = GqlXcSchemaFactory.create(this.connectionConfig, this.generateRendererArgs(ctx)).getString(); const newSchemaa = GqlXcSchemaFactory.create(this.connectionConfig, this.generateRendererArgs(ctx)).getString();
if (newSchemaa !== this.schemas[tn]) { if (newSchemaa !== this.schemas[tn]) {
const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', { // const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', {
title: tn // title: tn
}); // });
this.log(`onToggleModels : Updating and taking backup of schema for '%s'`, tn); // this.log(`onToggleModels : Updating and taking backup of schema for '%s'`, tn);
let previousSchemas = [this.schemas[tn]]; // let previousSchemas = [this.schemas[tn]];
if (oldModel.schema_previous) { // if (oldModel.schema_previous) {
previousSchemas = [...JSON.parse(oldModel.schema_previous), [this.schemas[tn]]].slice(-5); // previousSchemas = [...JSON.parse(oldModel.schema_previous), [this.schemas[tn]]].slice(-5);
} // }
this.schemas[tn] = newSchemaa; this.schemas[tn] = newSchemaa;
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: this.schemas[tn], schema: this.schemas[tn],
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: tn title: tn
}) })
@ -2019,19 +2021,19 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
if (oldSchema !== this.schemas[tn]) { if (oldSchema !== this.schemas[tn]) {
// this.log(`onTableUpdate : Updating and taking backup of schema - '%s' table`, tn); // this.log(`onTableUpdate : Updating and taking backup of schema - '%s' table`, tn);
const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', { // const oldModel = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', {
title: tn // title: tn
}); // });
// keep upto 5 schema backup on table update // // keep upto 5 schema backup on table update
let previousSchemas = [oldSchema] // let previousSchemas = [oldSchema]
if (oldModel.schema_previous) { // if (oldModel.schema_previous) {
previousSchemas = [...JSON.parse(oldModel.schema_previous), oldSchema].slice(-5); // previousSchemas = [...JSON.parse(oldModel.schema_previous), oldSchema].slice(-5);
} // }
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: this.schemas[tn], schema: this.schemas[tn],
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: tn, title: tn,
type: 'table' type: 'table'

14
packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts

@ -406,7 +406,7 @@ export default class NcMetaMgr {
const projectDetails = projectDetailsJSON && JSON.parse(projectDetailsJSON); const projectDetails = projectDetailsJSON && JSON.parse(projectDetailsJSON);
if (args.args.importsToCurrentProject) { if (args.args.importsToCurrentProject) {
await promisify(ncp)(path.join(this.config.toolDir, 'uploads', 'nc', projectDetails.id), path.join(this.config.toolDir, 'nc', projectId), {clobber:true}) await promisify(ncp)(path.join(this.config.toolDir, 'uploads', 'nc', projectDetails.id), path.join(this.config.toolDir, 'nc', projectId), {clobber: true})
} else { } else {
// decrypt with old key and encrypt again with latest key // decrypt with old key and encrypt again with latest key
const projectConfig = JSON.parse(CryptoJS.AES.decrypt(projectDetails.config, projectDetails.key).toString(CryptoJS.enc.Utf8)) const projectConfig = JSON.parse(CryptoJS.AES.decrypt(projectDetails.config, projectDetails.key).toString(CryptoJS.enc.Utf8))
@ -2413,6 +2413,7 @@ export default class NcMetaMgr {
} }
// todo: transaction
protected async xcM2MRelationCreate(args: any, req): Promise<any> { protected async xcM2MRelationCreate(args: any, req): Promise<any> {
const dbAlias = this.getDbAlias(args); const dbAlias = this.getDbAlias(args);
const projectId = this.getProjectId(args); const projectId = this.getProjectId(args);
@ -2529,7 +2530,7 @@ export default class NcMetaMgr {
} }
}); });
} }
const outrel1 = await this.projectMgr.getSqlMgr({id: projectId}).handleRequest('n', { const outrel1 = await this.projectMgr.getSqlMgr({id: projectId}).handleRequest('relationCreate', {
...args, ...args,
args: rel2Args args: rel2Args
}); });
@ -2677,6 +2678,7 @@ export default class NcMetaMgr {
const assocMeta = JSON.parse(assoc.meta); const assocMeta = JSON.parse(assoc.meta);
const rel1 = assocMeta.belongsTo.find(bt => bt.rtn === args.args.parentTable) const rel1 = assocMeta.belongsTo.find(bt => bt.rtn === args.args.parentTable)
const rel2 = assocMeta.belongsTo.find(bt => bt.rtn === args.args.childTable) const rel2 = assocMeta.belongsTo.find(bt => bt.rtn === args.args.childTable)
if (rel1) {
await this.xcRelationColumnDelete({ await this.xcRelationColumnDelete({
...args, ...args,
args: { args: {
@ -2688,6 +2690,8 @@ export default class NcMetaMgr {
type: 'bt', type: 'bt',
} }
}, req, false) }, req, false)
}
if (rel2) {
await this.xcRelationColumnDelete({ await this.xcRelationColumnDelete({
...args, ...args,
args: { args: {
@ -2699,6 +2703,10 @@ export default class NcMetaMgr {
type: 'bt', type: 'bt',
} }
}, req, false); }, req, false);
}
// ignore deleting table if it have more than 2 columns
if (assocMeta.columns.length === 2) {
const opArgs = { const opArgs = {
@ -2707,6 +2715,7 @@ export default class NcMetaMgr {
api: 'tableDelete', api: 'tableDelete',
sqlOpPlus: true, sqlOpPlus: true,
}; };
const out = await this.projectMgr.getSqlMgr({id: projectId}).handleRequest('tableDelete', opArgs); const out = await this.projectMgr.getSqlMgr({id: projectId}).handleRequest('tableDelete', opArgs);
if (this.listener) { if (this.listener) {
@ -2717,6 +2726,7 @@ export default class NcMetaMgr {
ctx: {req} ctx: {req}
}); });
} }
}
} }
break; break;

36
packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts

@ -1473,17 +1473,17 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}); });
const oldSwaggerDoc = JSON.parse(meta.schema); const oldSwaggerDoc = JSON.parse(meta.schema);
// keep upto 5 schema backup on table update // // keep upto 5 schema backup on table update
let previousSchemas = [oldSwaggerDoc] // let previousSchemas = [oldSwaggerDoc]
if (meta.schema_previous) { // if (meta.schema_previous) {
previousSchemas = [...JSON.parse(meta.schema_previous), oldSwaggerDoc].slice(-5); // previousSchemas = [...JSON.parse(meta.schema_previous), oldSwaggerDoc].slice(-5);
} // }
oldSwaggerDoc.definitions = swaggerDoc.definitions; oldSwaggerDoc.definitions = swaggerDoc.definitions;
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: JSON.stringify(oldSwaggerDoc), schema: JSON.stringify(oldSwaggerDoc),
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: changeObj.tn, title: changeObj.tn,
type: 'table' type: 'table'
@ -1779,17 +1779,17 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}); });
const oldSwaggerDoc = JSON.parse(meta.schema); const oldSwaggerDoc = JSON.parse(meta.schema);
// keep upto 5 schema backup on table update // // keep upto 5 schema backup on table update
let previousSchemas = [oldSwaggerDoc] // let previousSchemas = [oldSwaggerDoc]
if (meta.schema_previous) { // if (meta.schema_previous) {
previousSchemas = [...JSON.parse(meta.schema_previous), oldSwaggerDoc].slice(-5); // previousSchemas = [...JSON.parse(meta.schema_previous), oldSwaggerDoc].slice(-5);
} // }
oldSwaggerDoc.definitions = swaggerDoc.definitions; oldSwaggerDoc.definitions = swaggerDoc.definitions;
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: JSON.stringify(oldSwaggerDoc), schema: JSON.stringify(oldSwaggerDoc),
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: tn, title: tn,
type: 'table' type: 'table'
@ -1825,16 +1825,16 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}); });
const oldSwaggerDoc = JSON.parse(meta.schema); const oldSwaggerDoc = JSON.parse(meta.schema);
// keep upto 5 schema backup on table update // // keep upto 5 schema backup on table update
let previousSchemas = [oldSwaggerDoc] // let previousSchemas = [oldSwaggerDoc]
if (meta.schema_previous) { // if (meta.schema_previous) {
previousSchemas = [...JSON.parse(meta.schema_previous), oldSwaggerDoc].slice(-5); // previousSchemas = [...JSON.parse(meta.schema_previous), oldSwaggerDoc].slice(-5);
} // }
oldSwaggerDoc.definitions = swaggerDoc.definitions; oldSwaggerDoc.definitions = swaggerDoc.definitions;
await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', {
schema: JSON.stringify(oldSwaggerDoc), schema: JSON.stringify(oldSwaggerDoc),
schema_previous: JSON.stringify(previousSchemas) // schema_previous: JSON.stringify(previousSchemas)
}, { }, {
title: metaObj.tn, title: metaObj.tn,
type: 'table' type: 'table'

Loading…
Cancel
Save