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

Commit cb6e2c6

Browse files
authored
Fix background schema updates failing over a large upgrade gap (#15887)
1 parent 8e8431b commit cb6e2c6

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

changelog.d/15887.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix background schema updates failing over a large upgrade gap.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# http://www.apache.org/licenses/LICENSE-2.0
2+
#
3+
# Unless required by applicable law or agreed to in writing, software
4+
# distributed under the License is distributed on an "AS IS" BASIS,
5+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6+
# See the License for the specific language governing permissions and
7+
# limitations under the License.
8+
9+
10+
from synapse.storage.database import LoggingTransaction
11+
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine
12+
13+
14+
def run_create(
15+
cur: LoggingTransaction,
16+
database_engine: BaseDatabaseEngine,
17+
) -> None:
18+
"""
19+
An attempt to mitigate a painful race between foreground and background updates
20+
touching the `stream_ordering` column of the events table. More info can be found
21+
at https://github.com/matrix-org/synapse/issues/15677.
22+
"""
23+
24+
# technically the bg update we're concerned with below should only have been added in
25+
# postgres but it doesn't hurt to be extra careful
26+
if isinstance(database_engine, PostgresEngine):
27+
select_sql = """
28+
SELECT 1 FROM background_updates
29+
WHERE update_name = 'replace_stream_ordering_column'
30+
"""
31+
cur.execute(select_sql)
32+
res = cur.fetchone()
33+
34+
# if the background update `replace_stream_ordering_column` is still pending, we need
35+
# to drop the indexes added in 7403, and re-add them to the column `stream_ordering2`
36+
# with the idea that they will be preserved when the column is renamed `stream_ordering`
37+
# after the background update has finished
38+
if res:
39+
drop_cse_sql = """
40+
ALTER TABLE current_state_events DROP CONSTRAINT event_stream_ordering_fkey
41+
"""
42+
cur.execute(drop_cse_sql)
43+
44+
drop_lcm_sql = """
45+
ALTER TABLE local_current_membership DROP CONSTRAINT event_stream_ordering_fkey
46+
"""
47+
cur.execute(drop_lcm_sql)
48+
49+
drop_rm_sql = """
50+
ALTER TABLE room_memberships DROP CONSTRAINT event_stream_ordering_fkey
51+
"""
52+
cur.execute(drop_rm_sql)
53+
54+
add_cse_sql = """
55+
ALTER TABLE current_state_events ADD CONSTRAINT event_stream_ordering_fkey
56+
FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering2) NOT VALID;
57+
"""
58+
cur.execute(add_cse_sql)
59+
60+
add_lcm_sql = """
61+
ALTER TABLE local_current_membership ADD CONSTRAINT event_stream_ordering_fkey
62+
FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering2) NOT VALID;
63+
"""
64+
cur.execute(add_lcm_sql)
65+
66+
add_rm_sql = """
67+
ALTER TABLE room_memberships ADD CONSTRAINT event_stream_ordering_fkey
68+
FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering2) NOT VALID;
69+
"""
70+
cur.execute(add_rm_sql)

0 commit comments

Comments
 (0)