Files
nj/crates/erp-server/migration/src/m20260531_000177_create_sticker_packs.rs
iven 3d9896a676 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 个测试全通过
2026-05-31 22:29:56 +08:00

146 lines
5.1 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(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,
}