Fixed #5086 -- The 'flush' and 'sqlflush' management commands no longer 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, shaun@cuttshome.net

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6013 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-08-25 19:32:30 +00:00
parent 8b8a36c7d0
commit 132605d889
4 changed files with 50 additions and 9 deletions

View File

@ -24,7 +24,7 @@ class Command(NoArgsCommand):
except ImportError:
pass
sql_list = sql_flush(self.style)
sql_list = sql_flush(self.style, only_django=True)
if interactive:
confirm = raw_input("""You have requested a flush of the database.

View File

@ -7,4 +7,4 @@ class Command(NoArgsCommand):
def handle_noargs(self, **options):
from django.core.management.sql import sql_flush
return '\n'.join(sql_flush(self.style))
return '\n'.join(sql_flush(self.style, only_django=True))

View File

@ -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
are in INSTALLED_APPS.
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):

View File

@ -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
``INSTALLED_APPS``.
inspectdb
---------
@ -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.
sqlflush
--------
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
appnames.