fix: theme migration error (#2929)

* fix: theme migration error

* fix: themeConfig:list public

* fix: item.changed

* refactor: make better

---------

Co-authored-by: Rain <958414905@qq.com>
This commit is contained in:
chenos 2023-10-28 17:14:31 +08:00 committed by GitHub
parent 770f1b7af7
commit e1c30f25e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 47 deletions

View File

@ -613,7 +613,12 @@ export class Collection<
});
for (const model of models) {
await model.sync(syncOptions);
await model.sync(syncOptions || {
force: false,
alter: {
drop: false,
},
});
}
}

View File

@ -1,45 +0,0 @@
import { Migration } from '@nocobase/server';
import { compact, compactDark, dark, defaultTheme } from '../builtinThemes';
export default class ThemeEditorMigration extends Migration {
async up() {
const result = await this.app.version.satisfies('<0.14.0-alpha.8');
if (!result) {
return;
}
const themeRepo = this.db.getRepository('themeConfig');
if (!themeRepo) {
return;
}
themeRepo.collection.addField('uid', {
type: 'uid',
});
await this.db.sync();
const themes = [defaultTheme, dark, compact, compactDark];
const updates = [];
const creates = [];
for (const theme of themes) {
const { uid } = theme;
const { name } = theme.config;
const filter = { 'config.name': name === 'Default' ? 'Default theme of antd' : name };
const values = name === 'Default' ? { uid, config: { name } } : { uid };
const existingTheme = await themeRepo.findOne({ filter });
if (existingTheme) {
updates.push(themeRepo.update({ filter, values }));
} else {
creates.push(themeRepo.create({ values: [theme] }));
}
}
await Promise.all(updates);
await Promise.all(creates);
}
async down() {}
}

View File

@ -0,0 +1,71 @@
import { Model } from '@nocobase/database';
import { Migration } from '@nocobase/server';
import { uid } from '@nocobase/utils';
import { compact, compactDark, dark, defaultTheme } from '../builtinThemes';
export default class ThemeEditorMigration extends Migration {
async up() {
const result = await this.app.version.satisfies('<0.14.0-alpha.8');
if (!result) {
return;
}
const repository = this.db.getRepository('themeConfig');
if (!repository) {
return;
}
this.db.getCollection('themeConfig').sync();
const themes = {
[defaultTheme.uid]: defaultTheme,
[dark.uid]: dark,
[compact.uid]: compact,
[compactDark.uid]: compactDark,
};
const items: Model[] = await repository.find();
for (const item of items) {
// 1. 已经有 uid说明已经 migrate 过了
if (item.uid) {
if (themes[item.uid]) {
delete themes[item.uid];
}
continue;
}
const config = item.get('config');
// 2. 如果是之前旧版本的内置主题(通过 name 判断),需要设置上 uid并更改默认主题的 name
if (config.name === 'Default theme of antd') {
item.set('uid', defaultTheme.uid);
config.name = defaultTheme.config.name;
item.set('config', config);
item.changed('config', true);
delete themes[defaultTheme.uid];
} else if (config.name === dark.config.name) {
item.set('uid', dark.uid);
delete themes[dark.uid];
} else if (config.name === compact.config.name) {
item.set('uid', compact.uid);
delete themes[compact.uid];
} else if (config.name === compactDark.config.name) {
item.set('uid', compactDark.uid);
delete themes[compactDark.uid];
} else {
// 3. 如果是用户自定义的主题,需要设置上 uid
item.set('uid', uid());
}
await item.save();
}
// 4. 创建新的内置主题
if (Object.values(themes).length > 0) {
await repository.create({ values: Object.values(themes) });
}
}
async down() {}
}

View File

@ -41,7 +41,7 @@ export class ThemeEditorPlugin extends Plugin {
},
],
});
this.app.acl.allow('themeConfig', 'list', 'loggedIn');
this.app.acl.allow('themeConfig', 'list', 'public');
this.app.acl.registerSnippet({
name: `pm.${this.name}.themeConfig`,
actions: ['themeConfig:*'],