use loco_rs::schema::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
        // OAuth2 session store used by loco-oauth2 to correlate the provider's
        // access token with a local user during the callback flow. `user` adds
        // a user_id FK to the users table.
        create_table(
            m,
            "o_auth2_sessions",
            &[
                ("id", ColType::PkAuto),
                ("session_id", ColType::StringUniq),
                ("expires_at", ColType::TimestampWithTimeZone),
            ],
            &[("user", "")],
        )
        .await
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        drop_table(m, "o_auth2_sessions").await
    }
}
