use sea_orm_migration::prelude::*; #[derive(DeriveMigrationName)] pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { // care_plans — 护理计划主表 manager .create_table( Table::create() .table(Alias::new("care_plans")) .if_not_exists() .col( ColumnDef::new(Alias::new("id")) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null()) .col(ColumnDef::new(Alias::new("patient_id")).uuid().not_null()) .col( ColumnDef::new(Alias::new("plan_type")) .string_len(50) .not_null(), ) .col( ColumnDef::new(Alias::new("status")) .string_len(32) .not_null() .default("draft"), ) .col( ColumnDef::new(Alias::new("title")) .string_len(200) .not_null(), ) .col( ColumnDef::new(Alias::new("goals")) .json_binary() .not_null() .default(Expr::cust("'[]'::jsonb")), ) .col(ColumnDef::new(Alias::new("start_date")).date()) .col(ColumnDef::new(Alias::new("end_date")).date()) .col(ColumnDef::new(Alias::new("notes")).text()) .col( ColumnDef::new(Alias::new("created_at")) .timestamp_with_time_zone() .not_null(), ) .col( ColumnDef::new(Alias::new("updated_at")) .timestamp_with_time_zone() .not_null(), ) .col(ColumnDef::new(Alias::new("created_by")).uuid()) .col(ColumnDef::new(Alias::new("updated_by")).uuid()) .col(ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone()) .col( ColumnDef::new(Alias::new("version")) .integer() .not_null() .default(1), ) .to_owned(), ) .await?; manager .create_index( Index::create() .if_not_exists() .name("idx_care_plans_tenant_patient") .table(Alias::new("care_plans")) .col(Alias::new("tenant_id")) .col(Alias::new("patient_id")) .col(Alias::new("deleted_at")) .to_owned(), ) .await?; manager .create_index( Index::create() .if_not_exists() .name("idx_care_plans_tenant_status") .table(Alias::new("care_plans")) .col(Alias::new("tenant_id")) .col(Alias::new("status")) .col(Alias::new("deleted_at")) .to_owned(), ) .await?; // care_plan_items — 护理计划条目(干预/监测/目标) manager .create_table( Table::create() .table(Alias::new("care_plan_items")) .if_not_exists() .col( ColumnDef::new(Alias::new("id")) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null()) .col(ColumnDef::new(Alias::new("plan_id")).uuid().not_null()) .col( ColumnDef::new(Alias::new("item_type")) .string_len(32) .not_null(), ) .col( ColumnDef::new(Alias::new("title")) .string_len(200) .not_null(), ) .col(ColumnDef::new(Alias::new("description")).text()) .col( ColumnDef::new(Alias::new("status")) .string_len(32) .not_null() .default("pending"), ) .col(ColumnDef::new(Alias::new("schedule")).string_len(100)) .col( ColumnDef::new(Alias::new("sort_order")) .integer() .default(0), ) .col( ColumnDef::new(Alias::new("created_at")) .timestamp_with_time_zone() .not_null(), ) .col( ColumnDef::new(Alias::new("updated_at")) .timestamp_with_time_zone() .not_null(), ) .col(ColumnDef::new(Alias::new("created_by")).uuid()) .col(ColumnDef::new(Alias::new("updated_by")).uuid()) .col(ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone()) .col( ColumnDef::new(Alias::new("version")) .integer() .not_null() .default(1), ) .foreign_key( &mut ForeignKey::create() .name("fk_care_plan_items_plan_id") .from(Alias::new("care_plan_items"), Alias::new("plan_id")) .to(Alias::new("care_plans"), Alias::new("id")) .on_delete(ForeignKeyAction::Cascade) .to_owned(), ) .to_owned(), ) .await?; manager .create_index( Index::create() .if_not_exists() .name("idx_care_plan_items_tenant_plan") .table(Alias::new("care_plan_items")) .col(Alias::new("tenant_id")) .col(Alias::new("plan_id")) .col(Alias::new("deleted_at")) .to_owned(), ) .await?; // care_plan_outcomes — 护理计划预后测量 manager .create_table( Table::create() .table(Alias::new("care_plan_outcomes")) .if_not_exists() .col( ColumnDef::new(Alias::new("id")) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null()) .col(ColumnDef::new(Alias::new("plan_id")).uuid().not_null()) .col(ColumnDef::new(Alias::new("item_id")).uuid()) .col( ColumnDef::new(Alias::new("metric")) .string_len(100) .not_null(), ) .col( ColumnDef::new(Alias::new("baseline_value")) .string_len(50) .not_null(), ) .col( ColumnDef::new(Alias::new("target_value")) .string_len(50) .not_null(), ) .col(ColumnDef::new(Alias::new("current_value")).string_len(50)) .col(ColumnDef::new(Alias::new("measured_at")).timestamp_with_time_zone()) .col(ColumnDef::new(Alias::new("notes")).text()) .col( ColumnDef::new(Alias::new("created_at")) .timestamp_with_time_zone() .not_null(), ) .col( ColumnDef::new(Alias::new("updated_at")) .timestamp_with_time_zone() .not_null(), ) .col(ColumnDef::new(Alias::new("created_by")).uuid()) .col(ColumnDef::new(Alias::new("updated_by")).uuid()) .col(ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone()) .col( ColumnDef::new(Alias::new("version")) .integer() .not_null() .default(1), ) .foreign_key( &mut ForeignKey::create() .name("fk_care_plan_outcomes_plan_id") .from(Alias::new("care_plan_outcomes"), Alias::new("plan_id")) .to(Alias::new("care_plans"), Alias::new("id")) .on_delete(ForeignKeyAction::Cascade) .to_owned(), ) .to_owned(), ) .await?; manager .create_index( Index::create() .if_not_exists() .name("idx_care_plan_outcomes_tenant_plan") .table(Alias::new("care_plan_outcomes")) .col(Alias::new("tenant_id")) .col(Alias::new("plan_id")) .col(Alias::new("deleted_at")) .to_owned(), ) .await?; Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table( Table::drop() .table(Alias::new("care_plan_outcomes")) .to_owned(), ) .await?; manager .drop_table( Table::drop() .table(Alias::new("care_plan_items")) .to_owned(), ) .await?; manager .drop_table(Table::drop().table(Alias::new("care_plans")).to_owned()) .await?; Ok(()) } }