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

Commit c80878d

Browse files
authored
Add --run-background-updates option to update_database script. (#10954)
Signed-off-by: Nick Barrett <[email protected]>
1 parent f8d0f72 commit c80878d

File tree

8 files changed

+46
-21
lines changed

8 files changed

+46
-21
lines changed

.ci/scripts/test_synapse_port_db.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ python -m synapse.app.homeserver --generate-keys -c .ci/sqlite-config.yaml
2525
echo "--- Prepare test database"
2626

2727
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
28-
scripts-dev/update_database --database-config .ci/sqlite-config.yaml
28+
scripts/update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
2929

3030
# Create the PostgreSQL database.
3131
.ci/scripts/postgres_exec.py "CREATE DATABASE synapse"
@@ -46,7 +46,7 @@ echo "--- Prepare empty SQLite database"
4646
# we do this by deleting the sqlite db, and then doing the same again.
4747
rm .ci/test_db.db
4848

49-
scripts-dev/update_database --database-config .ci/sqlite-config.yaml
49+
scripts/update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
5050

5151
# re-create the PostgreSQL database.
5252
.ci/scripts/postgres_exec.py \

changelog.d/10954.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Include an `update_synapse_database` script in the distribution. Contributed by @Fizzadar at Beeper.

debian/changelog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
matrix-synapse-py3 (1.44.0~rc2+nmu1) UNRELEASED; urgency=medium
2+
3+
[ Nick @ Beeper ]
4+
* Include an `update_synapse_database` script in the distribution.
5+
6+
-- root <root@f7b8a71098d3> Mon, 04 Oct 2021 13:29:26 +0000
7+
18
matrix-synapse-py3 (1.44.0) stable; urgency=medium
29

310
* New synapse release 1.44.0.

debian/matrix-synapse-py3.links

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ opt/venvs/matrix-synapse/bin/register_new_matrix_user usr/bin/register_new_matri
33
opt/venvs/matrix-synapse/bin/synapse_port_db usr/bin/synapse_port_db
44
opt/venvs/matrix-synapse/bin/synapse_review_recent_signups usr/bin/synapse_review_recent_signups
55
opt/venvs/matrix-synapse/bin/synctl usr/bin/synctl
6+
opt/venvs/matrix-synapse/bin/update_synapse_database usr/bin/update_synapse_database

scripts-dev/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ else
9090
"scripts/hash_password"
9191
"scripts/register_new_matrix_user"
9292
"scripts/synapse_port_db"
93+
"scripts/update_synapse_database"
9394
"scripts-dev"
9495
"scripts-dev/build_debian_packages"
9596
"scripts-dev/sign_json"
96-
"scripts-dev/update_database"
9797
"contrib" "synctl" "setup.py" "synmark" "stubs" ".ci"
9898
)
9999
fi

scripts-dev/make_full_schema.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ python -m synapse.app.homeserver --generate-keys -c "$SQLITE_CONFIG"
147147

148148
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
149149
echo "Running db background jobs..."
150-
scripts-dev/update_database --database-config "$SQLITE_CONFIG"
150+
scripts/update_synapse_database --database-config --run-background-updates "$SQLITE_CONFIG"
151151

152152
# Create the PostgreSQL database.
153153
echo "Creating postgres database..."

scripts-dev/update_database renamed to scripts/update_synapse_database

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,29 @@ class MockHomeserver(HomeServer):
4242
self.version_string = "Synapse/" + get_version_string(synapse)
4343

4444

45-
if __name__ == "__main__":
45+
def run_background_updates(hs):
46+
store = hs.get_datastore()
47+
48+
async def run_background_updates():
49+
await store.db_pool.updates.run_background_updates(sleep=False)
50+
# Stop the reactor to exit the script once every background update is run.
51+
reactor.stop()
52+
53+
def run():
54+
# Apply all background updates on the database.
55+
defer.ensureDeferred(
56+
run_as_background_process("background_updates", run_background_updates)
57+
)
58+
59+
reactor.callWhenRunning(run)
60+
61+
reactor.run()
62+
63+
64+
def main():
4665
parser = argparse.ArgumentParser(
4766
description=(
48-
"Updates a synapse database to the latest schema and runs background updates"
67+
"Updates a synapse database to the latest schema and optionally runs background updates"
4968
" on it."
5069
)
5170
)
@@ -54,7 +73,13 @@ if __name__ == "__main__":
5473
"--database-config",
5574
type=argparse.FileType("r"),
5675
required=True,
57-
help="A database config file for either a SQLite3 database or a PostgreSQL one.",
76+
help="Synapse configuration file, giving the details of the database to be updated",
77+
)
78+
parser.add_argument(
79+
"--run-background-updates",
80+
action="store_true",
81+
required=False,
82+
help="run background updates after upgrading the database schema",
5883
)
5984

6085
args = parser.parse_args()
@@ -82,19 +107,10 @@ if __name__ == "__main__":
82107
# Setup instantiates the store within the homeserver object and updates the
83108
# DB.
84109
hs.setup()
85-
store = hs.get_datastore()
86110

87-
async def run_background_updates():
88-
await store.db_pool.updates.run_background_updates(sleep=False)
89-
# Stop the reactor to exit the script once every background update is run.
90-
reactor.stop()
111+
if args.run_background_updates:
112+
run_background_updates(hs)
91113

92-
def run():
93-
# Apply all background updates on the database.
94-
defer.ensureDeferred(
95-
run_as_background_process("background_updates", run_background_updates)
96-
)
97114

98-
reactor.callWhenRunning(run)
99-
100-
reactor.run()
115+
if __name__ == "__main__":
116+
main()

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ lint_targets =
4141
scripts/hash_password
4242
scripts/register_new_matrix_user
4343
scripts/synapse_port_db
44+
scripts/update_synapse_database
4445
scripts-dev
4546
scripts-dev/build_debian_packages
4647
scripts-dev/sign_json
47-
scripts-dev/update_database
4848
stubs
4949
contrib
5050
synctl

0 commit comments

Comments
 (0)