2015-01-13 04:20:40 +08:00
|
|
|
import psycopg2
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
|
|
|
2012-06-19 00:32:03 +08:00
|
|
|
|
|
|
|
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
2013-12-11 22:19:05 +08:00
|
|
|
|
2015-06-19 09:47:21 +08:00
|
|
|
sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s USING %(column)s::%(type)s"
|
|
|
|
|
2013-12-11 22:19:05 +08:00
|
|
|
sql_create_sequence = "CREATE SEQUENCE %(sequence)s"
|
|
|
|
sql_delete_sequence = "DROP SEQUENCE IF EXISTS %(sequence)s CASCADE"
|
|
|
|
sql_set_sequence_max = "SELECT setval('%(sequence)s', MAX(%(column)s)) FROM %(table)s"
|
2015-06-19 09:47:21 +08:00
|
|
|
|
2014-12-05 00:56:11 +08:00
|
|
|
sql_create_varchar_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s varchar_pattern_ops)%(extra)s"
|
|
|
|
sql_create_text_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s text_pattern_ops)%(extra)s"
|
2013-12-11 22:19:05 +08:00
|
|
|
|
2014-02-09 20:41:55 +08:00
|
|
|
def quote_value(self, value):
|
|
|
|
return psycopg2.extensions.adapt(value)
|
|
|
|
|
2014-12-05 00:56:11 +08:00
|
|
|
def _model_indexes_sql(self, model):
|
|
|
|
output = super(DatabaseSchemaEditor, self)._model_indexes_sql(model)
|
|
|
|
if not model._meta.managed or model._meta.proxy or model._meta.swapped:
|
|
|
|
return output
|
|
|
|
|
|
|
|
for field in model._meta.local_fields:
|
2015-11-08 00:08:03 +08:00
|
|
|
like_index_statement = self._create_like_index_sql(model, field)
|
|
|
|
if like_index_statement is not None:
|
|
|
|
output.append(like_index_statement)
|
2014-12-05 00:56:11 +08:00
|
|
|
return output
|
|
|
|
|
2015-11-08 00:08:03 +08:00
|
|
|
def _create_like_index_sql(self, model, field):
|
|
|
|
"""
|
|
|
|
Return the statement to create an index with varchar operator pattern
|
|
|
|
when the column type is 'varchar' or 'text', otherwise return None.
|
|
|
|
"""
|
|
|
|
db_type = field.db_type(connection=self.connection)
|
|
|
|
if db_type is not None and (field.db_index or field.unique):
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# The same doesn't apply to array fields such as varchar[size]
|
|
|
|
# and text[size], so skip them.
|
|
|
|
if '[' in db_type:
|
|
|
|
return None
|
|
|
|
if db_type.startswith('varchar'):
|
|
|
|
return self._create_index_sql(model, [field], suffix='_like', sql=self.sql_create_varchar_index)
|
|
|
|
elif db_type.startswith('text'):
|
|
|
|
return self._create_index_sql(model, [field], suffix='_like', sql=self.sql_create_text_index)
|
|
|
|
return None
|
|
|
|
|
2015-04-11 22:10:31 +08:00
|
|
|
def _alter_column_type_sql(self, table, old_field, new_field, new_type):
|
2013-12-11 22:19:05 +08:00
|
|
|
"""
|
|
|
|
Makes ALTER TYPE with SERIAL make sense.
|
|
|
|
"""
|
2015-07-02 16:43:15 +08:00
|
|
|
if new_type.lower() in ("serial", "bigserial"):
|
2015-04-11 22:10:31 +08:00
|
|
|
column = new_field.column
|
2013-12-11 22:19:05 +08:00
|
|
|
sequence_name = "%s_%s_seq" % (table, column)
|
2015-07-02 16:43:15 +08:00
|
|
|
col_type = "integer" if new_type.lower() == "serial" else "bigint"
|
2013-12-11 22:19:05 +08:00
|
|
|
return (
|
|
|
|
(
|
|
|
|
self.sql_alter_column_type % {
|
|
|
|
"column": self.quote_name(column),
|
2015-07-02 16:43:15 +08:00
|
|
|
"type": col_type,
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
[
|
|
|
|
(
|
|
|
|
self.sql_delete_sequence % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"sequence": self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.sql_create_sequence % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"sequence": self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.sql_alter_column % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"table": self.quote_name(table),
|
2013-12-11 22:19:05 +08:00
|
|
|
"changes": self.sql_alter_column_default % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"column": self.quote_name(column),
|
|
|
|
"default": "nextval('%s')" % self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.sql_set_sequence_max % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"table": self.quote_name(table),
|
|
|
|
"column": self.quote_name(column),
|
|
|
|
"sequence": self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
else:
|
2015-04-11 22:10:31 +08:00
|
|
|
return super(DatabaseSchemaEditor, self)._alter_column_type_sql(
|
|
|
|
table, old_field, new_field, new_type
|
|
|
|
)
|
2015-11-08 00:08:03 +08:00
|
|
|
|
|
|
|
def _alter_field(self, model, old_field, new_field, old_type, new_type,
|
|
|
|
old_db_params, new_db_params, strict=False):
|
|
|
|
super(DatabaseSchemaEditor, self)._alter_field(
|
|
|
|
model, old_field, new_field, old_type, new_type, old_db_params,
|
|
|
|
new_db_params, strict,
|
|
|
|
)
|
|
|
|
# Added an index? Create any PostgreSQL-specific indexes.
|
2016-06-23 12:46:11 +08:00
|
|
|
if ((not (old_field.db_index or old_field.unique) and new_field.db_index) or
|
|
|
|
(not old_field.unique and new_field.unique)):
|
2015-11-08 00:08:03 +08:00
|
|
|
like_index_statement = self._create_like_index_sql(model, new_field)
|
|
|
|
if like_index_statement is not None:
|
|
|
|
self.execute(like_index_statement)
|
|
|
|
|
|
|
|
# Removed an index? Drop any PostgreSQL-specific indexes.
|
2016-06-23 12:46:11 +08:00
|
|
|
if old_field.unique and not (new_field.db_index or new_field.unique):
|
2015-11-08 00:08:03 +08:00
|
|
|
index_to_remove = self._create_index_name(model, [old_field.column], suffix='_like')
|
|
|
|
index_names = self._constraint_names(model, [old_field.column], index=True)
|
|
|
|
for index_name in index_names:
|
|
|
|
if index_name == index_to_remove:
|
|
|
|
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
|