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

File syncdb-inital-sql-data.diff, 2.9 KB (added by anonymous, 18 years ago)
  • django/django/core/management.py

     
    328328get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)."
    329329get_sql_reset.args = APP_ARGS
    330330
    331 def get_sql_initial_data(app):
     331def get_sql_initial_data(app, model_list=None):
    332332    "Returns a list of the initial INSERT SQL statements for the given app."
    333333    from django.conf import settings
    334334    from django.db.models import get_models
     
    334334    from django.db.models import get_models
    335335    output = []
    336336
    337     app_models = get_models(app)
     337    if model_list is not None:
     338        app_models = model_list
     339    else:
     340        app_models = get_models(app)
    338341    app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
    339342
    340343    for klass in app_models:
     
    413416    table_list = introspection_module.get_table_list(cursor)
    414417
    415418    pending_references = []
     419    pending_initial_data = []
    416420
    417421    for app in models.get_apps():
    418422        model_list = models.get_models(app)
     423        new_models = []
     424        is_new_app = True
    419425        for model in model_list:
    420426            # Create the model's database table, if it doesn't already exist.
    421427            if model._meta.db_table in table_list:
     428                is_new_app = False
    422429                continue
     430            new_models.append(model)
    423431            field_metadata, table_metadata, references = sql_for_table(model)
    424432            pending_references.extend(references)
    425433            sql = "CREATE TABLE %s (\n    %s\n)" % \
     
    428436            print "Creating table %s" % model._meta.db_table
    429437            cursor.execute(sql)
    430438
     439        if is_new_app:
     440            # Remember to add initial sql data for this new app
     441            pending_initial_data.append((app, model_list))
     442        elif len(new_models) > 0:
     443            # Remember to add initial sql data for the new modules
     444            pending_initial_data.append((app, new_models))
     445
    431446        for model in model_list:
    432447            # Create the many-to-many join table, if it doesn't already exist.
    433448            for f in model._meta.many_to_many:
     
    470485                backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))
    471486            cursor.execute(sql)
    472487
     488    # Add initial sql data for the newly installed apps and or models
     489    if len(pending_initial_data) > 0:
     490        for app, model_list in pending_initial_data:
     491            for model in model_list:
     492                sql_list = get_sql_initial_data(app, [model])
     493                if len(sql_list) > 0:
     494                    print "Adding initial sql data for %s" % model._meta.db_table
     495                    for sql in sql_list:
     496                        cursor.execute(sql)
     497
     498
    473499    transaction.commit_unless_managed()
    474500syncdb.args = ''
    475501
Back to Top