2022-11-06 14:20:52 +08:00
# Database Migration
2022-10-31 22:41:24 +08:00
2022-11-06 14:20:52 +08:00
There may be some incompatible changes during the plugin update iteration, these incompatible upgrade scripts can be handled by writing migration files.
2022-10-31 22:41:24 +08:00
2022-11-06 14:20:52 +08:00
## How to add a migration file?
2022-10-31 22:41:24 +08:00
```ts
export class MyPlugin extends Plugin {
load() {
2022-11-06 14:20:52 +08:00
// Load a single Migration file
2022-10-31 22:41:24 +08:00
this.db.addMigration();
2022-11-06 14:20:52 +08:00
// Load multiple Migration files
2022-10-31 22:41:24 +08:00
this.db.addMigrations();
}
}
```
2022-11-06 14:20:52 +08:00
API reference.
2022-10-31 22:41:24 +08:00
- [db.addMigration() ](/api/database#addmigration )
- [db.addMigrations() ](/api/database#addmigrations )
2022-11-06 14:20:52 +08:00
## When to execute?
2022-10-31 22:41:24 +08:00
```bash
2022-11-06 14:20:52 +08:00
## When the app is upgraded, migrator.up() and db.sync() are executed
2022-10-31 22:41:24 +08:00
yarn nocobase upgrade
2022-11-06 14:20:52 +08:00
# Trigger migration separately
2022-10-31 22:41:24 +08:00
yarn nocobase migrator up
```
2022-11-06 14:20:52 +08:00
## When do I need to write a migration file?
2022-10-31 22:41:24 +08:00
2022-11-06 14:20:52 +08:00
It is usually used to update the system configuration stored in the database during the upgrade process. If it is just a collection configuration change, you don't need to configure migration, just execute ``yarn nocobase db:sync`` to synchronize to the database.
2022-10-31 22:41:24 +08:00
2022-11-06 14:20:52 +08:00
## Migration file
2022-10-31 22:41:24 +08:00
```ts
import { Migration } from '@nocobase/server';
export default class CustomMigration extends Migration {
async up() {
//
}
async down() {
//
}
}
```