rowt/migration/src/m20230209_074936_create_trip.rs
2023-03-14 14:00:53 +01:00

75 lines
2.4 KiB
Rust

use sea_orm_migration::prelude::*;
use sea_query::Expr;
use crate::m20230208_114547_create_day::Day;
use crate::m20230209_063357_create_user::User;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Trip::Table)
.if_not_exists()
.col(ColumnDef::new(Trip::Day).date().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_day")
.from(Trip::Table, Trip::Day)
.to(Day::Table, Day::Day),
)
.col(ColumnDef::new(Trip::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_userid")
.from(Trip::Table, Trip::UserId)
.to(User::Table, User::Id),
)
.col(ColumnDef::new(Trip::CoxId).integer())
.foreign_key(
ForeignKey::create()
.name("FK_coxid")
.from(Trip::Table, Trip::CoxId)
.to(User::Table, User::Id),
)
.col(ColumnDef::new(Trip::Begin).string())
.col(
ColumnDef::new(Trip::Created)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.primary_key(
Index::create()
.col(Trip::Day)
.col(Trip::UserId)
.col(Trip::CoxId)
.col(Trip::Begin),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Trip::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Trip {
Table,
Day,
UserId,
CoxId,
Begin,
Created,
}