diff --git a/src/migrations/m20260308_200000_add_indexes.rs b/src/migrations/m20260308_200000_add_indexes.rs new file mode 100644 index 0000000..5938702 --- /dev/null +++ b/src/migrations/m20260308_200000_add_indexes.rs @@ -0,0 +1,70 @@ +use rapina::migration::prelude::*; +use rapina::sea_orm_migration; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Index for PR lookups by repo + number + status (find_queued_pr, find_active_pr) + manager + .create_index( + Index::create() + .name("idx_pr_repo_number_status") + .table(PullRequests::Table) + .col(PullRequests::RepoOwner) + .col(PullRequests::RepoName) + .col(PullRequests::PrNumber) + .col(PullRequests::Status) + .to_owned(), + ) + .await?; + + // Index for queue ordering (get_queue, get_next_queued, dashboard active PRs) + manager + .create_index( + Index::create() + .name("idx_pr_status_priority_queued") + .table(PullRequests::Table) + .col(PullRequests::Status) + .col(PullRequests::Priority) + .col(PullRequests::QueuedAt) + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_index( + Index::drop() + .name("idx_pr_repo_number_status") + .table(PullRequests::Table) + .to_owned(), + ) + .await?; + manager + .drop_index( + Index::drop() + .name("idx_pr_status_priority_queued") + .table(PullRequests::Table) + .to_owned(), + ) + .await?; + Ok(()) + } +} + +#[derive(DeriveIden)] +enum PullRequests { + Table, + RepoOwner, + RepoName, + PrNumber, + Status, + Priority, + QueuedAt, +} diff --git a/src/migrations/mod.rs b/src/migrations/mod.rs index 9d1c997..310be3d 100644 --- a/src/migrations/mod.rs +++ b/src/migrations/mod.rs @@ -8,10 +8,13 @@ mod m20260307_200000_add_installation_id; mod m20260308_120000_add_approved_by; +mod m20260308_200000_add_indexes; + rapina::migrations! { m20260307_033928_create_pull_requests, m20260307_130846_create_batchs, m20260307_131338_create_merge_events, m20260307_200000_add_installation_id, m20260308_120000_add_approved_by, + m20260308_200000_add_indexes, }