Ticket #1397: syncdb_initial_sql_data.diff
File syncdb_initial_sql_data.diff, 2.5 KB (added by , 19 years ago) |
---|
-
django/branches/magic-removal/django/core/management.py
26 26 27 27 INVALID_PROJECT_NAMES = ('django', 'test') 28 28 29 def _get_app_label(app): 30 from django.db.models import get_models 31 app_models = get_models(app) 32 app_label = app_models[0]._meta.app_label 33 return app_label 34 29 35 def _get_packages_insert(app_label): 30 36 from django.db import backend 31 37 return "INSERT INTO %s (%s, %s) VALUES ('%s', '%s');" % \ … … 338 344 app_models = get_models(app) 339 345 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 340 346 347 output.append(_get_packages_insert(_get_app_label(app))) 348 341 349 for klass in app_models: 342 350 opts = klass._meta 343 351 … … 415 423 416 424 pending_references = [] 417 425 426 initial_data = [] 427 418 428 for app in models.get_apps(): 429 is_new_app = True 419 430 model_list = models.get_models(app) 420 431 for model in model_list: 421 432 # Create the model's database table, if it doesn't already exist. … … 420 431 for model in model_list: 421 432 # Create the model's database table, if it doesn't already exist. 422 433 if model._meta.db_table in table_list: 434 is_new_app = False 423 435 continue 424 436 field_metadata, table_metadata, references = sql_for_table(model) 425 437 pending_references.extend(references) … … 429 441 print "Creating table %s" % model._meta.db_table 430 442 cursor.execute(sql) 431 443 444 if is_new_app: 445 # This is a new app, get initial sql data 446 initial_data.append((_get_app_label(app), get_sql_initial_data(app))) 447 432 448 for model in model_list: 433 449 # Create the many-to-many join table, if it doesn't already exist. 434 450 for f in model._meta.many_to_many: … … 454 470 sql = '\n'.join(table_output) 455 471 print "Creating table %s" % f.m2m_db_table() 456 472 cursor.execute(sql) 473 474 # Add initial sql data for the newly installed apps 475 if len(initial_data) > 0: 476 for label, sql_list in initial_data: 477 print "Adding initial sql data for %s" % label 478 for sql in sql_list: 479 cursor.execute(sql) 457 480 458 481 # Create the pending references. 459 482 # Take care of any ALTER TABLE statements to add constraints