rowt/migration/src/m20230209_074936_create_trip.rs

69 lines
2.2 KiB
Rust
Raw Normal View History

2023-02-09 12:16:04 +01:00
use sea_orm_migration::prelude::*;
2023-02-09 17:08:07 +01:00
use sea_query::Expr;
2023-02-09 12:16:04 +01:00
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()
2023-02-09 17:08:07 +01:00
.default(Expr::current_timestamp()),
2023-02-09 12:16:04 +01:00
)
.primary_key(Index::create().col(Trip::Day).col(Trip::UserId))
.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,
}