Opened 16 years ago

Closed 15 years ago

Last modified 13 years ago

#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)

6064.diff (5.6 KB ) - added by floguy 16 years ago.
Patch to add CONNECTION_INIT_SQL setting and use it on connection initialization.
6064-using-signals.diff (7.8 KB ) - added by floguy 16 years ago.
Changed the implementation method to emit a signal instead of execute a tuple from the settings.
6064-using-signals-2.diff (6.1 KB ) - added by mdh 15 years ago.
Updated patch to apply against trunk as of r9477.
6064-using-signals-3.diff (6.4 KB ) - added by jbronn 15 years ago.

Download all attachments as: .zip

Change History (32)

comment:1 by Jacob, 16 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Marc Fargas, 16 years ago

Cc: telenieko@… added

cc'ing me

comment:3 by Jacob, 16 years ago

See also #3460.

comment:4 by floguy, 16 years ago

Cc: floguy@… 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 floguy, 16 years ago

Attachment: 6064.diff added

Patch to add CONNECTION_INIT_SQL setting and use it on connection initialization.

comment:5 by floguy, 16 years ago

Owner: changed from nobody to floguy
Status: newassigned

comment:6 by Simon G <dev@…>, 16 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:7 by Jacob, 16 years ago

Needs tests: set
Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

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 floguy, 16 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 floguy, 16 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 anonymous, 16 years ago

Needs tests: unset
Patch needs improvement: unset

comment:10 by John Shaffer, 16 years ago

Cc: John Shaffer added

comment:11 by anonymous, 16 years ago

Cc: sam@… added

comment:12 by MariusBoo, 16 years ago

Cc: feteanumarius@… 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:13 by jfsimon_fr, 16 years ago

Cc: contact@… added

cc'ing me too (hello !)

comment:14 by Greg Turner, 16 years ago

Cc: greg@… added

comment:15 by dan, 16 years ago

Cc: dan added

by mdh, 15 years ago

Attachment: 6064-using-signals-2.diff added

Updated patch to apply against trunk as of r9477.

comment:16 by mdh, 15 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 euphoria, 15 years ago

Cc: michael.greene@… added

comment:18 by mattrussell, 15 years ago

Cc: matthew.russell@… added

cc'ing

comment:19 by jbronn, 15 years ago

milestone: 1.1

by jbronn, 15 years ago

Attachment: 6064-using-signals-3.diff added

comment:20 by jbronn, 15 years ago

Resolution: fixed
Status: assignedclosed

(In [10182]) Fixed #6064 -- Added the connection_created signal for when a database connection is created.

comment:21 by hank.gay@…, 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 anonymous, 15 years ago

Cc: dan removed

comment:23 by hank.gay@…, 15 years ago

Cc: hank.gay@… added
Resolution: fixed
Status: closedreopened

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 euphoria, 15 years ago

Resolution: fixed
Status: reopenedclosed

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 Till Backhaus, 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 drdee, 14 years ago

Cc: dvanliere@… added

comment:27 by Brillgen Developers, 13 years ago

Cc: dev@… added
Easy pickings: unset
Severity: Normal
Type: Uncategorized

comment:28 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top