mirror of
https://github.com/django/django.git
synced 2025-10-25 22:56:12 +00:00
Refactored get_sql_sequence_reset() to DatabaseOperations.sequence_reset_sql(). Refs #5106
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5964 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -86,6 +86,35 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||
else:
|
||||
return []
|
||||
|
||||
def sequence_reset_sql(self, style, model_list):
|
||||
from django.db import models
|
||||
output = []
|
||||
for model in model_list:
|
||||
# Use `coalesce` to set the sequence for each model to the max pk value if there are records,
|
||||
# or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
|
||||
# if there are records (as the max pk value is already in use), otherwise set it to false.
|
||||
for f in model._meta.fields:
|
||||
if isinstance(f, models.AutoField):
|
||||
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
|
||||
(style.SQL_KEYWORD('SELECT'),
|
||||
style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
|
||||
style.SQL_FIELD(quote_name(f.column)),
|
||||
style.SQL_FIELD(quote_name(f.column)),
|
||||
style.SQL_KEYWORD('IS NOT'),
|
||||
style.SQL_KEYWORD('FROM'),
|
||||
style.SQL_TABLE(quote_name(model._meta.db_table))))
|
||||
break # Only one AutoField is allowed per model, so don't bother continuing.
|
||||
for f in model._meta.many_to_many:
|
||||
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
|
||||
(style.SQL_KEYWORD('SELECT'),
|
||||
style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
|
||||
style.SQL_FIELD(quote_name('id')),
|
||||
style.SQL_FIELD(quote_name('id')),
|
||||
style.SQL_KEYWORD('IS NOT'),
|
||||
style.SQL_KEYWORD('FROM'),
|
||||
style.SQL_TABLE(f.m2m_db_table())))
|
||||
return output
|
||||
|
||||
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||
ops = DatabaseOperations()
|
||||
|
||||
@@ -139,36 +168,6 @@ dictfetchall = util.dictfetchall
|
||||
def get_start_transaction_sql():
|
||||
return "BEGIN;"
|
||||
|
||||
def get_sql_sequence_reset(style, model_list):
|
||||
"Returns a list of the SQL statements to reset sequences for the given models."
|
||||
from django.db import models
|
||||
output = []
|
||||
for model in model_list:
|
||||
# Use `coalesce` to set the sequence for each model to the max pk value if there are records,
|
||||
# or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
|
||||
# if there are records (as the max pk value is already in use), otherwise set it to false.
|
||||
for f in model._meta.fields:
|
||||
if isinstance(f, models.AutoField):
|
||||
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
|
||||
(style.SQL_KEYWORD('SELECT'),
|
||||
style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
|
||||
style.SQL_FIELD(quote_name(f.column)),
|
||||
style.SQL_FIELD(quote_name(f.column)),
|
||||
style.SQL_KEYWORD('IS NOT'),
|
||||
style.SQL_KEYWORD('FROM'),
|
||||
style.SQL_TABLE(quote_name(model._meta.db_table))))
|
||||
break # Only one AutoField is allowed per model, so don't bother continuing.
|
||||
for f in model._meta.many_to_many:
|
||||
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
|
||||
(style.SQL_KEYWORD('SELECT'),
|
||||
style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
|
||||
style.SQL_FIELD(quote_name('id')),
|
||||
style.SQL_FIELD(quote_name('id')),
|
||||
style.SQL_KEYWORD('IS NOT'),
|
||||
style.SQL_KEYWORD('FROM'),
|
||||
style.SQL_TABLE(f.m2m_db_table())))
|
||||
return output
|
||||
|
||||
OPERATOR_MAPPING = {
|
||||
'exact': '= %s',
|
||||
'iexact': 'ILIKE %s',
|
||||
|
||||
Reference in New Issue
Block a user