2011-11-19 22:18:44 +08:00
|
|
|
import psycopg2.extensions
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.creation import BaseDatabaseCreation
|
2010-06-21 19:48:45 +08:00
|
|
|
from django.db.backends.util import truncate_name
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2011-04-02 16:39:08 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
|
|
|
# This dictionary maps Field objects to their associated PostgreSQL column
|
|
|
|
# types, as strings. Column-type strings can contain format strings; they'll
|
|
|
|
# be interpolated against the values of Field.__dict__ before being output.
|
|
|
|
# If a column type is set to None, it won't be included in the output.
|
|
|
|
data_types = {
|
|
|
|
'AutoField': 'serial',
|
|
|
|
'BooleanField': 'boolean',
|
|
|
|
'CharField': 'varchar(%(max_length)s)',
|
|
|
|
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
|
|
|
|
'DateField': 'date',
|
|
|
|
'DateTimeField': 'timestamp with time zone',
|
|
|
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
|
|
|
'FileField': 'varchar(%(max_length)s)',
|
|
|
|
'FilePathField': 'varchar(%(max_length)s)',
|
|
|
|
'FloatField': 'double precision',
|
|
|
|
'IntegerField': 'integer',
|
2009-12-17 23:10:38 +08:00
|
|
|
'BigIntegerField': 'bigint',
|
2008-08-11 20:11:25 +08:00
|
|
|
'IPAddressField': 'inet',
|
2011-06-11 21:48:24 +08:00
|
|
|
'GenericIPAddressField': 'inet',
|
2008-08-11 20:11:25 +08:00
|
|
|
'NullBooleanField': 'boolean',
|
|
|
|
'OneToOneField': 'integer',
|
|
|
|
'PositiveIntegerField': 'integer CHECK ("%(column)s" >= 0)',
|
|
|
|
'PositiveSmallIntegerField': 'smallint CHECK ("%(column)s" >= 0)',
|
|
|
|
'SlugField': 'varchar(%(max_length)s)',
|
|
|
|
'SmallIntegerField': 'smallint',
|
|
|
|
'TextField': 'text',
|
|
|
|
'TimeField': 'time',
|
|
|
|
}
|
|
|
|
|
|
|
|
def sql_table_creation_suffix(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
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']
|
2008-08-11 20:11:25 +08:00
|
|
|
return ''
|
2009-12-19 16:19:38 +08:00
|
|
|
|
|
|
|
def sql_indexes_for_field(self, model, f, style):
|
2012-12-18 16:56:30 +08:00
|
|
|
output = []
|
|
|
|
if f.db_index:
|
2009-12-19 16:19:38 +08:00
|
|
|
qn = self.connection.ops.quote_name
|
|
|
|
db_table = model._meta.db_table
|
|
|
|
tablespace = f.db_tablespace or model._meta.db_tablespace
|
|
|
|
if tablespace:
|
2011-10-15 05:49:43 +08:00
|
|
|
tablespace_sql = self.connection.ops.tablespace_sql(tablespace)
|
|
|
|
if tablespace_sql:
|
|
|
|
tablespace_sql = ' ' + tablespace_sql
|
2009-12-19 16:19:38 +08:00
|
|
|
else:
|
|
|
|
tablespace_sql = ''
|
|
|
|
|
|
|
|
def get_index_sql(index_name, opclass=''):
|
|
|
|
return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
|
2010-06-21 19:48:45 +08:00
|
|
|
style.SQL_TABLE(qn(truncate_name(index_name,self.connection.ops.max_name_length()))) + ' ' +
|
2009-12-19 16:19:38 +08:00
|
|
|
style.SQL_KEYWORD('ON') + ' ' +
|
|
|
|
style.SQL_TABLE(qn(db_table)) + ' ' +
|
|
|
|
"(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
|
|
|
|
"%s;" % tablespace_sql)
|
|
|
|
|
2012-12-18 16:56:30 +08:00
|
|
|
if not f.unique:
|
|
|
|
output = [get_index_sql('%s_%s' % (db_table, f.column))]
|
2009-12-19 16:19:38 +08:00
|
|
|
|
|
|
|
# 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.
|
2010-07-30 10:54:47 +08:00
|
|
|
db_type = f.db_type(connection=self.connection)
|
2009-12-19 16:19:38 +08:00
|
|
|
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'))
|
|
|
|
return output
|
2011-07-07 07:25:30 +08:00
|
|
|
|
|
|
|
def set_autocommit(self):
|
|
|
|
self._prepare_for_test_db_ddl()
|
|
|
|
|
|
|
|
def _prepare_for_test_db_ddl(self):
|
|
|
|
"""Rollback and close the active transaction."""
|
|
|
|
self.connection.connection.rollback()
|
2011-11-19 22:18:44 +08:00
|
|
|
self.connection.connection.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|