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

Commit d3cf073

Browse files
David Robertsonclokep
andauthored
Optionally use an on-disk sqlite db in tests (#11702)
* Optionally use an on-disk sqlite db in tests When debugging a test it is sometimes useful to inspect the state of the DB. This is not easy when the db is in-memory: one cannot attach the sqlite CLI to another process's DB. With this change, if SYNAPSE_TEST_PERSIST_SQLITE_DB is set, we use `_trial_temp/test.db` as our sqlite database. One can then use `sqlite3 _trial_temp/test.db` and query to your heart's content. The DB is destroyed and recreated between different test cases. Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>
1 parent 2bb4bd1 commit d3cf073

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

changelog.d/11702.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add the option to write sqlite test dbs to disk when running tests.

tests/server.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import hashlib
1515
import json
1616
import logging
17+
import os
18+
import os.path
1719
import time
1820
import uuid
1921
import warnings
@@ -71,6 +73,7 @@
7173
POSTGRES_HOST,
7274
POSTGRES_PASSWORD,
7375
POSTGRES_USER,
76+
SQLITE_PERSIST_DB,
7477
USE_POSTGRES_FOR_TESTS,
7578
MockClock,
7679
default_config,
@@ -739,9 +742,23 @@ def setup_test_homeserver(
739742
},
740743
}
741744
else:
745+
if SQLITE_PERSIST_DB:
746+
# The current working directory is in _trial_temp, so this gets created within that directory.
747+
test_db_location = os.path.abspath("test.db")
748+
logger.debug("Will persist db to %s", test_db_location)
749+
# Ensure each test gets a clean database.
750+
try:
751+
os.remove(test_db_location)
752+
except FileNotFoundError:
753+
pass
754+
else:
755+
logger.debug("Removed existing DB at %s", test_db_location)
756+
else:
757+
test_db_location = ":memory:"
758+
742759
database_config = {
743760
"name": "sqlite3",
744-
"args": {"database": ":memory:", "cp_min": 1, "cp_max": 1},
761+
"args": {"database": test_db_location, "cp_min": 1, "cp_max": 1},
745762
}
746763

747764
if "db_txn_limit" in kwargs:

tests/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
POSTGRES_PASSWORD = os.environ.get("SYNAPSE_POSTGRES_PASSWORD", None)
4343
POSTGRES_BASE_DB = "_synapse_unit_tests_base_%s" % (os.getpid(),)
4444

45+
# When debugging a specific test, it's occasionally useful to write the
46+
# DB to disk and query it with the sqlite CLI.
47+
SQLITE_PERSIST_DB = os.environ.get("SYNAPSE_TEST_PERSIST_SQLITE_DB") is not None
48+
4549
# the dbname we will connect to in order to create the base database.
4650
POSTGRES_DBNAME_FOR_INITIAL_CREATE = "postgres"
4751

0 commit comments

Comments
 (0)