migrator/migrations/
m20250409171833_add_permissions_object_subject_idx.rs

1use sqlx::{PgConnection, Postgres};
2use sqlx_migrator::Migration;
3use sqlx_migrator::Operation;
4use sqlx_migrator::error::Error;
5use sqlx_migrator::vec_box;
6
7pub(crate) struct AddPermissionsObjectSubjectIdx;
8#[async_trait::async_trait]
9impl Migration<Postgres> for AddPermissionsObjectSubjectIdx {
10    fn app(&self) -> &str {
11        "backend"
12    }
13    fn name(&self) -> &str {
14        "20250409171833_add_permissions_object_subject_idx"
15    }
16    fn parents(&self) -> Vec<Box<dyn Migration<Postgres>>> {
17        vec![]
18    }
19    fn operations(&self) -> Vec<Box<dyn Operation<Postgres>>> {
20        vec_box![MigrationOperation]
21    }
22
23    fn is_atomic(&self) -> bool {
24        false
25    }
26}
27
28struct MigrationOperation;
29#[async_trait::async_trait]
30impl Operation<Postgres> for MigrationOperation {
31    async fn up(&self, conn: &mut PgConnection) -> Result<(), Error> {
32        sqlx::query(
33            "
34            CREATE INDEX CONCURRENTLY IF NOT EXISTS permissions_object_subject_idx
35                ON permissions (object, subject);
36            ",
37        )
38        .execute(conn)
39        .await?;
40
41        Ok(())
42    }
43
44    async fn down(&self, conn: &mut PgConnection) -> Result<(), Error> {
45        sqlx::query(
46            "
47            DROP INDEX CONCURRENTLY IF EXISTS permissions_object_subject_idx;
48            ",
49        )
50        .execute(conn)
51        .await?;
52        Ok(())
53    }
54}