diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2009-12-22 14:37:40 +0000 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2009-12-22 14:37:40 +0000 |
commit | 45b4288bb66a3cda401b45901e85b645674c3988 (patch) | |
tree | 3594b06a5974a1bdcb79c6d22258d521d2493ce7 | |
parent | 27c43c3acc1ca29ff3d380cd2b5722803648a5dd (diff) | |
download | django-45b4288bb66a3cda401b45901e85b645674c3988.tar.gz |
[soc2009/multidb] Updated management commands to ensure that a database name is always available. Patch from Russell Keith-Magee.archive/soc2009/multidbsoc2009/multidb
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11950 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r-- | django/core/management/commands/createcachetable.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/dbshell.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/dumpdata.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/flush.py | 79 | ||||
-rw-r--r-- | django/core/management/commands/inspectdb.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/loaddata.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/reset.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sql.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlall.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlclear.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlcustom.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlflush.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlindexes.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlreset.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/sqlsequencereset.py | 2 | ||||
-rw-r--r-- | django/core/management/commands/syncdb.py | 2 |
16 files changed, 52 insertions, 57 deletions
diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index 44e9a5baa6..c0d3cb8219 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -18,7 +18,7 @@ class Command(LabelCommand): 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. diff --git a/django/core/management/commands/dbshell.py b/django/core/management/commands/dbshell.py index d42849459b..a6b5427830 100644 --- a/django/core/management/commands/dbshell.py +++ b/django/core/management/commands/dbshell.py @@ -16,7 +16,7 @@ class Command(BaseCommand): 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: diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index dd20d44fce..028095812b 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -28,7 +28,7 @@ class Command(BaseCommand): 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) diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index c94072fd02..6836fe35ca 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -21,62 +21,57 @@ class Command(NoArgsCommand): 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." diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 8592ecb130..c95c6400fb 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -23,7 +23,7 @@ class Command(NoArgsCommand): 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('-', '') diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 4967c64cd2..598c98cd6a 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -36,7 +36,7 @@ class Command(BaseCommand): ) def handle(self, *fixture_labels, **options): - using = options['database'] + using = options.get('database', DEFAULT_DB_ALIAS) excluded_apps = options.get('exclude', []) connection = connections[using] diff --git a/django/core/management/commands/reset.py b/django/core/management/commands/reset.py index 9b43a6b6d0..475fd25c1f 100644 --- a/django/core/management/commands/reset.py +++ b/django/core/management/commands/reset.py @@ -20,7 +20,7 @@ class Command(AppCommand): 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] diff --git a/django/core/management/commands/sql.py b/django/core/management/commands/sql.py index 62a42d4edb..582bc8e919 100644 --- a/django/core/management/commands/sql.py +++ b/django/core/management/commands/sql.py @@ -16,4 +16,4 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/sqlall.py b/django/core/management/commands/sqlall.py index c73582b036..8f3a38e3b3 100644 --- a/django/core/management/commands/sqlall.py +++ b/django/core/management/commands/sqlall.py @@ -16,4 +16,4 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/sqlclear.py b/django/core/management/commands/sqlclear.py index 9bb4b5e744..c7e3a5e670 100644 --- a/django/core/management/commands/sqlclear.py +++ b/django/core/management/commands/sqlclear.py @@ -16,4 +16,4 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/sqlcustom.py b/django/core/management/commands/sqlcustom.py index 1ef784b709..d206b0441b 100644 --- a/django/core/management/commands/sqlcustom.py +++ b/django/core/management/commands/sqlcustom.py @@ -16,4 +16,4 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/sqlflush.py b/django/core/management/commands/sqlflush.py index 56a5c82be0..004f0a5629 100644 --- a/django/core/management/commands/sqlflush.py +++ b/django/core/management/commands/sqlflush.py @@ -16,4 +16,4 @@ class Command(NoArgsCommand): 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') diff --git a/django/core/management/commands/sqlindexes.py b/django/core/management/commands/sqlindexes.py index 66de8c3e7a..c889f03382 100644 --- a/django/core/management/commands/sqlindexes.py +++ b/django/core/management/commands/sqlindexes.py @@ -17,4 +17,4 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/sqlreset.py b/django/core/management/commands/sqlreset.py index b439797ee5..13f40de81d 100644 --- a/django/core/management/commands/sqlreset.py +++ b/django/core/management/commands/sqlreset.py @@ -17,4 +17,4 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/sqlsequencereset.py b/django/core/management/commands/sqlsequencereset.py index f1d63c0e05..6aedcfb043 100644 --- a/django/core/management/commands/sqlsequencereset.py +++ b/django/core/management/commands/sqlsequencereset.py @@ -16,5 +16,5 @@ class Command(AppCommand): 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') diff --git a/django/core/management/commands/syncdb.py b/django/core/management/commands/syncdb.py index 31c1cb3121..a599975d78 100644 --- a/django/core/management/commands/syncdb.py +++ b/django/core/management/commands/syncdb.py @@ -49,7 +49,7 @@ class Command(NoArgsCommand): 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() |