Skip to content

Commit e934b22

Browse files
authored
Fix UNIQUE constraint on active personal access tokens per session (#5196)
2 parents 6d2ed5a + a01c0af commit e934b22

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Copyright 2025 Element Creations Ltd.
2+
--
3+
-- SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
4+
-- Please see LICENSE in the repository root for full details.
5+
6+
7+
-- Fix a faulty constraint.
8+
-- The condition was incorrectly specified as `revoked_at IS NOT NULL`
9+
-- when `revoked_at IS NULL` was meant.
10+
11+
DROP INDEX personal_access_tokens_personal_session_id_idx;
12+
13+
-- Ensure we can only have one active personal access token in each family.
14+
CREATE UNIQUE INDEX ON personal_access_tokens (personal_session_id) WHERE revoked_at IS NULL;

crates/storage-pg/src/personal/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ mod tests {
384384
assert!(!token.is_valid(clock.now()));
385385

386386
// Add a second access token, this time without expiration
387+
let _token = repo
388+
.personal_access_token()
389+
.revoke(&clock, token)
390+
.await
391+
.unwrap();
387392
let token = repo
388393
.personal_access_token()
389394
.add(&mut rng, &clock, &session, SECOND_TOKEN, None)

0 commit comments

Comments
 (0)