use sea_orm::{EnumIter, Iterable}; use sea_orm_migration::prelude::*; 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::(m).await?; create_enum::(m).await?; m.create_table( Table::create() .table(ParkingSpace::ParkingSpaces) .if_not_exists() .col( ColumnDef::new(ParkingSpace::Id) .integer() .not_null() .auto_increment() .primary_key(), ) .col( ColumnDef::new(ParkingSpace::State) .enumeration( ParkingSpaceState::ParkingSpaceState, ParkingSpaceState::iter().skip(1), ) .default(ParkingSpaceState::Pending.to_string()) .not_null(), ) .col( ColumnDef::new(ParkingSpace::CreatedAt) .timestamp() .default(SimpleExpr::Custom("NOW()".to_owned())) .not_null(), ) .col( ColumnDef::new(ParkingSpace::UpdatedAt) .timestamp() .default(SimpleExpr::Custom("NOW()".to_owned())) .not_null(), ) .if_not_exists() .to_owned(), ) .await?; m.create_table( Table::create() .table(ParkingSpaceRent::ParkingSpaceRents) .if_not_exists() .col( ColumnDef::new(ParkingSpaceRent::Id) .integer() .not_null() .auto_increment() .primary_key(), ) .col(ColumnDef::new(ParkingSpaceRent::Price).integer().not_null()) .col( ColumnDef::new(ParkingSpaceRent::CreatedAt) .timestamp() .default(SimpleExpr::Custom("NOW()".to_owned())) .not_null(), ) .col( ColumnDef::new(ParkingSpaceRent::UpdatedAt) .timestamp() .default(SimpleExpr::Custom("NOW()".to_owned())) .not_null(), ) .if_not_exists() .to_owned(), ) .await?; Ok(()) } async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { m.drop_table( Table::drop() .table(ParkingSpaceRent::ParkingSpaceRents) .to_owned(), ) .await?; m.drop_table(Table::drop().table(ParkingSpace::ParkingSpaces).to_owned()) .await?; drop_enum::(m).await?; drop_enum::(m).await?; Ok(()) } } #[derive(Iden)] enum ParkingSpace { ParkingSpaces, Id, State, Location, AccountId, CreatedAt, UpdatedAt, } #[derive(Iden)] enum ParkingSpaceRent { ParkingSpaceRents, Id, Price, CreatedAt, UpdatedAt, } #[derive(Iden, EnumIter)] pub enum ParkingSpaceState { ParkingSpaceState, #[iden(rename = "Pending")] Pending, #[iden(rename = "Verified")] Verified, #[iden(rename = "Banned")] Banned, } #[derive(Iden, EnumIter)] pub enum Side { Side, #[iden(rename = "Left")] Left, #[iden(rename = "Right")] Right, #[iden(rename = "Front")] Front, }