You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
I need to issue a set of SQL commands at the start of each connection, e.g. to set up the search path. Currently it looks like this can only be done by subclassing Database and completely reimplementing its connection method to return a subclass of Connection that overrides __aenter__. Would it be acceptable to add something akin to Django's connection_created signal? Syntax might look like
For reference, here is my subclass implementation:
fromdatabases.coreimportConnectionasBasicConnection, DatabaseasBasicDatabaseclassConnection(BasicConnection):
def__init__(self, backend, init_fns) ->None:
super().__init__(backend)
self.init_fns=init_fnsasyncdef__aenter__(self):
c=awaitsuper().__aenter__()
forinit_fninself.init_fns:
awaitinit_fn(c)
returncclassDatabase(BasicDatabase):
"""Adds a connection_created decorator to encode/databases."""def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._init_fns= []
defconnection_created(self, async_callback_fn):
self._init_fns.append(async_callback_fn)
defconnection(self):
"""This method copies the underlying implementation wholesale. Calling it with super is unfortunately not an option as it has a hard-coded reference to its own Connection implementation."""ifself._global_connectionisnotNone:
returnself._global_connectiontry:
returnself._connection_context.get()
exceptLookupError:
connection=Connection(self._backend, self._init_fns)
self._connection_context.set(connection)
returnconnection
I need to issue a set of SQL commands at the start of each connection, e.g. to set up the search path. Currently it looks like this can only be done by subclassing
Databaseand completely reimplementing itsconnectionmethod to return a subclass ofConnectionthat overrides__aenter__. Would it be acceptable to add something akin to Django's connection_created signal? Syntax might look like