Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refs #8138 -- Added a stealth option to the loaddata command so that …

…the use of transactions in loaddata can be disabled. This behavior isn't enabled as a commad line option because it doesn't make much sense, but it can be used when invoking loaddata from another script.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8336 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4edbb8b2caadd8bfb794846e231de18c7ec5b115 1 parent cb1368b
Russell Keith-Magee authored August 13, 2008

Showing 1 changed file with 21 additions and 10 deletions. Show diff stats Hide diff stats

  1. 31  django/core/management/commands/loaddata.py
31  django/core/management/commands/loaddata.py
@@ -28,7 +28,15 @@ def handle(self, *fixture_labels, **options):
28 28
 
29 29
         verbosity = int(options.get('verbosity', 1))
30 30
         show_traceback = options.get('traceback', False)
31  
-
  31
+        
  32
+        # commit is a stealth option - it isn't really useful as 
  33
+        # a command line option, but it can be useful when invoking
  34
+        # loaddata from within another script. 
  35
+        # If commit=True, loaddata will use its own transaction;
  36
+        # if commit=False, the data load SQL will become part of
  37
+        # the transaction in place when loaddata was invoked.
  38
+        commit = options.get('commit', True)
  39
+        
32 40
         # Keep a count of the installed objects and fixtures
33 41
         fixture_count = 0
34 42
         object_count = 0
@@ -44,9 +52,10 @@ def handle(self, *fixture_labels, **options):
44 52
 
45 53
         # Start transaction management. All fixtures are installed in a
46 54
         # single transaction to ensure that all references are resolved.
47  
-        transaction.commit_unless_managed()
48  
-        transaction.enter_transaction_management()
49  
-        transaction.managed(True)
  55
+        if commit:
  56
+            transaction.commit_unless_managed()
  57
+            transaction.enter_transaction_management()
  58
+            transaction.managed(True)
50 59
 
51 60
         app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
52 61
         for fixture_label in fixture_labels:
@@ -133,7 +142,7 @@ def handle(self, *fixture_labels, **options):
133 142
                                 (format, fixture_name, humanize(fixture_dir))
134 143
 
135 144
 
136  
-        # If any of the fixtures we loaded contain 0 objects, assume that an 
  145
+        # If any of the fixtures we loaded contain 0 objects, assume that an
137 146
         # error was encountered during fixture loading.
138 147
         if 0 in objects_per_fixture:
139 148
             sys.stderr.write(
@@ -143,7 +152,7 @@ def handle(self, *fixture_labels, **options):
143 152
             transaction.leave_transaction_management()
144 153
             return
145 154
             
146  
-        # If we found even one object in a fixture, we need to reset the 
  155
+        # If we found even one object in a fixture, we need to reset the
147 156
         # database sequences.
148 157
         if object_count > 0:
149 158
             sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
@@ -152,9 +161,10 @@ def handle(self, *fixture_labels, **options):
152 161
                     print "Resetting sequences"
153 162
                 for line in sequence_sql:
154 163
                     cursor.execute(line)
155  
-            
156  
-        transaction.commit()
157  
-        transaction.leave_transaction_management()
  164
+        
  165
+        if commit:
  166
+            transaction.commit()
  167
+            transaction.leave_transaction_management()
158 168
 
159 169
         if object_count == 0:
160 170
             if verbosity > 1:
@@ -167,4 +177,5 @@ def handle(self, *fixture_labels, **options):
167 177
         # edge case in MySQL: if the same connection is used to
168 178
         # create tables, load data, and query, the query can return
169 179
         # incorrect results. See Django #7572, MySQL #37735.
170  
-        connection.close()
  180
+        if commit:
  181
+            connection.close()

0 notes on commit 4edbb8b

Please sign in to comment.
Something went wrong with that request. Please try again.