Skip to content

Commit dd316ee

Browse files
Do not add the schema argument to the PostgreSQL connection string when not needed. (#248)
1 parent 1c50742 commit dd316ee

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Please use [this](https://docs.gitlab.com/ee/development/changelog.html) document as guidelines to keep a changelog.
44

5+
## [unreleased]
6+
7+
### Fixed
8+
9+
* database: Do not add the `schema` argument to the PostgreSQL connection string when not needed.
10+
511
## 4.2.4 - 2022-02-14
612

713
### Fixed

src/dipdup/config.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@
5454
from dipdup.utils import pascal_to_snake
5555
from dipdup.utils import snake_to_pascal
5656

57-
ENV_VARIABLE_REGEX = r'\${([\w]*):-(.*)}'
57+
ENV_VARIABLE_REGEX = r'\${([\w]*):-(.*)}' # ${VARIABLE:-default}
5858
DEFAULT_RETRY_COUNT = 3
5959
DEFAULT_RETRY_SLEEP = 1
6060
DEFAULT_METADATA_URL = 'https://metadata.dipdup.net'
6161
DEFAULT_IPFS_URL = 'https://ipfs.io/ipfs'
62+
DEFAULT_POSTGRES_SCHEMA = 'public'
63+
DEFAULT_POSTGRES_USER = DEFAULT_POSTGRES_DATABASE = 'postgres'
64+
DEFAULT_POSTGRES_PORT = 5432
65+
DEFAULT_SQLITE_PATH = ':memory'
6266

6367
_logger = logging.getLogger('dipdup.config')
6468

@@ -73,7 +77,7 @@ class SqliteDatabaseConfig:
7377
"""
7478

7579
kind: Literal['sqlite']
76-
path: str = ':memory:'
80+
path: str = DEFAULT_SQLITE_PATH
7781

7882
@cached_property
7983
def connection_string(self) -> str:
@@ -97,10 +101,10 @@ class PostgresDatabaseConfig:
97101

98102
kind: Literal['postgres']
99103
host: str
100-
user: str = 'postgres'
101-
database: str = 'postgres'
102-
port: int = 5432
103-
schema_name: str = 'public'
104+
user: str = DEFAULT_POSTGRES_USER
105+
database: str = DEFAULT_POSTGRES_DATABASE
106+
port: int = DEFAULT_POSTGRES_PORT
107+
schema_name: str = DEFAULT_POSTGRES_SCHEMA
104108
password: str = ''
105109
immune_tables: Tuple[str, ...] = field(default_factory=tuple)
106110
connection_timeout: int = 60
@@ -109,7 +113,10 @@ class PostgresDatabaseConfig:
109113
def connection_string(self) -> str:
110114
# NOTE: `maxsize=1` is important! Concurrency will be broken otherwise.
111115
# NOTE: https://github.com/tortoise/tortoise-orm/issues/792
112-
return f'{self.kind}://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}?schema={self.schema_name}&maxsize=1'
116+
connection_string = f'{self.kind}://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}?maxsize=1'
117+
if self.schema_name != DEFAULT_POSTGRES_SCHEMA:
118+
connection_string += f'&schema={self.schema_name}'
119+
return connection_string
113120

114121
@validator('immune_tables')
115122
def valid_immune_tables(cls, v):
@@ -1009,7 +1016,10 @@ def __post_init_post_parse__(self):
10091016

10101017
@cached_property
10111018
def schema_name(self) -> str:
1012-
return self.database.schema_name if isinstance(self.database, PostgresDatabaseConfig) else 'public'
1019+
if isinstance(self.database, PostgresDatabaseConfig):
1020+
return self.database.schema_name
1021+
# NOTE: Not exactly correct; historical reason
1022+
return DEFAULT_POSTGRES_SCHEMA
10131023

10141024
@cached_property
10151025
def package_path(self) -> str:

src/dipdup/dipdup.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
from dipdup.utils.database import generate_schema
7272
from dipdup.utils.database import get_schema_hash
7373
from dipdup.utils.database import prepare_models
74-
from dipdup.utils.database import set_schema
7574
from dipdup.utils.database import tortoise_wrapper
7675
from dipdup.utils.database import validate_models
7776

@@ -417,9 +416,6 @@ async def _initialize_schema(self) -> None:
417416
schema_name = self._config.schema_name
418417
conn = get_connection(None)
419418

420-
if isinstance(self._config.database, PostgresDatabaseConfig):
421-
await set_schema(conn, schema_name)
422-
423419
# NOTE: Try to fetch existing schema
424420
try:
425421
self._schema = await Schema.get_or_none(name=schema_name)

src/dipdup/hasura.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from tortoise import fields
2626
from tortoise.transactions import get_connection
2727

28+
from dipdup.config import DEFAULT_POSTGRES_SCHEMA
2829
from dipdup.config import HasuraConfig
2930
from dipdup.config import HTTPConfig
3031
from dipdup.config import PostgresDatabaseConfig
@@ -102,7 +103,8 @@ def __init__(
102103
async def configure(self, force: bool = False) -> None:
103104
"""Generate Hasura metadata and apply to instance with credentials from `hasura` config section."""
104105

105-
if self._database_config.schema_name != 'public':
106+
# TODO: Validate during config parsing
107+
if self._database_config.schema_name != DEFAULT_POSTGRES_SCHEMA:
106108
raise ConfigurationError('Hasura integration requires `schema_name` to be `public`')
107109

108110
self._logger.info('Configuring Hasura')

src/dipdup/utils/database.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ def get_schema_hash(conn: BaseDBAsyncClient) -> str:
132132
return hashlib.sha256(processed_schema_sql).hexdigest()
133133

134134

135-
async def set_schema(conn: BaseDBAsyncClient, name: str) -> None:
136-
"""Set schema for the connection"""
137-
if isinstance(conn, SqliteClient):
138-
raise NotImplementedError
139-
140-
await conn.execute_script(f'SET search_path TO {name}')
141-
142-
143135
async def create_schema(conn: BaseDBAsyncClient, name: str) -> None:
144136
if isinstance(conn, SqliteClient):
145137
raise NotImplementedError
@@ -164,7 +156,6 @@ async def generate_schema(conn: BaseDBAsyncClient, name: str) -> None:
164156
await Tortoise.generate_schemas()
165157
elif isinstance(conn, AsyncpgDBClient):
166158
await create_schema(conn, name)
167-
await set_schema(conn, name)
168159
await Tortoise.generate_schemas()
169160

170161
# NOTE: Apply built-in scripts before project ones

0 commit comments

Comments
 (0)