实体: - 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 个测试全通过
70 lines
2.5 KiB
Rust
70 lines
2.5 KiB
Rust
// 用户设置表
|
|
|
|
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,
|
|
}
|