Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit c1ca9e0

Browse files
authored
Allow creating database connection wrapper without a singleton (#1120)
* Allow creating database connection wrapper without a singleton The idea is that one would be able to pass the `_no_singleton` boolean flag to the class and if so, the class would be returned without creating a singleton. This is handy for testing, since it would allow for several connections to be open in parallel to different database paths, which indeed is a testing scenario. ``` >>> from codegate.db.connection import DbCodeGate >>> dbc = DbCodeGate(_no_singleton=True) >>> print(dbc) <codegate.db.connection.DbCodeGate object at 0x7f9a0f067650> >>> dbc2 = DbCodeGate(_no_singleton=True) >>> print(dbc2) <codegate.db.connection.DbCodeGate object at 0x7f9a0daad1f0> >>> dbc3 = DbCodeGate() >>> print(dbc3) <codegate.db.connection.DbCodeGate object at 0x7f9a0f065190> >>> dbc4 = DbCodeGate() >>> print(dbc4) <codegate.db.connection.DbCodeGate object at 0x7f9a0f065190> ``` Signed-off-by: Juan Antonio Osorio <[email protected]> * Allow passing args and kwargs to db subclasses Signed-off-by: Juan Antonio Osorio <[email protected]> --------- Signed-off-by: Juan Antonio Osorio <[email protected]>
1 parent d35151d commit c1ca9e0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/codegate/db/connection.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ class DbCodeGate:
6161
_instance = None
6262

6363
def __new__(cls, *args, **kwargs):
64+
# The _no_singleton flag is used to create a new instance of the class
65+
# It should only be used for testing
66+
if "_no_singleton" in kwargs and kwargs["_no_singleton"]:
67+
kwargs.pop("_no_singleton")
68+
return super().__new__(cls, *args, **kwargs)
69+
6470
if cls._instance is None:
6571
cls._instance = super().__new__(cls)
6672
return cls._instance
6773

68-
def __init__(self, sqlite_path: Optional[str] = None):
74+
def __init__(self, sqlite_path: Optional[str] = None, **kwargs):
6975
if not hasattr(self, "_initialized"):
7076
# Ensure __init__ is only executed once
7177
self._initialized = True
@@ -91,8 +97,8 @@ def does_db_exist(self):
9197

9298

9399
class DbRecorder(DbCodeGate):
94-
def __init__(self, sqlite_path: Optional[str] = None):
95-
super().__init__(sqlite_path)
100+
def __init__(self, sqlite_path: Optional[str] = None, *args, **kwargs):
101+
super().__init__(sqlite_path, *args, **kwargs)
96102

97103
async def _execute_update_pydantic_model(
98104
self, model: BaseModel, sql_command: TextClause, should_raise: bool = False
@@ -519,8 +525,8 @@ async def add_mux(self, mux: MuxRule) -> MuxRule:
519525

520526

521527
class DbReader(DbCodeGate):
522-
def __init__(self, sqlite_path: Optional[str] = None):
523-
super().__init__(sqlite_path)
528+
def __init__(self, sqlite_path: Optional[str] = None, *args, **kwargs):
529+
super().__init__(sqlite_path, *args, **kwargs)
524530

525531
async def _dump_result_to_pydantic_model(
526532
self, model_type: Type[BaseModel], result: CursorResult

0 commit comments

Comments
 (0)