mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
[soc2009/multidb] Updated management commands to ensure that a database name is always available. Patch from Russell Keith-Magee.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11950 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
27c43c3acc
commit
45b4288bb6
@ -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.
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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."
|
||||
|
@ -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('-', '')
|
||||
|
||||
|
@ -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]
|
||||
|
@ -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]
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user