Skip to content

Commit 27732d4

Browse files
committed
Change max_age to BIGINT milliseconds
1 parent 4d4441b commit 27732d4

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

mautrix/crypto/store/asyncpg/store.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ async def has_group_session(self, room_id: RoomID, session_id: SessionID) -> boo
289289

290290
async def add_outbound_group_session(self, session: OutboundGroupSession) -> None:
291291
pickle = session.pickle(self.pickle_key)
292-
max_age = session.max_age
293-
if self.db.scheme == Scheme.SQLITE:
294-
max_age = max_age.total_seconds()
292+
max_age = int(session.max_age.total_seconds() * 1000)
295293
q = """
296294
INSERT INTO crypto_megolm_outbound_session (
297295
room_id, session_id, session, shared, max_messages, message_count,
@@ -341,17 +339,14 @@ async def get_outbound_group_session(self, room_id: RoomID) -> OutboundGroupSess
341339
row = await self.db.fetchrow(q, room_id, self.account_id)
342340
if row is None:
343341
return None
344-
max_age = row["max_age"]
345-
if self.db.scheme == Scheme.SQLITE:
346-
max_age = timedelta(seconds=max_age)
347342
return OutboundGroupSession.from_pickle(
348343
row["session"],
349344
passphrase=self.pickle_key,
350345
room_id=row["room_id"],
351346
shared=row["shared"],
352347
max_messages=row["max_messages"],
353348
message_count=row["message_count"],
354-
max_age=max_age,
349+
max_age=timedelta(milliseconds=row["max_age"]),
355350
use_time=row["last_used"],
356351
creation_time=row["created_at"],
357352
)

mautrix/crypto/store/asyncpg/upgrade.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ async def upgrade_blank_to_latest(conn: Connection) -> None:
8080
PRIMARY KEY (account_id, session_id)
8181
)"""
8282
)
83-
# TODO chnge max_age to BIGINT
8483
await conn.execute(
8584
"""CREATE TABLE IF NOT EXISTS crypto_megolm_outbound_session (
8685
account_id TEXT,
@@ -90,7 +89,7 @@ async def upgrade_blank_to_latest(conn: Connection) -> None:
9089
shared BOOLEAN NOT NULL,
9190
max_messages INTEGER NOT NULL,
9291
message_count INTEGER NOT NULL,
93-
max_age INTERVAL NOT NULL,
92+
max_age BIGINT NOT NULL,
9493
created_at timestamp NOT NULL,
9594
last_used timestamp NOT NULL,
9695
PRIMARY KEY (account_id, room_id)
@@ -167,7 +166,7 @@ async def upgrade_v2(conn: Connection, scheme: Scheme) -> None:
167166
shared BOOLEAN NOT NULL,
168167
max_messages INTEGER NOT NULL,
169168
message_count INTEGER NOT NULL,
170-
max_age INTERVAL NOT NULL,
169+
max_age BIGINT NOT NULL,
171170
created_at timestamp NOT NULL,
172171
last_used timestamp NOT NULL,
173172
PRIMARY KEY (account_id, room_id)
@@ -298,6 +297,11 @@ async def upgrade_v8_postgres(conn: Connection) -> None:
298297
"ALTER TABLE crypto_cross_signing_signatures ALTER COLUMN signature SET NOT NULL"
299298
)
300299

300+
await conn.execute(
301+
"ALTER TABLE crypto_megolm_outbound_session ALTER COLUMN max_age TYPE BIGINT "
302+
"USING (EXTRACT(EPOCH from max_age)*1000)::int"
303+
)
304+
301305

302306
async def upgrade_v8_sqlite(conn: Connection) -> None:
303307
await conn.execute("PRAGMA foreign_keys = OFF")
@@ -351,6 +355,8 @@ async def upgrade_v8_sqlite(conn: Connection) -> None:
351355
"ALTER TABLE new_crypto_megolm_inbound_session RENAME TO crypto_megolm_inbound_session"
352356
)
353357

358+
await conn.execute("UPDATE crypto_megolm_outbound_session SET max_age=max_age*1000")
359+
354360
await conn.execute(
355361
"""CREATE TABLE new_crypto_cross_signing_keys (
356362
user_id TEXT,

0 commit comments

Comments
 (0)