From f435b784b97a336055d18d25d48cedc2e8ef9b14 Mon Sep 17 00:00:00 2001 From: Yassine Sallemi Date: Mon, 7 Oct 2024 21:41:15 +0100 Subject: [PATCH 1/3] fix: database migrations through cli --- api/.gitignore | 3 +- api/README.md | 23 +- api/migrations/config/create.ts | 63 ---- api/package-lock.json | 356 +++++++++++++++++--- api/package.json | 7 +- api/patches/ts-migrate-mongoose+3.8.3.patch | 72 ---- 6 files changed, 323 insertions(+), 201 deletions(-) delete mode 100644 api/migrations/config/create.ts delete mode 100644 api/patches/ts-migrate-mongoose+3.8.3.patch diff --git a/api/.gitignore b/api/.gitignore index a3d0bf5f..4487e4ae 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -3,4 +3,5 @@ dist/ coverage/ uploads/ documentation/ -avatars \ No newline at end of file +avatars +migrations/*.ts \ No newline at end of file diff --git a/api/README.md b/api/README.md index b9904611..b5a478ec 100644 --- a/api/README.md +++ b/api/README.md @@ -3,15 +3,18 @@ [Hexabot](https://hexabot.ai/)'s API is a RESTful API built with NestJS, designed to handle requests from both the UI admin panel and various communication channels. The API powers core functionalities such as chatbot management, message flow, NLU (Natural Language Understanding), and plugin integrations. ## Key Features + - **RESTful Architecture:** Simple, standardized API architecture following REST principles. - **Multi-Channel Support:** Handles requests from different communication channels (e.g., web, mobile). - **Modular Design:** Organized into multiple modules for better scalability and maintainability. - **Real-Time Communication:** Integrates WebSocket support for real-time features. ## API Modules + The API is divided into several key modules, each responsible for specific functionalities: ### Core Modules + - **Analytics:** Tracks and serves analytics data such as the number of messages exchanged and end-user retention statistics. - **Attachment:** Manages file uploads and downloads, enabling attachment handling across the chatbot. - **Channel:** Manages different communication channels through which the chatbot operates (e.g., web, mobile apps, etc.). @@ -24,6 +27,7 @@ The API is divided into several key modules, each responsible for specific funct - **Settings:** A module for management all types of settings that can be adjusted to customize the chatbot. ### Utility Modules + - **WebSocket:** Adds support for Websicket with Socket.IO, enabling real-time communication for events like live chat and user interactions. - **Logger:** Provides logging functionality to track and debug API requests and events. @@ -60,8 +64,8 @@ $ 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. +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. ### Creating a Migration @@ -69,13 +73,13 @@ You need to navigate to the `api` folder to run the following commands. To create a new migration, use the following command: ```bash -$ npm run create-migration +$ npm run migrate create ``` Example: ```bash -$ npm run create-migration all-users-language-fr +$ npm run migrate create all-users-language-fr ``` This command generates a new migration file in the `./migrations` folder. The file will look like this: @@ -107,20 +111,18 @@ export async function down(): Promise {} ### Running Migrations Up -All migrations are run automatically when the app starts. - -Alternatively, you can run the following command in the `root` directory to run all pending migrations: +You can run the following command to run all pending migrations: ```bash -$ make migrate-up +$ npm run migrate up ``` ### Running Migrations Manually -If you want to run specific actions manually, you need to gain access to the `api` container and use the following command to run what you specifically want: +If you want to run specific actions manually, you can get help by running the following command: ```bash -$ npm run migrate -h +$ npm run migrate help ``` ## Documentation @@ -131,13 +133,14 @@ It's also possible to access the API reference documentation by running `npm run For detailed information about the API routes and usage, refer to the API documentation or visit [https://docs.hexabot.ai](https://docs.hexabot.ai). +## Contributing -## Contributing We welcome contributions from the community! Whether you want to report a bug, suggest new features, or submit a pull request, your input is valuable to us. Feel free to join us on [Discord](https://discord.gg/rNb9t2MFkG) ## License + This software is licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. diff --git a/api/migrations/config/create.ts b/api/migrations/config/create.ts deleted file mode 100644 index 75704242..00000000 --- a/api/migrations/config/create.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright © 2024 Hexastack. All rights reserved. - * - * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: - * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. - * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). - */ - -/* eslint-disable no-console */ -import fs from 'fs'; -import path from 'path'; - -import escapeRegExp from 'lodash/escapeRegExp'; - -// Get the argument passed (e.g., "all-users-fr") -const arg: string | undefined = process.argv[2]; - -// Check if the argument exists -if (!arg) { - console.error('Please provide a name for a new migration.'); - process.exit(1); -} -// Define the path to the migrations directory and the template file -const migrationsDir: string = path.join(__dirname, '../'); -const templatePath: string = path.join(__dirname, '../config/template.ts'); - -// Check if a migration with the same name (excluding timestamp) already exists -const migrationExists: boolean = fs.readdirSync(migrationsDir).some((file) => { - const regex = new RegExp(`^[0-9]+-${escapeRegExp(arg)}\\.ts$`); - return regex.test(file); -}); - -if (migrationExists) { - console.error(`A migration with the name "${arg}" already exists.`); - process.exit(1); -} - -// Generate a timestamp -const timestamp: string = Date.now().toString(); - -// Create the filename using the timestamp and argument -const filename: string = `${timestamp}-${arg}.ts`; - -// Read the template content from the file -let template: string; -try { - template = fs.readFileSync(templatePath, 'utf8'); -} catch (err) { - console.error('Error reading template file:', err); - process.exit(1); -} - -// Define the full path to save the file -const filePath: string = path.join(migrationsDir, filename); - -// Write the template to the file -fs.writeFile(filePath, template, (err) => { - if (err) { - console.error('Error writing file:', err); - } else { - console.log(`File created: ${filename}`); - } -}); diff --git a/api/package-lock.json b/api/package-lock.json index fcd03234..54720c53 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -52,7 +52,7 @@ "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", "slug": "^8.2.2", - "ts-migrate-mongoose": "^3.8.3", + "ts-migrate-mongoose": "^3.8.4", "uuid": "^9.0.1" }, "devDependencies": { @@ -3467,6 +3467,37 @@ "node": ">= 10" } }, + "node_modules/@emnapi/core": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.3.0.tgz", + "integrity": "sha512-9hRqVlhwqBqCoToZ3hFcNVqL+uyHV06Y47ax4UB8L6XgVRqYz7MFnfessojo6+5TK89pKwJnpophwjTMOeKI9Q==", + "license": "MIT", + "optional": true, + "dependencies": { + "@emnapi/wasi-threads": "1.0.1", + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.3.0.tgz", + "integrity": "sha512-XMBySMuNZs3DM96xcJmLW4EfGnf+uGmFNjzpehMjuX5PLB5j87ar2Zc4e3PVeZ3I5g3tYtAqskB28manlF69Zw==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/wasi-threads": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@emnapi/wasi-threads/-/wasi-threads-1.0.1.tgz", + "integrity": "sha512-iIBu7mwkq4UQGeMEM8bLwNK962nXdhodeScX4slfQnRhEMMzvYivHhutCIk8uojvmASXXPC2WNEjwxFWk72Oqw==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/@eslint-community/eslint-utils": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz", @@ -4228,6 +4259,18 @@ "sparse-bitfield": "^3.0.3" } }, + "node_modules/@napi-rs/wasm-runtime": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-0.2.5.tgz", + "integrity": "sha512-kwUxR7J9WLutBbulqg1dfOrMTwhMdXLdcGUhcbCcGwnPLt3gz19uHVdwH1syKVDbE022ZS2vZxOWflFLS0YTjw==", + "license": "MIT", + "optional": true, + "dependencies": { + "@emnapi/core": "^1.1.0", + "@emnapi/runtime": "^1.1.0", + "@tybys/wasm-util": "^0.9.0" + } + }, "node_modules/@nestjs-modules/mailer": { "version": "1.11.2", "resolved": "https://registry.npmjs.org/@nestjs-modules/mailer/-/mailer-1.11.2.tgz", @@ -4939,6 +4982,152 @@ "npm": ">=5.0.0" } }, + "node_modules/@oxc-resolver/binding-darwin-arm64": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-darwin-arm64/-/binding-darwin-arm64-1.12.0.tgz", + "integrity": "sha512-wYe+dlF8npM7cwopOOxbdNjtmJp17e/xF5c0K2WooQXy5VOh74icydM33+Uh/SZDgwyum09/U1FVCX5GdeQk+A==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@oxc-resolver/binding-darwin-x64": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-darwin-x64/-/binding-darwin-x64-1.12.0.tgz", + "integrity": "sha512-FZxxp99om+SlvBr1cjzF8A3TjYcS0BInCqjUlM+2f9m9bPTR2Bng9Zq5Q09ZQyrKJjfGKqlOEHs3akuVOnrx3Q==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@oxc-resolver/binding-freebsd-x64": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-freebsd-x64/-/binding-freebsd-x64-1.12.0.tgz", + "integrity": "sha512-BZi0iU6IEOnXGSkqt1OjTTkN9wfyaK6kTpQwL/axl8eCcNDc7wbv1vloHgILf7ozAY1TP75nsLYlASYI4B5kGA==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@oxc-resolver/binding-linux-arm-gnueabihf": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.12.0.tgz", + "integrity": "sha512-L2qnMEnZAqxbG9b1J3di/w/THIm+1fMVfbbTMWIQNMMXdMeqqDN6ojnOLDtuP564rAh4TBFPdLyEfGhMz6ipNA==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@oxc-resolver/binding-linux-arm64-gnu": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.12.0.tgz", + "integrity": "sha512-otVbS4zeo3n71zgGLBYRTriDzc0zpruC0WI3ICwjpIk454cLwGV0yzh4jlGYWQJYJk0BRAmXFd3ooKIF+bKBHw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@oxc-resolver/binding-linux-arm64-musl": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.12.0.tgz", + "integrity": "sha512-IStQDjIT7Lzmqg1i9wXvPL/NsYsxF24WqaQFS8b8rxra+z0VG7saBOsEnOaa4jcEY8MVpLYabFhTV+fSsA2vnA==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@oxc-resolver/binding-linux-x64-gnu": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.12.0.tgz", + "integrity": "sha512-SipT7EVORz8pOQSFwemOm91TpSiBAGmOjG830/o+aLEsvQ4pEy223+SAnCfITh7+AahldYsJnVoIs519jmIlKQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@oxc-resolver/binding-linux-x64-musl": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-linux-x64-musl/-/binding-linux-x64-musl-1.12.0.tgz", + "integrity": "sha512-mGh0XfUzKdn+WFaqPacziNraCWL5znkHRfQVxG9avGS9zb2KC/N1EBbPzFqutDwixGDP54r2gx4q54YCJEZ4iQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@oxc-resolver/binding-wasm32-wasi": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-wasm32-wasi/-/binding-wasm32-wasi-1.12.0.tgz", + "integrity": "sha512-SZN6v7apKmQf/Vwiqb6e/s3Y2Oacw8uW8V2i1AlxtyaEFvnFE0UBn89zq6swEwE3OCajNWs0yPvgAXUMddYc7Q==", + "cpu": [ + "wasm32" + ], + "license": "MIT", + "optional": true, + "dependencies": { + "@napi-rs/wasm-runtime": "^0.2.4" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@oxc-resolver/binding-win32-arm64-msvc": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.12.0.tgz", + "integrity": "sha512-GRe4bqCfFsyghruEn5bv47s9w3EWBdO2q72xCz5kpQ0LWbw+enPHtTjw3qX5PUcFYpKykM55FaO0hFDs1yzatw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@oxc-resolver/binding-win32-x64-msvc": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/@oxc-resolver/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.12.0.tgz", + "integrity": "sha512-Z3llHH0jfJP4mlWq3DT7bK6qV+/vYe0+xzCgfc67+Tc/U3eYndujl880bexeGdGNPh87JeYznpZAOJ44N7QVVQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, "node_modules/@pkgjs/parseargs": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz", @@ -5686,12 +5875,63 @@ "resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.1.tgz", "integrity": "sha512-dzJtaDAAoXx4GCOJpbB2eG/Qj8VDpdwkLsWGzGm+0L7E8/434RyMbAHmk9ubXWVAb9nXmc44jUf8GKqVDiKezg==" }, + "node_modules/@swc-node/core": { + "version": "1.13.3", + "resolved": "https://registry.npmjs.org/@swc-node/core/-/core-1.13.3.tgz", + "integrity": "sha512-OGsvXIid2Go21kiNqeTIn79jcaX4l0G93X2rAnas4LFoDyA9wAwVK7xZdm+QsKoMn5Mus2yFLCc4OtX2dD/PWA==", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "peerDependencies": { + "@swc/core": ">= 1.4.13", + "@swc/types": ">= 0.1" + } + }, + "node_modules/@swc-node/register": { + "version": "1.10.9", + "resolved": "https://registry.npmjs.org/@swc-node/register/-/register-1.10.9.tgz", + "integrity": "sha512-iXy2sjP0phPEpK2yivjRC3PAgoLaT4sjSk0LDWCTdcTBJmR4waEog0E6eJbvoOkLkOtWw37SB8vCkl/bbh4+8A==", + "license": "MIT", + "dependencies": { + "@swc-node/core": "^1.13.3", + "@swc-node/sourcemap-support": "^0.5.1", + "colorette": "^2.0.20", + "debug": "^4.3.5", + "oxc-resolver": "^1.10.2", + "pirates": "^4.0.6", + "tslib": "^2.6.3" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "peerDependencies": { + "@swc/core": ">= 1.4.13", + "typescript": ">= 4.3" + } + }, + "node_modules/@swc-node/sourcemap-support": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/@swc-node/sourcemap-support/-/sourcemap-support-0.5.1.tgz", + "integrity": "sha512-JxIvIo/Hrpv0JCHSyRpetAdQ6lB27oFYhv0PKCNf1g2gUXOjpeR1exrXccRxLMuAV5WAmGFBwRnNOJqN38+qtg==", + "license": "MIT", + "dependencies": { + "source-map-support": "^0.5.21", + "tslib": "^2.6.3" + } + }, "node_modules/@swc/core": { "version": "1.7.4", "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.7.4.tgz", "integrity": "sha512-+wSycNxOw9QQz81AJAZlNS34EtOIifwUXMPACg05PWjECsjOKDTXLCVPx6J0lRaxhHSGBU2OYs9mRfIvxGt3CA==", "hasInstallScript": true, "license": "Apache-2.0", + "peer": true, "dependencies": { "@swc/counter": "^0.1.3", "@swc/types": "^0.1.12" @@ -5736,6 +5976,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">=10" } @@ -5752,6 +5993,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">=10" } @@ -5760,31 +6002,15 @@ "version": "0.1.3", "resolved": "https://registry.npmjs.org/@swc/counter/-/counter-0.1.3.tgz", "integrity": "sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==", - "license": "Apache-2.0" - }, - "node_modules/@swc/register": { - "version": "0.1.10", - "resolved": "https://registry.npmjs.org/@swc/register/-/register-0.1.10.tgz", - "integrity": "sha512-6STwH/q4dc3pitXLVkV7sP0Hiy+zBsU2wOF1aXpXR95pnH3RYHKIsDC+gvesfyB7jxNT9OOZgcqOp9RPxVTx9A==", - "deprecated": "Use @swc-node/register instead", - "license": "(Apache-2.0 OR MIT)", - "dependencies": { - "lodash.clonedeep": "^4.5.0", - "pirates": "^4.0.1", - "source-map-support": "^0.5.13" - }, - "bin": { - "swc-node": "bin/swc-node" - }, - "peerDependencies": { - "@swc/core": "^1.0.46" - } + "license": "Apache-2.0", + "peer": true }, "node_modules/@swc/types": { "version": "0.1.12", "resolved": "https://registry.npmjs.org/@swc/types/-/types-0.1.12.tgz", "integrity": "sha512-wBJA+SdtkbFhHjTMYH+dEH1y4VpfGdAc2Kw/LK09i9bXd/K6j6PkDcFCEzb6iVfZMkPRrl/q0e3toqTAJdkIVA==", "license": "Apache-2.0", + "peer": true, "dependencies": { "@swc/counter": "^0.1.3" } @@ -5857,6 +6083,16 @@ "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", "dev": true }, + "node_modules/@tybys/wasm-util": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.9.0.tgz", + "integrity": "sha512-6+7nlbMVX/PVDCwaIQ8nTOPveOcFLSt8GcXdx8hD0bt39uWxYT88uXzqTd4fTvqta7oeUJqudepapKNt2DYJFw==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/@types/babel__core": { "version": "7.20.4", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.4.tgz", @@ -8019,6 +8255,12 @@ "resolved": "https://registry.npmjs.org/colord/-/colord-2.9.3.tgz", "integrity": "sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==" }, + "node_modules/colorette": { + "version": "2.0.20", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", + "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", + "license": "MIT" + }, "node_modules/colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", @@ -8572,11 +8814,12 @@ } }, "node_modules/debug": { - "version": "4.3.4", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", - "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz", + "integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==", + "license": "MIT", "dependencies": { - "ms": "2.1.2" + "ms": "^2.1.3" }, "engines": { "node": ">=6.0" @@ -13956,9 +14199,9 @@ } }, "node_modules/mongodb": { - "version": "6.7.0", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.7.0.tgz", - "integrity": "sha512-TMKyHdtMcO0fYBNORiYdmM25ijsHs+Njs963r4Tro4OQZzqYigAzYQouwWRg4OIaiLRUEGUh/1UAcH5lxdSLIA==", + "version": "6.8.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.8.0.tgz", + "integrity": "sha512-HGQ9NWDle5WvwMnrvUxsFYPd3JEbqD3RgABHBQRuoCEND0qzhsd0iH5ypHsf1eJ+sXmvmyKpP+FLOKY8Il7jMw==", "license": "Apache-2.0", "dependencies": { "@mongodb-js/saslprep": "^1.1.5", @@ -14155,14 +14398,14 @@ } }, "node_modules/mongoose": { - "version": "8.5.2", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.5.2.tgz", - "integrity": "sha512-GZB4rHMdYfGatV+23IpCrqFbyCOjCNOHXgWbirr92KRwTEncBrtW3kgU9vmpKjsGf7nMmnAy06SwWUv1vhDkSg==", + "version": "8.6.2", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.6.2.tgz", + "integrity": "sha512-ErbDVvuUzUfyQpXvJ6sXznmZDICD8r6wIsa0VKjJtB6/LZncqwUn5Um040G1BaNo6L3Jz+xItLSwT0wZmSmUaQ==", "license": "MIT", "dependencies": { "bson": "^6.7.0", "kareem": "2.6.3", - "mongodb": "6.7.0", + "mongodb": "6.8.0", "mpath": "0.9.0", "mquery": "5.0.0", "ms": "2.1.3", @@ -14221,11 +14464,6 @@ "node": ">=4.0.0" } }, - "node_modules/mongoose/node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" - }, "node_modules/morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -14289,9 +14527,10 @@ } }, "node_modules/ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" }, "node_modules/multer": { "version": "1.4.5-lts.1", @@ -14770,6 +15009,28 @@ "node": ">=0.10.0" } }, + "node_modules/oxc-resolver": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/oxc-resolver/-/oxc-resolver-1.12.0.tgz", + "integrity": "sha512-YlaCIArvWNKCWZFRrMjhh2l5jK80eXnpYP+bhRc1J/7cW3TiyEY0ngJo73o/5n8hA3+4yLdTmXLNTQ3Ncz50LQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/Boshen" + }, + "optionalDependencies": { + "@oxc-resolver/binding-darwin-arm64": "1.12.0", + "@oxc-resolver/binding-darwin-x64": "1.12.0", + "@oxc-resolver/binding-freebsd-x64": "1.12.0", + "@oxc-resolver/binding-linux-arm-gnueabihf": "1.12.0", + "@oxc-resolver/binding-linux-arm64-gnu": "1.12.0", + "@oxc-resolver/binding-linux-arm64-musl": "1.12.0", + "@oxc-resolver/binding-linux-x64-gnu": "1.12.0", + "@oxc-resolver/binding-linux-x64-musl": "1.12.0", + "@oxc-resolver/binding-wasm32-wasi": "1.12.0", + "@oxc-resolver/binding-win32-arm64-msvc": "1.12.0", + "@oxc-resolver/binding-win32-x64-msvc": "1.12.0" + } + }, "node_modules/p-event": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/p-event/-/p-event-4.2.0.tgz", @@ -16832,11 +17093,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, - "node_modules/send/node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" - }, "node_modules/serialize-javascript": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz", @@ -17934,18 +18190,17 @@ } }, "node_modules/ts-migrate-mongoose": { - "version": "3.8.3", - "resolved": "https://registry.npmjs.org/ts-migrate-mongoose/-/ts-migrate-mongoose-3.8.3.tgz", - "integrity": "sha512-xdpraG4qirhpeRPjmNmNFv13RbswldCMxekvuk+yrnR9gJ4aH+Wb4Kd5g9SklbGJolplfg7OIuSs5aNABmzPYg==", + "version": "3.8.4", + "resolved": "https://registry.npmjs.org/ts-migrate-mongoose/-/ts-migrate-mongoose-3.8.4.tgz", + "integrity": "sha512-jOxPONLRXGaC/BiCyMziNsM6ZKdSKyzoriIIv4fINqu5Caj07l5OggZwPeF3S3cqGWxZMwJQftQ9NdfvX4BmLg==", "license": "MIT", "dependencies": { - "@swc/core": "1.7.4", - "@swc/register": "0.1.10", + "@swc-node/register": "1.10.9", "chalk": "4.1.2", "commander": "12.1.0", "dotenv": "16.4.5", "inquirer": "8.2.6", - "mongoose": "8.5.2" + "mongoose": "8.6.2" }, "bin": { "migrate": "dist/cjs/bin.js" @@ -18197,7 +18452,6 @@ "version": "5.3.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", - "devOptional": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/api/package.json b/api/package.json index 1c8b5a45..ce4fb665 100644 --- a/api/package.json +++ b/api/package.json @@ -27,8 +27,7 @@ "typecheck": "tsc --noEmit", "reset": "npm install && npm run containers:restart", "reset:hard": "npm clean-install && npm run containers:rebuild", - "migrate": "./node_modules/ts-migrate-mongoose/dist/cjs/bin.js --config-path ./migrations/config/migrate.ts", - "create-migration": "ts-node ./migrations/config/create.ts" + "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" }, "dependencies": { "@nestjs-modules/mailer": "^1.11.2", @@ -73,7 +72,7 @@ "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", "slug": "^8.2.2", - "ts-migrate-mongoose": "^3.8.3", + "ts-migrate-mongoose": "^3.8.4", "uuid": "^9.0.1" }, "devDependencies": { @@ -157,4 +156,4 @@ "@/(.*)": "/$1" } } -} +} \ No newline at end of file diff --git a/api/patches/ts-migrate-mongoose+3.8.3.patch b/api/patches/ts-migrate-mongoose+3.8.3.patch deleted file mode 100644 index 04e7b0de..00000000 --- a/api/patches/ts-migrate-mongoose+3.8.3.patch +++ /dev/null @@ -1,72 +0,0 @@ -diff --git a/node_modules/ts-migrate-mongoose/dist/cjs/migrator.js b/node_modules/ts-migrate-mongoose/dist/cjs/migrator.js -index e319b94..342aa69 100644 ---- a/node_modules/ts-migrate-mongoose/dist/cjs/migrator.js -+++ b/node_modules/ts-migrate-mongoose/dist/cjs/migrator.js -@@ -115,7 +115,8 @@ class Migrator { - .filter((file) => !file.existsInDatabase) - .map((file) => file.filename); - this.log('Synchronizing database with file system migrations...'); -- migrationsToImport = await this.choseMigrations(migrationsToImport, 'The following migrations exist in the migrations folder but not in the database.\nSelect the ones you want to import into the database'); -+ // This line is commented out because it is not necessary to ask the user to import the migrations into the database -+ // migrationsToImport = await this.choseMigrations(migrationsToImport, 'The following migrations exist in the migrations folder but not in the database.\nSelect the ones you want to import into the database'); - return this.syncMigrations(migrationsToImport); - } - catch (error) { -@@ -133,7 +134,8 @@ class Migrator { - let migrationsToDelete = migrationsInDb - .filter((migration) => !migrationsInFs.find((file) => file.filename === migration.filename)) - .map((migration) => migration.name); -- migrationsToDelete = await this.choseMigrations(migrationsToDelete, 'The following migrations exist in the database but not in the migrations folder.\nSelect the ones you want to remove from the database'); -+ // This line is commented out because it is not necessary to ask the user to delete the migrations from the database -+ // migrationsToDelete = await this.choseMigrations(migrationsToDelete, 'The following migrations exist in the database but not in the migrations folder.\nSelect the ones you want to remove from the database'); - if (migrationsToDelete.length) { - migrationsDeleted = await this.migrationModel.find({ name: { $in: migrationsToDelete } }).exec(); - this.log(`Removing migration(s) from database: \n${chalk_1.default.cyan(migrationsToDelete.join('\n'))} `); -diff --git a/node_modules/ts-migrate-mongoose/dist/esm/migrator.js b/node_modules/ts-migrate-mongoose/dist/esm/migrator.js -index 7ad7150..11e951c 100644 ---- a/node_modules/ts-migrate-mongoose/dist/esm/migrator.js -+++ b/node_modules/ts-migrate-mongoose/dist/esm/migrator.js -@@ -111,7 +111,8 @@ class Migrator { - .filter((file) => !file.existsInDatabase) - .map((file) => file.filename); - this.log('Synchronizing database with file system migrations...'); -- migrationsToImport = await this.choseMigrations(migrationsToImport, 'The following migrations exist in the migrations folder but not in the database.\nSelect the ones you want to import into the database'); -+ // This line is commented out because it is not necessary to ask the user to import the migrations into the database -+ // migrationsToImport = await this.choseMigrations(migrationsToImport, 'The following migrations exist in the migrations folder but not in the database.\nSelect the ones you want to import into the database'); - return this.syncMigrations(migrationsToImport); - } - catch (error) { -@@ -129,7 +130,8 @@ class Migrator { - let migrationsToDelete = migrationsInDb - .filter((migration) => !migrationsInFs.find((file) => file.filename === migration.filename)) - .map((migration) => migration.name); -- migrationsToDelete = await this.choseMigrations(migrationsToDelete, 'The following migrations exist in the database but not in the migrations folder.\nSelect the ones you want to remove from the database'); -+ // This line is commented out because it is not necessary to ask the user to delete the migrations from the database -+ // migrationsToDelete = await this.choseMigrations(migrationsToDelete, 'The following migrations exist in the database but not in the migrations folder.\nSelect the ones you want to remove from the database'); - if (migrationsToDelete.length) { - migrationsDeleted = await this.migrationModel.find({ name: { $in: migrationsToDelete } }).exec(); - this.log(`Removing migration(s) from database: \n${chalk.cyan(migrationsToDelete.join('\n'))} `); -diff --git a/node_modules/ts-migrate-mongoose/src/migrator.ts b/node_modules/ts-migrate-mongoose/src/migrator.ts -index db54f87..7256796 100644 ---- a/node_modules/ts-migrate-mongoose/src/migrator.ts -+++ b/node_modules/ts-migrate-mongoose/src/migrator.ts -@@ -202,7 +202,8 @@ class Migrator { - .map((file) => file.filename) - - this.log('Synchronizing database with file system migrations...') -- migrationsToImport = await this.choseMigrations(migrationsToImport, 'The following migrations exist in the migrations folder but not in the database.\nSelect the ones you want to import into the database') -+ // This line is commented out because it is not necessary to ask the user to import the migrations into the database -+ // migrationsToImport = await this.choseMigrations(migrationsToImport, 'The following migrations exist in the migrations folder but not in the database.\nSelect the ones you want to import into the database') - - return this.syncMigrations(migrationsToImport) - } catch (error) { -@@ -232,7 +233,8 @@ class Migrator { - .filter((migration) => !migrationsInFs.find((file) => file.filename === migration.filename)) - .map((migration) => migration.name) - -- migrationsToDelete = await this.choseMigrations(migrationsToDelete, 'The following migrations exist in the database but not in the migrations folder.\nSelect the ones you want to remove from the database') -+ // This line is commented out because it is not necessary to ask the user to delete the migrations from the database -+ // migrationsToDelete = await this.choseMigrations(migrationsToDelete, 'The following migrations exist in the database but not in the migrations folder.\nSelect the ones you want to remove from the database') - - if (migrationsToDelete.length) { - migrationsDeleted = await this.migrationModel.find({ name: { $in: migrationsToDelete } }).exec() From 58158e8f9eebb63ce89c235bf600b55db7606d2d Mon Sep 17 00:00:00 2001 From: Yassine Sallemi Date: Tue, 8 Oct 2024 17:18:36 +0100 Subject: [PATCH 2/3] fix: database migrations through hexabot-cli --- api/.gitignore | 3 +-- api/README.md | 15 +++++++-------- api/package.json | 4 ++-- cli/src/index.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) 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(); } + From 395d16021a8193997b57f53b7674f1bd352cd192 Mon Sep 17 00:00:00 2001 From: Yassine Sallemi Date: Tue, 8 Oct 2024 17:20:50 +0100 Subject: [PATCH 3/3] fix: cli typo fix --- cli/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/index.ts b/cli/src/index.ts index 53c186de..31620dfe 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -102,7 +102,7 @@ const dockerExec = ( stdio: 'inherit', }); } catch (error) { - console.error(chalk.red('Error executing Docker Compose command.')); + console.error(chalk.red('Error executing Docker Exec command.')); process.exit(1); } };