Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
MSC2918: make access_tokens.used nullable
Browse files Browse the repository at this point in the history
This avoids rewriting the whole table on disk on Postgres < 11

Signed-off-by: Quentin Gliech <quentingliech@gmail.com>
  • Loading branch information
sandhose committed Jun 18, 2021
1 parent 0060bc9 commit 18628fc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 38 deletions.
13 changes: 11 additions & 2 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,15 @@ def _query_for_auth(self, txn, token: str) -> Optional[TokenLookupResult]:

txn.execute(sql, (token,))
rows = self.db_pool.cursor_to_dict(txn)

if rows:
return TokenLookupResult(**rows[0])
row = rows[0]

# This field is nullable, ensure it comes out as a boolean
if row["token_used"] is None:
row["token_used"] = False

return TokenLookupResult(**row)

return None

Expand Down Expand Up @@ -1157,7 +1164,8 @@ def _lookup_refresh_token_txn(txn) -> Optional[RefreshTokenLookupResult]:
device_id=row[2],
next_token_id=row[3],
has_next_refresh_token_been_refreshed=row[4],
has_next_access_token_been_used=row[5],
# This column is nullable, ensure it's a boolean
has_next_access_token_been_used=(row[5] or False),
)

return await self.db_pool.runInteraction(
Expand Down Expand Up @@ -1435,6 +1443,7 @@ async def add_access_token_to_user(
"puppets_user_id": puppets_user_id,
"last_validated": now,
"refresh_token_id": refresh_token_id,
"used": False,
},
desc="add_access_token_to_user",
)
Expand Down
4 changes: 4 additions & 0 deletions synapse/storage/schema/main/delta/59/14refresh_tokens.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ CREATE TABLE refresh_tokens (
-- Add a reference to the refresh token generated alongside each access token
ALTER TABLE "access_tokens"
ADD COLUMN refresh_token_id BIGINT REFERENCES refresh_tokens (id) ON DELETE CASCADE;

-- Add a flag whether the token was already used or not
ALTER TABLE "access_tokens"
ADD COLUMN used BOOLEAN;
18 changes: 0 additions & 18 deletions synapse/storage/schema/main/delta/59/14refresh_tokens.sql.postgres

This file was deleted.

18 changes: 0 additions & 18 deletions synapse/storage/schema/main/delta/59/14refresh_tokens.sql.sqlite

This file was deleted.

0 comments on commit 18628fc

Please sign in to comment.