fix: database migrations through hexabot-cli

This commit is contained in:
Yassine Sallemi 2024-10-08 17:18:36 +01:00
parent f435b784b9
commit 58158e8f9e
4 changed files with 40 additions and 12 deletions

1
api/.gitignore vendored
View File

@ -4,4 +4,3 @@ coverage/
uploads/
documentation/
avatars
migrations/*.ts

View File

@ -65,24 +65,23 @@ $ npm run test:cov
## Migrations
The API 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 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.
### Creating a Migration
You need to navigate to the `api` folder to run the following commands.
To create a new migration, use the following command:
To create a new migration, use the following command from the root directory of Hexabot:
```bash
$ npm run migrate create <migration-name>
$ npx hexabot migrate create <migration-name>
```
Example:
```bash
$ npm run migrate create all-users-language-fr
$ npx hexabot migrate create all-users-language-fr
```
This command generates a new migration file in the `./migrations` folder. The file will look like this:
This command generates a new migration file in the `/api/migrations` folder. The file will look like this:
```typescript
import getModels from '@/models/index';
@ -114,7 +113,7 @@ export async function down(): Promise<void> {}
You can run the following command to run all pending migrations:
```bash
$ npm run migrate up
$ npx hexabot migrate up
```
### Running Migrations Manually
@ -122,7 +121,7 @@ $ npm run migrate up
If you want to run specific actions manually, you can get help by running the following command:
```bash
$ npm run migrate help
$ npx hexabot migrate help
```
## Documentation

View File

@ -27,7 +27,7 @@
"typecheck": "tsc --noEmit",
"reset": "npm install && npm run containers:restart",
"reset:hard": "npm clean-install && npm run containers:rebuild",
"migrate": "export USER_ID=$(id -u) && export GROUP_ID=$(id -g) && docker exec -it --user $USER_ID:$GROUP_ID api npx ts-migrate-mongoose --config-path ./migrations/config/migrate.ts"
"migrate": "npx ts-migrate-mongoose --config-path ./migrations/config/migrate.ts"
},
"dependencies": {
"@nestjs-modules/mailer": "^1.11.2",

View File

@ -86,6 +86,27 @@ const dockerCompose = (args: string): void => {
}
};
/**
* Execute a Docker Exec command.
* @param container Container for the docker exec command
* @param options Additional options for the docker exec command
* @param command Command to be executed within the container
*/
const dockerExec = (
container: string,
command: string,
options?: string
): void => {
try {
execSync(`docker exec -it ${options} ${container} ${command}`, {
stdio: 'inherit',
});
} catch (error) {
console.error(chalk.red('Error executing Docker Compose command.'));
process.exit(1);
}
};
/**
* Parse the comma-separated service list.
* @param serviceString Comma-separated list of services
@ -153,6 +174,14 @@ program
dockerCompose(`${composeArgs} up --build -d`);
});
program
.command('migrate [args...]')
.description('Run database migrations')
.action((args) => {
const migrateArgs = args.join(' ');
dockerExec('api', `npm run migrate ${migrateArgs}`, '--user $(id -u):$(id -g)');
});
program
.command('start-prod')
.description(
@ -275,3 +304,4 @@ program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}