oswilno/crates/migration/src/m20230726_135630_parking_spaces.rs

177 lines
5.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::*;
2023-07-26 21:49:34 +02:00
use crate::m20220101_000001_create_table::Account;
2023-07-26 16:30:35 +02:00
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::<ParkingSpaceState>(m).await?;
create_enum::<Side>(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(),
)
2023-07-26 21:49:34 +02:00
.col(ColumnDef::new(ParkingSpace::Location).string().not_null())
.col(ColumnDef::new(ParkingSpace::AccountId).integer().not_null())
2023-07-26 16:30:35 +02:00
.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?;
2023-07-26 21:49:34 +02:00
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?;
2023-07-26 16:30:35 +02:00
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())
2023-07-26 21:49:34 +02:00
.col(
ColumnDef::new(ParkingSpaceRent::ParkingSpaceId)
.integer()
.not_null(),
)
.col(
ColumnDef::new(ParkingSpaceRent::Available)
.boolean()
.default(true)
.not_null(),
)
2023-07-26 16:30:35 +02:00
.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?;
2023-07-26 21:49:34 +02:00
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?;
2023-07-26 16:30:35 +02:00
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::<Side>(m).await?;
drop_enum::<ParkingSpaceState>(m).await?;
Ok(())
}
}
#[derive(Iden)]
2023-07-26 21:49:34 +02:00
pub enum ParkingSpace {
2023-07-26 16:30:35 +02:00
ParkingSpaces,
Id,
State,
Location,
AccountId,
CreatedAt,
UpdatedAt,
}
#[derive(Iden)]
2023-07-26 21:49:34 +02:00
pub enum ParkingSpaceRent {
2023-07-26 16:30:35 +02:00
ParkingSpaceRents,
Id,
Price,
2023-07-26 21:49:34 +02:00
Available,
ParkingSpaceId,
2023-07-26 16:30:35 +02:00
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,
}