oswilno/crates/migration/src/m20230726_124452_images.rs

77 lines
2.1 KiB
Rust
Raw Normal View History

2023-07-26 16:30:35 +02:00
use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::*;
use crate::create_enum;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
create_enum::<ImageState>(m).await?;
m.create_table(
Table::create()
.table(Image::Images)
.if_not_exists()
.col(
ColumnDef::new(Image::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Image::LocalPath).string().not_null())
.col(ColumnDef::new(Image::PublicPath).string().not_null())
.col(
ColumnDef::new(Image::ImageState)
.enumeration(ImageState::ImageState, ImageState::iter().skip(1))
.default(ImageState::Pending.to_string())
.not_null(),
)
.col(ColumnDef::new(Image::AccountId).integer().not_null())
.col(
ColumnDef::new(Image::CreatedAt)
.timestamp()
.default(SimpleExpr::Custom("NOW()".to_owned()))
.not_null(),
)
.col(
ColumnDef::new(Image::UpdatedAt)
.timestamp()
.default(SimpleExpr::Custom("NOW()".to_owned()))
.not_null(),
)
.if_not_exists()
.to_owned(),
)
.await
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(Image::Images).to_owned())
.await
}
}
#[derive(Iden)]
enum Image {
Images,
Id,
LocalPath,
PublicPath,
ImageState,
AccountId,
CreatedAt,
UpdatedAt,
}
#[derive(Iden, EnumIter)]
enum ImageState {
ImageState,
Pending,
Approved,
Banned,
}