From bcbbde8270f82d052adb18fb04572b33ee5b01c7 Mon Sep 17 00:00:00 2001 From: Chief-Rishab Date: Tue, 17 Oct 2023 08:29:30 +0530 Subject: [PATCH] feat(grant): add unique index for active grants --- internal/store/postgres/grant_repository_test.go | 11 +++++++++++ .../000019_create_grant_unique_index.down.sql | 1 + .../000019_create_grant_unique_index.up.sql | 3 +++ 3 files changed, 15 insertions(+) create mode 100644 internal/store/postgres/migrations/000019_create_grant_unique_index.down.sql create mode 100644 internal/store/postgres/migrations/000019_create_grant_unique_index.up.sql diff --git a/internal/store/postgres/grant_repository_test.go b/internal/store/postgres/grant_repository_test.go index 0147c4771..684876d1d 100644 --- a/internal/store/postgres/grant_repository_test.go +++ b/internal/store/postgres/grant_repository_test.go @@ -103,6 +103,17 @@ func (s *GrantRepositoryTestSuite) SetupSuite() { s.Require().NoError(err) } +func (s *GrantRepositoryTestSuite) AfterTest(suiteName, testName string) { + // clean grants table + db, err := s.store.DB().DB() + if err != nil { + s.T().Fatal(err) + } + if _, err := db.Exec("DELETE FROM grants"); err != nil { + s.T().Fatal(err) + } +} + func (s *GrantRepositoryTestSuite) TearDownSuite() { // Clean tests db, err := s.store.DB().DB() diff --git a/internal/store/postgres/migrations/000019_create_grant_unique_index.down.sql b/internal/store/postgres/migrations/000019_create_grant_unique_index.down.sql new file mode 100644 index 000000000..8de222b64 --- /dev/null +++ b/internal/store/postgres/migrations/000019_create_grant_unique_index.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS "unique_active_grants_index"; \ No newline at end of file diff --git a/internal/store/postgres/migrations/000019_create_grant_unique_index.up.sql b/internal/store/postgres/migrations/000019_create_grant_unique_index.up.sql new file mode 100644 index 000000000..398112dbb --- /dev/null +++ b/internal/store/postgres/migrations/000019_create_grant_unique_index.up.sql @@ -0,0 +1,3 @@ +CREATE UNIQUE INDEX IF NOT EXISTS "unique_active_grants_index" ON "grants" ("account_id", "account_type", "permissions") +WHERE + "status" = 'active'; \ No newline at end of file