diff --git a/django/core/management.py b/django/core/management.py index ed7c8b60b3..cd6acd138f 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -295,9 +295,27 @@ def get_sql_reset(app): get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." get_sql_reset.args = APP_ARGS +def get_sql_initial_data_for_model(model): + from django.db import models + from django.conf import settings + + opts = model._meta + app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) + output = [] + + # Find custom SQL, if it's available. + sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)), + os.path.join(app_dir, "%s.sql" % opts.object_name.lower())] + for sql_file in sql_files: + if os.path.exists(sql_file): + fp = open(sql_file) + output.append(fp.read()) + fp.close() + + return output + def get_sql_initial_data(app): "Returns a list of the initial INSERT SQL statements for the given app." - from django.conf import settings from django.db.models import get_models output = [] @@ -305,23 +323,8 @@ def get_sql_initial_data(app): app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) for klass in app_models: - opts = klass._meta + output.extend(get_sql_initial_data_for_model(klass)) - # Add custom SQL, if it's available. - # TODO: This probably needs changing - sql_files = [os.path.join(app_dir, opts.module_name + '.' + settings.DATABASE_ENGINE + '.sql'), - os.path.join(app_dir, opts.module_name + '.sql')] - for sql_file in sql_files: - if os.path.exists(sql_file): - fp = open(sql_file) - output.append(fp.read()) - fp.close() - - # TODO: This is temporarily commented out until we come up - # with a better way of letting people initialize permissions. -# # Permissions. -# for codename, name in _get_all_permissions(opts): -# output.append(_get_permission_insert(name, codename, opts)) return output get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)." get_sql_initial_data.args = APP_ARGS @@ -365,7 +368,6 @@ def get_sql_all(app): get_sql_all.help_doc = "Prints the CREATE TABLE and initial-data SQL statements for the given model module name(s)." get_sql_all.args = APP_ARGS -# TODO: syncdb() should include initial SQL data # TODO: Put "example.com" site in initial SQL data for sites app def syncdb(): "Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." @@ -423,13 +425,32 @@ def syncdb(): try: installperms(app) except Exception, e: + # stop trying to install permissions + install_permissions = False sys.stderr.write("Permissions will not be installed because it "\ "appears that you are not using Django's auth framework. "\ "If you want to install them in the future, re-run syncdb."\ - "\n(The full error was: %s)" % (app_name, app_name, e)) + "\n(The full error was: %s)" % e) transaction.rollback_unless_managed() else: transaction.commit_unless_managed() + + # Install initial data for the app (but only if this is a model we've + # just created) + for model in model_list: + if model in created_models: + initial_sql = get_sql_initial_data_for_model(model) + if initial_sql: + print "Installing initial data for %s model" % model._meta.object_name + try: + for sql in initial_sql: + cursor.execute(sql) + except Exception, e: + sys.stderr.write("Failed to install initial SQL data for %s model: %s" % \ + (model._meta.object_name, e)) + transaction.rollback_unless_managed() + else: + transaction.commit_unless_managed() syncdb.args = '' @@ -460,7 +481,6 @@ get_admin_index.args = APP_ARGS def install(app): "Executes the equivalent of 'get_sql_all' in the current database." from django.db import connection, transaction - from django.conf import settings app_name = app.__name__.split('.')[-2]