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
|
requires_model_validation = False
|
||||||
|
|
||||||
def handle_label(self, tablename, **options):
|
def handle_label(self, tablename, **options):
|
||||||
alias = options['database']
|
alias = options.get('database', DEFAULT_DB_ALIAS)
|
||||||
connection = connections[alias]
|
connection = connections[alias]
|
||||||
fields = (
|
fields = (
|
||||||
# "key" is a reserved word in MySQL, so use "cache_key" instead.
|
# "key" is a reserved word in MySQL, so use "cache_key" instead.
|
||||||
|
@ -16,7 +16,7 @@ class Command(BaseCommand):
|
|||||||
requires_model_validation = False
|
requires_model_validation = False
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
connection = connections[options['database']]
|
connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
|
||||||
try:
|
try:
|
||||||
connection.client.runshell()
|
connection.client.runshell()
|
||||||
except OSError:
|
except OSError:
|
||||||
|
@ -28,7 +28,7 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
format = options.get('format','json')
|
format = options.get('format','json')
|
||||||
indent = options.get('indent',None)
|
indent = options.get('indent',None)
|
||||||
using = options['database']
|
using = options.get('database', DEFAULT_DB_ALIAS)
|
||||||
connection = connections[using]
|
connection = connections[using]
|
||||||
exclude = options.get('exclude',[])
|
exclude = options.get('exclude',[])
|
||||||
show_traceback = options.get('traceback', False)
|
show_traceback = options.get('traceback', False)
|
||||||
|
@ -21,62 +21,57 @@ class Command(NoArgsCommand):
|
|||||||
help = "Executes ``sqlflush`` on the current database."
|
help = "Executes ``sqlflush`` on the current database."
|
||||||
|
|
||||||
def handle_noargs(self, **options):
|
def handle_noargs(self, **options):
|
||||||
if not options['database']:
|
db = options.get('database', DEFAULT_DB_ALIAS)
|
||||||
dbs = connections
|
connection = connections[db]
|
||||||
else:
|
|
||||||
dbs = [options['database']]
|
|
||||||
|
|
||||||
verbosity = int(options.get('verbosity', 1))
|
verbosity = int(options.get('verbosity', 1))
|
||||||
interactive = options.get('interactive')
|
interactive = options.get('interactive')
|
||||||
|
|
||||||
self.style = no_style()
|
self.style = no_style()
|
||||||
|
|
||||||
|
# Import the 'management' module within each installed app, to register
|
||||||
|
# dispatcher events.
|
||||||
for app_name in settings.INSTALLED_APPS:
|
for app_name in settings.INSTALLED_APPS:
|
||||||
try:
|
try:
|
||||||
import_module('.management', app_name)
|
import_module('.management', app_name)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
for db in dbs:
|
sql_list = sql_flush(self.style, connection, only_django=True)
|
||||||
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)
|
|
||||||
|
|
||||||
if interactive:
|
if interactive:
|
||||||
confirm = raw_input("""You have requested a flush of the database.
|
confirm = raw_input("""You have requested a flush of the database.
|
||||||
This will IRREVERSIBLY DESTROY all data currently in the %r database,
|
This will IRREVERSIBLY DESTROY all data currently in the %r database,
|
||||||
and return each table to the state it was in after syncdb.
|
and return each table to the state it was in after syncdb.
|
||||||
Are you sure you want to do this?
|
Are you sure you want to do this?
|
||||||
|
|
||||||
Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
|
Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
|
||||||
else:
|
else:
|
||||||
confirm = 'yes'
|
confirm = 'yes'
|
||||||
|
|
||||||
if confirm == 'yes':
|
if confirm == 'yes':
|
||||||
try:
|
try:
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
for sql in sql_list:
|
for sql in sql_list:
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
transaction.rollback_unless_managed(using=db)
|
transaction.rollback_unless_managed(using=db)
|
||||||
raise CommandError("""Database %s couldn't be flushed. Possible reasons:
|
raise CommandError("""Database %s couldn't be flushed. Possible reasons:
|
||||||
* The database isn't running or isn't configured correctly.
|
* The database isn't running or isn't configured correctly.
|
||||||
* At least one of the expected database tables doesn't exist.
|
* At least one of the expected database tables doesn't exist.
|
||||||
* The SQL was invalid.
|
* 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.
|
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))
|
The full error: %s""" % (connection.settings_dict['NAME'], e))
|
||||||
transaction.commit_unless_managed(using=db)
|
transaction.commit_unless_managed(using=db)
|
||||||
|
|
||||||
# Emit the post sync signal. This allows individual
|
# Emit the post sync signal. This allows individual
|
||||||
# applications to respond as if the database had been
|
# applications to respond as if the database had been
|
||||||
# sync'd from scratch.
|
# sync'd from scratch.
|
||||||
emit_post_sync_signal(models.get_models(), verbosity, interactive, db)
|
emit_post_sync_signal(models.get_models(), verbosity, interactive, db)
|
||||||
|
|
||||||
# Reinstall the initial_data fixture.
|
# Reinstall the initial_data fixture.
|
||||||
kwargs = options.copy()
|
kwargs = options.copy()
|
||||||
kwargs['database'] = db
|
kwargs['database'] = db
|
||||||
call_command('loaddata', 'initial_data', **kwargs)
|
call_command('loaddata', 'initial_data', **kwargs)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print "Flush cancelled."
|
print "Flush cancelled."
|
||||||
|
@ -23,7 +23,7 @@ class Command(NoArgsCommand):
|
|||||||
raise CommandError("Database inspection isn't supported for the currently selected database backend.")
|
raise CommandError("Database inspection isn't supported for the currently selected database backend.")
|
||||||
|
|
||||||
def handle_inspection(self, options):
|
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('-', '')
|
table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '')
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ class Command(BaseCommand):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, *fixture_labels, **options):
|
def handle(self, *fixture_labels, **options):
|
||||||
using = options['database']
|
using = options.get('database', DEFAULT_DB_ALIAS)
|
||||||
excluded_apps = options.get('exclude', [])
|
excluded_apps = options.get('exclude', [])
|
||||||
|
|
||||||
connection = connections[using]
|
connection = connections[using]
|
||||||
|
@ -20,7 +20,7 @@ class Command(AppCommand):
|
|||||||
output_transaction = True
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
def handle_app(self, app, **options):
|
||||||
using = options['database']
|
using = options.get('database', DEFAULT_DB_ALIAS)
|
||||||
connection = connections[using]
|
connection = connections[using]
|
||||||
|
|
||||||
app_name = app.__name__.split('.')[-2]
|
app_name = app.__name__.split('.')[-2]
|
||||||
|
@ -16,4 +16,4 @@ class Command(AppCommand):
|
|||||||
output_transaction = True
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_noargs(self, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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
|
output_transaction = True
|
||||||
|
|
||||||
def handle_app(self, app, **options):
|
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')
|
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:
|
if not msg.startswith('No module named') or 'management' not in msg:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
db = options['database']
|
db = options.get('database', DEFAULT_DB_ALIAS)
|
||||||
connection = connections[db]
|
connection = connections[db]
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user