Django

Code

Changeset 6013

Show
Ignore:
Timestamp:
08/25/07 14:32:30 (1 year ago)
Author:
adrian
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/commands/flush.py

    r5903 r6013  
    2525                pass 
    2626 
    27         sql_list = sql_flush(self.style
     27        sql_list = sql_flush(self.style, only_django=True
    2828 
    2929        if interactive: 
  • django/trunk/django/core/management/commands/sqlflush.py

    r5903 r6013  
    88    def handle_noargs(self, **options): 
    99        from django.core.management.sql import sql_flush 
    10         return '\n'.join(sql_flush(self.style)) 
     10        return '\n'.join(sql_flush(self.style, only_django=True)) 
  • django/trunk/django/core/management/sql.py

    r5980 r6013  
    1313    cursor = connection.cursor() 
    1414    return get_introspection_module().get_table_list(cursor) 
     15 
     16def django_table_list(only_existing=False): 
     17    """ 
     18    Returns a list of all table names that have associated Django models and 
     19    are in INSTALLED_APPS. 
     20 
     21    If only_existing is True, the resulting list will only include the tables 
     22    that actually exist in the database. 
     23    """ 
     24    from django.db import models 
     25    tables = [] 
     26    for app in models.get_apps(): 
     27        for model in models.get_models(app): 
     28            tables.append(model._meta.db_table) 
     29            tables.extend([f.m2m_db_table() for f in model._meta.many_to_many]) 
     30    if only_existing: 
     31        existing = table_list() 
     32        tables = [t for t in tables if t in existing] 
     33    return tables 
    1534 
    1635def installed_models(table_list): 
     
    182201    return sql_delete(app, style) + sql_all(app, style) 
    183202 
    184 def sql_flush(style): 
    185     "Returns a list of the SQL statements used to flush the database." 
     203def sql_flush(style, only_django=False): 
     204    """ 
     205    Returns a list of the SQL statements used to flush the database. 
     206     
     207    If only_django is True, then only table names that have associated Django 
     208    models and are in INSTALLED_APPS will be included. 
     209    """ 
    186210    from django.db import connection 
    187     statements = connection.ops.sql_flush(style, table_list(), sequence_list()) 
     211    if only_django: 
     212        tables = django_table_list() 
     213    else: 
     214        tables = table_list() 
     215    statements = connection.ops.sql_flush(style, tables, sequence_list()) 
    188216    return statements 
    189217 
  • django/trunk/docs/django-admin.txt

    r5929 r6013  
    124124post-synchronization handlers will be re-executed, and the ``initial_data`` 
    125125fixture will be re-installed. 
     126 
     127The behavior of this command has changed in the Django development version. 
     128Previously, this command cleared *every* table in the database, including any 
     129table that Django didn't know about (i.e., tables that didn't have associated 
     130models and/or weren't in ``INSTALLED_APPS``). Now, the command only clears 
     131tables that are represented by Django models and are activated in 
     132``INSTALLED_APPS``. 
    126133 
    127134inspectdb 
     
    241248runfcgi [options] 
    242249----------------- 
     250 
    243251Starts a set of FastCGI processes suitable for use with any web server 
    244252which supports the FastCGI protocol. See the `FastCGI deployment 
     
    338346 
    339347sqlclear [appname appname ...] 
    340 -------------------------------------- 
     348------------------------------ 
    341349 
    342350Prints the DROP TABLE SQL statements for the given appnames. 
     
    361369Note that the order in which the SQL files are processed is undefined. 
    362370 
     371sqlflush 
     372-------- 
     373 
     374Prints the SQL statements that would be executed for the `flush`_ command. 
     375 
    363376sqlindexes [appname appname ...] 
    364 ---------------------------------------- 
     377-------------------------------- 
    365378 
    366379Prints the CREATE INDEX SQL statements for the given appnames. 
    367380 
    368381sqlreset [appname appname ...] 
     382------------------------------ 
     383 
     384Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames. 
     385 
     386sqlsequencereset [appname appname ...] 
    369387-------------------------------------- 
    370  
    371 Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames. 
    372  
    373 sqlsequencereset [appname appname ...] 
    374 ---------------------------------------------- 
    375388 
    376389Prints the SQL statements for resetting sequences for the given