This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Tidy up and type-hint the database engine modules #12734
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e36410c
BaseDatabaseEngine: use ConnectionType generic
e9dcff7
Let the DatabaseEngines provide their db module
c8f1458
Don't propgate DB config upwards
7cf1719
Stub out the DB2API module
467b2a0
Let's disallow untyped defs while I'm at it
44af3e4
Changelog
4d8b840
Revert "Don't propgate DB config upwards"
6dcbf99
Describe the DBAPI2 exception heirarchy
24d9189
Tweak writing (thanks Sean)
27e90f0
Fix weird indent??
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
# limitations under the License. | ||
|
||
import logging | ||
from typing import Mapping, Optional | ||
from typing import TYPE_CHECKING, Any, Mapping, Optional, cast | ||
|
||
from synapse.storage.engines._base import ( | ||
BaseDatabaseEngine, | ||
|
@@ -22,30 +22,35 @@ | |
) | ||
from synapse.storage.types import Connection | ||
|
||
if TYPE_CHECKING: | ||
import psycopg2 # noqa: F401 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PostgresEngine(BaseDatabaseEngine): | ||
def __init__(self, database_module, database_config): | ||
super().__init__(database_module, database_config) | ||
self.module.extensions.register_type(self.module.extensions.UNICODE) | ||
class PostgresEngine(BaseDatabaseEngine["psycopg2.connection"]): | ||
def __init__(self, database_config: Mapping[str, Any]): | ||
import psycopg2.extensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dislike importing this here rather than at the top level. But this was the least-bad/easiest way I could see to not require existing installations to install psycopg2. |
||
|
||
super().__init__(psycopg2, database_config) | ||
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | ||
|
||
# Disables passing `bytes` to txn.execute, c.f. #6186. If you do | ||
# actually want to use bytes than wrap it in `bytearray`. | ||
def _disable_bytes_adapter(_): | ||
raise Exception("Passing bytes to DB is disabled.") | ||
|
||
self.module.extensions.register_adapter(bytes, _disable_bytes_adapter) | ||
psycopg2.extensions.register_adapter(bytes, _disable_bytes_adapter) | ||
self.synchronous_commit = database_config.get("synchronous_commit", True) | ||
self._version = None # unknown as yet | ||
self._version: Optional[int] = None # unknown as yet | ||
|
||
self.isolation_level_map: Mapping[int, int] = { | ||
IsolationLevel.READ_COMMITTED: self.module.extensions.ISOLATION_LEVEL_READ_COMMITTED, | ||
IsolationLevel.REPEATABLE_READ: self.module.extensions.ISOLATION_LEVEL_REPEATABLE_READ, | ||
IsolationLevel.SERIALIZABLE: self.module.extensions.ISOLATION_LEVEL_SERIALIZABLE, | ||
IsolationLevel.READ_COMMITTED: psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED, | ||
IsolationLevel.REPEATABLE_READ: psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ, | ||
IsolationLevel.SERIALIZABLE: psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE, | ||
} | ||
self.default_isolation_level = ( | ||
self.module.extensions.ISOLATION_LEVEL_REPEATABLE_READ | ||
psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ | ||
) | ||
self.config = database_config | ||
|
||
|
@@ -65,7 +70,7 @@ def check_database(self, db_conn, allow_outdated_version: bool = False): | |
# docs: The number is formed by converting the major, minor, and | ||
# revision numbers into two-decimal-digit numbers and appending them | ||
# together. For example, version 8.1.5 will be returned as 80105 | ||
self._version = db_conn.server_version | ||
self._version = cast(int, db_conn.server_version) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If accepted, python/typeshed#7834 would make this cast redundant. |
||
allow_unsafe_locale = self.config.get("allow_unsafe_locale", False) | ||
|
||
# Are we on a supported PostgreSQL version? | ||
|
@@ -166,7 +171,9 @@ def supports_returning(self) -> bool: | |
return True | ||
|
||
def is_deadlock(self, error): | ||
if isinstance(error, self.module.DatabaseError): | ||
import psycopg2.extensions | ||
|
||
if isinstance(error, psycopg2.DatabaseError): | ||
# https://www.postgresql.org/docs/current/static/errcodes-appendix.html | ||
# "40001" serialization_failure | ||
# "40P01" deadlock_detected | ||
|
@@ -198,7 +205,9 @@ def server_version(self): | |
return "%i.%i.%i" % (numver / 10000, (numver % 10000) / 100, numver % 100) | ||
|
||
def in_transaction(self, conn: Connection) -> bool: | ||
return conn.status != self.module.extensions.STATUS_READY # type: ignore | ||
import psycopg2.extensions | ||
|
||
return conn.status != psycopg2.extensions.STATUS_READY # type: ignore | ||
|
||
def attempt_to_set_autocommit(self, conn: Connection, autocommit: bool): | ||
return conn.set_session(autocommit=autocommit) # type: ignore | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
poor commit hygiene.