trifid_api_migration 0.2.0

Database migrations for trifid-api
Documentation
use crate::m20230403_173431_create_table_networks::Network;
use crate::m20230404_133809_create_table_roles::Role;
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(Host::Table)
                    .col(ColumnDef::new(Host::Id).string().not_null().primary_key())
                    .col(ColumnDef::new(Host::Name).string().not_null())
                    .col(ColumnDef::new(Host::Network).string().not_null())
                    .col(ColumnDef::new(Host::Role).string().not_null())
                    .col(ColumnDef::new(Host::IP).string().not_null())
                    .col(ColumnDef::new(Host::ListenPort).unsigned().not_null())
                    .col(ColumnDef::new(Host::IsLighthouse).boolean().not_null())
                    .col(ColumnDef::new(Host::IsRelay).boolean().not_null())
                    .col(ColumnDef::new(Host::Counter).unsigned().not_null())
                    .col(ColumnDef::new(Host::CreatedAt).big_integer().not_null())
                    .col(ColumnDef::new(Host::IsBlocked).boolean().not_null())
                    .col(ColumnDef::new(Host::LastSeenAt).big_integer().not_null())
                    .col(ColumnDef::new(Host::LastVersion).integer().not_null())
                    .col(ColumnDef::new(Host::LastPlatform).string().not_null())
                    .col(ColumnDef::new(Host::LastOutOfDate).boolean().not_null())
                    .foreign_key(
                        ForeignKey::create()
                            .from(Host::Table, Host::Network)
                            .to(Network::Table, Network::Id)
                            .on_update(ForeignKeyAction::Cascade)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .from(Host::Table, Host::Role)
                            .to(Role::Table, Role::Id)
                            .on_update(ForeignKeyAction::Cascade)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .index(
                        Index::create()
                            .name("idx-hosts-net-name-unique")
                            .table(Host::Table)
                            .col(Host::Network)
                            .col(Host::Name)
                            .unique(),
                    )
                    .index(
                        Index::create()
                            .name("idx-hosts-net-ip-unique")
                            .table(Host::Table)
                            .col(Host::Network)
                            .col(Host::IP)
                            .unique(),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Host::Table).to_owned())
            .await
    }
}

/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
pub enum Host {
    Table,
    Id,
    Name,
    Network,
    Role,
    IP,
    ListenPort,
    IsLighthouse,
    IsRelay,
    Counter,
    CreatedAt,
    IsBlocked,
    LastSeenAt,
    LastVersion,
    LastPlatform,
    LastOutOfDate,
}