From 0ce0ca6441b3e60557577b7274373b3951b09367 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 9 Jan 2024 22:19:43 +0800 Subject: [PATCH] fix: dump with sql collection (#3350) * chore: dump with sql collection * fix: dump with view that not exists in database * fix: test * fix: test * fix: test * fix: test --- packages/core/database/src/database.ts | 29 +++++--- .../src/server/__tests__/dumper.test.ts | 68 +++++++++++++++++++ 2 files changed, 88 insertions(+), 9 deletions(-) diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 27fda529b..d53f0718f 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -951,16 +951,19 @@ export class Database extends EventEmitter implements AsyncEmitter { }, async onDump(dumper, collection: Collection) { - const viewDef = await collection.db.queryInterface.viewDef(collection.getTableNameWithSchemaAsString()); - - dumper.writeSQLContent(`view-${collection.name}`, { - sql: [ - `DROP VIEW IF EXISTS ${collection.getTableNameWithSchemaAsString()}`, - `CREATE VIEW ${collection.getTableNameWithSchemaAsString()} AS ${viewDef}`, - ], - group: 'required', - }); + try { + const viewDef = await collection.db.queryInterface.viewDef(collection.getTableNameWithSchemaAsString()); + dumper.writeSQLContent(`view-${collection.name}`, { + sql: [ + `DROP VIEW IF EXISTS ${collection.getTableNameWithSchemaAsString()}`, + `CREATE VIEW ${collection.getTableNameWithSchemaAsString()} AS ${viewDef}`, + ], + group: 'required', + }); + } catch (e) { + return; + } return; }, }); @@ -969,6 +972,14 @@ export class Database extends EventEmitter implements AsyncEmitter { condition: (options) => { return options.sql; }, + + async onSync() { + return; + }, + + async onDump(dumper, collection: Collection) { + return; + }, }); } } diff --git a/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts b/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts index 8a7f3ad47..d06890bdb 100644 --- a/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts +++ b/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts @@ -379,6 +379,74 @@ describe('dumper', () => { expect(list.length).toBe(2); }); + it('should dump and restore with sql collection', async () => { + const userCollection = db.getCollection('users'); + + await db.getRepository('collections').create({ + values: { + name: 'tests', + sql: `select count(*) as count from ${userCollection.getTableNameWithSchemaAsString()}`, + fields: [ + { + type: 'integer', + name: 'count', + }, + ], + }, + context: {}, + }); + + const usersCount = await db.getRepository('users').count(); + const res = await db.getRepository('tests').findOne(); + expect(res.get('count')).toEqual(usersCount); + + const dumper = new Dumper(app); + const result = await dumper.dump({ + groups: new Set(['required', 'custom']), + }); + + const restorer = new Restorer(app, { + backUpFilePath: result.filePath, + }); + + await restorer.restore({ + groups: new Set(['required', 'custom']), + }); + + const res2 = await app.db.getRepository('tests').findOne(); + expect(res2.get('count')).toEqual(usersCount); + }); + + it('should dump with view that not exists', async () => { + await db.getRepository('collections').create({ + values: { + name: 'view_not_exists', + view: true, + schema: db.inDialect('postgres') ? 'public' : undefined, + fields: [ + { + type: 'string', + name: 'name', + }, + ], + }, + context: {}, + }); + + const dumper = new Dumper(app); + const result = await dumper.dump({ + groups: new Set(['required', 'custom']), + }); + + const restorer = new Restorer(app, { + backUpFilePath: result.filePath, + }); + + await restorer.restore({ + groups: new Set(['required', 'custom']), + }); + }); + it('should dump and restore with view collection', async () => { await db.getRepository('collections').create({ values: {