The `migration` module for **Hexabot** provides a simple and effective way to manage database migrations. It allows you to create, execute, and roll back migrations, ensuring the database schema stays in sync with the version DB updates.
Whenever a new version is released which requires some DB updates, the onApplicationBootstrap()
will apply migrations automatically but only if it's a dev environement and `config.mongo.autoMigrate` is enabled.
## Features
- Generate timestamped migration files automatically in kebab-case.
- Track migration execution status in a MongoDB collection (`migrations`).
Replace `<version>` with the next version for your migration, such as `v2.1.1`.
Example:
```bash
npm run cli migration create v2.1.1
```
This will generate a new file under `src/migration/migrations/` with a timestamped filename in kebab-case.
### Running Migrations
#### Running a Specific Migration
To execute a specific migration, use:
```bash
npm run cli migration migrate up <version>
```
Example:
```bash
npm run cli migration migrate up v2.1.1
```
#### Rolling Back a Specific Migration
To roll back a specific migration, use:
```bash
npm run cli migration migrate down <version>
```
Example:
```bash
npm run cli migration migrate down v2.1.1
```
#### Running All Migrations
To execute all pending migrations:
```bash
npm run cli migration migrate up
```
#### Rolling Back All Migrations
To roll back all migrations:
```bash
npm run cli migration migrate down
```
### Tracking Migration Status
The migration status is stored in a MongoDB collection called `migrations`. This collection helps ensure that each migration is executed or rolled back only once, avoiding duplicate operations.