From 83dc81c51bcae22e2dddbf47f42847f0f9c2a582 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 7 Jun 2023 11:06:35 +0800 Subject: [PATCH] chore(collection-manager): should not throw error when source collection destoryed (#1999) --- .../http-api/view-collection.test.ts | 82 +++++++++++++++++-- .../plugins/collection-manager/src/server.ts | 2 +- 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/packages/plugins/collection-manager/src/__tests__/http-api/view-collection.test.ts b/packages/plugins/collection-manager/src/__tests__/http-api/view-collection.test.ts index 559f0be5e..2d3e667fb 100644 --- a/packages/plugins/collection-manager/src/__tests__/http-api/view-collection.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/http-api/view-collection.test.ts @@ -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: { diff --git a/packages/plugins/collection-manager/src/server.ts b/packages/plugins/collection-manager/src/server.ts index 338918ab3..b4a6d695a 100644 --- a/packages/plugins/collection-manager/src/server.ts +++ b/packages/plugins/collection-manager/src/server.ts @@ -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;