Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/migration module #496

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 2 additions & 57 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,64 +65,9 @@ $ npm run test:cov

## Migrations

Hexabot includes a migrations feature to help manage database schema and data changes over time. Migrations allow you to apply or revert changes to the database in a consistent and controlled manner.
Hexabot includes a migration module to help manage database schema and data changes over time. Migrations allows us to apply or revert changes to the database and keep it in sync with the version release.

### Creating a Migration

To create a new migration, use the following command from the root directory of Hexabot:

```bash
$ npx hexabot migrate create <migration-name>
```

Example:

```bash
$ npx hexabot migrate create all-users-language-fr
```

This command generates a new migration file in the `/api/migrations` folder. The file will look like this:

```typescript
import getModels from '@/models/index';

export async function up(): Promise<void> {
// Write migration here
}

export async function down(): Promise<void> {
// Write migration here
}
```

Within the migration file, you can define the changes to be made in the up() function. For example, if you want to update the language field of all users to 'fr', your migration might look like this:

```typescript
import getModels from '@/models/index';

export async function up(): Promise<void> {
const { UserModel } = await getModels();
await UserModel.updateMany({}, { language: 'fr' });
}

export async function down(): Promise<void> {}
```

### Running Migrations Up

You can run the following command to run all pending migrations:

```bash
$ npx hexabot migrate up
```

### Running Migrations Manually

If you want to run specific actions manually, you can get help by running the following command:

```bash
$ npx hexabot migrate help
```
Check the Migration README file for more : [Migration Module](./src/migration/README.md)

## Documentation

Expand Down
145 changes: 0 additions & 145 deletions api/migrations/models/index.ts

This file was deleted.

86 changes: 84 additions & 2 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"typecheck": "tsc --noEmit",
"reset": "npm install && npm run containers:restart",
"reset:hard": "npm clean-install && npm run containers:rebuild",
"migrate": "npx ts-migrate-mongoose --config-path ./migrations/config/migrate.ts"
"cli": "DEBUG=ts-node* ts-node --files --log-error --compiler-options '{\"diagnostics\": true}' src/cli.ts"
},
"lint-staged": {
"*.{js,ts}": "eslint --fix -c \".eslintrc-staged.js\""
Expand Down Expand Up @@ -77,6 +77,7 @@
"mongoose-lean-getters": "^1.1.0",
"mongoose-lean-virtuals": "^0.9.1",
"multer": "^1.4.5-lts.1",
"nest-commander": "^3.15.0",
"nestjs-dynamic-providers": "^0.3.4",
"nestjs-i18n": "^10.4.0",
"nodemailer": "^6.9.13",
Expand Down Expand Up @@ -143,7 +144,8 @@
},
"optionalDependencies": {
"@css-inline/css-inline-linux-arm64-musl": "^0.14.1",
"@resvg/resvg-js-linux-arm64-musl": "^2.6.2"
"@resvg/resvg-js-linux-arm64-musl": "^2.6.2",
"@resvg/resvg-js-darwin-arm64": "^2.6.2"
},
"overrides": {
"mjml": "5.0.0-alpha.4"
Expand Down Expand Up @@ -176,4 +178,4 @@
"@/(.*)": "<rootDir>/$1"
}
}
}
}
Loading
Loading