diff --git a/api/.gitignore b/api/.gitignore index 4487e4ae..a3d0bf5f 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -3,5 +3,4 @@ dist/ coverage/ uploads/ documentation/ -avatars -migrations/*.ts \ No newline at end of file +avatars \ No newline at end of file diff --git a/api/README.md b/api/README.md index b5a478ec..3e8dfc08 100644 --- a/api/README.md +++ b/api/README.md @@ -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 +$ npx hexabot migrate create ``` 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 {} 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 diff --git a/api/package.json b/api/package.json index ce4fb665..1ef51c5e 100644 --- a/api/package.json +++ b/api/package.json @@ -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", @@ -156,4 +156,4 @@ "@/(.*)": "/$1" } } -} \ No newline at end of file +} diff --git a/cli/src/index.ts b/cli/src/index.ts index c81e19fb..53c186de 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -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(); } +