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

Commit 6efa674

Browse files
authored
Add type hints to schema deltas (#15497)
Cleans-up the schema delta files: * Removes no-op functions. * Adds missing type hints to function parameters. * Fixes any issues with type hints. This also renames one (very old) schema delta to avoid a conflict that mypy complains about.
1 parent a346b43 commit 6efa674

30 files changed

+132
-144
lines changed

changelog.d/15497.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type hints.

mypy.ini

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ files =
2121
tests/,
2222
build_rust.py
2323

24-
# Note: Better exclusion syntax coming in mypy > 0.910
25-
# https://github.com/python/mypy/pull/11329
26-
#
27-
# For now, set the (?x) flag enable "verbose" regexes
28-
# https://docs.python.org/3/library/re.html#re.X
29-
exclude = (?x)
30-
^(
31-
|synapse/storage/schema/
32-
)$
33-
3424
[mypy-synapse.metrics._reactor_metrics]
3525
# This module imports select.epoll. That exists on Linux, but doesn't on macOS.
3626
# See https://github.com/matrix-org/synapse/pull/11771.

synapse/storage/prepare_database.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from typing_extensions import Counter as CounterType
2323

2424
from synapse.config.homeserver import HomeServerConfig
25-
from synapse.storage.database import LoggingDatabaseConnection
25+
from synapse.storage.database import LoggingDatabaseConnection, LoggingTransaction
2626
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine
2727
from synapse.storage.schema import SCHEMA_COMPAT_VERSION, SCHEMA_VERSION
2828
from synapse.storage.types import Cursor
@@ -168,7 +168,9 @@ def prepare_database(
168168

169169

170170
def _setup_new_database(
171-
cur: Cursor, database_engine: BaseDatabaseEngine, databases: Collection[str]
171+
cur: LoggingTransaction,
172+
database_engine: BaseDatabaseEngine,
173+
databases: Collection[str],
172174
) -> None:
173175
"""Sets up the physical database by finding a base set of "full schemas" and
174176
then applying any necessary deltas, including schemas from the given data
@@ -289,7 +291,7 @@ def _setup_new_database(
289291

290292

291293
def _upgrade_existing_database(
292-
cur: Cursor,
294+
cur: LoggingTransaction,
293295
current_schema_state: _SchemaState,
294296
database_engine: BaseDatabaseEngine,
295297
config: Optional[HomeServerConfig],

synapse/storage/schema/main/delta/20/pushers.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424

2525
import logging
2626

27+
from synapse.storage.database import LoggingTransaction
28+
from synapse.storage.engines import BaseDatabaseEngine
29+
2730
logger = logging.getLogger(__name__)
2831

2932

30-
def run_create(cur, database_engine, *args, **kwargs):
33+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
3134
logger.info("Porting pushers table...")
3235
cur.execute(
3336
"""
@@ -61,8 +64,8 @@ def run_create(cur, database_engine, *args, **kwargs):
6164
"""
6265
)
6366
count = 0
64-
for row in cur.fetchall():
65-
row = list(row)
67+
for tuple_row in cur.fetchall():
68+
row = list(tuple_row)
6669
row[8] = bytes(row[8]).decode("utf-8")
6770
row[11] = bytes(row[11]).decode("utf-8")
6871
cur.execute(
@@ -81,7 +84,3 @@ def run_create(cur, database_engine, *args, **kwargs):
8184
cur.execute("DROP TABLE pushers")
8285
cur.execute("ALTER TABLE pushers2 RENAME TO pushers")
8386
logger.info("Moved %d pushers to new table", count)
84-
85-
86-
def run_upgrade(*args, **kwargs):
87-
pass

synapse/storage/schema/main/delta/25/fts.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import json
1515
import logging
1616

17-
from synapse.storage.engines import PostgresEngine, Sqlite3Engine
17+
from synapse.storage.database import LoggingTransaction
18+
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine
1819
from synapse.storage.prepare_database import get_statements
1920

2021
logger = logging.getLogger(__name__)
@@ -41,7 +42,7 @@
4142
)
4243

4344

44-
def run_create(cur, database_engine, *args, **kwargs):
45+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
4546
if isinstance(database_engine, PostgresEngine):
4647
for statement in get_statements(POSTGRES_TABLE.splitlines()):
4748
cur.execute(statement)
@@ -72,7 +73,3 @@ def run_create(cur, database_engine, *args, **kwargs):
7273
)
7374

7475
cur.execute(sql, ("event_search", progress_json))
75-
76-
77-
def run_upgrade(*args, **kwargs):
78-
pass

synapse/storage/schema/main/delta/27/ts.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import json
1515
import logging
1616

17+
from synapse.storage.database import LoggingTransaction
18+
from synapse.storage.engines import BaseDatabaseEngine
1719
from synapse.storage.prepare_database import get_statements
1820

1921
logger = logging.getLogger(__name__)
@@ -25,7 +27,7 @@
2527
)
2628

2729

28-
def run_create(cur, database_engine, *args, **kwargs):
30+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
2931
for statement in get_statements(ALTER_TABLE.splitlines()):
3032
cur.execute(statement)
3133

@@ -51,7 +53,3 @@ def run_create(cur, database_engine, *args, **kwargs):
5153
)
5254

5355
cur.execute(sql, ("event_origin_server_ts", progress_json))
54-
55-
56-
def run_upgrade(*args, **kwargs):
57-
pass

synapse/storage/schema/main/delta/30/as_users.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15+
from typing import Dict, Iterable, List, Tuple, cast
1516

1617
from synapse.config.appservice import load_appservices
18+
from synapse.config.homeserver import HomeServerConfig
19+
from synapse.storage.database import LoggingTransaction
20+
from synapse.storage.engines import BaseDatabaseEngine
1721

1822
logger = logging.getLogger(__name__)
1923

2024

21-
def run_create(cur, database_engine, *args, **kwargs):
25+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
2226
# NULL indicates user was not registered by an appservice.
2327
try:
2428
cur.execute("ALTER TABLE users ADD COLUMN appservice_id TEXT")
@@ -27,9 +31,13 @@ def run_create(cur, database_engine, *args, **kwargs):
2731
pass
2832

2933

30-
def run_upgrade(cur, database_engine, config, *args, **kwargs):
34+
def run_upgrade(
35+
cur: LoggingTransaction,
36+
database_engine: BaseDatabaseEngine,
37+
config: HomeServerConfig,
38+
) -> None:
3139
cur.execute("SELECT name FROM users")
32-
rows = cur.fetchall()
40+
rows = cast(Iterable[Tuple[str]], cur.fetchall())
3341

3442
config_files = []
3543
try:
@@ -39,7 +47,7 @@ def run_upgrade(cur, database_engine, config, *args, **kwargs):
3947

4048
appservices = load_appservices(config.server.server_name, config_files)
4149

42-
owned = {}
50+
owned: Dict[str, List[str]] = {}
4351

4452
for row in rows:
4553
user_id = row[0]

synapse/storage/schema/main/delta/31/pushers.py renamed to synapse/storage/schema/main/delta/31/pushers_0.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020

2121
import logging
2222

23+
from synapse.storage.database import LoggingTransaction
24+
from synapse.storage.engines import BaseDatabaseEngine
25+
2326
logger = logging.getLogger(__name__)
2427

2528

26-
def token_to_stream_ordering(token):
29+
def token_to_stream_ordering(token: str) -> int:
2730
return int(token[1:].split("_")[0])
2831

2932

30-
def run_create(cur, database_engine, *args, **kwargs):
33+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
3134
logger.info("Porting pushers table, delta 31...")
3235
cur.execute(
3336
"""
@@ -61,8 +64,8 @@ def run_create(cur, database_engine, *args, **kwargs):
6164
"""
6265
)
6366
count = 0
64-
for row in cur.fetchall():
65-
row = list(row)
67+
for tuple_row in cur.fetchall():
68+
row = list(tuple_row)
6669
row[12] = token_to_stream_ordering(row[12])
6770
cur.execute(
6871
"""
@@ -80,7 +83,3 @@ def run_create(cur, database_engine, *args, **kwargs):
8083
cur.execute("DROP TABLE pushers")
8184
cur.execute("ALTER TABLE pushers2 RENAME TO pushers")
8285
logger.info("Moved %d pushers to new table", count)
83-
84-
85-
def run_upgrade(cur, database_engine, *args, **kwargs):
86-
pass

synapse/storage/schema/main/delta/31/search_update.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import json
1515
import logging
1616

17-
from synapse.storage.engines import PostgresEngine
17+
from synapse.storage.database import LoggingTransaction
18+
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine
1819
from synapse.storage.prepare_database import get_statements
1920

2021
logger = logging.getLogger(__name__)
@@ -26,7 +27,7 @@
2627
"""
2728

2829

29-
def run_create(cur, database_engine, *args, **kwargs):
30+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
3031
if not isinstance(database_engine, PostgresEngine):
3132
return
3233

@@ -56,7 +57,3 @@ def run_create(cur, database_engine, *args, **kwargs):
5657
)
5758

5859
cur.execute(sql, ("event_search_order", progress_json))
59-
60-
61-
def run_upgrade(cur, database_engine, *args, **kwargs):
62-
pass

synapse/storage/schema/main/delta/33/event_fields.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import json
1515
import logging
1616

17+
from synapse.storage.database import LoggingTransaction
18+
from synapse.storage.engines import BaseDatabaseEngine
1719
from synapse.storage.prepare_database import get_statements
1820

1921
logger = logging.getLogger(__name__)
@@ -25,7 +27,7 @@
2527
"""
2628

2729

28-
def run_create(cur, database_engine, *args, **kwargs):
30+
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
2931
for statement in get_statements(ALTER_TABLE.splitlines()):
3032
cur.execute(statement)
3133

@@ -51,7 +53,3 @@ def run_create(cur, database_engine, *args, **kwargs):
5153
)
5254

5355
cur.execute(sql, ("event_fields_sender_url", progress_json))
54-
55-
56-
def run_upgrade(cur, database_engine, *args, **kwargs):
57-
pass

0 commit comments

Comments
 (0)