Ticket #1438: syncdb-inital-sql-data.diff
File syncdb-inital-sql-data.diff, 2.9 KB (added by , 19 years ago) |
---|
-
django/django/core/management.py
328 328 get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." 329 329 get_sql_reset.args = APP_ARGS 330 330 331 def get_sql_initial_data(app ):331 def get_sql_initial_data(app, model_list=None): 332 332 "Returns a list of the initial INSERT SQL statements for the given app." 333 333 from django.conf import settings 334 334 from django.db.models import get_models … … 334 334 from django.db.models import get_models 335 335 output = [] 336 336 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) 338 341 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 339 342 340 343 for klass in app_models: … … 413 416 table_list = introspection_module.get_table_list(cursor) 414 417 415 418 pending_references = [] 419 pending_initial_data = [] 416 420 417 421 for app in models.get_apps(): 418 422 model_list = models.get_models(app) 423 new_models = [] 424 is_new_app = True 419 425 for model in model_list: 420 426 # Create the model's database table, if it doesn't already exist. 421 427 if model._meta.db_table in table_list: 428 is_new_app = False 422 429 continue 430 new_models.append(model) 423 431 field_metadata, table_metadata, references = sql_for_table(model) 424 432 pending_references.extend(references) 425 433 sql = "CREATE TABLE %s (\n %s\n)" % \ … … 428 436 print "Creating table %s" % model._meta.db_table 429 437 cursor.execute(sql) 430 438 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 431 446 for model in model_list: 432 447 # Create the many-to-many join table, if it doesn't already exist. 433 448 for f in model._meta.many_to_many: … … 470 485 backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col)) 471 486 cursor.execute(sql) 472 487 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 473 499 transaction.commit_unless_managed() 474 500 syncdb.args = '' 475 501