fix: restore backup before 1.0 (#4228)

* fix: restore backup before 1.0

* chore: test

* chore: test

* chore: rename plugins
This commit is contained in:
ChengLei Shao 2024-04-29 22:01:56 +08:00 committed by GitHub
parent 9c4fde6ed6
commit 9e37140197
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 4 deletions

View File

@ -30,6 +30,20 @@ describe('dumper', () => {
});
});
it.skip('should restore from version 0.21 backup file', async () => {
const file = path.resolve(__dirname, 'files', 'backup_20240429_110942_7061.nbdump');
const restorer = new Restorer(app, {
backUpFilePath: file,
});
const { dumpableCollectionsGroupByGroup } = await restorer.parseBackupFile();
await restorer.restore({
groups: new Set(Object.keys(dumpableCollectionsGroupByGroup)),
});
});
it('should write sql content', async () => {
const dumper = new Dumper(app);
@ -422,7 +436,8 @@ describe('dumper', () => {
await db.getRepository('collections').create({
values: {
name: 'tests',
sql: `select count(*) as count from ${userCollection.getTableNameWithSchemaAsString()}`,
sql: `select count(*) as count
from ${userCollection.getTableNameWithSchemaAsString()}`,
fields: [
{
type: 'integer',

View File

@ -16,6 +16,35 @@ type RestoreOptions = {
groups: Set<DumpRulesGroupType>;
};
const renamePlugins = async (app) => {
const names = {
oidc: '@nocobase/plugin-auth-oidc',
cas: '@nocobase/plugin-auth-cas',
saml: '@nocobase/plugin-auth-saml',
'collection-manager': '@nocobase/plugin-data-source-main',
'china-region': '@nocobase/plugin-field-china-region',
'custom-request': '@nocobase/plugin-action-custom-request',
export: '@nocobase/plugin-action-export',
import: '@nocobase/plugin-action-import',
'formula-field': '@nocobase/plugin-field-formula',
'iframe-block': '@nocobase/plugin-block-iframe',
'localization-management': '@nocobase/plugin-localization',
'sequence-field': '@nocobase/plugin-field-sequence',
'sms-auth': '@nocobase/plugin-auth-sms',
};
for (const original of Object.keys(names)) {
await app.pm.repository.update({
filter: {
name: original,
},
values: {
name: names[original].replace('@nocobase/plugin-', ''),
packageName: names[original],
},
});
}
};
export class Restorer extends AppMigrator {
direction = 'restore' as const;
backUpFilePath: string;
@ -120,17 +149,22 @@ export class Restorer extends AppMigrator {
});
};
const preImportCollections = ['applicationPlugins'];
const { dumpableCollectionsGroupByGroup, delayCollections } = await this.parseBackupFile();
// import plugins
await importCollection('applicationPlugins');
// import pre import collections
for (const collectionName of preImportCollections) {
await importCollection(collectionName);
}
await renamePlugins(this.app);
await this.app.reload();
// import required collections
const metaCollections = dumpableCollectionsGroupByGroup.required;
for (const collection of metaCollections) {
if (collection.name === 'applicationPlugins') {
if (preImportCollections.includes(collection.name)) {
continue;
}