427 | | Imperative migration:: an imperative migration is a Java class that has access to the //DeepaMehta Core Service//. Thus, you can perform arbitrary database operations like creation, retrieval, update, deletion. Use an imperative migration when (a later version of) your plugin needs to modify existing type definitions and/or transform existing database content. |
428 | | |
429 | | ==== Declarative migrations ==== |
| 427 | * An **Imperative Migration** is a Java class that has access to the //DeepaMehta Core Service//. Thus, you can perform arbitrary database operations like creation, retrieval, update, deletion. Use an imperative migration when (a later version of) your plugin needs to modify existing type definitions and/or transform existing database content. |
| 428 | |
| 429 | The developer can equip a plugin with an arbitrary number of both, declarative migrations and imperative migrations. |
| 430 | |
| 431 | === Directory structure === |
| 432 | |
| 433 | In order to let DeepaMehta find the plugin's migration files, you must adhere to a fixed directory structure and file names. Each migration file must contain its number, so DeepaMehta can run them consecutively. |
| 434 | |
| 435 | A declarative migration must be named `migration<nr>.json` and must be located in the plugin's `src/main/resources/migrations/` directory. |
| 436 | |
| 437 | An imperative migration must be named `Migration<nr>.java` and must be located in the plugin's `src/main/java/<your plugin package>/migrations/` directory. |
| 438 | |
| 439 | Example: |
| 440 | |
| 441 | {{{ |
| 442 | #!txt |
| 443 | dm4-myplugin/ |
| 444 | src/ |
| 445 | main/ |
| 446 | java/ |
| 447 | org/ |
| 448 | mydomain/ |
| 449 | deepamehta4/ |
| 450 | myplugin/ |
| 451 | migrations/ |
| 452 | Migration2.java |
| 453 | Migration5.java |
| 454 | resources/ |
| 455 | migrations/ |
| 456 | migration1.json |
| 457 | migration3.json |
| 458 | migration4.json |
| 459 | migration6.json |
| 460 | plugin.properties |
| 461 | }}} |
| 462 | |
| 463 | This example plugin would have set `requiredPluginMigrationNr` to 6 (configured in `plugin.properties`), so 6 migrations are involved. 4 are declarative and 2 are imperative here. |
| 464 | |
| 465 | Important: for each number between 1 and `requiredPluginMigrationNr` exactly one migration file must exist. That is //either// a declarative migration file //or// an imperative migration file. |
| 466 | |
| 467 | It would be invalid if for a given number a) no migration file exists, or b) two migration files exist (one declarative and one imperative). In these cases the DeepaMehta migration machinery throws an error and the plugin is not activated. |
| 468 | |
| 469 | === Writing a declarative migration === |