Browse files

Fixed #5086 -- The 'flush' and 'sqlflush' management commands no lon…

…ger touch tables that Django is not aware of (tables that are not in INSTALLED_APPS and/or do not have associated models. Thanks for bringing this up,

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
adrianholovaty committed Aug 25, 2007
1 parent 8b8a36c commit 132605d889db8767a40243259066b8450428714c
@@ -24,7 +24,7 @@ def handle_noargs(self, **options):
except ImportError:
- sql_list = sql_flush(
+ sql_list = sql_flush(, only_django=True)
if interactive:
confirm = raw_input("""You have requested a flush of the database.
@@ -7,4 +7,4 @@ class Command(NoArgsCommand):
def handle_noargs(self, **options):
from import sql_flush
- return '\n'.join(sql_flush(
+ return '\n'.join(sql_flush(, only_django=True))
@@ -13,6 +13,25 @@ def table_list():
cursor = connection.cursor()
return get_introspection_module().get_table_list(cursor)
+def django_table_list(only_existing=False):
+ """
+ Returns a list of all table names that have associated Django models and
+ If only_existing is True, the resulting list will only include the tables
+ that actually exist in the database.
+ """
+ from django.db import models
+ tables = []
+ for app in models.get_apps():
+ for model in models.get_models(app):
+ tables.append(model._meta.db_table)
+ tables.extend([f.m2m_db_table() for f in model._meta.many_to_many])
+ if only_existing:
+ existing = table_list()
+ tables = [t for t in tables if t in existing]
+ return tables
def installed_models(table_list):
"Returns a set of all models that are installed, given a list of existing table names."
from django.db import connection, models
@@ -181,10 +200,19 @@ def sql_reset(app, style):
"Returns a list of the DROP TABLE SQL, then the CREATE TABLE SQL, for the given module."
return sql_delete(app, style) + sql_all(app, style)
-def sql_flush(style):
- "Returns a list of the SQL statements used to flush the database."
+def sql_flush(style, only_django=False):
+ """
+ Returns a list of the SQL statements used to flush the database.
+ If only_django is True, then only table names that have associated Django
+ models and are in INSTALLED_APPS will be included.
+ """
from django.db import connection
- statements = connection.ops.sql_flush(style, table_list(), sequence_list())
+ if only_django:
+ tables = django_table_list()
+ else:
+ tables = table_list()
+ statements = connection.ops.sql_flush(style, tables, sequence_list())
return statements
def sql_custom(app):
@@ -124,6 +124,13 @@ executed. This means that all data will be removed from the database, any
post-synchronization handlers will be re-executed, and the ``initial_data``
fixture will be re-installed.
+The behavior of this command has changed in the Django development version.
+Previously, this command cleared *every* table in the database, including any
+table that Django didn't know about (i.e., tables that didn't have associated
+models and/or weren't in ``INSTALLED_APPS``). Now, the command only clears
+tables that are represented by Django models and are activated in
@@ -240,6 +247,7 @@ Executes the equivalent of ``sqlreset`` for the given appnames.
runfcgi [options]
Starts a set of FastCGI processes suitable for use with any web server
which supports the FastCGI protocol. See the `FastCGI deployment
documentation`_ for details. Requires the Python FastCGI module from
@@ -337,7 +345,7 @@ Refer to the description of ``sqlcustom`` for an explanation of how to
specify initial data.
sqlclear [appname appname ...]
Prints the DROP TABLE SQL statements for the given appnames.
@@ -360,18 +368,23 @@ table modifications, or insert any SQL functions into the database.
Note that the order in which the SQL files are processed is undefined.
+Prints the SQL statements that would be executed for the `flush`_ command.
sqlindexes [appname appname ...]
Prints the CREATE INDEX SQL statements for the given appnames.
sqlreset [appname appname ...]
Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames.
sqlsequencereset [appname appname ...]
Prints the SQL statements for resetting sequences for the given

0 comments on commit 132605d

Please sign in to comment.