From f464fc5e4536ff7b3d339ffeedf3a740db5e6b04 Mon Sep 17 00:00:00 2001 From: Mohamed Marrouchi Date: Thu, 2 Jan 2025 08:39:44 +0100 Subject: [PATCH] fix: remove old migration implementation and update README --- api/README.md | 59 +----------- api/migrations/config/migrate.ts | 17 ---- api/migrations/config/template.ts | 17 ---- api/migrations/models/index.ts | 145 ------------------------------ 4 files changed, 2 insertions(+), 236 deletions(-) delete mode 100644 api/migrations/config/migrate.ts delete mode 100644 api/migrations/config/template.ts delete mode 100644 api/migrations/models/index.ts diff --git a/api/README.md b/api/README.md index 3e8dfc08..b61a071d 100644 --- a/api/README.md +++ b/api/README.md @@ -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 -``` - -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 { - // Write migration here -} - -export async function down(): Promise { - // 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 { - const { UserModel } = await getModels(); - await UserModel.updateMany({}, { language: 'fr' }); -} - -export async function down(): Promise {} -``` - -### 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 diff --git a/api/migrations/config/migrate.ts b/api/migrations/config/migrate.ts deleted file mode 100644 index 74c06405..00000000 --- a/api/migrations/config/migrate.ts +++ /dev/null @@ -1,17 +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). - */ - -import { config } from '@/config'; - -export default { - uri: `${config.mongo.uri}${config.mongo.dbName}?authSource=admin`, - collection: 'migrations', - migrationsPath: './migrations', - templatePath: './migrations/config/template.ts', - autosync: false, -}; diff --git a/api/migrations/config/template.ts b/api/migrations/config/template.ts deleted file mode 100644 index 26e2b71f..00000000 --- a/api/migrations/config/template.ts +++ /dev/null @@ -1,17 +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). - */ - -import getModels from 'migrations/models/index'; - -export async function up(): Promise { - // Write migration here -} - -export async function down(): Promise { - // Write migration here -} diff --git a/api/migrations/models/index.ts b/api/migrations/models/index.ts deleted file mode 100644 index 02622a85..00000000 --- a/api/migrations/models/index.ts +++ /dev/null @@ -1,145 +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). - */ - -import mongoose from 'mongoose'; -import leanDefaults from 'mongoose-lean-defaults'; -import leanGetters from 'mongoose-lean-getters'; -import leanVirtuals from 'mongoose-lean-virtuals'; - -import botStatsSchema, { BotStats } from '@/analytics/schemas/bot-stats.schema'; -import attachmentSchema, { - Attachment, -} from '@/attachment/schemas/attachment.schema'; -import blockSchema, { Block } from '@/chat/schemas/block.schema'; -import contextVarSchema, { - ContextVar, -} from '@/chat/schemas/context-var.schema'; -import conversationSchema, { - Conversation, -} from '@/chat/schemas/conversation.schema'; -import labelSchema, { Label } from '@/chat/schemas/label.schema'; -import messageSchema, { Message } from '@/chat/schemas/message.schema'; -import subscriberSchema, { Subscriber } from '@/chat/schemas/subscriber.schema'; -import { ContentType } from '@/cms/schemas/content-type.schema'; -import contentSchema, { Content } from '@/cms/schemas/content.schema'; -import menuSchema, { Menu } from '@/cms/schemas/menu.schema'; -import { config } from '@/config'; -import translationSchema, { - Translation, -} from '@/i18n/schemas/translation.schema'; -import nlpEntitySchema, { NlpEntity } from '@/nlp/schemas/nlp-entity.schema'; -import nlpSampleEntitySchema, { - NlpSampleEntity, -} from '@/nlp/schemas/nlp-sample-entity.schema'; -import nlpSampleSchema, { NlpSample } from '@/nlp/schemas/nlp-sample.schema'; -import nlpValueSchema, { NlpValue } from '@/nlp/schemas/nlp-value.schema'; -import settingSchema, { Setting } from '@/setting/schemas/setting.schema'; -import invitationSchema, { Invitation } from '@/user/schemas/invitation.schema'; -import modelSchema, { Model } from '@/user/schemas/model.schema'; -import permissionSchema, { Permission } from '@/user/schemas/permission.schema'; -import roleSchema, { Role } from '@/user/schemas/role.schema'; -import userSchema, { User } from '@/user/schemas/user.schema'; -import idPlugin from '@/utils/schema-plugin/id.plugin'; - -async function mongoMigrationConnection() { - try { - const connection = await mongoose.connect(config.mongo.uri, { - dbName: config.mongo.dbName, - }); - - connection.plugin(idPlugin); - connection.plugin(leanVirtuals); - connection.plugin(leanGetters); - connection.plugin(leanDefaults); - } catch (err) { - throw err; - } -} - -async function getModels() { - await mongoMigrationConnection(); - const AttachmentModel = mongoose.model( - Attachment.name, - attachmentSchema, - ); - const BlockModel = mongoose.model(Block.name, blockSchema); - const BotstatsModel = mongoose.model(BotStats.name, botStatsSchema); - const ContentModel = mongoose.model(Content.name, contentSchema); - const ContenttypeModel = mongoose.model( - ContentType.name, - contentSchema, - ); - const ContextVarModel = mongoose.model( - ContextVar.name, - contextVarSchema, - ); - const ConversationModel = mongoose.model( - Conversation.name, - conversationSchema, - ); - const InvitationModel = mongoose.model( - Invitation.name, - invitationSchema, - ); - const LabelModel = mongoose.model