Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/__init__.py
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | def connect(cls, *args, **kwargs): | ||||
Accepts all arguments of psycopg2.connect; only some specific | Accepts all arguments of psycopg2.connect; only some specific | ||||
possibilities are reported below. | possibilities are reported below. | ||||
Args: | Args: | ||||
connstring: libpq2 connection string | connstring: libpq2 connection string | ||||
""" | """ | ||||
conn = psycopg2.connect(*args, **kwargs) | conn = psycopg2.connect(*args, **kwargs) | ||||
cls.adapt_conn(conn) | |||||
return cls(conn) | return cls(conn) | ||||
@classmethod | @classmethod | ||||
def from_pool(cls, pool): | def from_pool(cls, pool): | ||||
conn = pool.getconn() | conn = pool.getconn() | ||||
cls.adapt_conn(conn) | |||||
return cls(conn, pool=pool) | return cls(conn, pool=pool) | ||||
def __init__(self, conn, pool=None): | def __init__(self, conn, pool=None): | ||||
"""create a DB proxy | """create a DB proxy | ||||
Args: | Args: | ||||
conn: psycopg2 connection to the SWH DB | conn: psycopg2 connection to the SWH DB | ||||
pool: psycopg2 pool of connections | pool: psycopg2 pool of connections | ||||
""" | """ | ||||
ardumont: I'm unsure of that factorisation (because i'm not so sure of the workflow around those calls… | |||||
Done Inline ActionsThat will only be an issue when a connection object is created directly, instead of using one of these classmethods. grep "psycopg2\.conn" ../*/swh/ -r shows only two results outside swh.core: the vault (before D1108) and the swh.journal.checker. Neither use BaseDb.
Which ones? vlorentz: That will only be an issue when a connection object is created directly, instead of using one… | |||||
self.adapt_conn(conn) | |||||
self.conn = conn | self.conn = conn | ||||
self.pool = pool | self.pool = pool | ||||
def __del__(self): | def __del__(self): | ||||
if self.pool: | if self.pool: | ||||
self.pool.putconn(self.conn) | self.pool.putconn(self.conn) | ||||
def cursor(self, cur_arg=None): | def cursor(self, cur_arg=None): | ||||
Show All 26 Lines | def transaction(self): | ||||
self.conn.rollback() | self.conn.rollback() | ||||
raise | raise | ||||
def copy_to(self, items, tblname, columns, | def copy_to(self, items, tblname, columns, | ||||
cur=None, item_cb=None, default_values={}): | cur=None, item_cb=None, default_values={}): | ||||
"""Copy items' entries to table tblname with columns information. | """Copy items' entries to table tblname with columns information. | ||||
Args: | Args: | ||||
items (dict): dictionary of data to copy over tblname. | items (List[dict]): dictionaries of data to copy over tblname. | ||||
tblname (str): destination table's name. | tblname (str): destination table's name. | ||||
columns ([str]): keys to access data in items and also the | columns ([str]): keys to access data in items and also the | ||||
column names in the destination table. | column names in the destination table. | ||||
default_values (dict): dictionnary of default values to use when | default_values (dict): dictionnary of default values to use when | ||||
inserting entried int the tblname table. | inserting entried int the tblname table. | ||||
cur: a db cursor; if not given, a new cursor will be created. | cur: a db cursor; if not given, a new cursor will be created. | ||||
item_cb (fn): optional function to apply to items's entry. | item_cb (fn): optional function to apply to items's entry. | ||||
""" | """ | ||||
Show All 29 Lines |
I'm unsure of that factorisation (because i'm not so sure of the workflow around those calls: from_pool, connect).
What i'm sure though is that it could be cause of regression.
I'd feel safer if we added tests around those cases.
Reading swh.storage.{storage,db} and scheduler's, the initialization sounds fine with this change but still...