diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index 03c2ace95..66d22e88f 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -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, + }, + }); } } diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts deleted file mode 100644 index 1b07c98ee..000000000 --- a/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts +++ /dev/null @@ -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() {} -} diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250851-theme-editor.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250851-theme-editor.ts new file mode 100644 index 000000000..0b51f4183 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250851-theme-editor.ts @@ -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() {} +} diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts index caeb6f044..3a65e1bb6 100644 --- a/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts @@ -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:*'],