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> { // 班次表 manager .create_table( Table::create() .table(Shift::Table) .col( ColumnDef::new(Shift::Id) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(Shift::TenantId).uuid().not_null()) .col(ColumnDef::new(Shift::ShiftDate).date().not_null()) .col(ColumnDef::new(Shift::Period).string_len(20).not_null()) .col(ColumnDef::new(Shift::NurseId).uuid()) .col(ColumnDef::new(Shift::Status).string_len(20).not_null().default("scheduled")) .col(ColumnDef::new(Shift::Notes).text()) .col(ColumnDef::new(Shift::CreatedAt).timestamp_with_time_zone().not_null()) .col(ColumnDef::new(Shift::UpdatedAt).timestamp_with_time_zone().not_null()) .col(ColumnDef::new(Shift::CreatedBy).uuid()) .col(ColumnDef::new(Shift::UpdatedBy).uuid()) .col(ColumnDef::new(Shift::DeletedAt).timestamp_with_time_zone()) .col(ColumnDef::new(Shift::Version).integer().not_null().default(1)) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_shifts_tenant_date") .table(Shift::Table) .col(Shift::TenantId) .col(Shift::ShiftDate) .col(Shift::DeletedAt) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_shifts_tenant_nurse") .table(Shift::Table) .col(Shift::TenantId) .col(Shift::NurseId) .col(Shift::DeletedAt) .to_owned(), ) .await?; // 患者分配表 manager .create_table( Table::create() .table(PatientAssignment::Table) .col( ColumnDef::new(PatientAssignment::Id) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(PatientAssignment::TenantId).uuid().not_null()) .col(ColumnDef::new(PatientAssignment::ShiftId).uuid().not_null()) .col(ColumnDef::new(PatientAssignment::PatientId).uuid().not_null()) .col(ColumnDef::new(PatientAssignment::CareLevel).string_len(20).not_null().default("routine")) .col(ColumnDef::new(PatientAssignment::Notes).text()) .col(ColumnDef::new(PatientAssignment::CreatedAt).timestamp_with_time_zone().not_null()) .col(ColumnDef::new(PatientAssignment::UpdatedAt).timestamp_with_time_zone().not_null()) .col(ColumnDef::new(PatientAssignment::CreatedBy).uuid()) .col(ColumnDef::new(PatientAssignment::UpdatedBy).uuid()) .col(ColumnDef::new(PatientAssignment::DeletedAt).timestamp_with_time_zone()) .col(ColumnDef::new(PatientAssignment::Version).integer().not_null().default(1)) .to_owned(), ) .await?; manager .create_foreign_key( ForeignKey::create() .name("fk_patient_assignments_shift") .from(PatientAssignment::Table, PatientAssignment::ShiftId) .to(Shift::Table, Shift::Id) .on_delete(ForeignKeyAction::Cascade) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_patient_assignments_shift") .table(PatientAssignment::Table) .col(PatientAssignment::TenantId) .col(PatientAssignment::ShiftId) .col(PatientAssignment::DeletedAt) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_patient_assignments_patient") .table(PatientAssignment::Table) .col(PatientAssignment::TenantId) .col(PatientAssignment::PatientId) .col(PatientAssignment::DeletedAt) .to_owned(), ) .await?; // 交接日志表 manager .create_table( Table::create() .table(HandoffLog::Table) .col( ColumnDef::new(HandoffLog::Id) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(HandoffLog::TenantId).uuid().not_null()) .col(ColumnDef::new(HandoffLog::FromShiftId).uuid().not_null()) .col(ColumnDef::new(HandoffLog::ToShiftId).uuid().not_null()) .col(ColumnDef::new(HandoffLog::PatientId).uuid().not_null()) .col(ColumnDef::new(HandoffLog::Notes).text()) .col(ColumnDef::new(HandoffLog::PendingItems).json_binary()) .col(ColumnDef::new(HandoffLog::CreatedAt).timestamp_with_time_zone().not_null()) .col(ColumnDef::new(HandoffLog::UpdatedAt).timestamp_with_time_zone().not_null()) .col(ColumnDef::new(HandoffLog::CreatedBy).uuid()) .col(ColumnDef::new(HandoffLog::UpdatedBy).uuid()) .col(ColumnDef::new(HandoffLog::DeletedAt).timestamp_with_time_zone()) .col(ColumnDef::new(HandoffLog::Version).integer().not_null().default(1)) .to_owned(), ) .await?; manager .create_foreign_key( ForeignKey::create() .name("fk_handoff_log_from_shift") .from(HandoffLog::Table, HandoffLog::FromShiftId) .to(Shift::Table, Shift::Id) .on_delete(ForeignKeyAction::Cascade) .to_owned(), ) .await?; manager .create_foreign_key( ForeignKey::create() .name("fk_handoff_log_to_shift") .from(HandoffLog::Table, HandoffLog::ToShiftId) .to(Shift::Table, Shift::Id) .on_delete(ForeignKeyAction::Cascade) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_handoff_log_to_shift") .table(HandoffLog::Table) .col(HandoffLog::TenantId) .col(HandoffLog::ToShiftId) .col(HandoffLog::DeletedAt) .to_owned(), ) .await?; Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table(Table::drop().table(HandoffLog::Table).to_owned()) .await?; manager .drop_table(Table::drop().table(PatientAssignment::Table).to_owned()) .await?; manager .drop_table(Table::drop().table(Shift::Table).to_owned()) .await?; Ok(()) } } #[derive(DeriveIden)] enum Shift { Table, Id, TenantId, ShiftDate, Period, NurseId, Status, Notes, CreatedAt, UpdatedAt, CreatedBy, UpdatedBy, DeletedAt, Version, } #[derive(DeriveIden)] enum PatientAssignment { Table, Id, TenantId, ShiftId, PatientId, CareLevel, Notes, CreatedAt, UpdatedAt, CreatedBy, UpdatedBy, DeletedAt, Version, } #[derive(DeriveIden)] enum HandoffLog { Table, Id, TenantId, FromShiftId, ToShiftId, PatientId, Notes, PendingItems, CreatedAt, UpdatedAt, CreatedBy, UpdatedBy, DeletedAt, Version, }