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
This commit is contained in:
ChengLei Shao 2024-01-09 22:19:43 +08:00 committed by GitHub
parent 6e55d91f58
commit 0ce0ca6441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 9 deletions

View File

@ -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;
},
});
}
}

View File

@ -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: {