升级脚本
插件在更新迭代过程中,可能会出现某些不兼容的改动,这些不兼容的升级脚本可以通过编写 migration 文件来处理,由 tachybase upgrade
命令触发,相关流程如下:
升级的 migrations 有 beforeLoad、afterSync 和 afterLoad 之分:
- beforeLoad:在各模块加载前执行,分为三个阶段:
- 内核模块加载前
- preset 插件加载前
- 其他插件加载前
- afterSync:在数据表配置与数据库同步之后,分为三个阶段:
- 内核表与数据库同步之后
- preset 插件的表与数据库同步之后
- 其他插件的表与数据库同步后
- afterLoad:应用全部加载之后才执行
创建 migration 文件
通过 create-migration 命令创建 migration 文件
1pnpm tachybase create-migration -h
2
3Usage: tachybase create-migration [options] <name>
4
5Options:
6 --pkg <pkg> package name
7 --on [on] Options include beforeLoad, afterSync and afterLoad
8 -h, --help display help for command
示例
1$ pnpm tachybase create-migration update-ui --pkg=@tachybase/plugin-web
2
32024-01-07 17:33:13 [info ] add app main into supervisor
42024-01-07 17:33:13 [info ] migration file in /packages/@tachybase/plugin-web/src/server/migrations/20240107173313-update-ui.ts
5✨ Done in 5.02s.
将在插件包 @tachybase/plugin-client 的 src/server/migrations 里生成一个 migration 文件,名为 20240107173313-update-ui.ts,初始内容如下:
1import { Migration } from '@tachybase/server';
2
3export default class extends Migration {
4 on = 'afterLoad'; // 'beforeLoad' | 'afterSync' | 'afterLoad'
5 appVersion = '<0.19.0-alpha.3';
6
7 async up() {
8 // coding
9 }
10}
触发 migration
通过 tachybase upgrade
命令触发
1$ pnpm tachybase upgrade
测试 migration
1import { createMockServer, MockServer } from '@tachybase/test';
2
3describe('test example', () => {
4 let app: MockServer;
5
6 beforeEach(async () => {
7 app = await createMockServer({
8 plugins: ['my-plugin'], // 插件
9 version: '0.18.0-alpha.5', // 升级前的版本
10 });
11 });
12
13 afterEach(async () => {
14 await app.destroy();
15 });
16
17 test('case1', async () => {
18 await app.runCommand('upgrade');
19 // coding...
20 });
21});