68 lines
2.2 KiB
Rust
68 lines
2.2 KiB
Rust
|
use sea_orm_migration::prelude::*;
|
||
|
|
||
|
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("CURRENT_TIMESTAMP"),
|
||
|
)
|
||
|
.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,
|
||
|
}
|