25
25
from synapse .storage ._base import SQLBaseStore , db_to_json
26
26
from synapse .storage .database import DatabasePool , LoggingTransaction
27
27
from synapse .storage .databases .main .search import SearchStore
28
+ from synapse .storage .types import Cursor
28
29
from synapse .types import JsonDict , ThirdPartyInstanceID
29
30
from synapse .util import json_encoder
30
31
from synapse .util .caches .descriptors import cached
@@ -1022,10 +1023,22 @@ def get_rooms_for_retention_period_in_range_txn(txn):
1022
1023
)
1023
1024
1024
1025
1025
- class RoomBackgroundUpdateStore ( SQLBaseStore ) :
1026
+ class _BackgroundUpdates :
1026
1027
REMOVE_TOMESTONED_ROOMS_BG_UPDATE = "remove_tombstoned_rooms_from_directory"
1027
1028
ADD_ROOMS_ROOM_VERSION_COLUMN = "add_rooms_room_version_column"
1029
+ POPULATE_ROOM_DEPTH_MIN_DEPTH2 = "populate_room_depth_min_depth2"
1030
+ REPLACE_ROOM_DEPTH_MIN_DEPTH = "replace_room_depth_min_depth"
1031
+
1032
+
1033
+ _REPLACE_ROOM_DEPTH_SQL_COMMANDS = (
1034
+ "DROP TRIGGER populate_min_depth2_trigger ON room_depth" ,
1035
+ "DROP FUNCTION populate_min_depth2()" ,
1036
+ "ALTER TABLE room_depth DROP COLUMN min_depth" ,
1037
+ "ALTER TABLE room_depth RENAME COLUMN min_depth2 TO min_depth" ,
1038
+ )
1039
+
1028
1040
1041
+ class RoomBackgroundUpdateStore (SQLBaseStore ):
1029
1042
def __init__ (self , database : DatabasePool , db_conn , hs ):
1030
1043
super ().__init__ (database , db_conn , hs )
1031
1044
@@ -1037,15 +1050,25 @@ def __init__(self, database: DatabasePool, db_conn, hs):
1037
1050
)
1038
1051
1039
1052
self .db_pool .updates .register_background_update_handler (
1040
- self .REMOVE_TOMESTONED_ROOMS_BG_UPDATE ,
1053
+ _BackgroundUpdates .REMOVE_TOMESTONED_ROOMS_BG_UPDATE ,
1041
1054
self ._remove_tombstoned_rooms_from_directory ,
1042
1055
)
1043
1056
1044
1057
self .db_pool .updates .register_background_update_handler (
1045
- self .ADD_ROOMS_ROOM_VERSION_COLUMN ,
1058
+ _BackgroundUpdates .ADD_ROOMS_ROOM_VERSION_COLUMN ,
1046
1059
self ._background_add_rooms_room_version_column ,
1047
1060
)
1048
1061
1062
+ # BG updates to change the type of room_depth.min_depth
1063
+ self .db_pool .updates .register_background_update_handler (
1064
+ _BackgroundUpdates .POPULATE_ROOM_DEPTH_MIN_DEPTH2 ,
1065
+ self ._background_populate_room_depth_min_depth2 ,
1066
+ )
1067
+ self .db_pool .updates .register_background_update_handler (
1068
+ _BackgroundUpdates .REPLACE_ROOM_DEPTH_MIN_DEPTH ,
1069
+ self ._background_replace_room_depth_min_depth ,
1070
+ )
1071
+
1049
1072
async def _background_insert_retention (self , progress , batch_size ):
1050
1073
"""Retrieves a list of all rooms within a range and inserts an entry for each of
1051
1074
them into the room_retention table.
@@ -1164,7 +1187,9 @@ def _background_add_rooms_room_version_column_txn(txn: LoggingTransaction):
1164
1187
new_last_room_id = room_id
1165
1188
1166
1189
self .db_pool .updates ._background_update_progress_txn (
1167
- txn , self .ADD_ROOMS_ROOM_VERSION_COLUMN , {"room_id" : new_last_room_id }
1190
+ txn ,
1191
+ _BackgroundUpdates .ADD_ROOMS_ROOM_VERSION_COLUMN ,
1192
+ {"room_id" : new_last_room_id },
1168
1193
)
1169
1194
1170
1195
return False
@@ -1176,7 +1201,7 @@ def _background_add_rooms_room_version_column_txn(txn: LoggingTransaction):
1176
1201
1177
1202
if end :
1178
1203
await self .db_pool .updates ._end_background_update (
1179
- self .ADD_ROOMS_ROOM_VERSION_COLUMN
1204
+ _BackgroundUpdates .ADD_ROOMS_ROOM_VERSION_COLUMN
1180
1205
)
1181
1206
1182
1207
return batch_size
@@ -1215,7 +1240,7 @@ def _get_rooms(txn):
1215
1240
1216
1241
if not rooms :
1217
1242
await self .db_pool .updates ._end_background_update (
1218
- self .REMOVE_TOMESTONED_ROOMS_BG_UPDATE
1243
+ _BackgroundUpdates .REMOVE_TOMESTONED_ROOMS_BG_UPDATE
1219
1244
)
1220
1245
return 0
1221
1246
@@ -1224,7 +1249,7 @@ def _get_rooms(txn):
1224
1249
await self .set_room_is_public (room_id , False )
1225
1250
1226
1251
await self .db_pool .updates ._background_update_progress (
1227
- self .REMOVE_TOMESTONED_ROOMS_BG_UPDATE , {"room_id" : rooms [- 1 ]}
1252
+ _BackgroundUpdates .REMOVE_TOMESTONED_ROOMS_BG_UPDATE , {"room_id" : rooms [- 1 ]}
1228
1253
)
1229
1254
1230
1255
return len (rooms )
@@ -1268,6 +1293,71 @@ async def has_auth_chain_index(self, room_id: str) -> bool:
1268
1293
1269
1294
return max_ordering is None
1270
1295
1296
+ async def _background_populate_room_depth_min_depth2 (
1297
+ self , progress : JsonDict , batch_size : int
1298
+ ) -> int :
1299
+ """Populate room_depth.min_depth2
1300
+
1301
+ This is to deal with the fact that min_depth was initially created as a
1302
+ 32-bit integer field.
1303
+ """
1304
+
1305
+ def process (txn : Cursor ) -> int :
1306
+ last_room = progress .get ("last_room" , "" )
1307
+ txn .execute (
1308
+ """
1309
+ UPDATE room_depth SET min_depth2=min_depth
1310
+ WHERE room_id IN (
1311
+ SELECT room_id FROM room_depth WHERE room_id > ?
1312
+ ORDER BY room_id LIMIT ?
1313
+ )
1314
+ RETURNING room_id;
1315
+ """ ,
1316
+ (last_room , batch_size ),
1317
+ )
1318
+ row_count = txn .rowcount
1319
+ if row_count == 0 :
1320
+ return 0
1321
+ last_room = max (row [0 ] for row in txn )
1322
+ logger .info ("populated room_depth up to %s" , last_room )
1323
+
1324
+ self .db_pool .updates ._background_update_progress_txn (
1325
+ txn ,
1326
+ _BackgroundUpdates .POPULATE_ROOM_DEPTH_MIN_DEPTH2 ,
1327
+ {"last_room" : last_room },
1328
+ )
1329
+ return row_count
1330
+
1331
+ result = await self .db_pool .runInteraction (
1332
+ "_background_populate_min_depth2" , process
1333
+ )
1334
+
1335
+ if result != 0 :
1336
+ return result
1337
+
1338
+ await self .db_pool .updates ._end_background_update (
1339
+ _BackgroundUpdates .POPULATE_ROOM_DEPTH_MIN_DEPTH2
1340
+ )
1341
+ return 0
1342
+
1343
+ async def _background_replace_room_depth_min_depth (
1344
+ self , progress : JsonDict , batch_size : int
1345
+ ) -> int :
1346
+ """Drop the old 'min_depth' column and rename 'min_depth2' into its place."""
1347
+
1348
+ def process (txn : Cursor ) -> None :
1349
+ for sql in _REPLACE_ROOM_DEPTH_SQL_COMMANDS :
1350
+ logger .info ("completing room_depth migration: %s" , sql )
1351
+ txn .execute (sql )
1352
+
1353
+ await self .db_pool .runInteraction ("_background_replace_room_depth" , process )
1354
+
1355
+ await self .db_pool .updates ._end_background_update (
1356
+ _BackgroundUpdates .REPLACE_ROOM_DEPTH_MIN_DEPTH ,
1357
+ )
1358
+
1359
+ return 0
1360
+
1271
1361
1272
1362
class RoomStore (RoomBackgroundUpdateStore , RoomWorkerStore , SearchStore ):
1273
1363
def __init__ (self , database : DatabasePool , db_conn , hs ):
0 commit comments