1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

[multi-db] Updated django.core.management to use SchemaBuilder for each model to create sequence reset statements.

git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3444 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jason Pellerin 2006-07-25 00:47:55 +00:00
parent 6bd4b275af
commit 3cc04ec696

View File

@ -206,28 +206,23 @@ get_sql_initial_data.args = APP_ARGS
def get_sql_sequence_reset(app): def get_sql_sequence_reset(app):
"Returns a list of the SQL statements to reset PostgreSQL sequences for the given app." "Returns a list of the SQL statements to reset PostgreSQL sequences for the given app."
from django.db import backend, models from django.db import model_connection_name
output = [] from django.db.models import get_models
for model in models.get_models(app): connection_output = {}
for f in model._meta.fields: for model in get_models(app):
if isinstance(f, models.AutoField): connection_name = model_connection_name(model)
output.append("%s setval('%s', (%s max(%s) %s %s));" % \ output = connection_output.setdefault(connection_name, [])
(style.SQL_KEYWORD('SELECT'), builder = model._default_manager.db.get_creation_module().builder
style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)), try:
style.SQL_KEYWORD('SELECT'), output.extend(builder.get_sequence_reset(model, style))
style.SQL_FIELD(backend.quote_name(f.column)), except AttributeError:
style.SQL_KEYWORD('FROM'), sys.stderr.write(
style.SQL_TABLE(backend.quote_name(model._meta.db_table)))) "%s is configured to use database engine %s, which does "
break # Only one AutoField is allowed per model, so don't bother continuing. "not support sequence reset.\n" %
for f in model._meta.many_to_many: (model.__name__,
output.append("%s setval('%s', (%s max(%s) %s %s));" % \ model._default_manager.db.connection.settings.DATABASE_ENGINE))
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()), return _collate(connection_output)
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(backend.quote_name('id')),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(f.m2m_db_table())))
return output
get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)." get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)."
get_sql_sequence_reset.args = APP_ARGS get_sql_sequence_reset.args = APP_ARGS
@ -450,6 +445,8 @@ def install(app):
"pending statements that need it: %s" "pending statements that need it: %s"
% (model, statements)) % (model, statements))
except Exception, e: except Exception, e:
import traceback
print traceback.format_exception(*sys.exc_info())
sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons: sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons:
* The database isn't running or isn't configured correctly. * The database isn't running or isn't configured correctly.
* At least one of the database tables already exists. * At least one of the database tables already exists.