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

Commit 51065c4

Browse files
authored
Fix port_db on empty db (#9930)
... and test it.
1 parent 6c84778 commit 51065c4

File tree

6 files changed

+69
-54
lines changed

6 files changed

+69
-54
lines changed

.buildkite/scripts/create_postgres_db.py

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
# Copyright 2019 The Matrix.org Foundation C.I.C.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import sys
17+
18+
import psycopg2
19+
20+
# a very simple replacment for `psql`, to make up for the lack of the postgres client
21+
# libraries in the synapse docker image.
22+
23+
# We use "postgres" as a database because it's bound to exist and the "synapse" one
24+
# doesn't exist yet.
25+
db_conn = psycopg2.connect(
26+
user="postgres", host="postgres", password="postgres", dbname="postgres"
27+
)
28+
db_conn.autocommit = True
29+
cur = db_conn.cursor()
30+
for c in sys.argv[1:]:
31+
cur.execute(c)

.buildkite/scripts/test_synapse_port_db.sh

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env bash
22
#
3-
# Test script for 'synapse_port_db', which creates a virtualenv, installs Synapse along
4-
# with additional dependencies needed for the test (such as coverage or the PostgreSQL
5-
# driver), update the schema of the test SQLite database and run background updates on it,
6-
# create an empty test database in PostgreSQL, then run the 'synapse_port_db' script to
7-
# test porting the SQLite database to the PostgreSQL database (with coverage).
3+
# Test script for 'synapse_port_db'.
4+
# - sets up synapse and deps
5+
# - runs the port script on a prepopulated test sqlite db
6+
# - also runs it against an new sqlite db
7+
88

99
set -xe
1010
cd `dirname $0`/../..
@@ -22,15 +22,32 @@ echo "--- Generate the signing key"
2222
# Generate the server's signing key.
2323
python -m synapse.app.homeserver --generate-keys -c .buildkite/sqlite-config.yaml
2424

25-
echo "--- Prepare the databases"
25+
echo "--- Prepare test database"
2626

2727
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
2828
scripts-dev/update_database --database-config .buildkite/sqlite-config.yaml
2929

3030
# Create the PostgreSQL database.
31-
./.buildkite/scripts/create_postgres_db.py
31+
./.buildkite/scripts/postgres_exec.py "CREATE DATABASE synapse"
32+
33+
echo "+++ Run synapse_port_db against test database"
34+
coverage run scripts/synapse_port_db --sqlite-database .buildkite/test_db.db --postgres-config .buildkite/postgres-config.yaml
35+
36+
#####
37+
38+
# Now do the same again, on an empty database.
39+
40+
echo "--- Prepare empty SQLite database"
41+
42+
# we do this by deleting the sqlite db, and then doing the same again.
43+
rm .buildkite/test_db.db
44+
45+
scripts-dev/update_database --database-config .buildkite/sqlite-config.yaml
3246

33-
echo "+++ Run synapse_port_db"
47+
# re-create the PostgreSQL database.
48+
./.buildkite/scripts/postgres_exec.py \
49+
"DROP DATABASE synapse" \
50+
"CREATE DATABASE synapse"
3451

35-
# Run the script
52+
echo "+++ Run synapse_port_db against empty database"
3653
coverage run scripts/synapse_port_db --sqlite-database .buildkite/test_db.db --postgres-config .buildkite/postgres-config.yaml

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ jobs:
273273
python-version: ${{ matrix.python-version }}
274274
- name: Patch Buildkite-specific test scripts
275275
run: |
276-
sed -i -e 's/host="postgres"/host="localhost"/' .buildkite/scripts/create_postgres_db.py
276+
sed -i -e 's/host="postgres"/host="localhost"/' .buildkite/scripts/postgres_exec.py
277277
sed -i -e 's/host: postgres/host: localhost/' .buildkite/postgres-config.yaml
278278
sed -i -e 's|/src/||' .buildkite/{sqlite,postgres}-config.yaml
279279
sed -i -e 's/\$TOP/\$GITHUB_WORKSPACE/' .coveragerc

changelog.d/9930.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bugs introduced in v1.23.0 which made the PostgreSQL port script fail when run with a newly-created SQLite database.

scripts/synapse_port_db

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,11 @@ class Porter(object):
913913
(curr_forward_id + 1,),
914914
)
915915

916-
txn.execute(
917-
"ALTER SEQUENCE events_backfill_stream_seq RESTART WITH %s",
918-
(curr_backward_id + 1,),
919-
)
916+
if curr_backward_id:
917+
txn.execute(
918+
"ALTER SEQUENCE events_backfill_stream_seq RESTART WITH %s",
919+
(curr_backward_id + 1,),
920+
)
920921

921922
await self.postgres_store.db_pool.runInteraction(
922923
"_setup_events_stream_seqs", _setup_events_stream_seqs_set_pos,
@@ -954,10 +955,11 @@ class Porter(object):
954955
(curr_chain_id,),
955956
)
956957

957-
await self.postgres_store.db_pool.runInteraction(
958-
"_setup_event_auth_chain_id", r,
959-
)
960-
958+
if curr_chain_id is not None:
959+
await self.postgres_store.db_pool.runInteraction(
960+
"_setup_event_auth_chain_id",
961+
r,
962+
)
961963

962964

963965
##############################################

0 commit comments

Comments
 (0)