138 lines
4.4 KiB
Rust
138 lines
4.4 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(Banner::Table)
|
|
.col(ColumnDef::new(Banner::Id).uuid().not_null().primary_key())
|
|
.col(ColumnDef::new(Banner::TenantId).uuid().not_null())
|
|
.col(ColumnDef::new(Banner::MediaItemId).uuid().not_null())
|
|
.col(ColumnDef::new(Banner::Title).string_len(100).null())
|
|
.col(ColumnDef::new(Banner::Subtitle).string_len(255).null())
|
|
.col(ColumnDef::new(Banner::LinkType).string_len(20).null())
|
|
.col(ColumnDef::new(Banner::LinkTarget).string_len(500).null())
|
|
.col(
|
|
ColumnDef::new(Banner::SortOrder)
|
|
.integer()
|
|
.not_null()
|
|
.default(0),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Banner::Status)
|
|
.string_len(20)
|
|
.not_null()
|
|
.default("active"),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Banner::StartTime)
|
|
.timestamp_with_time_zone()
|
|
.null(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Banner::EndTime)
|
|
.timestamp_with_time_zone()
|
|
.null(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Banner::CreatedAt)
|
|
.timestamp_with_time_zone()
|
|
.not_null()
|
|
.default(Expr::current_timestamp()),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Banner::UpdatedAt)
|
|
.timestamp_with_time_zone()
|
|
.not_null()
|
|
.default(Expr::current_timestamp()),
|
|
)
|
|
.col(ColumnDef::new(Banner::CreatedBy).uuid().null())
|
|
.col(ColumnDef::new(Banner::UpdatedBy).uuid().null())
|
|
.col(
|
|
ColumnDef::new(Banner::DeletedAt)
|
|
.timestamp_with_time_zone()
|
|
.null(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Banner::Version)
|
|
.integer()
|
|
.not_null()
|
|
.default(1),
|
|
)
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.name("fk_banner_media_item")
|
|
.from(Banner::Table, Banner::MediaItemId)
|
|
.to(MediaItemRef::Table, MediaItemRef::Id)
|
|
.on_delete(ForeignKeyAction::Restrict),
|
|
)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_index(
|
|
Index::create()
|
|
.name("idx_banner_tenant_status")
|
|
.table(Banner::Table)
|
|
.col(Banner::TenantId)
|
|
.col(Banner::Status)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_index(
|
|
Index::create()
|
|
.name("idx_banner_sort")
|
|
.table(Banner::Table)
|
|
.col(Banner::SortOrder)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager
|
|
.drop_table(Table::drop().table(Banner::Table).to_owned())
|
|
.await
|
|
}
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Banner {
|
|
Table,
|
|
Id,
|
|
TenantId,
|
|
MediaItemId,
|
|
Title,
|
|
Subtitle,
|
|
LinkType,
|
|
LinkTarget,
|
|
SortOrder,
|
|
Status,
|
|
StartTime,
|
|
EndTime,
|
|
CreatedAt,
|
|
UpdatedAt,
|
|
CreatedBy,
|
|
UpdatedBy,
|
|
DeletedAt,
|
|
Version,
|
|
}
|
|
|
|
/// 外键引用 media_item 表
|
|
#[derive(Iden)]
|
|
enum MediaItemRef {
|
|
#[iden = "media_item"]
|
|
Table,
|
|
Id,
|
|
}
|