chore(collection-manager): should not throw error when source collection destoryed (#1999)

This commit is contained in:
ChengLei Shao 2023-06-07 11:06:35 +08:00 committed by GitHub
parent 83a6cae4e5
commit 83dc81c51b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 6 deletions

View File

@ -113,11 +113,6 @@ SELECT * FROM numbers;
2,
),
);
// cannot get field type in sqlite
// if (app.db.options.dialect === 'sqlite') {
// expect(data.fields.n.possibleTypes).toBeTruthy();
// }
});
it('should return possible types for json fields', async () => {
@ -147,6 +142,83 @@ SELECT * FROM numbers;
expect(data.fields.json_field.possibleTypes).toBeTruthy();
});
it('should not throw error when source collection destroyed', async () => {
await app.db.getCollection('collections').repository.create({
values: {
name: 'users',
fields: [
{
name: 'name',
type: 'string',
interface: 'text',
uiSchema: 'name-uiSchema',
},
{
name: 'age',
type: 'integer',
interface: 'number',
uiSchema: 'age-uiSchema',
},
],
},
context: {},
});
await app.db.sync();
const UserCollection = app.db.getCollection('users');
const viewName = `t_${uid(6)}`;
const dropSQL = `DROP VIEW IF EXISTS ${viewName}`;
await app.db.sequelize.query(dropSQL);
const viewSQL = `CREATE VIEW ${viewName} AS SELECT * FROM ${UserCollection.quotedTableName()}`;
await app.db.sequelize.query(viewSQL);
// create view collection
const viewCollection = await app.db.getCollection('collections').repository.create({
values: {
name: viewName,
view: true,
schema: app.db.inDialect('postgres') ? 'public' : undefined,
fields: [
{
name: 'name',
type: 'string',
source: 'users.name',
},
{
name: 'age',
type: 'integer',
source: 'users.age',
},
],
},
context: {},
});
const response = await agent.resource('collections').list({
appends: ['fields'],
paginate: false,
});
expect(response.status).toBe(200);
// drop view first
await app.db.sequelize.query(dropSQL);
// remove source collection
await app.db.getCollection('collections').repository.destroy({
filterByTk: 'users',
context: {},
});
const response2 = await agent.resource('collections').list({
appends: ['fields'],
paginate: false,
});
expect(response2.statusCode).toBe(200);
});
it('should list collections fields with source interface', async () => {
await app.db.getCollection('collections').repository.create({
values: {

View File

@ -283,7 +283,7 @@ export class CollectionManagerPlugin extends Plugin {
if (field.get('source')) {
const [collectionSource, fieldSource] = field.get('source').split('.');
// find original field
const collectionField = this.app.db.getCollection(collectionSource).getField(fieldSource);
const collectionField = this.app.db.getCollection(collectionSource)?.getField(fieldSource);
if (!collectionField) {
continue;