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> {
        let db = manager.get_connection();

        db.execute_unprepared(
            r#"
            ALTER TABLE "user_keypair" ADD CONSTRAINT "REL_f4853eb41ab722fe05f81cedeb6" UNIQUE ("userId");
            ALTER TABLE "user_profile" ADD CONSTRAINT "REL_51cb79b5555effaf7d69ba1cff9" UNIQUE ("userId");
            ALTER TABLE "user_publickey" ADD CONSTRAINT "REL_10c146e4b39b443ede016f6736d" UNIQUE ("userId");
            ALTER TABLE "poll" ADD CONSTRAINT "REL_da851e06d0dfe2ef397d8b1bf1b" UNIQUE ("noteId");
            ALTER TABLE "promo_note" ADD CONSTRAINT "REL_e263909ca4fe5d57f8d4230dd5c" UNIQUE ("noteId");
            "#,
        )
        .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let db = manager.get_connection();

        db.execute_unprepared(
            r#"
            ALTER TABLE "user_keypair" DROP CONSTRAINT "REL_f4853eb41ab722fe05f81cedeb6";
            ALTER TABLE "user_profile" DROP CONSTRAINT "REL_51cb79b5555effaf7d69ba1cff9";
            ALTER TABLE "user_publickey" DROP CONSTRAINT "REL_10c146e4b39b443ede016f6736d";
            ALTER TABLE "poll" DROP CONSTRAINT "REL_da851e06d0dfe2ef397d8b1bf1b";
            ALTER TABLE "promo_note" DROP CONSTRAINT "REL_e263909ca4fe5d57f8d4230dd5c";
            "#,
        )
        .await?;

        Ok(())
    }
}
