Ticket #1438: syncdb-inital-sql-data-3.diff

File syncdb-inital-sql-data-3.diff, 3.2 KB (added by anonymous, 19 years ago)

Updated to work with latest management.py changes

  • django/django/core/management.py

     
    198198        final_output.append('\n'.join(table_output))
    199199    return final_output
    200200
     201def _get_initial_sql_for_model(model):
     202    "Returns a list of the initial INSERT SQL statements for the given model."
     203    from django.conf import settings
     204    from django.db.models.loading import get_app
     205
     206    module_name = model._meta.module_name
     207    app = get_app(model._meta.app_label)
     208    app_sql_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
     209
     210    sql_files = [os.path.join(app_sql_dir, module_name + '.' + settings.DATABASE_ENGINE +  '.sql'),
     211                 os.path.join(app_sql_dir, module_name + '.sql')]
     212
     213    final_output = []
     214    for sql_file in sql_files:
     215        if os.path.exists(sql_file):
     216            fp = open(sql_file)
     217            final_output.append(fp.read())
     218            fp.close()
     219    return final_output
     220
    201221def get_sql_delete(app):
    202222    "Returns a list of the DROP TABLE SQL statements for the given app."
    203223    from django.db import backend, connection, models, transaction
     
    315335    from django.conf import settings
    316336    from django.db.models import get_models
    317337    output = []
    318 
    319     app_models = get_models(app)
    320     app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
    321 
    322     for klass in app_models:
    323         opts = klass._meta
    324 
    325         # Add custom SQL, if it's available.
    326         # TODO: This probably needs changing
    327         sql_files = [os.path.join(app_dir, opts.module_name + '.' + settings.DATABASE_ENGINE +  '.sql'),
    328                      os.path.join(app_dir, opts.module_name + '.sql')]
    329         for sql_file in sql_files:
    330             if os.path.exists(sql_file):
    331                 fp = open(sql_file)
    332                 output.append(fp.read())
    333                 fp.close()
    334 
    335         # TODO: This is temporarily commented out until we come up
    336         # with a better way of letting people initialize permissions.
    337 #         # Permissions.
    338 #         for codename, name in _get_all_permissions(opts):
    339 #             output.append(_get_permission_insert(name, codename, opts))
     338    for model in get_models(app):
     339        output.extend(_get_initial_sql_for_model(model))
    340340    return output
    341 get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)."
     341get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name."
    342342get_sql_initial_data.args = APP_ARGS
    343343
    344344def get_sql_sequence_reset(app):
     
    430430                    for statement in sql:
    431431                        cursor.execute(statement)
    432432
     433    # Add initial sql data if any for the newly created models
     434    for model in model_list:
     435        if model in created_models:
     436            sql = _get_initial_sql_for_model(model)
     437            if sql:
     438                print "Adding initial sql data for %s" % model._meta.db_table
     439                for statement in sql:
     440                    cursor.execute(statement)
     441
    433442    transaction.commit_unless_managed()
    434443syncdb.args = ''
    435444
Back to Top