🐸 Frogue(Fed)

~ololduck/Frogue

Raw

// SPDX-FileCopyrightText: 2026 Ololduck <ololduck@vit.am> and contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

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
            .get_connection()
            .execute_unprepared(
                "CREATE TABLE IF NOT EXISTS reports (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    uuid TEXT NOT NULL UNIQUE,
                    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
                    reporter_local_user_id INTEGER,
                    reporter_remote_actor_url TEXT,
                    target_local_user_id INTEGER,
                    target_remote_actor_id INTEGER,
                    object_urls TEXT NOT NULL,
                    comment TEXT,
                    state TEXT NOT NULL DEFAULT 'open',
                    forwarded INTEGER NOT NULL DEFAULT 0,
                    inbound INTEGER NOT NULL DEFAULT 0,
                    FOREIGN KEY (reporter_local_user_id) REFERENCES users(id) ON DELETE CASCADE,
                    FOREIGN KEY (target_local_user_id) REFERENCES users(id) ON DELETE CASCADE,
                    FOREIGN KEY (target_remote_actor_id) REFERENCES ap_remote_actors(id) ON DELETE CASCADE,
                    CHECK (
                        (reporter_local_user_id IS NOT NULL AND reporter_remote_actor_url IS NULL) OR
                        (reporter_local_user_id IS NULL AND reporter_remote_actor_url IS NOT NULL)
                    ),
                    CHECK (
                        (target_local_user_id IS NOT NULL AND target_remote_actor_id IS NULL) OR
                        (target_local_user_id IS NULL AND target_remote_actor_id IS NOT NULL)
                    ),
                    CHECK (state IN ('open', 'resolved', 'dismissed'))
                )",
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .get_connection()
            .execute_unprepared("DROP TABLE IF EXISTS reports")
            .await?;

        Ok(())
    }
}