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

File syncdb-inital-sql-data-2.diff, 2.8 KB (added by Steven Armstrong <sa@…>, 19 years ago)

Cleaner impl.

  • 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 = []
    419424        for model in model_list:
    420425            # Create the model's database table, if it doesn't already exist.
    421426            if model._meta.db_table in table_list:
     
    420425            # Create the model's database table, if it doesn't already exist.
    421426            if model._meta.db_table in table_list:
    422427                continue
     428            new_models.append(model)
    423429            field_metadata, table_metadata, references = sql_for_table(model)
    424430            pending_references.extend(references)
    425431            sql = "CREATE TABLE %s (\n    %s\n)" % \
     
    428434            print "Creating table %s" % model._meta.db_table
    429435            cursor.execute(sql)
    430436
     437        if len(new_models) > 0:
     438            # Remember to add initial sql data for the new modules
     439            pending_initial_data.append((app, new_models))
     440
    431441        for model in model_list:
    432442            # Create the many-to-many join table, if it doesn't already exist.
    433443            for f in model._meta.many_to_many:
     
    470480                backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))
    471481            cursor.execute(sql)
    472482
     483    # Add initial sql data for the newly installed apps and or models
     484    if len(pending_initial_data) > 0:
     485        for app, model_list in pending_initial_data:
     486            for model in model_list:
     487                sql_list = get_sql_initial_data(app, [model])
     488                if len(sql_list) > 0:
     489                    print "Adding initial sql data for %s" % model._meta.db_table
     490                    for sql in sql_list:
     491                        cursor.execute(sql)
     492
     493
    473494    transaction.commit_unless_managed()
    474495syncdb.args = ''
    475496
Back to Top