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

Commit 7df04ca

Browse files
authored
Populate the room version from state events (#7070)
Fixes #7065 This is basically the same as #6847 except it tries to populate events from `state_events` rather than `current_state_events`, since the latter might have been cleared from the state of some rooms too early, leaving them with a `NULL` room version.
1 parent beb19cf commit 7df04ca

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

changelog.d/7070.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Repair a data-corruption issue which was introduced in Synapse 1.10, and fixed in Synapse 1.11, and which could cause `/sync` to return with 404 errors about missing events and unknown rooms.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright 2020 The Matrix.org Foundation C.I.C.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- When we first added the room_version column to the rooms table, it was populated from
17+
-- the current_state_events table. However, there was an issue causing a background
18+
-- update to clean up the current_state_events table for rooms where the server is no
19+
-- longer participating, before that column could be populated. Therefore, some rooms had
20+
-- a NULL room_version.
21+
22+
-- The rooms_version_column_2.sql.* delta files were introduced to make the populating
23+
-- synchronous instead of running it in a background update, which fixed this issue.
24+
-- However, all of the instances of Synapse installed or updated in the meantime got
25+
-- their rooms table corrupted with NULL room_versions.
26+
27+
-- This query fishes out the room versions from the create event using the state_events
28+
-- table instead of the current_state_events one, as the former still have all of the
29+
-- create events.
30+
31+
UPDATE rooms SET room_version=(
32+
SELECT COALESCE(json::json->'content'->>'room_version','1')
33+
FROM state_events se INNER JOIN event_json ej USING (event_id)
34+
WHERE se.room_id=rooms.room_id AND se.type='m.room.create' AND se.state_key=''
35+
LIMIT 1
36+
) WHERE rooms.room_version IS NULL;
37+
38+
-- see also rooms_version_column_3.sql.sqlite which has a copy of the above query, using
39+
-- sqlite syntax for the json extraction.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Copyright 2020 The Matrix.org Foundation C.I.C.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- see rooms_version_column_3.sql.postgres for details of what's going on here.
17+
18+
UPDATE rooms SET room_version=(
19+
SELECT COALESCE(json_extract(ej.json, '$.content.room_version'), '1')
20+
FROM state_events se INNER JOIN event_json ej USING (event_id)
21+
WHERE se.room_id=rooms.room_id AND se.type='m.room.create' AND se.state_key=''
22+
LIMIT 1
23+
) WHERE rooms.room_version IS NULL;

0 commit comments

Comments
 (0)