Commits

Anonymous committed 1c3ddc1

[soc2009/multidb] Updated management commands to ensure that a database name is always available. Patch from Russell Keith-Magee.

Comments (0)

Files changed (16)

django/core/management/commands/createcachetable.py

     requires_model_validation = False
 
     def handle_label(self, tablename, **options):
-        alias = options['database']
+        alias = options.get('database', DEFAULT_DB_ALIAS)
         connection = connections[alias]
         fields = (
             # "key" is a reserved word in MySQL, so use "cache_key" instead.

django/core/management/commands/dbshell.py

     requires_model_validation = False
 
     def handle(self, **options):
-        connection = connections[options['database']]
+        connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
         try:
             connection.client.runshell()
         except OSError:

django/core/management/commands/dumpdata.py

 
         format = options.get('format','json')
         indent = options.get('indent',None)
-        using = options['database']
+        using = options.get('database', DEFAULT_DB_ALIAS)
         connection = connections[using]
         exclude = options.get('exclude',[])
         show_traceback = options.get('traceback', False)

django/core/management/commands/flush.py

     help = "Executes ``sqlflush`` on the current database."
 
     def handle_noargs(self, **options):
-        if not options['database']:
-            dbs = connections
-        else:
-            dbs = [options['database']]
-
+        db = options.get('database', DEFAULT_DB_ALIAS)
+        connection = connections[db]
         verbosity = int(options.get('verbosity', 1))
         interactive = options.get('interactive')
 
         self.style = no_style()
 
+        # Import the 'management' module within each installed app, to register
+        # dispatcher events.
         for app_name in settings.INSTALLED_APPS:
             try:
                 import_module('.management', app_name)
             except ImportError:
                 pass
 
-        for db in dbs:
-            connection = connections[db]
-            # Import the 'management' module within each installed app, to register
-            # dispatcher events.
-            sql_list = sql_flush(self.style, connection, only_django=True)
+        sql_list = sql_flush(self.style, connection, only_django=True)
 
-            if interactive:
-                confirm = raw_input("""You have requested a flush of the database.
-    This will IRREVERSIBLY DESTROY all data currently in the %r database,
-    and return each table to the state it was in after syncdb.
-    Are you sure you want to do this?
+        if interactive:
+            confirm = raw_input("""You have requested a flush of the database.
+This will IRREVERSIBLY DESTROY all data currently in the %r database,
+and return each table to the state it was in after syncdb.
+Are you sure you want to do this?
 
-        Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
-            else:
-                confirm = 'yes'
+    Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
+        else:
+            confirm = 'yes'
 
-            if confirm == 'yes':
-                try:
-                    cursor = connection.cursor()
-                    for sql in sql_list:
-                        cursor.execute(sql)
-                except Exception, e:
-                    transaction.rollback_unless_managed(using=db)
-                    raise CommandError("""Database %s couldn't be flushed. Possible reasons:
-          * The database isn't running or isn't configured correctly.
-          * At least one of the expected database tables doesn't exist.
-          * The SQL was invalid.
-        Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
-        The full error: %s""" % (connection.settings_dict['NAME'], e))
-                transaction.commit_unless_managed(using=db)
+        if confirm == 'yes':
+            try:
+                cursor = connection.cursor()
+                for sql in sql_list:
+                    cursor.execute(sql)
+            except Exception, e:
+                transaction.rollback_unless_managed(using=db)
+                raise CommandError("""Database %s couldn't be flushed. Possible reasons:
+  * The database isn't running or isn't configured correctly.
+  * At least one of the expected database tables doesn't exist.
+  * The SQL was invalid.
+Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
+The full error: %s""" % (connection.settings_dict['NAME'], e))
+            transaction.commit_unless_managed(using=db)
 
-                # Emit the post sync signal. This allows individual
-                # applications to respond as if the database had been
-                # sync'd from scratch.
-                emit_post_sync_signal(models.get_models(), verbosity, interactive, db)
+            # Emit the post sync signal. This allows individual
+            # applications to respond as if the database had been
+            # sync'd from scratch.
+            emit_post_sync_signal(models.get_models(), verbosity, interactive, db)
 
-                # Reinstall the initial_data fixture.
-                kwargs = options.copy()
-                kwargs['database'] = db
-                call_command('loaddata', 'initial_data', **kwargs)
+            # Reinstall the initial_data fixture.
+            kwargs = options.copy()
+            kwargs['database'] = db
+            call_command('loaddata', 'initial_data', **kwargs)
 
-            else:
-                print "Flush cancelled."
+        else:
+            print "Flush cancelled."

django/core/management/commands/inspectdb.py

             raise CommandError("Database inspection isn't supported for the currently selected database backend.")
 
     def handle_inspection(self, options):
-        connection = connections[options['database']]
+        connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
 
         table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '')
 

django/core/management/commands/loaddata.py

     )
 
     def handle(self, *fixture_labels, **options):
-        using = options['database']
+        using = options.get('database', DEFAULT_DB_ALIAS)
         excluded_apps = options.get('exclude', [])
 
         connection = connections[using]

django/core/management/commands/reset.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        using = options['database']
+        using = options.get('database', DEFAULT_DB_ALIAS)
         connection = connections[using]
 
         app_name = app.__name__.split('.')[-2]

django/core/management/commands/sql.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        return u'\n'.join(sql_create(app, self.style, connections[options['database']])).encode('utf-8')
+        return u'\n'.join(sql_create(app, self.style, connections[options.get('database', DEFAULT_DB_ALIAS)])).encode('utf-8')

django/core/management/commands/sqlall.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        return u'\n'.join(sql_all(app, self.style, connections[options['database']])).encode('utf-8')
+        return u'\n'.join(sql_all(app, self.style, connections[options.get('database', DEFAULT_DB_ALIAS)])).encode('utf-8')

django/core/management/commands/sqlclear.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        return u'\n'.join(sql_delete(app, self.style, connections[options['database']])).encode('utf-8')
+        return u'\n'.join(sql_delete(app, self.style, connections[options.get('database', DEFAULT_DB_ALIAS)])).encode('utf-8')

django/core/management/commands/sqlcustom.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        return u'\n'.join(sql_custom(app, self.style, connections[options['database']])).encode('utf-8')
+        return u'\n'.join(sql_custom(app, self.style, connections[options.get('database', DEFAULT_DB_ALIAS)])).encode('utf-8')

django/core/management/commands/sqlflush.py

     output_transaction = True
 
     def handle_noargs(self, **options):
-        return u'\n'.join(sql_flush(self.style, connections[options['database']], only_django=True)).encode('utf-8')
+        return u'\n'.join(sql_flush(self.style, connections[options.get('database', DEFAULT_DB_ALIAS)], only_django=True)).encode('utf-8')

django/core/management/commands/sqlindexes.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        return u'\n'.join(sql_indexes(app, self.style, connections[options['database']])).encode('utf-8')
+        return u'\n'.join(sql_indexes(app, self.style, connections[options.get('database', DEFAULT_DB_ALIAS)])).encode('utf-8')

django/core/management/commands/sqlreset.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        return u'\n'.join(sql_reset(app, self.style, connections[options['database']])).encode('utf-8')
+        return u'\n'.join(sql_reset(app, self.style, connections[options.get('database', DEFAULT_DB_ALIAS)])).encode('utf-8')

django/core/management/commands/sqlsequencereset.py

     output_transaction = True
 
     def handle_app(self, app, **options):
-        connection = connections[options['database']]
+        connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
         return u'\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app))).encode('utf-8')

django/core/management/commands/syncdb.py

                 if not msg.startswith('No module named') or 'management' not in msg:
                     raise
 
-        db = options['database']
+        db = options.get('database', DEFAULT_DB_ALIAS)
         connection = connections[db]
         cursor = connection.cursor()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.