oswilno/crates/migration/src/m20230809_135630_add_spot.rs

168 lines
4.8 KiB
Rust
Raw Normal View History

2023-08-09 14:56:46 +02:00
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> {
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::Location).string().not_null())
.col(ColumnDef::new(ParkingSpace::AccountId).integer().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_foreign_key(
ForeignKeyCreateStatement::new()
.from_tbl(ParkingSpace::ParkingSpaces)
.from_col(ParkingSpace::AccountId)
.to_tbl(Account::Accounts)
.to_col(Account::Id)
.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::ParkingSpaceId)
.integer()
.not_null(),
)
.col(
ColumnDef::new(ParkingSpaceRent::Available)
.boolean()
.default(true)
.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?;
m.create_foreign_key(
ForeignKeyCreateStatement::new()
.from_tbl(ParkingSpaceRent::ParkingSpaceRents)
.from_col(ParkingSpaceRent::ParkingSpaceId)
.to_tbl(ParkingSpace::ParkingSpaces)
.to_col(ParkingSpace::Id)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.alter_table(
Table::alter_table()
.table(ParkingSpaceRent::ParkingSpaceRents)
.to_owned(),
)
.await?;
Ok(())
}
}
#[derive(Iden)]
pub enum ParkingSpace {
ParkingSpaces,
Id,
State,
Location,
AccountId,
CreatedAt,
UpdatedAt,
}
#[derive(Iden)]
pub enum ParkingSpaceRent {
ParkingSpaceRents,
Id,
Price,
Available,
ParkingSpaceId,
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,
}