Django

Code

Ticket #1051 (closed: duplicate)

Opened 3 years ago

Last modified 1 year ago

Add support to set search_path in PostgreSQL

Reported by: Mao Xizeng <mao.xizeng@gmail.com> Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords: PostgreSQL Schemas
Cc: landonf@opendarwin.org, RahmCoff@Radio1190.org, david@dawninglight.net, sam@robots.org.uk Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

Sometimes PostgreSQL schemas feature is very helpful to developers: we can use this feature to maintain multiple versions of databases, or integrate external PostgreSQL databases by cross-schema view without making database organization confusing. So it seems necessary to add PostgreSQL schemas support to django:)

I implemented this feature over only four lines. First, one additional field, "DATABASE_SCHEMAS" in django setting, is used to control PostgreSQL schemas setting, default is blank; second, when initializaing database cursor, three hook codes will double check DATABASE_ENGINE and DATABASE_SCHEMAS, if DATABASE_SCHEMAS is not blank and DATABASE_ENGINE is postgresql, django will set postgresql to appropriate schemas search order.

For PostgreSQL schemas detail, please refer to this url: http://www.postgresql.org/docs/8.0/static/ddl-schemas.html

Attachments

django-postgresql-schemas.diff (1.9 kB) - added by Mao Xizeng <mao.xizeng@gmail.com> on 12/12/05 21:28:01.
django-postgresql-schemas-v2.diff (1.3 kB) - added by Landon Fuller <landonf@opendarwin.org> on 06/19/06 01:13:33.
PostgreSQL SCHEMA support (Updated)
django_pgsql_schemas_rev4201.patch (2.0 kB) - added by telenieko@telenieko.com on 12/14/06 03:33:16.
Added the same changes on psycopg2
pg_schemas_full_rev4347.diff (2.7 kB) - added by Marc Fargas <telenieko@telenieko.com> on 01/18/07 03:50:36.
All In One, psycop, psycop2 and documentation (settings.txt)
1051.diff (2.4 kB) - added by Marc Fargas <telenieko@telenieko.com> on 05/21/07 08:27:58.
Updated to apply with latest trunk
ticket_1051_rev6098.diff (3.7 kB) - added by roppert <robert@blogg.se> on 09/11/07 10:48:05.
Patch to apply cleanly against trunk (rev 6098)
ticket_1051_rev6669.diff (4.4 kB) - added by roppert on 11/13/07 06:27:51.
Patche done against rev 6669

Change History

12/12/05 21:28:01 changed by Mao Xizeng <mao.xizeng@gmail.com>

  • attachment django-postgresql-schemas.diff added.

01/14/06 18:20:15 changed by adrian

See also [1208].

06/19/06 01:12:50 changed by Landon Fuller <landonf@opendarwin.org>

  • cc set to landonf@opendarwin.org.

I updated this patch for trunk. It's still only four lines, and it'd be great to get it in.

06/19/06 01:13:33 changed by Landon Fuller <landonf@opendarwin.org>

  • attachment django-postgresql-schemas-v2.diff added.

PostgreSQL SCHEMA support (Updated)

12/13/06 11:05:52 changed by telenieko@telenieko.com

adrian: Maybe you were refering to #1208, not [1208] ;)

What's about this patch? It's stopped since june.

Cheers, Marc.

12/14/06 03:33:16 changed by telenieko@telenieko.com

  • attachment django_pgsql_schemas_rev4201.patch added.

Added the same changes on psycopg2

12/14/06 03:34:41 changed by telenieko@telenieko.com

Except it does not issue a CREATE SCHEMA (which is fine) it works perfectly. Please consider applying the patch to trunk.

01/17/07 22:08:51 changed by jacob

  • needs_better_patch set to 1.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests set to 1.
  • needs_docs set to 1.

01/18/07 03:50:36 changed by Marc Fargas <telenieko@telenieko.com>

  • attachment pg_schemas_full_rev4347.diff added.

All In One, psycop, psycop2 and documentation (settings.txt)

01/18/07 03:51:48 changed by Marc Fargas <telenieko@telenieko.com>

  • needs_docs deleted.

Documentation added on latest patch.

01/18/07 11:36:53 changed by Marc Fargas <telenieko@telenieko.com>

On thing missing on this patch is the unit test, one way to achieve that would be simply setting the search_path variable and if no error raises you're done (it's up to postgresql to complain if the schema does not exist or something).

Another way would be "set search_path, create table, do something on table, set search_path to public, try again against table" but It's a test that should be done by postgresql devs, not us ;) We should trust that if search_path is set without trouble we can assume it went fine.

So the unittest for that should simply:

  • check that the database backend is psycopg or psycopg2 << otherwise all other backends would fail
  • try to set search_path to 'public'
  • succeed!!

Is this okay that way?

PS: maybe the default value for DATABASE_SCHEMAS should be "public" so the test would work even if the setting is not defined on settings.py

01/18/07 21:49:33 changed by anonymous

  • cc changed from landonf@opendarwin.org to landonf@opendarwin.org, RahmCoff@Radio1190.org.

05/04/07 10:03:20 changed by david@dawninglight.net

  • cc changed from landonf@opendarwin.org, RahmCoff@Radio1190.org to landonf@opendarwin.org, RahmCoff@Radio1190.org, david@dawninglight.net.

Since the other bugs have been marked duplicate of this one, I'll post here.

My ideal use of PostgreSQL schemas would be to have each app live in its own schema by default, but with the option of specifying a different one (i.e., Model._meta.db_schema).

These patches provide the ability to have models in separate schemas, but do not change anything else WRT SQL generation. Until either the QuerySet? refactor or the multi-db branch hits trunk, the only way to allow per-app schemas by default in postgres is to change the quote_name functions for the 2 pg wrappers (in base.py). You would need to check for a '.' and quote appropriately. This really isn't compatible with how the position taken where django guarantees that it will quote everything sent to the db literally. However, there's really no other way to split out the table name from the schema name.

If the decision is made to go with the concept of just adding simple schema support (a la DATABASE_SCHEMAS), the setting should be a tuple, and the logic in the pg wrappers (base.py) should be:

        if settings.DATABASE_SCHEMAS:
            cursor.execute("SET search_path TO %s;" % ','.join(settings.DATABASE_SCHEMAS))

05/21/07 08:27:58 changed by Marc Fargas <telenieko@telenieko.com>

  • attachment 1051.diff added.

Updated to apply with latest trunk

05/21/07 08:35:10 changed by Marc Fargas <telenieko@telenieko.com>

Updated patch to apply cleanly with latest trunk.

In reply to david@dawninglight.net : There's no way, actually to set "application wide preferences" so setting a schema for an application could get a bit weird, maybe some APPLICATION_SCHEMAS = {'myapp': 'myschema'} setting or something like that, but this is out of the scope if this ticket which attemps to provide someway to use schemas in pgsql. So you should rather fill a new ticket and state on it's description very very very clearly that it's not a duplicate of #1051 as it does not attempt the same.

On the other side, any volunteer to write the tests for this so we can stop patching by hand on every svn up ? ;)))

05/23/07 06:06:34 changed by Birju Prajapati

Why not have the ability (in settings) to specify a regular expression of what to unquote? i.e. DATABASE_UNQUOTE_REGEXP = '\.' Or even completely switch quoting off? i.e DATABASE_QUOTE_NAMES = False

05/23/07 08:56:41 changed by Birju Prajapati

Issues with the search path route:
1. When there are two tables of the same name in different schemas you wont be able to access the table later in the search path.
2. If you have many apps in one project, importing a model that is in a separate app into the 'current' one could mean that the entire search path chain may have to be searched.
I'm not a fan of the project-wide search path as it seems redundant: you can set up a user in Postgres that Django uses and have that user set up using

'ALTER USER BLAH SET SEARCH PATH = ....'

What are the issues in using the db_table meta options by explicitly specifying 'schema.table' and not quoting the first '.' wrt to some setting?

05/23/07 09:12:35 changed by Marc Fargas <telenieko@telenieko.com>

  • needs_better_patch deleted.
  • summary changed from [patch] Add support for PostgreSQL Schemas to Add support to set search_path in PostgreSQL.

Supporting schemas on per application/model basis is out of the scope of this ticket. This ticket was originally created to support modifying the search_path to have a **whole project** inside a different schema than public. And as it states "would be useful to developers" i.e. If you are working in multiple projects you can have them in separate schemas without needing different databases and/or users.

Supporting a more granular control of schemas like the per-app idea should be filled as a different ticket.

To get things more clear I'm changing the summary of the ticket to "Support setting search_path on postgresql" which is exactly what the patch does and what the original submiter asked for.

Also removing "Patch needs improvement" as the patch is now completed, only remaining "needs tests"

Side note: Recalling again for somebody with deep knowledge on tests to take a look at the patch and try to write them :)

07/17/07 19:35:43 changed by anonymous

  • cc changed from landonf@opendarwin.org, RahmCoff@Radio1190.org, david@dawninglight.net to landonf@opendarwin.org, RahmCoff@Radio1190.org, david@dawninglight.net, sam@robots.org.uk.

08/28/07 10:49:18 changed by j.states@gmail.com

Will Schemas be supported in Django? I svn'd to version 6023 and it killed my development server - I have not tested in Production. I had to rollback to version 6009. Someone please make a decision on this and add it to the svn.

09/11/07 10:48:05 changed by roppert <robert@blogg.se>

  • attachment ticket_1051_rev6098.diff added.

Patch to apply cleanly against trunk (rev 6098)

11/13/07 06:27:51 changed by roppert

  • attachment ticket_1051_rev6669.diff added.

Patche done against rev 6669

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

  • status changed from new to closed.
  • resolution set to duplicate.

I've created #6064 to keep track of a general-purpose way of fixing this problem, so I'm marking this as a duplicate.


Add/Change #1051 (Add support to set search_path in PostgreSQL)




Change Properties
Action