Django

Code

Ticket #6064 (closed: fixed)

Opened 2 years ago

Last modified 3 months ago

Allow database connection initialization commands

Reported by: jacob Assigned to: floguy
Milestone: 1.1 Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: telenieko@telenieko.com, dan90, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com, michael.greene@gmail.com, matthew.russell@sanoodi.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

6064.diff (5.6 kB) - added by floguy on 12/01/07 18:27:03.
Patch to add CONNECTION_INIT_SQL setting and use it on connection initialization.
6064-using-signals.diff (7.8 kB) - added by floguy on 12/04/07 14:51:08.
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 on 11/16/08 18:54:43.
Updated patch to apply against trunk as of r9477.
6064-using-signals-3.diff (6.4 kB) - added by jbronn on 03/29/09 17:16:49.

Change History

11/30/07 15:16:13 changed by jacob

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

12/01/07 07:36:24 changed by telenieko

  • cc set to telenieko@telenieko.com.

cc'ing me

12/01/07 08:45:46 changed by jacob

See also #3460.

12/01/07 18:25:50 changed by floguy

  • cc changed from telenieko@telenieko.com to telenieko@telenieko.com, floguy@gmail.com.
  • has_patch set to 1.
  • needs_tests set to 1.

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.

12/01/07 18:27:03 changed by floguy

  • attachment 6064.diff added.

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

12/01/07 19:04:32 changed by floguy

  • owner changed from nobody to floguy.
  • status changed from new to assigned.

12/01/07 22:04:23 changed by Simon G <dev@simon.net.nz>

  • needs_tests deleted.
  • stage changed from Accepted to Ready for checkin.

12/04/07 13:55:39 changed by jacob

  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • stage changed from Ready for checkin to 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.

12/04/07 14:50:49 changed by floguy

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.

12/04/07 14:51:08 changed by floguy

  • attachment 6064-using-signals.diff added.

Changed the implementation method to emit a signal instead of execute a tuple from the settings.

12/31/07 00:07:13 changed by anonymous

  • needs_better_patch deleted.
  • needs_tests deleted.

12/31/07 00:09:57 changed by jshaffer

  • cc changed from telenieko@telenieko.com, floguy@gmail.com to telenieko@telenieko.com, floguy@gmail.com, jshaffer.

01/21/08 06:04:43 changed by anonymous

  • cc changed from telenieko@telenieko.com, floguy@gmail.com, jshaffer to telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk.

03/02/08 02:56:30 changed by MariusBoo

  • cc changed from telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk to telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com.

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.

05/28/08 01:41:44 changed by jfsimon_fr

  • cc changed from telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com to telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr.

cc'ing me too (hello !)

07/02/08 03:22:41 changed by cogat

  • cc changed from telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr to telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com.

07/02/08 07:52:06 changed by dan90

  • cc changed from telenieko@telenieko.com, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com to telenieko@telenieko.com, dan90, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com.

11/16/08 18:54:43 changed by mdh

  • attachment 6064-using-signals-2.diff added.

Updated patch to apply against trunk as of r9477.

11/16/08 19:01:14 changed by mdh

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.

01/09/09 02:53:16 changed by euphoria

  • cc changed from telenieko@telenieko.com, dan90, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com to telenieko@telenieko.com, dan90, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com, michael.greene@gmail.com.

01/22/09 15:25:17 changed by mattrussell

  • cc changed from telenieko@telenieko.com, dan90, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com, michael.greene@gmail.com to telenieko@telenieko.com, dan90, floguy@gmail.com, jshaffer, sam@robots.org.uk, feteanumarius@yahoo.com, contact@jfsimon.fr, greg@gregturner.com, michael.greene@gmail.com, matthew.russell@sanoodi.com.

cc'ing

03/09/09 18:48:41 changed by jbronn

  • milestone set to 1.1.

03/29/09 17:16:49 changed by jbronn

  • attachment 6064-using-signals-3.diff added.

03/29/09 18:15:58 changed by jbronn

  • status changed from assigned to closed.
  • resolution set to fixed.

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


Add/Change #6064 (Allow database connection initialization commands)




Change Properties
Action