Files
hms/crates/erp-server/migration/src/m20260510_000134_create_media_folder.rs

100 lines
3.3 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(MediaFolder::Table)
.col(
ColumnDef::new(MediaFolder::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(MediaFolder::TenantId).uuid().not_null())
.col(ColumnDef::new(MediaFolder::Name).string_len(100).not_null())
.col(ColumnDef::new(MediaFolder::ParentId).uuid().null())
.col(
ColumnDef::new(MediaFolder::SortOrder)
.integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(MediaFolder::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(MediaFolder::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(MediaFolder::CreatedBy).uuid().null())
.col(ColumnDef::new(MediaFolder::UpdatedBy).uuid().null())
.col(
ColumnDef::new(MediaFolder::DeletedAt)
.timestamp_with_time_zone()
.null(),
)
.col(
ColumnDef::new(MediaFolder::Version)
.integer()
.not_null()
.default(1),
)
.foreign_key(
ForeignKey::create()
.name("fk_media_folder_parent")
.from(MediaFolder::Table, MediaFolder::ParentId)
.to(MediaFolder::Table, MediaFolder::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_media_folder_tenant_parent")
.table(MediaFolder::Table)
.col(MediaFolder::TenantId)
.col(MediaFolder::ParentId)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(MediaFolder::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum MediaFolder {
Table,
Id,
TenantId,
Name,
ParentId,
SortOrder,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}