Ticket #11074: ticket_11074_against_r10706.diff

File ticket_11074_against_r10706.diff, 6.0 KB (added by psmith, 15 years ago)
  • django/core/management/commands/syncdb.py

     
    1919    def handle_noargs(self, **options):
    2020        from django.db import connection, transaction, models
    2121        from django.conf import settings
    22         from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
     22        from django.core.management.sql import custom_field_sql_for_model, initial_sql_for_model, emit_post_sync_signal
    2323
    2424        verbosity = int(options.get('verbosity', 1))
    2525        interactive = options.get('interactive')
     
    100100
    101101        # The connection may have been closed by a syncdb handler.
    102102        cursor = connection.cursor()
     103       
     104        def execute_custom_sql(label, sql_statements, app_name, model):
     105            if verbosity >= 1:
     106                print "Installing %s SQL for %s.%s model" % (label, app_name, model._meta.object_name)
     107            try:
     108                for sql in sql_statements:
     109                    cursor.execute(sql)
     110            except Exception, e:
     111                sys.stderr.write("Failed to install %s SQL for %s.%s model: %s\n" % \
     112                                 (label, app_name, model._meta.object_name, e))
     113                if show_traceback:
     114                    import traceback
     115                    traceback.print_exc()
     116                transaction.rollback_unless_managed()
     117            else:
     118                transaction.commit_unless_managed()
    103119
    104120        # Install custom SQL for the app (but only if this
    105121        # is a model we've just created)
     
    107123            app_name = app.__name__.split('.')[-2]
    108124            for model in models.get_models(app):
    109125                if model in created_models:
    110                     custom_sql = custom_sql_for_model(model, self.style)
    111                     if custom_sql:
    112                         if verbosity >= 1:
    113                             print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)
    114                         try:
    115                             for sql in custom_sql:
    116                                 cursor.execute(sql)
    117                         except Exception, e:
    118                             sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \
    119                                                 (app_name, model._meta.object_name, e))
    120                             if show_traceback:
    121                                 import traceback
    122                                 traceback.print_exc()
    123                             transaction.rollback_unless_managed()
    124                         else:
    125                             transaction.commit_unless_managed()
     126                    custom_field_sql = custom_field_sql_for_model(model, self.style)
     127                    initial_sql = initial_sql_for_model(model, self.style)
     128                    if custom_field_sql:
     129                        execute_custom_sql('custom field', custom_field_sql, app_name, model)
    126130                    else:
    127131                        if verbosity >= 2:
    128                             print "No custom SQL for %s.%s model" % (app_name, model._meta.object_name)
     132                            print "No custom field SQL for %s.%s model" % (app_name, model._meta.object_name)
     133                    if initial_sql:
     134                        execute_custom_sql('initial', initial_sql, app_name, model)
     135                    else:
     136                        if verbosity >= 2:
     137                            print "No initial SQL for %s.%s model" % (app_name, model._meta.object_name)
     138
    129139        # Install SQL indicies for all newly created models
    130140        for app in models.get_apps():
    131141            app_name = app.__name__.split('.')[-2]
     
    148158        # Install the 'initial_data' fixture, using format discovery
    149159        from django.core.management import call_command
    150160        call_command('loaddata', 'initial_data', verbosity=verbosity)
     161
  • django/core/management/sql.py

     
    137137    output = []
    138138
    139139    app_models = get_models(app)
    140     app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
    141140
    142141    for model in app_models:
    143142        output.extend(custom_sql_for_model(model, style))
     
    156155    "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
    157156    return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style)
    158157
    159 def custom_sql_for_model(model, style):
     158def custom_field_sql_for_model(model, style):
    160159    from django.db import models
    161     from django.conf import settings
    162160
    163161    opts = model._meta
    164     app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
    165162    output = []
    166163
    167164    # Post-creation SQL should come before any initial SQL data is loaded.
    168165    # However, this should not be done for fields that are part of a a parent
    169166    # model (via model inheritance).
    170     nm = opts.init_name_map()
    171167    post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')]
    172168    for f in post_sql_fields:
    173169        output.extend(f.post_create_sql(style, model._meta.db_table))
    174170
     171    return output
     172
     173def initial_sql_for_model(model, style):
     174    from django.db import models
     175    from django.conf import settings
     176
     177    opts = model._meta
     178    app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
     179    output = []
     180
    175181    # Some backends can't execute more than one SQL statement at a time,
    176182    # so split into separate statements.
    177183    statements = re.compile(r";[ \t]*$", re.M)
     
    191197
    192198    return output
    193199
    194 
    195200def emit_post_sync_signal(created_models, verbosity, interactive):
    196201    from django.db import models
    197202    from django.dispatch import dispatcher
Back to Top