use sea_orm_migration::{prelude::*, schema::*};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.get_connection()
.execute_unprepared("PRAGMA foreign_keys = ON;")
.await?;
manager
.create_table(
Table::create()
.table(LocationRuleSets::Table)
.if_not_exists()
.col(pk_auto(LocationRuleSets::Id))
.col(string_uniq(LocationRuleSets::Pattern))
.col(timestamp(LocationRuleSets::CreatedAt))
.col(timestamp(LocationRuleSets::UpdatedAt))
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(LocationRules::Table)
.if_not_exists()
.col(pk_auto(LocationRules::Id))
.col(integer(LocationRules::LocationId))
.col(integer(LocationRules::Action))
.col(json(LocationRules::Exprs))
.col(timestamp(LocationRules::CreatedAt))
.col(timestamp(LocationRules::UpdatedAt))
.foreign_key(
ForeignKey::create()
.name("fk-location_rule-location_id")
.from(LocationRules::Table, LocationRules::LocationId)
.to(LocationRuleSets::Table, LocationRuleSets::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(LocationRules::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(LocationRuleSets::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum LocationRuleSets {
Table,
Id,
Pattern,
CreatedAt,
UpdatedAt,
}
#[derive(DeriveIden)]
enum LocationRules {
Table,
Id,
LocationId,
Action,
Exprs,
CreatedAt,
UpdatedAt,
}