Ticket #1398: CASCADEOption.diff

File CASCADEOption.diff, 2.5 KB (added by Lllama, 9 years ago)

The patch itself

  • core/management.py

     
    159159            # The table doesn't exist, so it doesn't need to be dropped.
    160160            db.db.rollback()
    161161        else:
    162             output.append("DROP TABLE %s;" % db.db.quote_name(klass._meta.db_table))
     162            if CASCADE:
     163                output.append("DROP TABLE %s CASCADE;" % db.db.quote_name(klass._meta.db_table))
     164            else:
     165                output.append("DROP TABLE %s;" % db.db.quote_name(klass._meta.db_table))
    163166
    164167    # Output DROP TABLE statements for many-to-many tables.
    165168    for klass in mod._MODELS:
     
    171174            except:
    172175                db.db.rollback()
    173176            else:
    174                 output.append("DROP TABLE %s;" % db.db.quote_name(f.get_m2m_db_table(opts)))
     177                if CASCADE:
     178                    output.append("DROP TABLE %s CASCADE;" % db.db.quote_name(klass._meta.db_table))
     179                else:
     180                    output.append("DROP TABLE %s;" % db.db.quote_name(f.get_m2m_db_table(opts)))
    175181
    176182    app_label = mod._MODELS[0]._meta.app_label
    177183
     
    872878    'validate': validate,
    873879}
    874880
     881CASCADE = False
     882
    875883NO_SQL_TRANSACTION = ('adminindex', 'createcachetable', 'dbcheck', 'install', 'installperms', 'sqlindexes')
    876884
    877885class DjangoOptionParser(OptionParser):
     
    899907    sys.exit(1)
    900908
    901909def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
     910    global CASCADE
    902911    # Parse the command-line arguments. optparse handles the dirty work.
    903912    parser = DjangoOptionParser(get_usage(action_mapping))
    904913    parser.add_option('--settings',
     
    907916        help='Lets you manually add a directory the Python path, e.g. "/home/djangoprojects/myproject".')
    908917    parser.add_option('--plain', action='store_true', dest='plain',
    909918        help='Tells Django to use plain Python, not IPython, for "shell" command.')
     919    parser.add_option('--cascade', action='store_true', dest='cascade',
     920        help='Tells Django to output CASCADEing DROP TABLE statements.')
    910921    options, args = parser.parse_args()
    911922
    912923    # Take care of options.
     
    914925        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
    915926    if options.pythonpath:
    916927        sys.path.insert(0, options.pythonpath)
     928    CASCADE = options.cascade
    917929
    918930    # Run the appropriate action. Unfortunately, optparse can't handle
    919931    # positional arguments, so this has to parse/validate them.
Back to Top