Skip to content

Commit 1df24c4

Browse files
BC-8909 - Update migration documentation for MikroORM commands and best practices (#49)
--------- Co-authored-by: Max Bischof <maximilian.bischof@dataport.de>
1 parent fff4307 commit 1df24c4

File tree

1 file changed

+66
-62
lines changed

1 file changed

+66
-62
lines changed
Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,100 @@
11
## Migrations for server database
22

3-
Migration mean to change the database structure by updating a model and/or updating user data to the target model.
3+
Executing a migration means to change the database structure by updating a model and/or updating user data to the target model.
44

5-
The documentation about migration concept and tool on npm can be found at [MikroORM documentation](https://mikro-orm.io/docs/5.9/migrations). Please be aware of current mikro-orm version.
5+
The documentation about the migration concept and tool on npm can be found at [MikroORM documentation](https://mikro-orm.io/docs/migrations). Please be aware of current mikro-orm version.
66

7-
Here, we explain only how we create migrations currently.
8-
9-
We use the [cli commands](https://mikro-orm.io/docs/5.9/migrations#using-via-cli) provided by MikroORM.
7+
### Create a new migration
108

9+
`npm run migration:create` will create a new migration file in `./apps/server/src/migrations/mikro-orm` folder.
1110

12-
### Create a new migration
13-
`npx mikro-orm migration:create` will create a new migration file in `./apps/server/src/migrations/mikro-orm` folder.
14-
* please note that argument `--name=SOME_MIGRATION_NAME` is supported in 5.9 only
15-
* do log all database changes (before and after state of documents or at least all modified document id's)
11+
- Please log all database changes (before and after state of documents or at least all modified document id's)
1612

1713
### Apply migrations
18-
To run migraitons you can use one of the commands below:
19-
* `npx mikro-orm migration:up` will apply all migrations that are not applied yet
20-
* `npm run migration:up` will run the compiled js file. This command is safer, and used in production.
21-
* you can also apply any specific migration. Look at the documentation for more details.
14+
15+
To run migrations you can use the command below:
16+
17+
- `npm run migration:up` will run the compiled js file. This command is safer, and used in production.
18+
- You can also apply any specific migration. Look at the documentation for more details.
2219

2320
### Undo migration
24-
`npx mikro-orm migration:down` will undo the last migration that has been applied.
21+
22+
`npm run migration:down` will undo the last migration that has been applied.
2523

2624
### Notes about setup
27-
* migrations are stored in database `migrations` collection
28-
* there is no status for a migration. Once the migration has been applied, a record is added in the database.
29-
* the command will compare migrations files to database records to know which migrations to apply
30-
* migration configuration is in `./apps/server/src/mikro-orm.config.ts` file. MikroORM uses the config file to connect to the database and to find the migrations folder.
3125

26+
- Migrations are stored in the database `migrations` collection.
27+
- There is no status for a migration. Once the migration has been applied, a record is added in the database.
28+
- The command will compare migration files and database records to know which migrations to apply.
29+
- Migration configuration is locates in `./apps/server/src/mikro-orm.config.ts` file. MikroORM uses the config file to connect to the database and to find the migrations folder.
3230

33-
## Test migration
34-
* to check migrations to be executed:
35-
* in local development you can use `npx mikro-orm migration:pending` command
36-
* `npm run migration:pending` to run the compiled js file. The second command is safer, and used in CI and should be used in K8 clusters.
37-
* The CI job `./.github/workflows/migrations.yml` will check that the migrations are already included in the seed data. If not, it will fail. This is to ensure that the seed data is always up to date with the migrations.
31+
## Test migration
32+
33+
- Use the following command to check for pending migrations:
34+
- `npm run migration:pending` to run the compiled js file. The second command is safer, and used in CI and should be used in K8 clusters.
35+
- The CI job `./.github/workflows/migrations.yml` will check that the migrations are already included in the seed data. If not, it will fail. This is to ensure that the seed data is always up to date with the migrations.
3836

3937
## Committing a migration
40-
* `npm run setup:db:seed` - to update the whole database
41-
or use `npm run nest:start:console -- database --help` to see how to import/export single collection
42-
* `npx mikro-orm migration:up` - to apply the migration update the migrations collection
43-
* `npm run backup` (copies the collections to folder backup/DATE, move the changed files of this folder to backup/setup and commit the updated files)
44-
or use `npm run nest:start:console -- database export --collection <collection> --override` to override seed data directly.
45-
The updated collections, modified by your migration added in backup/setup folder
38+
39+
- `npm run setup:db:seed` - to update the whole database
40+
or use `npm run nest:start:console -- database --help` to see how to import/export single collection
41+
- `npm run migration:up` - to apply the migration update to the migrations collection
42+
- `migration:persisted` (copies the collections to folder backup/DATE, move the changed files of this folder to backup/setup and commit the updated files)
43+
or use `npm run nest:start:console -- database export --collection <collection> --override` to override seed data directly.
44+
The updated collections that where modified by your migration are added to backup/setup folder.
4645

4746
Commit the changes:
48-
* `git add .`
49-
* `git commit -m "migration: <migration name>"`
50-
* `git push`
51-
* test that the migration was applied `npx mikro-orm migrations:pending` (or better use `npm run migration:pending`) should return nothing
5247

48+
- `git add .`
49+
- `git commit -m "migration: <migration name>"`
50+
- `git push`
51+
- Test that the migration was applied `npm run migration:pending` should return nothing
5352

5453
## Best Practices
54+
5555
### General
5656

57-
* consider if a migration is the right tool for this job
58-
* recurring tasks are better placed in a script/console
59-
* test the migration well
60-
* if a migration should be deleted, do not delete the migration file itself, but remove the content of the up and down method and log a skip message
61-
* consider if the migration can be written to be idempotent (can be executed multiple times without problems)
57+
- Consider if a migration is the right tool for this job
58+
- Recurring tasks are better placed in a script/console
59+
- Test the migration well
60+
- If a migration should be deleted, do not delete the migration file itself, but remove the content of the up and down method and log a skip message
61+
- Consider if the migration can be written to be idempotent (can be executed multiple times without problems)
6262

6363
### Performance
64-
* think about the size of the collection. On production, it can be that the collection has a lot of data, and your migration might take a long time or can lead to time-out errors.
65-
* use methods already provided by mongo for bulk operations (e.g. updateMany, deleteMany etc.)
66-
* but think about handle items separately with extra logging and separate error handling
67-
* load the data chunkwise and process them asynchronously
68-
* query the data with `skip` and `limit` (example migration)
69-
* or use async iterators with mongoose queries (blog post, example migration)
70-
* migrations should in general not be executed in parallel (on multiple pods)
71-
* Use for loops with synchronous execution and logging. Avoid subtasks awaiting Promise.all()
72-
* Beside loading data in chunks or cursors, performance must not be an issue.
64+
65+
- Think about the size of the collection. On production, large collections with a lot of data, might take a long time to migrate or can lead to time-out errors.
66+
- Use methods already provided by mongo for bulk operations (e.g. updateMany, deleteMany etc.). But think about handle items separately with extra logging and separate error handling in that case.
67+
- Load the data chunkwise and process them asynchronously
68+
- Query the data with `skip` and `limit` (example migration) or use async iterators with mongoose queries (blog post, example migration).
69+
- Migrations should in general not be executed in parallel (on multiple pods).
70+
- Use for loops with synchronous execution and logging. Avoid subtasks awaiting Promise.all().
71+
- As long as data is loaded in chunks or cursors, performance should not be an issue.
7372

7473
### Error Handling/Logging
75-
* if a migration throws an error, the subsequent migrations are not executed as well
76-
* catch errors if the errors are acceptable for the next release, so it will not become a release blocker
77-
* log start and end of a migration (so that we know which migration currently is running)
78-
* log intermediate results for long-running migrations (so that we know if it is still running)
79-
* use log level `alert` or above, so the output can be seen on production
80-
* logging might be difficult to fix, instead it might be helpful to keep before-states in a separate collection
74+
75+
- If a migration throws an error, the subsequent migrations are not executed.
76+
- Catch errors if the errors can be handled by the next release, so it will not become a release blocker.
77+
- Log start and end of a migration (so that we know which migration currently is running).
78+
- Log intermediate results for long-running migration (so that we know if it is still running).
79+
- Use log level `alert` or above, so the output can be seen on production.
80+
- Logging might be difficult to fix, instead it might be helpful to keep before-states in a separate collection
8181

8282
### Debugging
83-
* use env `MIKRO_ORM_CLI_VERBOSE=1`
84-
* can add mikro-orm debug info in config file
85-
* `debug: true` will log all queries
86-
* `logger: console.log` will log all queries
87-
* can add debug breakpoints in migration files
88-
* run with typical debug options for your IDE (e.g. in [Webstorm create a Run/Debug](https://blog.jetbrains.com/webstorm/2018/01/how-to-debug-with-webstorm/#prepare_for_debugging_create_a_run_debug_configuration) npm command and run it with debug options)
8983

84+
- Use env `MIKRO_ORM_CLI_VERBOSE=1`
85+
- Mikro-orm debug info can be added in config file
86+
- `debug: true` will log all queries
87+
- `logger: console.log` will log all queries
88+
- Debug breakpoints can be added in migration files
89+
- Run with typical debug options for your IDE (e.g. in [Webstorm create a Run/Debug](https://blog.jetbrains.com/webstorm/2018/01/how-to-debug-with-webstorm/#prepare_for_debugging_create_a_run_debug_configuration) npm command and run it with debug options)
9090

9191
### Caveats
92+
9293
#### using entity manager
93-
* According to documentation, the entity manager should not be used in migrations. Instead, the migration should use the `mongoClient` to access the database.
94+
95+
- According to documentation, the entity manager should not be used in migrations. Instead, the migration must use the `mongoClient` to access the database.
96+
9497
#### Outdated Migrations
95-
* By their nature, migrations become outdated as the database model changes. You are never expected to update migrations due to any changes in the code that are made.
96-
* If needed, for example because the migration shows errors due to a code (model) change, migrations can be deleted, since they will still be accessible in the git history.
98+
99+
- By their nature, migrations become outdated as the database model changes. You are never expected to update migrations due to any changes in the code that are made.
100+
- If needed, for example because the migration shows errors due to a code (model) changes, migrations can be deleted, since they will still be accessible in the git history.

0 commit comments

Comments
 (0)