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> {
// Add the `change_id` column to `patch_proposals`: a jj change-id that
// survives rewrites, so pushes to `refs/patches` for the same logical
// change update the existing patch proposal rather than creating a new
// one.
manager
.get_connection()
.execute_unprepared("ALTER TABLE patch_proposals ADD COLUMN change_id varchar(32)")
.await?;
// Unique per (repository_id, change_id) — NULLs never collide in
// SQLite's partial unique index (only non-NULL rows
// participate), so plain-git patch proposals with no change-id
// remain unaffected.
manager
.get_connection()
.execute_unprepared(
"CREATE UNIQUE INDEX IF NOT EXISTS uq_patch_proposals_repository_change_id \
ON patch_proposals (repository_id, change_id) \
WHERE change_id IS NOT NULL",
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.get_connection()
.execute_unprepared("DROP INDEX IF EXISTS uq_patch_proposals_repository_change_id")
.await?;
manager
.get_connection()
.execute_unprepared("ALTER TABLE patch_proposals DROP COLUMN IF EXISTS change_id")
.await?;
Ok(())
}
}