1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::m20230402_233043_create_table_api_keys::ApiKey;
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(ApiKeyScope::Table)
                    .col(
                        ColumnDef::new(ApiKeyScope::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(ApiKeyScope::Scope).string().not_null())
                    .col(ColumnDef::new(ApiKeyScope::ApiKey).string().not_null())
                    .foreign_key(
                        ForeignKey::create()
                            .from(ApiKeyScope::Table, ApiKeyScope::ApiKey)
                            .to(ApiKey::Table, ApiKey::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

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

/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
pub enum ApiKeyScope {
    Table,
    Id,
    Scope,
    ApiKey,
}