Skip to content

Commit d8e41a2

Browse files
authored
Merge pull request #699 from tisnik/lcore-741-refactored-code-to-connect-to-databases
LCORE-741: refactored code used to connect to databases with quota
2 parents ddc9c6c + e0c19d1 commit d8e41a2

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

src/quota/connect_pg.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""PostgreSQL connection handler."""
2+
3+
from typing import Any
4+
import psycopg2
5+
6+
from log import get_logger
7+
from models.config import PostgreSQLDatabaseConfiguration
8+
9+
logger = get_logger(__name__)
10+
11+
12+
def connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any:
13+
"""Initialize connection to PostgreSQL database."""
14+
logger.info("Connecting to PostgreSQL storage")
15+
try:
16+
connection = psycopg2.connect(
17+
host=config.host,
18+
port=config.port,
19+
user=config.user,
20+
password=config.password.get_secret_value(),
21+
dbname=config.db,
22+
sslmode=config.ssl_mode,
23+
# sslrootcert=config.ca_cert_path,
24+
gssencmode=config.gss_encmode,
25+
)
26+
if connection is not None:
27+
connection.autocommit = True
28+
return connection
29+
except psycopg2.Error as e:
30+
logger.exception("Error connecting to PostgreSQL database:\n%s", e)
31+
raise

src/quota/connect_sqlite.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""SQLite connection handler."""
2+
3+
import sqlite3
4+
from typing import Any
5+
6+
from log import get_logger
7+
from models.config import SQLiteDatabaseConfiguration
8+
9+
logger = get_logger(__name__)
10+
11+
12+
def connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any:
13+
"""Initialize connection to database."""
14+
logger.info("Connecting to SQLite storage")
15+
# make sure the connection will have known state
16+
# even if SQLite is not alive
17+
connection = None
18+
try:
19+
connection = sqlite3.connect(database=config.db_path)
20+
if connection is not None:
21+
connection.autocommit = True
22+
return connection
23+
except sqlite3.Error as e:
24+
logger.exception("Error initializing SQLite cache:\n%s", e)
25+
raise

src/runners/quota_scheduler.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
from threading import Thread
55
from time import sleep
66

7-
import sqlite3
8-
import psycopg2
97

108
import constants
119
from log import get_logger
1210
from models.config import (
1311
Configuration,
1412
QuotaHandlersConfiguration,
1513
QuotaLimiterConfiguration,
16-
PostgreSQLDatabaseConfiguration,
17-
SQLiteDatabaseConfiguration,
1814
)
1915

16+
from quota.connect_pg import connect_pg
17+
from quota.connect_sqlite import connect_sqlite
18+
2019
from quota.sql import (
2120
CREATE_QUOTA_TABLE,
2221
INCREASE_QUOTA_STATEMENT_PG,
@@ -211,41 +210,6 @@ def connect(config: QuotaHandlersConfiguration) -> Any:
211210
return None
212211

213212

214-
def connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any:
215-
"""Initialize connection to PostgreSQL database."""
216-
logger.info("Connecting to PostgreSQL storage")
217-
connection = psycopg2.connect(
218-
host=config.host,
219-
port=config.port,
220-
user=config.user,
221-
password=config.password.get_secret_value(),
222-
dbname=config.db,
223-
sslmode=config.ssl_mode,
224-
# sslrootcert=config.ca_cert_path,
225-
gssencmode=config.gss_encmode,
226-
)
227-
if connection is not None:
228-
connection.autocommit = True
229-
return connection
230-
231-
232-
def connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any:
233-
"""Initialize connection to database."""
234-
logger.info("Connecting to SQLite storage")
235-
# make sure the connection will have known state
236-
# even if SQLite is not alive
237-
connection = None
238-
try:
239-
connection = sqlite3.connect(database=config.db_path)
240-
except sqlite3.Error as e:
241-
if connection is not None:
242-
connection.close()
243-
logger.exception("Error initializing SQLite cache:\n%s", e)
244-
raise
245-
connection.autocommit = True
246-
return connection
247-
248-
249213
def init_tables(connection: Any) -> None:
250214
"""Initialize tables used by quota limiter."""
251215
logger.info("Initializing tables for quota limiter")

0 commit comments

Comments
 (0)