#6064 closed Uncategorized (fixed)
Allow database connection initialization commands
Reported by: | Jacob | Owned by: | floguy |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | telenieko@…, floguy@…, John Shaffer, sam@…, feteanumarius@…, contact@…, greg@…, michael.greene@…, matthew.russell@…, hank.gay@…, dvanliere@…, dev@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
There's sometimes a need to pass specific SQL that'll get executed at the beginning of every database connection (for just one example see #1051).
This could be in the form of a setting, a signal, or perhaps some other mechanism.
Attachments (4)
Change History (32)
comment:1 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 17 years ago
Cc: | added |
---|
comment:4 by , 17 years ago
Cc: | added |
---|---|
Has patch: | set |
Needs tests: | set |
Wrote a patch to fix this, with documentation. It should be fairly straighforward:
Added a CONNECTION_INIT_SQL setting, which is a tuple of strings to be executed on connection initialization.
For example,
CONNECTION_INIT_SQL = ( 'SET search_path TO django_application_schema', )
would accomplish the same thing as ticket:1051
Ran test suite and there were no regressions, but tests for this new feature are needed.
by , 17 years ago
Patch to add CONNECTION_INIT_SQL setting and use it on connection initialization.
comment:5 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 17 years ago
Needs tests: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:7 by , 17 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Ready for checkin → Accepted |
Looking at this, I *REALLY* don't like having SQL in the settings file. So I think that a connection-created signal is the right way to do this, not a setting.
comment:8 by , 17 years ago
I have added a new patch which uses signals instead of a setting. I think you're right, too, that the signal method is better. It allows for more flexibility while not cluttering up the settings file. The only reason that I originally implemented the settings file originally was due to concerns about performance.
One thing: I wasn't sure where to put the new signal. It doesn't seem to fit where any of the other signals are defined, so I created django.db.backends.signals with this one signal in it.
by , 17 years ago
Attachment: | 6064-using-signals.diff added |
---|
Changed the implementation method to emit a signal instead of execute a tuple from the settings.
comment:9 by , 17 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
comment:10 by , 17 years ago
Cc: | added |
---|
comment:11 by , 17 years ago
Cc: | added |
---|
comment:12 by , 17 years ago
Cc: | added |
---|
For anyone looking for a quick and dirty solution you ca define your table like this: db_table = '"django"."company"'. This will fool the quote function to think that your table is properly quoted. This also means that you have to specify the schema for each table manually.
comment:14 by , 16 years ago
Cc: | added |
---|
comment:15 by , 16 years ago
Cc: | added |
---|
by , 16 years ago
Attachment: | 6064-using-signals-2.diff added |
---|
Updated patch to apply against trunk as of r9477.
comment:16 by , 16 years ago
I just updated this patch to work with the current trunk, since it hadn't been touched for a year and had gotten rather stale. I also fixed some bugs that were causing connection_created
signals to be sent on every cursor-creation for some back ends, rather than only when a new database connection has really been made.
Unfortunately, fixing that bug broke the test for sqlite3, since in-memory sqlite3 databases (of the sort used by the tests) are never actually closed, and thus cannot be reopened by the tests. So I've had to disable the test altogether for sqlite3, unless someone can come up with a definitely-safe way to open a second temporary database somehow without clobbering the old one. Seems like more trouble than it's worth.
It occurs to me that a separate cursor_created
signal might actually be useful, especially if it were passed a reference to the new cursor, but, well, one thing at a time.
comment:17 by , 16 years ago
Cc: | added |
---|
comment:19 by , 16 years ago
milestone: | → 1.1 |
---|
by , 16 years ago
Attachment: | 6064-using-signals-3.diff added |
---|
comment:20 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:21 by , 15 years ago
Does this address the case where I'd like to do some work with the connection? It seems like the sender should be the newly created connection, not the class.
comment:22 by , 15 years ago
Cc: | removed |
---|
comment:23 by , 15 years ago
Cc: | added |
---|---|
Resolution: | fixed |
Status: | closed → reopened |
I am reopening this ticket because the accepted patch does not appear to address the same issue as the original patch. The original patch performed initialization work on each connection as it was established. The accepted patch fires a signal when a connection has been created, but the sender is the class, not the newly created connection, so how can the desired initialization be performed? If there is a consensus that the sender should be changed to the new connection, I am happy to submit a patch that does that.
comment:24 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Please ask questions like this on an appropriate mailing list (django-user in this case) instead of resurrecting old tickets.
def set_schema(sender, **kwargs): from django.db import connection cursor = connection.cursor() cursor.execute("INITIALIZE SOME THINGS VIA SQL") if django.VERSION >= (1,1): from django.db.backends.signals import connection_created connection_created.connect(set_schema)
comment:25 by , 14 years ago
i never found out where at what place i had to register the signal handler.
the best solution i found for the schema problem was to set a connect_query in pgbouncer.
comment:26 by , 14 years ago
Cc: | added |
---|
comment:27 by , 14 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
Severity: | → Normal |
Type: | → Uncategorized |
cc'ing me