🐸 Frogue(Fed)

~ololduck/Frogue

Raw

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(SshKeys::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SshKeys::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SshKeys::UserId).integer().not_null())
                    .col(ColumnDef::new(SshKeys::Name).string_len(255).not_null())
                    .col(ColumnDef::new(SshKeys::Algorithm).string_len(64).not_null())
                    // Indexed and unique: it is what an SSH connection is
                    // looked up by, before anything else is known.
                    .col(
                        ColumnDef::new(SshKeys::Fingerprint)
                            .string_len(128)
                            .not_null()
                            .unique_key(),
                    )
                    .col(ColumnDef::new(SshKeys::PublicKey).string().not_null())
                    .col(
                        ColumnDef::new(SshKeys::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp())
                            .not_null(),
                    )
                    .col(ColumnDef::new(SshKeys::LastUsedAt).timestamp_with_time_zone())
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_ssh_keys_user_id")
                            .from(SshKeys::Table, SshKeys::UserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

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

/// The ssh keys table, as this migration knows it.
#[derive(Iden)]
pub enum SshKeys {
    Table,
    Id,
    UserId,
    Name,
    Algorithm,
    Fingerprint,
    PublicKey,
    CreatedAt,
    LastUsedAt,
}

/// The users table, for the foreign key.
#[derive(Iden)]
pub enum Users {
    Table,
    Id,
}