This commit is contained in:
2023-02-09 12:16:04 +01:00
parent 1934fef0dd
commit 5200150828
16 changed files with 446 additions and 20 deletions

View File

@ -1,12 +1,18 @@
pub use sea_orm_migration::prelude::*;
mod m20230208_114547_create_day;
mod m20230209_063357_create_user;
mod m20230209_074936_create_trip;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20230208_114547_create_day::Migration)]
vec![
Box::new(m20230208_114547_create_day::Migration),
Box::new(m20230209_063357_create_user::Migration),
Box::new(m20230209_074936_create_trip::Migration),
]
}
}

View File

@ -12,7 +12,12 @@ impl MigrationTrait for Migration {
.table(Day::Table)
.if_not_exists()
.col(ColumnDef::new(Day::Day).date().not_null().primary_key())
.col(ColumnDef::new(Day::PlannedAmountCox).integer().default(0))
.col(
ColumnDef::new(Day::PlannedAmountCox)
.not_null()
.integer()
.default(0),
)
.col(
ColumnDef::new(Day::PlannedStartingTime)
.string()
@ -38,7 +43,7 @@ impl MigrationTrait for Migration {
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Day {
pub enum Day {
Table,
Day,
PlannedAmountCox,

View File

@ -0,0 +1,54 @@
use sea_orm_migration::prelude::*;
#[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(User::Table)
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(User::Name).string().not_null().unique_key())
.col(
ColumnDef::new(User::IsCox)
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(User::IsAdmin)
.boolean()
.not_null()
.default(false),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
pub enum User {
Table,
Id,
Name,
IsCox,
IsAdmin,
}

View File

@ -0,0 +1,67 @@
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,
}