diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 27fda529b7..d53f0718f8 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 8a7f3ad47a..d06890bdb7 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: {