Migration
During NocoBase plugin development and updates, plugin database structures or configurations may undergo incompatible changes. To ensure smooth upgrades, NocoBase provides a Migration mechanism -- handling these changes by writing migration files.
Migration Concepts
Migration is a script that automatically executes during plugin upgrades, used to solve the following problems:
- Data table structure adjustments (adding fields, modifying field types, etc.)
- Data migration (such as batch updates of field values)
- Plugin configuration or internal logic updates
Migration execution timing is divided into three types:
Create Migration Files
Migration files should be placed in src/server/migrations/*.ts in the plugin directory. NocoBase provides the create-migration command to quickly generate migration files.
Optional Parameters
Example
Generated migration file path:
File initial content:
appVersion is used to identify the version targeted by the upgrade. Environments with versions less than the specified version will execute this migration.
Writing Migration
In Migration files, you can access the following common properties and APIs through this to conveniently operate database, plugins, and application instances:
Common Properties
-
this.app
Current NocoBase application instance. Can be used to access global services, plugins, or configuration. -
this.db
Database service instance, provides interfaces for operating on models (Collections). -
this.plugin
Current plugin instance, can be used to access plugin custom methods. -
this.sequelize
Sequelize instance, can directly execute raw SQL or transaction operations. -
this.queryInterface
Sequelize's QueryInterface, commonly used to modify table structures, such as adding fields, deleting tables, etc.
Writing Migration Example
In addition to the common properties listed above, Migration provides more APIs. For detailed usage, see Migration API.
Trigger Migration
Migration execution is triggered by the nocobase upgrade command:
During upgrade, the system will determine execution order based on Migration type and appVersion.
Testing Migration
In plugin development, it's recommended to use Mock Server to test whether migration executes correctly, avoiding damage to real data.
Using Mock Server can quickly simulate upgrade scenarios and verify Migration execution order and data changes.
Development Practice Recommendations
-
Split Migration
Try to generate one migration file per upgrade, to maintain atomicity and simplify troubleshooting. -
Specify Execution Timing
ChoosebeforeLoad,afterSync, orafterLoadbased on operation objects, avoid depending on unloaded modules. -
Handle Versioning
UseappVersionto clearly specify the version applicable to the migration to prevent repeated execution. -
Test Coverage
Verify the migration on a Mock Server before executing the upgrade in a real environment.
Related Links
- Collections -- Data table structure definitions commonly adjusted in Migration
- Database -- APIs for operating data via
this.dbin Migration - Plugin -- How Migration files are organized and loaded within plugins
- Command -- Triggering migrations via
nocobase upgradeandcreate-migrationcommands - Test -- Using Mock Server to test Migration execution results
- Migration API -- Complete API reference for the Migration class

