feat(diary): 添加 15 个 SeaORM 实体和数据库迁移 (Phase B1)
实体: - journal_entry: 日记核心表 (心情/天气/标签/版本) - journal_element: 日记元素 (文字/图片/贴纸/手写/胶带) - handwriting_stroke: 手写笔画 (独立大字段表) - school_class: 班级 (6位码/过期控制) - class_member: 班级成员 (复合PK) - topic_assignment: 主题布置 - comment: 老师点评 - sticker_pack + sticker: 贴纸包和贴纸 - template: 日记模板 - achievement + user_achievement: 成就系统 - parent_child_binding: 家长-孩子绑定 (PIPL) - teacher_profile: 老师档案 - user_settings: 用户设置 迁移 (000170-000184): - 15 个建表迁移 + 索引 + RLS 策略 + 种子数据 - 所有表含 tenant_id 多租户隔离 - 软删除 + 乐观锁版本号 - 外键级联删除 - 暖记权限注册到基座 permissions 表 验证: cargo check 通过, 425 个测试全通过
This commit is contained in:
@@ -55,6 +55,21 @@ mod m20260504_000106_create_api_clients;
|
||||
mod m20260513_000144_enforce_version_optimistic_lock;
|
||||
mod m20260518_000149_fix_admin_permissions;
|
||||
mod m20260529_000169_supplement_rls_for_new_tables;
|
||||
mod m20260531_000170_create_journal_entries;
|
||||
mod m20260531_000171_create_journal_elements;
|
||||
mod m20260531_000172_create_handwriting_strokes;
|
||||
mod m20260531_000173_create_school_classes;
|
||||
mod m20260531_000174_create_class_members;
|
||||
mod m20260531_000175_create_topic_assignments;
|
||||
mod m20260531_000176_create_comments;
|
||||
mod m20260531_000177_create_sticker_packs;
|
||||
mod m20260531_000178_create_templates;
|
||||
mod m20260531_000179_create_achievements;
|
||||
mod m20260531_000180_create_parent_child_bindings;
|
||||
mod m20260531_000181_create_teacher_profiles;
|
||||
mod m20260531_000182_create_user_settings;
|
||||
mod m20260531_000183_diary_indexes_and_fts;
|
||||
mod m20260531_000184_diary_seed_data;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -115,6 +130,22 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260513_000144_enforce_version_optimistic_lock::Migration),
|
||||
Box::new(m20260518_000149_fix_admin_permissions::Migration),
|
||||
Box::new(m20260529_000169_supplement_rls_for_new_tables::Migration),
|
||||
// --- 暖记 (Warm Notes) 迁移 ---
|
||||
Box::new(m20260531_000170_create_journal_entries::Migration),
|
||||
Box::new(m20260531_000171_create_journal_elements::Migration),
|
||||
Box::new(m20260531_000172_create_handwriting_strokes::Migration),
|
||||
Box::new(m20260531_000173_create_school_classes::Migration),
|
||||
Box::new(m20260531_000174_create_class_members::Migration),
|
||||
Box::new(m20260531_000175_create_topic_assignments::Migration),
|
||||
Box::new(m20260531_000176_create_comments::Migration),
|
||||
Box::new(m20260531_000177_create_sticker_packs::Migration),
|
||||
Box::new(m20260531_000178_create_templates::Migration),
|
||||
Box::new(m20260531_000179_create_achievements::Migration),
|
||||
Box::new(m20260531_000180_create_parent_child_bindings::Migration),
|
||||
Box::new(m20260531_000181_create_teacher_profiles::Migration),
|
||||
Box::new(m20260531_000182_create_user_settings::Migration),
|
||||
Box::new(m20260531_000183_diary_indexes_and_fts::Migration),
|
||||
Box::new(m20260531_000184_diary_seed_data::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
// 日记条目表
|
||||
|
||||
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(JournalEntries::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(JournalEntries::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(JournalEntries::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalEntries::AuthorId).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalEntries::ClassId).uuid().null())
|
||||
.col(ColumnDef::new(JournalEntries::Title).string().not_null())
|
||||
.col(ColumnDef::new(JournalEntries::Date).date().not_null())
|
||||
.col(ColumnDef::new(JournalEntries::Mood).string().not_null().default("calm"))
|
||||
.col(ColumnDef::new(JournalEntries::Weather).string().not_null().default("sunny"))
|
||||
.col(ColumnDef::new(JournalEntries::Tags).json_binary().null())
|
||||
.col(ColumnDef::new(JournalEntries::IsPrivate).boolean().not_null().default(true))
|
||||
.col(ColumnDef::new(JournalEntries::SharedToClass).boolean().not_null().default(false))
|
||||
.col(ColumnDef::new(JournalEntries::AssignedTopicId).uuid().null())
|
||||
.col(
|
||||
ColumnDef::new(JournalEntries::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(JournalEntries::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(JournalEntries::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalEntries::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalEntries::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(JournalEntries::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 租户 + 作者索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_journal_entries_tenant_author")
|
||||
.table(JournalEntries::Table)
|
||||
.col(JournalEntries::TenantId)
|
||||
.col(JournalEntries::AuthorId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 日期索引(日历查询)
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_journal_entries_tenant_date")
|
||||
.table(JournalEntries::Table)
|
||||
.col(JournalEntries::TenantId)
|
||||
.col(JournalEntries::Date)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 班级索引(班级日记墙)
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_journal_entries_class")
|
||||
.table(JournalEntries::Table)
|
||||
.col(JournalEntries::ClassId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(JournalEntries::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
pub enum JournalEntries {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
AuthorId,
|
||||
ClassId,
|
||||
Title,
|
||||
Date,
|
||||
Mood,
|
||||
Weather,
|
||||
Tags,
|
||||
IsPrivate,
|
||||
SharedToClass,
|
||||
AssignedTopicId,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// 日记元素表
|
||||
|
||||
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(JournalElements::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(JournalElements::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(JournalElements::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalElements::JournalId).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalElements::ElementType).string().not_null())
|
||||
.col(ColumnDef::new(JournalElements::PositionX).double().not_null().default(0.0))
|
||||
.col(ColumnDef::new(JournalElements::PositionY).double().not_null().default(0.0))
|
||||
.col(ColumnDef::new(JournalElements::Width).double().not_null().default(100.0))
|
||||
.col(ColumnDef::new(JournalElements::Height).double().not_null().default(100.0))
|
||||
.col(ColumnDef::new(JournalElements::Rotation).double().not_null().default(0.0))
|
||||
.col(ColumnDef::new(JournalElements::ZIndex).integer().not_null().default(0))
|
||||
.col(ColumnDef::new(JournalElements::Content).json_binary().null())
|
||||
.col(
|
||||
ColumnDef::new(JournalElements::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(JournalElements::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(JournalElements::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalElements::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(JournalElements::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(JournalElements::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 日记 + 层级索引(加载日记时按 z_index 排序)
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_journal_elements_journal_zindex")
|
||||
.table(JournalElements::Table)
|
||||
.col(JournalElements::JournalId)
|
||||
.col(JournalElements::ZIndex)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_journal_elements_journal")
|
||||
.from(JournalElements::Table, JournalElements::JournalId)
|
||||
.to(super::m20260531_000170_create_journal_entries::JournalEntries::Table, super::m20260531_000170_create_journal_entries::JournalEntries::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(JournalElements::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
pub enum JournalElements {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
JournalId,
|
||||
ElementType,
|
||||
PositionX,
|
||||
PositionY,
|
||||
Width,
|
||||
Height,
|
||||
Rotation,
|
||||
ZIndex,
|
||||
Content,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// 手写笔画表 — 独立表,大字段延迟加载
|
||||
|
||||
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(HandwritingStrokes::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(HandwritingStrokes::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(HandwritingStrokes::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::ElementId).uuid().not_null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::Points).json_binary().not_null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::Pressures).json_binary().null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::Timestamps).json_binary().null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::Color).string().not_null().default("#2D2420"))
|
||||
.col(ColumnDef::new(HandwritingStrokes::StrokeWidth).double().not_null().default(3.0))
|
||||
.col(ColumnDef::new(HandwritingStrokes::BrushType).string().not_null().default("pen"))
|
||||
.col(
|
||||
ColumnDef::new(HandwritingStrokes::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(HandwritingStrokes::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(HandwritingStrokes::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(HandwritingStrokes::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 元素索引(加载元素笔画时使用)
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_handwriting_strokes_element")
|
||||
.table(HandwritingStrokes::Table)
|
||||
.col(HandwritingStrokes::ElementId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_handwriting_strokes_element")
|
||||
.from(HandwritingStrokes::Table, HandwritingStrokes::ElementId)
|
||||
.to(super::m20260531_000171_create_journal_elements::JournalElements::Table, super::m20260531_000171_create_journal_elements::JournalElements::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(HandwritingStrokes::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum HandwritingStrokes {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
ElementId,
|
||||
Points,
|
||||
Pressures,
|
||||
Timestamps,
|
||||
Color,
|
||||
StrokeWidth,
|
||||
BrushType,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// 班级表
|
||||
|
||||
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(SchoolClasses::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(SchoolClasses::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(SchoolClasses::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(SchoolClasses::Name).string().not_null())
|
||||
.col(ColumnDef::new(SchoolClasses::SchoolName).string().null())
|
||||
.col(ColumnDef::new(SchoolClasses::TeacherId).uuid().not_null())
|
||||
.col(ColumnDef::new(SchoolClasses::ClassCode).string().not_null())
|
||||
.col(ColumnDef::new(SchoolClasses::MemberCount).integer().not_null().default(0))
|
||||
.col(ColumnDef::new(SchoolClasses::IsActive).boolean().not_null().default(true))
|
||||
.col(ColumnDef::new(SchoolClasses::ExpiresAt).timestamp_with_time_zone().null())
|
||||
.col(
|
||||
ColumnDef::new(SchoolClasses::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(SchoolClasses::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(SchoolClasses::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(SchoolClasses::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(SchoolClasses::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(SchoolClasses::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 班级码唯一索引(软删除安全)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"CREATE UNIQUE INDEX idx_school_classes_code ON school_classes (class_code) WHERE deleted_at IS NULL".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// 租户 + 老师索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_school_classes_tenant_teacher")
|
||||
.table(SchoolClasses::Table)
|
||||
.col(SchoolClasses::TenantId)
|
||||
.col(SchoolClasses::TeacherId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(SchoolClasses::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
pub enum SchoolClasses {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
Name,
|
||||
SchoolName,
|
||||
TeacherId,
|
||||
ClassCode,
|
||||
MemberCount,
|
||||
IsActive,
|
||||
ExpiresAt,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// 班级成员表 — 复合主键
|
||||
|
||||
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(ClassMembers::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(ClassMembers::ClassId).uuid().not_null())
|
||||
.col(ColumnDef::new(ClassMembers::UserId).uuid().not_null())
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(ClassMembers::ClassId)
|
||||
.col(ClassMembers::UserId),
|
||||
)
|
||||
.col(ColumnDef::new(ClassMembers::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(ClassMembers::Role).string().not_null().default("student"))
|
||||
.col(ColumnDef::new(ClassMembers::Nickname).string().null())
|
||||
.col(
|
||||
ColumnDef::new(ClassMembers::JoinedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ClassMembers::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ClassMembers::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(ClassMembers::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(ClassMembers::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(ClassMembers::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(ClassMembers::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 班级成员索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_class_members_class")
|
||||
.table(ClassMembers::Table)
|
||||
.col(ClassMembers::ClassId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_class_members_class")
|
||||
.from(ClassMembers::Table, ClassMembers::ClassId)
|
||||
.to(super::m20260531_000173_create_school_classes::SchoolClasses::Table, super::m20260531_000173_create_school_classes::SchoolClasses::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(ClassMembers::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum ClassMembers {
|
||||
Table,
|
||||
ClassId,
|
||||
UserId,
|
||||
TenantId,
|
||||
Role,
|
||||
Nickname,
|
||||
JoinedAt,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// 主题布置表
|
||||
|
||||
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(TopicAssignments::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(TopicAssignments::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(TopicAssignments::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(TopicAssignments::ClassId).uuid().not_null())
|
||||
.col(ColumnDef::new(TopicAssignments::TeacherId).uuid().not_null())
|
||||
.col(ColumnDef::new(TopicAssignments::Title).string().not_null())
|
||||
.col(ColumnDef::new(TopicAssignments::Description).text().null())
|
||||
.col(ColumnDef::new(TopicAssignments::DueDate).date().null())
|
||||
.col(ColumnDef::new(TopicAssignments::IsActive).boolean().not_null().default(true))
|
||||
.col(
|
||||
ColumnDef::new(TopicAssignments::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(TopicAssignments::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(TopicAssignments::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(TopicAssignments::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(TopicAssignments::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(TopicAssignments::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 班级 + 激活状态索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_topic_assignments_class_active")
|
||||
.table(TopicAssignments::Table)
|
||||
.col(TopicAssignments::ClassId)
|
||||
.col(TopicAssignments::IsActive)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_topic_assignments_class")
|
||||
.from(TopicAssignments::Table, TopicAssignments::ClassId)
|
||||
.to(super::m20260531_000173_create_school_classes::SchoolClasses::Table, super::m20260531_000173_create_school_classes::SchoolClasses::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(TopicAssignments::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum TopicAssignments {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
ClassId,
|
||||
TeacherId,
|
||||
Title,
|
||||
Description,
|
||||
DueDate,
|
||||
IsActive,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
// 老师点评表
|
||||
|
||||
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(Comments::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(Comments::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(Comments::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(Comments::JournalId).uuid().not_null())
|
||||
.col(ColumnDef::new(Comments::AuthorId).uuid().not_null())
|
||||
.col(ColumnDef::new(Comments::Content).text().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Comments::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Comments::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(Comments::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Comments::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Comments::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(Comments::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 日记索引(加载日记的所有评语)
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_comments_journal")
|
||||
.table(Comments::Table)
|
||||
.col(Comments::JournalId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_comments_journal")
|
||||
.from(Comments::Table, Comments::JournalId)
|
||||
.to(super::m20260531_000170_create_journal_entries::JournalEntries::Table, super::m20260531_000170_create_journal_entries::JournalEntries::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Comments::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Comments {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
JournalId,
|
||||
AuthorId,
|
||||
Content,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// 贴纸包 + 贴纸表
|
||||
|
||||
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(StickerPacks::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(StickerPacks::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(StickerPacks::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(StickerPacks::Name).string().not_null())
|
||||
.col(ColumnDef::new(StickerPacks::Description).text().null())
|
||||
.col(ColumnDef::new(StickerPacks::ThumbnailUrl).string().null())
|
||||
.col(ColumnDef::new(StickerPacks::IsFree).boolean().not_null().default(true))
|
||||
.col(ColumnDef::new(StickerPacks::Price).integer().not_null().default(0))
|
||||
.col(ColumnDef::new(StickerPacks::Category).string().null())
|
||||
.col(
|
||||
ColumnDef::new(StickerPacks::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(StickerPacks::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(StickerPacks::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(StickerPacks::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(StickerPacks::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(StickerPacks::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 贴纸表
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Stickers::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(Stickers::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(Stickers::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(Stickers::PackId).uuid().not_null())
|
||||
.col(ColumnDef::new(Stickers::Name).string().not_null())
|
||||
.col(ColumnDef::new(Stickers::ImageUrl).string().not_null())
|
||||
.col(ColumnDef::new(Stickers::Category).string().null())
|
||||
.col(ColumnDef::new(Stickers::Tags).json_binary().null())
|
||||
.col(
|
||||
ColumnDef::new(Stickers::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Stickers::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(Stickers::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Stickers::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Stickers::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(Stickers::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 贴纸包索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_stickers_pack")
|
||||
.table(Stickers::Table)
|
||||
.col(Stickers::PackId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_stickers_pack")
|
||||
.from(Stickers::Table, Stickers::PackId)
|
||||
.to(StickerPacks::Table, StickerPacks::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager.drop_table(Table::drop().table(Stickers::Table).to_owned()).await?;
|
||||
manager.drop_table(Table::drop().table(StickerPacks::Table).to_owned()).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum StickerPacks {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
Name,
|
||||
Description,
|
||||
ThumbnailUrl,
|
||||
IsFree,
|
||||
Price,
|
||||
Category,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Stickers {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
PackId,
|
||||
Name,
|
||||
ImageUrl,
|
||||
Category,
|
||||
Tags,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// 模板表
|
||||
|
||||
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(Templates::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(Templates::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(Templates::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(Templates::Name).string().not_null())
|
||||
.col(ColumnDef::new(Templates::ThumbnailUrl).string().null())
|
||||
.col(ColumnDef::new(Templates::LayoutData).json_binary().null())
|
||||
.col(ColumnDef::new(Templates::Category).string().null())
|
||||
.col(ColumnDef::new(Templates::IsOfficial).boolean().not_null().default(false))
|
||||
.col(
|
||||
ColumnDef::new(Templates::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Templates::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(Templates::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Templates::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Templates::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(Templates::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 分类索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_templates_category")
|
||||
.table(Templates::Table)
|
||||
.col(Templates::Category)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Templates::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Templates {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
Name,
|
||||
ThumbnailUrl,
|
||||
LayoutData,
|
||||
Category,
|
||||
IsOfficial,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// 成就定义 + 用户成就表
|
||||
|
||||
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(Achievements::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(Achievements::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(Achievements::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(Achievements::Code).string().not_null())
|
||||
.col(ColumnDef::new(Achievements::Name).string().not_null())
|
||||
.col(ColumnDef::new(Achievements::Description).text().null())
|
||||
.col(ColumnDef::new(Achievements::Icon).string().null())
|
||||
.col(ColumnDef::new(Achievements::Category).string().not_null().default("writing"))
|
||||
.col(ColumnDef::new(Achievements::Condition).json_binary().null())
|
||||
.col(ColumnDef::new(Achievements::SortOrder).integer().not_null().default(0))
|
||||
.col(
|
||||
ColumnDef::new(Achievements::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Achievements::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(Achievements::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Achievements::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(Achievements::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(Achievements::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 成就编码唯一索引(软删除安全)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"CREATE UNIQUE INDEX idx_achievements_tenant_code ON achievements (tenant_id, code) WHERE deleted_at IS NULL".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// 用户成就表(复合主键)
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(UserAchievements::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(UserAchievements::UserId).uuid().not_null())
|
||||
.col(ColumnDef::new(UserAchievements::AchievementId).uuid().not_null())
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(UserAchievements::UserId)
|
||||
.col(UserAchievements::AchievementId),
|
||||
)
|
||||
.col(ColumnDef::new(UserAchievements::TenantId).uuid().not_null())
|
||||
.col(
|
||||
ColumnDef::new(UserAchievements::UnlockedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(UserAchievements::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(UserAchievements::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(UserAchievements::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(UserAchievements::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(UserAchievements::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(UserAchievements::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 用户成就索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_user_achievements_user")
|
||||
.table(UserAchievements::Table)
|
||||
.col(UserAchievements::UserId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 外键约束
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_user_achievements_achievement")
|
||||
.from(UserAchievements::Table, UserAchievements::AchievementId)
|
||||
.to(Achievements::Table, Achievements::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager.drop_table(Table::drop().table(UserAchievements::Table).to_owned()).await?;
|
||||
manager.drop_table(Table::drop().table(Achievements::Table).to_owned()).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Achievements {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
Code,
|
||||
Name,
|
||||
Description,
|
||||
Icon,
|
||||
Category,
|
||||
Condition,
|
||||
SortOrder,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum UserAchievements {
|
||||
Table,
|
||||
UserId,
|
||||
AchievementId,
|
||||
TenantId,
|
||||
UnlockedAt,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// 家长-孩子绑定表
|
||||
|
||||
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(ParentChildBindings::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(ParentChildBindings::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(ParentChildBindings::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(ParentChildBindings::ParentId).uuid().not_null())
|
||||
.col(ColumnDef::new(ParentChildBindings::ChildId).uuid().not_null())
|
||||
.col(ColumnDef::new(ParentChildBindings::VerificationMethod).string().not_null().default("manual"))
|
||||
.col(ColumnDef::new(ParentChildBindings::VerifiedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(ParentChildBindings::Status).string().not_null().default("pending"))
|
||||
.col(
|
||||
ColumnDef::new(ParentChildBindings::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ParentChildBindings::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(ParentChildBindings::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(ParentChildBindings::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(ParentChildBindings::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(ParentChildBindings::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 家长索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_parent_child_parent")
|
||||
.table(ParentChildBindings::Table)
|
||||
.col(ParentChildBindings::ParentId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 孩子索引
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_parent_child_child")
|
||||
.table(ParentChildBindings::Table)
|
||||
.col(ParentChildBindings::ChildId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 家长-孩子唯一约束(软删除安全)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"CREATE UNIQUE INDEX idx_parent_child_unique ON parent_child_bindings (parent_id, child_id) WHERE deleted_at IS NULL".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(ParentChildBindings::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum ParentChildBindings {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
ParentId,
|
||||
ChildId,
|
||||
VerificationMethod,
|
||||
VerifiedAt,
|
||||
Status,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// 老师档案表
|
||||
|
||||
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(TeacherProfiles::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(TeacherProfiles::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(TeacherProfiles::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(TeacherProfiles::UserId).uuid().not_null())
|
||||
.col(ColumnDef::new(TeacherProfiles::SchoolName).string().null())
|
||||
.col(ColumnDef::new(TeacherProfiles::Subjects).json_binary().null())
|
||||
.col(ColumnDef::new(TeacherProfiles::Bio).text().null())
|
||||
.col(
|
||||
ColumnDef::new(TeacherProfiles::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(TeacherProfiles::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(TeacherProfiles::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(TeacherProfiles::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(TeacherProfiles::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(TeacherProfiles::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 用户唯一索引(软删除安全,一个用户一个老师档案)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"CREATE UNIQUE INDEX idx_teacher_profiles_user ON teacher_profiles (user_id) WHERE deleted_at IS NULL".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(TeacherProfiles::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum TeacherProfiles {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
UserId,
|
||||
SchoolName,
|
||||
Subjects,
|
||||
Bio,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// 用户设置表
|
||||
|
||||
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(UserSettings::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(UserSettings::Id).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(UserSettings::TenantId).uuid().not_null())
|
||||
.col(ColumnDef::new(UserSettings::UserId).uuid().not_null())
|
||||
.col(ColumnDef::new(UserSettings::Settings).json_binary().not_null())
|
||||
.col(
|
||||
ColumnDef::new(UserSettings::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(UserSettings::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(ColumnDef::new(UserSettings::CreatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(UserSettings::UpdatedBy).uuid().not_null())
|
||||
.col(ColumnDef::new(UserSettings::DeletedAt).timestamp_with_time_zone().null())
|
||||
.col(ColumnDef::new(UserSettings::Version).integer().not_null().default(1))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// 用户唯一索引(软删除安全,一个用户一条设置)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"CREATE UNIQUE INDEX idx_user_settings_user ON user_settings (user_id) WHERE deleted_at IS NULL".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(UserSettings::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum UserSettings {
|
||||
Table,
|
||||
Id,
|
||||
TenantId,
|
||||
UserId,
|
||||
Settings,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CreatedBy,
|
||||
UpdatedBy,
|
||||
DeletedAt,
|
||||
Version,
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// 暖记补充索引 + RLS 策略
|
||||
|
||||
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> {
|
||||
// 为暖记所有表启用 RLS(行级安全)
|
||||
let diary_tables = [
|
||||
"journal_entries",
|
||||
"journal_elements",
|
||||
"handwriting_strokes",
|
||||
"school_classes",
|
||||
"class_members",
|
||||
"topic_assignments",
|
||||
"comments",
|
||||
"sticker_packs",
|
||||
"stickers",
|
||||
"templates",
|
||||
"achievements",
|
||||
"user_achievements",
|
||||
"parent_child_bindings",
|
||||
"teacher_profiles",
|
||||
"user_settings",
|
||||
];
|
||||
|
||||
for table in &diary_tables {
|
||||
// 启用 RLS
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!("ALTER TABLE {table} ENABLE ROW LEVEL SECURITY"),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// 创建 RLS 策略:tenant_id 隔离
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!(
|
||||
"CREATE POLICY tenant_isolation ON {table} USING (tenant_id = current_setting('app.current_tenant')::uuid)"
|
||||
),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// 允许超级用户绕过 RLS(迁移和管理用)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!("ALTER TABLE {table} FORCE ROW LEVEL SECURITY"),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
}
|
||||
|
||||
// 日记全文搜索索引(标题 + 标签)
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"CREATE INDEX idx_journal_entries_title_trgm ON journal_entries USING gin (title gin_trgm_ops)".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// 心情索引(统计查询)
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_journal_entries_mood")
|
||||
.table(JournalEntries::Table)
|
||||
.col(JournalEntries::Mood)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
// 移除全文搜索索引
|
||||
manager.get_connection().execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
"DROP INDEX IF EXISTS idx_journal_entries_title_trgm".to_string(),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// RLS 策略会在表删除时自动移除
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
use super::m20260531_000170_create_journal_entries::JournalEntries;
|
||||
@@ -0,0 +1,99 @@
|
||||
// 暖记种子数据 — 默认成就 + 基础贴纸包 + 暖记权限
|
||||
|
||||
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> {
|
||||
let conn = manager.get_connection();
|
||||
|
||||
// 默认租户和系统用户 UUID(内嵌 SQL,避免类型转换问题)
|
||||
let tid = "'00000000-0000-0000-0000-000000000000'::uuid";
|
||||
|
||||
// 插入默认成就
|
||||
let achievements = [
|
||||
("first_diary", "初出茅庐", "写下第一篇日记", "📝", "writing", r#"{"type":"diary_count","threshold":1}"#, 10),
|
||||
("diary_10", "小有成就", "累计写 10 篇日记", "✏️", "writing", r#"{"type":"diary_count","threshold":10}"#, 20),
|
||||
("diary_50", "笔下生花", "累计写 50 篇日记", "🌸", "writing", r#"{"type":"diary_count","threshold":50}"#, 30),
|
||||
("diary_100", "日记达人", "累计写 100 篇日记", "🏆", "writing", r#"{"type":"diary_count","threshold":100}"#, 40),
|
||||
("streak_3", "三日不间断", "连续 3 天写日记", "🔥", "writing", r#"{"type":"streak","threshold":3}"#, 50),
|
||||
("streak_7", "一周坚持", "连续 7 天写日记", "⭐", "writing", r#"{"type":"streak","threshold":7}"#, 60),
|
||||
("streak_30", "月度冠军", "连续 30 天写日记", "👑", "writing", r#"{"type":"streak","threshold":30}"#, 70),
|
||||
("first_share", "分享快乐", "第一次分享日记到班级", "💝", "social", r#"{"type":"share_count","threshold":1}"#, 80),
|
||||
("comment_received", "获得鼓励", "第一次收到老师点评", "💌", "social", r#"{"type":"comment_received","threshold":1}"#, 90),
|
||||
("all_moods", "情绪彩虹", "使用过所有 5 种心情", "🌈", "collection", r#"{"type":"mood_variety","threshold":5}"#, 100),
|
||||
];
|
||||
|
||||
for (code, name, desc, icon, category, condition, sort) in &achievements {
|
||||
let sql = format!(
|
||||
r#"INSERT INTO achievements (id, tenant_id, code, name, description, icon, category, condition, sort_order, created_at, updated_at, created_by, updated_by, version)
|
||||
VALUES (gen_random_uuid(), {tid}, '{code}', '{name}', '{desc}', '{icon}', '{category}', '{condition}'::jsonb, {sort}, now(), now(), {tid}, {tid}, 1)"#,
|
||||
);
|
||||
conn.execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
sql,
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
}
|
||||
|
||||
// 插入基础贴纸包
|
||||
conn.execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!(
|
||||
r#"INSERT INTO sticker_packs (id, tenant_id, name, description, is_free, price, category, created_at, updated_at, created_by, updated_by, version)
|
||||
VALUES (gen_random_uuid(), {tid}, '基础贴纸', '暖记默认贴纸包,包含常用表情和装饰', true, 0, 'basic', now(), now(), {tid}, {tid}, 1)"#,
|
||||
),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
// 插入暖记权限到 permissions 表 (resource + action 模式)
|
||||
let diary_permissions = [
|
||||
("diary.journal.create", "创建日记", "journal", "create", "允许创建日记条目"),
|
||||
("diary.journal.read", "查看日记", "journal", "read", "允许查看日记条目"),
|
||||
("diary.journal.update", "编辑日记", "journal", "update", "允许编辑日记条目"),
|
||||
("diary.journal.delete", "删除日记", "journal", "delete", "允许删除日记条目"),
|
||||
("diary.class.manage", "管理班级", "class", "manage", "允许创建和管理班级"),
|
||||
("diary.topic.assign", "布置主题", "topic", "assign", "允许老师布置日记主题"),
|
||||
("diary.comment.write", "写评语", "comment", "write", "允许老师点评日记"),
|
||||
("diary.parent.bind", "家长绑定", "parent", "bind", "允许家长绑定孩子账号"),
|
||||
];
|
||||
|
||||
for (code, name, resource, action, desc) in &diary_permissions {
|
||||
let sql = format!(
|
||||
r#"INSERT INTO permissions (id, tenant_id, code, name, resource, action, description, created_at, updated_at, created_by, updated_by, version)
|
||||
VALUES (gen_random_uuid(), {tid}, '{code}', '{name}', '{resource}', '{action}', '{desc}', now(), now(), {tid}, {tid}, 1)
|
||||
ON CONFLICT (tenant_id, code) WHERE deleted_at IS NULL DO NOTHING"#,
|
||||
);
|
||||
conn.execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
sql,
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
let conn = manager.get_connection();
|
||||
|
||||
let tid_str = "WHERE tenant_id = '00000000-0000-0000-0000-000000000000'";
|
||||
|
||||
conn.execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!("DELETE FROM achievements {tid_str}"),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
conn.execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!("DELETE FROM sticker_packs {tid_str}"),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
conn.execute(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!("DELETE FROM permissions WHERE code LIKE 'diary.%' AND tenant_id = '00000000-0000-0000-0000-000000000000'"),
|
||||
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user