From 0a05b9c586569cba0a86591cf815930adcdfd7a1 Mon Sep 17 00:00:00 2001 From: InfiniteStash <117855276+InfiniteStash@users.noreply.github.com> Date: Wed, 28 Feb 2024 23:01:16 +0100 Subject: [PATCH] Drop unique index on pending invites and fix invite key list (#765) --- pkg/database/database.go | 2 +- .../migrations/postgres/36_drop_unique_invite.up.sql | 1 + pkg/sqlx/querybuilder_invite_key.go | 9 +++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 pkg/database/migrations/postgres/36_drop_unique_invite.up.sql diff --git a/pkg/database/database.go b/pkg/database/database.go index 1b2fe7a97..b8144804d 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -4,7 +4,7 @@ import ( "github.com/jmoiron/sqlx" ) -var appSchemaVersion uint = 35 +var appSchemaVersion uint = 36 var databaseProviders map[string]databaseProvider diff --git a/pkg/database/migrations/postgres/36_drop_unique_invite.up.sql b/pkg/database/migrations/postgres/36_drop_unique_invite.up.sql new file mode 100644 index 000000000..8f5fb140a --- /dev/null +++ b/pkg/database/migrations/postgres/36_drop_unique_invite.up.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS "pending_activation_invite_key_idx"; diff --git a/pkg/sqlx/querybuilder_invite_key.go b/pkg/sqlx/querybuilder_invite_key.go index aa19f999d..7e3ef347d 100644 --- a/pkg/sqlx/querybuilder_invite_key.go +++ b/pkg/sqlx/querybuilder_invite_key.go @@ -109,8 +109,13 @@ func (qb *inviteKeyQueryBuilder) Find(id uuid.UUID) (*models.InviteKey, error) { func (qb *inviteKeyQueryBuilder) FindActiveKeysForUser(userID uuid.UUID, expireTime time.Time) (models.InviteKeys, error) { query := `SELECT i.* FROM ` + inviteKeyTable + ` i - LEFT JOIN ` + pendingActivationTable + ` a ON a.invite_key = i.id AND a.time > ? - WHERE i.generated_by = ? AND a.id IS NULL` + LEFT JOIN ( + SELECT invite_key, COUNT(*) as count + FROM pending_activations + WHERE time > ? + GROUP BY invite_key + ) a ON a.invite_key = i.id + WHERE i.generated_by = ? AND (a.invite_key IS NULL OR i.uses IS NULL OR a.count < i.uses)` var args []interface{} args = append(args, expireTime) args = append(args, userID)