robson-core 0.0.2

Rust async agent orchestrator for automated development workflows
Documentation
use sea_orm_migration::{prelude::*, schema::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // 1. Create gateways table with a unique name constraint
        manager
            .create_table(
                Table::create()
                    .table(Gateways::Table)
                    .if_not_exists()
                    .col(pk_auto(Gateways::Id))
                    .col(string_uniq(Gateways::Name))
                    .to_owned(),
            )
            .await?;

        // 2. Seed the two default gateways
        manager
            .get_connection()
            .execute_unprepared("INSERT INTO gateways (name) VALUES ('slack'), ('webhook')")
            .await?;

        // 3. Rename conversations.channel_id → gateway_channel_id (SQLite ≥ 3.25)
        manager
            .get_connection()
            .execute_unprepared(
                "ALTER TABLE conversations RENAME COLUMN channel_id TO gateway_channel_id",
            )
            .await?;

        // 4. Add gateway_id FK column (nullable; existing rows will be backfilled below)
        manager
            .get_connection()
            .execute_unprepared(
                "ALTER TABLE conversations ADD COLUMN gateway_id INTEGER REFERENCES gateways(id)",
            )
            .await?;

        // 5. Default existing rows to the slack gateway
        manager
            .get_connection()
            .execute_unprepared(
                "UPDATE conversations SET gateway_id = (SELECT id FROM gateways WHERE name = 'slack')",
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .get_connection()
            .execute_unprepared(
                "ALTER TABLE conversations RENAME COLUMN gateway_channel_id TO channel_id",
            )
            .await?;
        manager
            .drop_table(Table::drop().table(Gateways::Table).to_owned())
            .await?;
        Ok(())
    }
}

#[derive(DeriveIden)]
enum Gateways {
    Table,
    Id,
    Name,
}