Browse Source

Merge pull request #5832 from nocodb/fix/5814-reusing-tablename

fix: Clear alias cache when renaming table/view
pull/5839/head
Pranav C 1 year ago committed by GitHub
parent
commit
f81ed92e2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      packages/nocodb/src/models/Model.ts
  2. 29
      packages/nocodb/src/models/View.ts
  3. 1
      packages/nocodb/src/modules/datas/helpers.ts
  4. 2
      packages/nocodb/tests/unit/rest/tests/viewRow.test.ts

12
packages/nocodb/src/models/Model.ts

@ -570,13 +570,25 @@ export default class Model implements TableType {
// get existing cache
const key = `${CacheScope.MODEL}:${tableId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let oldModel = { ...o };
// update alias
if (o) {
o.title = title;
o.table_name = table_name;
// set cache
await NocoCache.set(key, o);
} else {
oldModel = await this.get(tableId);
}
// delete alias cache
await NocoCache.del(
`${CacheScope.MODEL}:${oldModel.project_id}:${oldModel.base_id}:${oldModel.title}`,
);
await NocoCache.del(
`${CacheScope.MODEL}:${oldModel.project_id}:${oldModel.title}`,
);
// set meta
return await ncMeta.metaUpdate(
null,

29
packages/nocodb/src/models/View.ts

@ -1,3 +1,4 @@
import { title } from 'process';
import { isSystemColumn, UITypes, ViewTypes } from 'nocodb-sdk';
import Noco from '../Noco';
import {
@ -165,13 +166,19 @@ export default class View implements ViewType {
],
},
);
view.meta = parseMetaProp(view);
// todo: cache - titleOrId can be viewId so we need a different scope here
await NocoCache.set(
`${CacheScope.VIEW}:${fk_model_id}:${titleOrId}`,
view.id,
);
await NocoCache.set(`${CacheScope.VIEW}:${fk_model_id}:${view.id}`, view);
if (view) {
await NocoCache.set(
`${CacheScope.VIEW}:${fk_model_id}:${view.id}`,
view,
);
view.meta = parseMetaProp(view);
// todo: cache - titleOrId can be viewId so we need a different scope here
await NocoCache.set(
`${CacheScope.VIEW}:${fk_model_id}:${titleOrId}`,
view.id,
);
}
return view && new View(view);
}
return viewId && this.get(viewId?.id || viewId);
@ -952,6 +959,7 @@ export default class View implements ViewType {
// get existing cache
const key = `${CacheScope.VIEW}:${viewId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let oldView = { ...o };
if (o) {
// update data
o = {
@ -963,8 +971,15 @@ export default class View implements ViewType {
}
// set cache
await NocoCache.set(key, o);
} else {
oldView = await this.get(viewId);
}
// reset alias cache
await NocoCache.del(
`${CacheScope.VIEW}:${oldView.fk_model_id}:${oldView.title}`,
);
// if meta data defined then stringify it
if ('meta' in updateObj) {
updateObj.meta = stringifyMetaProp(updateObj);

1
packages/nocodb/src/modules/datas/helpers.ts

@ -43,6 +43,7 @@ export async function getViewAndModelByAliasOrId(param: {
fk_model_id: model.id,
}));
if (!model) NcError.notFound('Table not found');
if (param.viewName && !view) NcError.notFound('View not found');
return { model, view };
}

2
packages/nocodb/tests/unit/rest/tests/viewRow.test.ts

@ -780,7 +780,7 @@ function viewRowTests() {
.send({
title: 'Test',
})
.expect(400);
.expect(404);
};
it('Create table row grid wrong grid id', async function () {

Loading…
Cancel
Save