web/lib/django/db/backends/postgresql/creation.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
--- a/web/lib/django/db/backends/postgresql/creation.py	Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/db/backends/postgresql/creation.py	Tue May 25 02:43:45 2010 +0200
@@ -1,4 +1,3 @@
-from django.conf import settings
 from django.db.backends.creation import BaseDatabaseCreation
 
 class DatabaseCreation(BaseDatabaseCreation):
@@ -18,6 +17,7 @@
         'FilePathField':     'varchar(%(max_length)s)',
         'FloatField':        'double precision',
         'IntegerField':      'integer',
+        'BigIntegerField':   'bigint',
         'IPAddressField':    'inet',
         'NullBooleanField':  'boolean',
         'OneToOneField':     'integer',
@@ -30,7 +30,46 @@
     }
 
     def sql_table_creation_suffix(self):
-        assert settings.TEST_DATABASE_COLLATION is None, "PostgreSQL does not support collation setting at database creation time."
-        if settings.TEST_DATABASE_CHARSET:
-            return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET
+        assert self.connection.settings_dict['TEST_COLLATION'] is None, "PostgreSQL does not support collation setting at database creation time."
+        if self.connection.settings_dict['TEST_CHARSET']:
+            return "WITH ENCODING '%s'" % self.connection.settings_dict['TEST_CHARSET']
         return ''
+
+    def sql_indexes_for_field(self, model, f, style):
+        if f.db_index and not f.unique:
+            qn = self.connection.ops.quote_name
+            db_table = model._meta.db_table
+            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 = ''
+
+            def get_index_sql(index_name, opclass=''):
+                return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
+                        style.SQL_TABLE(qn(index_name)) + ' ' +
+                        style.SQL_KEYWORD('ON') + ' ' +
+                        style.SQL_TABLE(qn(db_table)) + ' ' +
+                        "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
+                        "%s;" % tablespace_sql)
+
+            output = [get_index_sql('%s_%s' % (db_table, f.column))]
+
+            # Fields with database column types of `varchar` and `text` need
+            # a second index that specifies their operator class, which is
+            # needed when performing correct LIKE queries outside the
+            # C locale. See #12234.
+            db_type = f.db_type()
+            if db_type.startswith('varchar'):
+                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
+                                            ' varchar_pattern_ops'))
+            elif db_type.startswith('text'):
+                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
+                                            ' text_pattern_ops'))
+        else:
+            output = []
+        return output