diff --git a/crates/erp-server/migration/src/lib.rs b/crates/erp-server/migration/src/lib.rs index 16b8d5b..667a169 100644 --- a/crates/erp-server/migration/src/lib.rs +++ b/crates/erp-server/migration/src/lib.rs @@ -172,6 +172,7 @@ mod m20260522_000162_seed_patient_miniprogram_permissions; mod m20260526_000163_points_rule_unique_event_type; mod m20260526_000164_ai_prompt_add_analysis_type; mod m20260526_000165_ai_prompt_fix_analysis_type; +mod m20260526_000166_create_ai_knowledge_bases; pub struct Migrator; @@ -351,6 +352,7 @@ impl MigratorTrait for Migrator { Box::new(m20260526_000163_points_rule_unique_event_type::Migration), Box::new(m20260526_000164_ai_prompt_add_analysis_type::Migration), Box::new(m20260526_000165_ai_prompt_fix_analysis_type::Migration), + Box::new(m20260526_000166_create_ai_knowledge_bases::Migration), ] } } diff --git a/crates/erp-server/migration/src/m20260526_000166_create_ai_knowledge_bases.rs b/crates/erp-server/migration/src/m20260526_000166_create_ai_knowledge_bases.rs new file mode 100644 index 0000000..59291a5 --- /dev/null +++ b/crates/erp-server/migration/src/m20260526_000166_create_ai_knowledge_bases.rs @@ -0,0 +1,141 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[derive(Iden)] +enum AiKnowledgeBases { + Table, + Id, + TenantId, + Name, + KbType, + Description, + Icon, + ChunkStrategy, + IntentKeywords, + EmbeddingModel, + IsEnabled, + DocumentCount, + ChunkCount, + CreatedAt, + UpdatedAt, + CreatedBy, + UpdatedBy, + DeletedAt, + VersionLock, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(AiKnowledgeBases::Table) + .if_not_exists() + .col( + ColumnDef::new(AiKnowledgeBases::Id) + .uuid() + .not_null() + .primary_key(), + ) + .col(ColumnDef::new(AiKnowledgeBases::TenantId).uuid().not_null()) + .col( + ColumnDef::new(AiKnowledgeBases::Name) + .string_len(200) + .not_null(), + ) + .col( + ColumnDef::new(AiKnowledgeBases::KbType) + .string_len(50) + .not_null(), + ) + .col(ColumnDef::new(AiKnowledgeBases::Description).text()) + .col(ColumnDef::new(AiKnowledgeBases::Icon).string_len(50)) + .col( + ColumnDef::new(AiKnowledgeBases::ChunkStrategy) + .json_binary() + .not_null() + .default(Expr::cust("'{}'")), + ) + .col( + ColumnDef::new(AiKnowledgeBases::IntentKeywords) + .json_binary() + .not_null() + .default(Expr::cust("'[]'")), + ) + .col(ColumnDef::new(AiKnowledgeBases::EmbeddingModel).string_len(100)) + .col( + ColumnDef::new(AiKnowledgeBases::IsEnabled) + .boolean() + .not_null() + .default(true), + ) + .col( + ColumnDef::new(AiKnowledgeBases::DocumentCount) + .integer() + .not_null() + .default(0), + ) + .col( + ColumnDef::new(AiKnowledgeBases::ChunkCount) + .integer() + .not_null() + .default(0), + ) + .col( + ColumnDef::new(AiKnowledgeBases::CreatedAt) + .timestamp_with_time_zone() + .not_null() + .default(Expr::current_timestamp()), + ) + .col( + ColumnDef::new(AiKnowledgeBases::UpdatedAt) + .timestamp_with_time_zone() + .not_null() + .default(Expr::current_timestamp()), + ) + .col(ColumnDef::new(AiKnowledgeBases::CreatedBy).uuid()) + .col(ColumnDef::new(AiKnowledgeBases::UpdatedBy).uuid()) + .col(ColumnDef::new(AiKnowledgeBases::DeletedAt).timestamp_with_time_zone()) + .col( + ColumnDef::new(AiKnowledgeBases::VersionLock) + .integer() + .not_null() + .default(1), + ) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .if_not_exists() + .name("idx_kb_tenant") + .table(AiKnowledgeBases::Table) + .col(AiKnowledgeBases::TenantId) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .if_not_exists() + .name("idx_kb_type") + .table(AiKnowledgeBases::Table) + .col(AiKnowledgeBases::TenantId) + .col(AiKnowledgeBases::KbType) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(AiKnowledgeBases::Table).to_owned()) + .await + } +}