Skip to content

Commit 3142054

Browse files
committed
feat(db): added config for postgres support
1 parent e23da26 commit 3142054

3 files changed

Lines changed: 108 additions & 13 deletions

File tree

config.py

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,59 @@ class ServerConfig:
7373
def from_dict(cls, config_dict: dict):
7474
return cls(**config_dict)
7575

76+
@dataclass
77+
class SQLiteConfig:
78+
"""Configuration for SQLite database."""
79+
db_path: str
80+
81+
@classmethod
82+
def from_dict(cls, config_dict: dict):
83+
"""Create a PostgresConfig instance from dictionary.
84+
85+
Args:
86+
config_dict (dict): Dictionary containing the database configuration.
87+
88+
Returns:
89+
DatabaseConfig: Instance of DatabaseConfig with provided configuration.
90+
"""
91+
return cls(db_path=config_dict['db_path'])
92+
93+
94+
@dataclass
95+
class PostgresConfig:
96+
"""Configuration for PostgreSQL database."""
97+
host: str
98+
port: int
99+
user: str
100+
password: str
101+
db: str
102+
@classmethod
103+
def from_dict(cls, config_dict: dict):
104+
"""Create a PostgresConfig instance from dictionary.
105+
106+
Args:
107+
config_dict (dict): Dictionary containing the database configuration.
108+
109+
Returns:
110+
DatabaseConfig: Instance of DatabaseConfig with provided configuration.
111+
"""
112+
return cls(host=config_dict['host'],
113+
port=config_dict['port'],
114+
user=config_dict['user'],
115+
password=config_dict['password'],
116+
db=config_dict['db'])
117+
118+
76119
@dataclass
77120
class DatabaseConfig:
78121
"""Dataclass to store the configuration for the database, including the file path."""
79-
db_file: str
122+
db_type: str
80123
cache_translation: bool
81124
use_cached_translation: bool
82125
use_latest_records: bool
83126
init_latest_records: int
127+
sqlite_config: SQLiteConfig
128+
postgres_config: PostgresConfig
84129

85130
@classmethod
86131
def from_dict(cls, config_dict: dict):
@@ -92,7 +137,18 @@ def from_dict(cls, config_dict: dict):
92137
Returns:
93138
DatabaseConfig: Instance of DatabaseConfig with provided configuration.
94139
"""
95-
return cls(**config_dict)
140+
141+
print(config_dict)
142+
return cls(
143+
db_type=config_dict['db_type'],
144+
cache_translation=config_dict['cache_translation'],
145+
use_cached_translation=config_dict['use_cached_translation'],
146+
use_latest_records=config_dict['use_latest_records'],
147+
init_latest_records=config_dict['init_latest_records'],
148+
sqlite_config = SQLiteConfig.from_dict(config_dict['sqlite_config']),
149+
postgres_config = PostgresConfig.from_dict(config_dict['postgres_config'])
150+
)
151+
96152

97153
@dataclass
98154
class HistoryConfig:
@@ -202,11 +258,21 @@ def from_args(cls, args):
202258
"use_latest_history": args.use_latest_history
203259
}),
204260
database_config=DatabaseConfig.from_dict({
205-
"db_file": args.db_file,
261+
"db_type": args.db_type,
206262
"cache_translation": args.cache_translation,
207263
"use_cached_translation": args.use_cached_translation,
208264
"use_latest_records": args.use_latest_records,
209-
"init_latest_records": args.init_latest_records
265+
"init_latest_records": args.init_latest_records,
266+
"sqlite_config": {
267+
"db_path": args.sqlite_db_path
268+
},
269+
"postgres_config": {
270+
"host": args.postgres_host,
271+
"port": args.postgres_port,
272+
"user": args.postgres_user,
273+
"password": args.postgres_password,
274+
"db": args.postgres_db
275+
}
210276
}),
211277
logging_config=LoggingConfig.from_dict({
212278
"log_file": args.log_file,
@@ -255,11 +321,21 @@ def parse_args():
255321
parser.add_argument("--use_latest_history", action="store_true", help="Use latest history records")
256322

257323
# Database Config
258-
parser.add_argument("--db_file", type=str, default="translated_texts.db", help="Path to the database file")
324+
parser.add_argument("--db_type", type=str, default="sqlite", help="Database type to use")
259325
parser.add_argument("--cache_translation", action="store_true", help="Enable translation caching")
260326
parser.add_argument("--use_cached_translation", action="store_true", help="Use cached translations if available")
261327
parser.add_argument("--use_latest_records", action="store_true", help="Use latest database records")
262328
parser.add_argument("--init_latest_records", type=int, default=20, help="Number of initial latest records")
329+
330+
# PostgreSQL Configs
331+
parser.add_argument("--postgres_host", type=str, default="localhost", help="PostgreSQL server host")
332+
parser.add_argument("--postgres_port", type=int, default=5432, help="PostgreSQL server port")
333+
parser.add_argument("--postgres_user", type=str, help="PostgreSQL username")
334+
parser.add_argument("--postgres_password", type=str, help="PostgreSQL password")
335+
parser.add_argument("--postgres_db", type=str, help="PostgreSQL database name")
336+
337+
# SQLite Config
338+
parser.add_argument("--sqlite_db_path", type=str, default="translated_texts.db", help="Path to the SQLite database file")
263339

264340
# Logging Config
265341
parser.add_argument("--log_file", type=str, help="Log file path")

db.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import sqlite3
2-
from dataclasses import dataclass
2+
import psycopg
3+
from dataclasses import dataclass, field
4+
from typing import Union
5+
from config import DatabaseConfig
36

47
@dataclass
58
class DB:
6-
connector: sqlite3.Connection
7-
cursor: sqlite3.Cursor
9+
connector: Union[sqlite3.Connection]
10+
cursor: Union[sqlite3.Cursor, psycopg.Cursor]
11+
db_config: DatabaseConfig = field(init=False)
812

913
@classmethod
10-
def from_file(cls, db_file: str):
11-
connector = sqlite3.connect(db_file)
14+
def from_config(cls, db_config: DatabaseConfig):
15+
cls.db_config = db_config
16+
match cls.db_config.db_type:
17+
case "sqlite":
18+
connector = sqlite3.connect(db_config.sqlite_config.db_path)
19+
case "postgres":
20+
connector = psycopg.connect("host=%s port=%d dbname=%s user=%s password=%s".format(cls.db_config.postgres_config.host,
21+
cls.db_config.postgres_config.port,
22+
cls.db_config.postgres_config.db,
23+
cls.db_config.postgres_config.user,
24+
cls.db_config.postgres_config.password))
1225
cursor = connector.cursor()
1326

1427
if "translations" not in cls.get_table_list(cursor):
@@ -17,8 +30,14 @@ def from_file(cls, db_file: str):
1730
return cls(connector, cursor)
1831

1932
@classmethod
20-
def get_table_list(cls, cursor: sqlite3.Cursor) -> list:
21-
cursor.execute("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'")
33+
def get_table_list(cls, cursor: Union[sqlite3.Cursor, psycopg.Cursor]) -> list:
34+
match cls.db_config.db_type:
35+
case "sqlite":
36+
query = "SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'"
37+
case "postgres":
38+
query = "SELECT * FROM information_schema.tables"
39+
40+
cursor.execute(query)
2241
return [t[0] for t in cursor.fetchall()]
2342

2443
@classmethod

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
else:
1515
config = Config.from_args(args)
1616
client = LLMClient.from_config(config)
17-
db = DB.from_file(config.database_config.db_file)
17+
db = DB.from_config(config.database_config)
1818

1919
@proxy_server.get("/translate", response_class=PlainTextResponse)
2020
async def translation_handler(

0 commit comments

Comments
 (0)