oswilno/crates/migration/src/m20230726_124452_images.rs

94 lines
2.6 KiB
Rust

use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::*;
use crate::m20220101_000001_create_table::Account;
use crate::{create_enum, drop_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?;
m.create_foreign_key(
ForeignKeyCreateStatement::new()
.from_tbl(Image::Images)
.from_col(Image::AccountId)
.to_tbl(Account::Accounts)
.to_col(Account::Id)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(Image::Images).to_owned())
.await?;
drop_enum::<ImageState>(m).await?;
Ok(())
}
}
#[derive(Iden)]
pub enum Image {
Images,
Id,
LocalPath,
PublicPath,
ImageState,
AccountId,
CreatedAt,
UpdatedAt,
}
#[derive(Iden, EnumIter)]
pub enum ImageState {
ImageState,
Pending,
Approved,
Banned,
}