postgresql_with_schemas: now will properly create indexes.
authorgfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Tue, 9 Feb 2010 01:00:15 +0000 (01:00 +0000)
committergfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Tue, 9 Feb 2010 01:00:15 +0000 (01:00 +0000)
Django tries to derive index names from the db_table names and column
names; but the default Postgres backend, being non-schema-aware, was
trying to use dotted names for indexes (and that's wrong).

This patch corrects the issue by overriding the index-naming code and
removing the dotted names.

git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/branches/eg-schema-experiment@771 6d9bc8c9-1ec2-4278-b937-99fde70a366f

conifer/evergreen/backends/postgresql_with_schemas/base.py
conifer/evergreen/backends/postgresql_with_schemas/creation.py [new file with mode: 0644]

index e9ff1b8..f387b1b 100755 (executable)
@@ -8,11 +8,13 @@ from django.db.backends.postgresql_psycopg2.base import (DatabaseFeatures,
 from django.conf import settings
 from .operations import DatabaseOperations
 from .introspection import DatabaseIntrospection
+from .creation import DatabaseCreation
 
 class DatabaseWrapper(DatabaseWrapper):
     def __init__(self, *args, **kwargs):
         super(DatabaseWrapper, self).__init__(*args, **kwargs)
         self.ops = DatabaseOperations()
+        self.creation = DatabaseCreation(self)
         self.introspection = DatabaseIntrospection(self)
 
     def _cursor(self):
diff --git a/conifer/evergreen/backends/postgresql_with_schemas/creation.py b/conifer/evergreen/backends/postgresql_with_schemas/creation.py
new file mode 100644 (file)
index 0000000..19dbaea
--- /dev/null
@@ -0,0 +1,28 @@
+from django.db.backends.postgresql.creation import DatabaseCreation as PgDatabaseCreation
+
+class DatabaseCreation(PgDatabaseCreation):
+
+    def sql_indexes_for_field(self, model, f, style):
+        "Return the CREATE INDEX SQL statements for a single model field"
+        if f.db_index and not f.unique:
+            qn = self.connection.ops.quote_name
+            tablespace = f.db_tablespace or model._meta.db_tablespace
+            if tablespace:
+                sql = self.connection.ops.tablespace_sql(tablespace)
+                if sql:
+                    tablespace_sql = ' ' + sql
+                else:
+                    tablespace_sql = ''
+            else:
+                tablespace_sql = ''
+            idx_name = '%s_%s' % (model._meta.db_table.replace('.', '_'), f.column)
+            output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
+                style.SQL_TABLE(qn(idx_name)) + ' ' +
+                style.SQL_KEYWORD('ON') + ' ' +
+                style.SQL_TABLE(qn(model._meta.db_table)) + ' ' +
+                "(%s)" % style.SQL_FIELD(qn(f.column)) +
+                "%s;" % tablespace_sql]
+        else:
+            output = []
+        return output
+