Ticket #11074: ticket_11074_against_r10706.diff
File ticket_11074_against_r10706.diff, 6.0 KB (added by , 15 years ago) |
---|
-
django/core/management/commands/syncdb.py
19 19 def handle_noargs(self, **options): 20 20 from django.db import connection, transaction, models 21 21 from django.conf import settings 22 from django.core.management.sql import custom_ sql_for_model, emit_post_sync_signal22 from django.core.management.sql import custom_field_sql_for_model, initial_sql_for_model, emit_post_sync_signal 23 23 24 24 verbosity = int(options.get('verbosity', 1)) 25 25 interactive = options.get('interactive') … … 100 100 101 101 # The connection may have been closed by a syncdb handler. 102 102 cursor = connection.cursor() 103 104 def execute_custom_sql(label, sql_statements, app_name, model): 105 if verbosity >= 1: 106 print "Installing %s SQL for %s.%s model" % (label, app_name, model._meta.object_name) 107 try: 108 for sql in sql_statements: 109 cursor.execute(sql) 110 except Exception, e: 111 sys.stderr.write("Failed to install %s SQL for %s.%s model: %s\n" % \ 112 (label, app_name, model._meta.object_name, e)) 113 if show_traceback: 114 import traceback 115 traceback.print_exc() 116 transaction.rollback_unless_managed() 117 else: 118 transaction.commit_unless_managed() 103 119 104 120 # Install custom SQL for the app (but only if this 105 121 # is a model we've just created) … … 107 123 app_name = app.__name__.split('.')[-2] 108 124 for model in models.get_models(app): 109 125 if model in created_models: 110 custom_sql = custom_sql_for_model(model, self.style) 111 if custom_sql: 112 if verbosity >= 1: 113 print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name) 114 try: 115 for sql in custom_sql: 116 cursor.execute(sql) 117 except Exception, e: 118 sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ 119 (app_name, model._meta.object_name, e)) 120 if show_traceback: 121 import traceback 122 traceback.print_exc() 123 transaction.rollback_unless_managed() 124 else: 125 transaction.commit_unless_managed() 126 custom_field_sql = custom_field_sql_for_model(model, self.style) 127 initial_sql = initial_sql_for_model(model, self.style) 128 if custom_field_sql: 129 execute_custom_sql('custom field', custom_field_sql, app_name, model) 126 130 else: 127 131 if verbosity >= 2: 128 print "No custom SQL for %s.%s model" % (app_name, model._meta.object_name) 132 print "No custom field SQL for %s.%s model" % (app_name, model._meta.object_name) 133 if initial_sql: 134 execute_custom_sql('initial', initial_sql, app_name, model) 135 else: 136 if verbosity >= 2: 137 print "No initial SQL for %s.%s model" % (app_name, model._meta.object_name) 138 129 139 # Install SQL indicies for all newly created models 130 140 for app in models.get_apps(): 131 141 app_name = app.__name__.split('.')[-2] … … 148 158 # Install the 'initial_data' fixture, using format discovery 149 159 from django.core.management import call_command 150 160 call_command('loaddata', 'initial_data', verbosity=verbosity) 161 -
django/core/management/sql.py
137 137 output = [] 138 138 139 139 app_models = get_models(app) 140 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))141 140 142 141 for model in app_models: 143 142 output.extend(custom_sql_for_model(model, style)) … … 156 155 "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module." 157 156 return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style) 158 157 159 def custom_ sql_for_model(model, style):158 def custom_field_sql_for_model(model, style): 160 159 from django.db import models 161 from django.conf import settings162 160 163 161 opts = model._meta 164 app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))165 162 output = [] 166 163 167 164 # Post-creation SQL should come before any initial SQL data is loaded. 168 165 # However, this should not be done for fields that are part of a a parent 169 166 # model (via model inheritance). 170 nm = opts.init_name_map()171 167 post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')] 172 168 for f in post_sql_fields: 173 169 output.extend(f.post_create_sql(style, model._meta.db_table)) 174 170 171 return output 172 173 def initial_sql_for_model(model, style): 174 from django.db import models 175 from django.conf import settings 176 177 opts = model._meta 178 app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) 179 output = [] 180 175 181 # Some backends can't execute more than one SQL statement at a time, 176 182 # so split into separate statements. 177 183 statements = re.compile(r";[ \t]*$", re.M) … … 191 197 192 198 return output 193 199 194 195 200 def emit_post_sync_signal(created_models, verbosity, interactive): 196 201 from django.db import models 197 202 from django.dispatch import dispatcher