From f4bcbbfa8bca397850a0060225906243e7ed27f4 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 3 Jun 2009 02:26:16 +0000 Subject: [PATCH] [soc2009/multidb] Updated several strings and internal API names to be more consistant and descriptive git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10897 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- TODO.TXT | 2 + .../management/commands/createcachetable.py | 3 +- django/core/management/commands/dbshell.py | 3 +- django/core/management/commands/flush.py | 56 +++++++++++++++++-- django/core/management/commands/inspectdb.py | 3 +- django/core/management/commands/reset.py | 10 ++-- django/core/management/commands/sql.py | 3 +- django/core/management/commands/sqlall.py | 3 +- django/core/management/commands/sqlclear.py | 3 +- django/core/management/commands/sqlcustom.py | 3 +- django/core/management/commands/sqlflush.py | 3 +- django/core/management/commands/sqlindexes.py | 3 +- django/core/management/commands/sqlreset.py | 3 +- .../management/commands/sqlsequencereset.py | 5 +- django/core/management/commands/syncdb.py | 49 ++++++++-------- django/db/models/sql/where.py | 2 - django/db/utils.py | 10 +++- 17 files changed, 111 insertions(+), 53 deletions(-) diff --git a/TODO.TXT b/TODO.TXT index 506cf75a6e..7863fd3577 100644 --- a/TODO.TXT +++ b/TODO.TXT @@ -13,6 +13,8 @@ that need to be done. I'm trying to be as granular as possible. The remaining items will be fixed as the code for them enters the code base. + Replace old instances of :setting:`DATABASE_` with a new tag or something. + 3) Update all management commands in the following way: diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index 1678d57d58..b0ad180ee9 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -10,7 +10,8 @@ class Command(LabelCommand): option_list = LabelCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to install the cache table to.'), + default='default', help='Nominates a specific database to install ' + 'the cache table to. Defaults to the "default" database.'), ) requires_model_validation = False diff --git a/django/core/management/commands/dbshell.py b/django/core/management/commands/dbshell.py index 2f49f5b3de..c6e139af81 100644 --- a/django/core/management/commands/dbshell.py +++ b/django/core/management/commands/dbshell.py @@ -9,7 +9,8 @@ class Command(BaseCommand): option_list = BaseCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to connection to.'), + default='default', help='Nominates a database to open a shell for.' + ' Defaults to the "default" database.'), ) requires_model_validation = False diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index 1b0218cf18..0b164d249d 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -15,7 +15,8 @@ class Command(NoArgsCommand): make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), make_option('--database', action='store', dest='database', - default='', help='Selects what database to flush.'), + default='', help='Nominates a database to flush. Defaults to ' + 'flushing all databases.'), ) help = "Executes ``sqlflush`` on the current database." @@ -24,11 +25,11 @@ class Command(NoArgsCommand): dbs = connections.all() else: dbs = [options['database']] - for connection in dbs: - verbosity = int(options.get('verbosity', 1)) - interactive = options.get('interactive') - self.style = no_style() + 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. @@ -73,5 +74,50 @@ class Command(NoArgsCommand): # Reinstall the initial_data fixture. call_command('loaddata', 'initial_data', **options) + for app_name in settings.INSTALLED_APPS: + try: + import_module('.management', app_name) + except ImportError: + pass + + for connection in dbs: + + # Import the 'management' module within each installed app, to register + # dispatcher events. + 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? + + Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['DATABASE_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() + 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.DATABASE_NAME, e)) + transaction.commit_unless_managed() + + # 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, connection) + + # Reinstall the initial_data fixture. + call_command('loaddata', 'initial_data', **options) + else: print "Flush cancelled." diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index fb22007f6a..ec116046bc 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -9,7 +9,8 @@ class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to introspect.'), + default='default', help='Nominates a database to introspect. ' + 'Defaults to using the "default" database.'), ) requires_model_validation = False diff --git a/django/core/management/commands/reset.py b/django/core/management/commands/reset.py index 005c533589..4cb8720c8a 100644 --- a/django/core/management/commands/reset.py +++ b/django/core/management/commands/reset.py @@ -11,7 +11,8 @@ class Command(AppCommand): make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), make_option('--database', action='store', dest='database', - default='', help='Selects what database reset.'), + default='', help='Nominates a database to reset. Defaults to ' + 'reseting all databases.'), ) help = "Executes ``sqlreset`` for the given app(s) in the current database." args = '[appname ...]' @@ -23,10 +24,11 @@ class Command(AppCommand): dbs = connections.all() else: dbs = [options['database']] - for connection in dbs: - app_name = app.__name__.split('.')[-2] - self.style = no_style() + app_name = app.__name__.split('.')[-2] + self.style = no_style() + + for connection in dbs: sql_list = sql_reset(app, self.style, connection) diff --git a/django/core/management/commands/sql.py b/django/core/management/commands/sql.py index f5f498c319..8f6d4e5644 100644 --- a/django/core/management/commands/sql.py +++ b/django/core/management/commands/sql.py @@ -9,7 +9,8 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlall.py b/django/core/management/commands/sqlall.py index 30af486083..33aaca8527 100644 --- a/django/core/management/commands/sqlall.py +++ b/django/core/management/commands/sqlall.py @@ -9,7 +9,8 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlclear.py b/django/core/management/commands/sqlclear.py index cab048fc95..cb5dfeb8e6 100644 --- a/django/core/management/commands/sqlclear.py +++ b/django/core/management/commands/sqlclear.py @@ -9,7 +9,8 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlcustom.py b/django/core/management/commands/sqlcustom.py index 745535a8b7..9521e4196c 100644 --- a/django/core/management/commands/sqlcustom.py +++ b/django/core/management/commands/sqlcustom.py @@ -9,7 +9,8 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlflush.py b/django/core/management/commands/sqlflush.py index f0ae11c37f..6f2d961b8f 100644 --- a/django/core/management/commands/sqlflush.py +++ b/django/core/management/commands/sqlflush.py @@ -9,7 +9,8 @@ class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlindexes.py b/django/core/management/commands/sqlindexes.py index ead8c01dbf..fad0b4f8f7 100644 --- a/django/core/management/commands/sqlindexes.py +++ b/django/core/management/commands/sqlindexes.py @@ -9,7 +9,8 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlreset.py b/django/core/management/commands/sqlreset.py index 4e3035084d..66d7e56280 100644 --- a/django/core/management/commands/sqlreset.py +++ b/django/core/management/commands/sqlreset.py @@ -9,7 +9,8 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), ) output_transaction = True diff --git a/django/core/management/commands/sqlsequencereset.py b/django/core/management/commands/sqlsequencereset.py index 9ade0eb37e..3373c607e2 100644 --- a/django/core/management/commands/sqlsequencereset.py +++ b/django/core/management/commands/sqlsequencereset.py @@ -8,8 +8,9 @@ class Command(AppCommand): option_list = AppCommand.option_list + ( make_option('--database', action='store', dest='database', - default='default', help='Selects what database to print the SQL for.'), - ) + default='default', help='Nominates a database to print the SQL ' + 'for. Defaults to the "default" database.'), + ) output_transaction = True diff --git a/django/core/management/commands/syncdb.py b/django/core/management/commands/syncdb.py index 76eb9099dd..fd6b5b5b1a 100644 --- a/django/core/management/commands/syncdb.py +++ b/django/core/management/commands/syncdb.py @@ -18,7 +18,8 @@ class Command(NoArgsCommand): make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), make_option('--database', action='store', dest='database', - default='', help='Selects what database to flush.'), + default='', help='Nominates a database to sync. Defaults to the ' + '"default" database.'), ) help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." @@ -34,27 +35,27 @@ class Command(NoArgsCommand): dbs = connections.all() else: dbs = [connections[options['database']]] + + # 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, exc: + # This is slightly hackish. We want to ignore ImportErrors + # if the "management" module itself is missing -- but we don't + # want to ignore the exception if the management module exists + # but raises an ImportError for some reason. The only way we + # can do this is to check the text of the exception. Note that + # we're a bit broad in how we check the text, because different + # Python implementations may not use the same text. + # CPython uses the text "No module named management" + # PyPy uses "No module named myproject.myapp.management" + msg = exc.args[0] + if not msg.startswith('No module named') or 'management' not in msg: + raise + for connection in dbs: - - # 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, exc: - # This is slightly hackish. We want to ignore ImportErrors - # if the "management" module itself is missing -- but we don't - # want to ignore the exception if the management module exists - # but raises an ImportError for some reason. The only way we - # can do this is to check the text of the exception. Note that - # we're a bit broad in how we check the text, because different - # Python implementations may not use the same text. - # CPython uses the text "No module named management" - # PyPy uses "No module named myproject.myapp.management" - msg = exc.args[0] - if not msg.startswith('No module named') or 'management' not in msg: - raise - cursor = connection.cursor() # Get a list of already installed *models* so that references work right. @@ -154,15 +155,9 @@ class Command(NoArgsCommand): else: transaction.commit_unless_managed() -<<<<<<< HEAD:django/core/management/commands/syncdb.py - # Install the 'initial_data' fixture, using format discovery - from django.core.management import call_command - call_command('loaddata', 'initial_data', verbosity=verbosity) -======= # Install the 'initial_data' fixture, using format discovery # FIXME we only load the fixture data for one DB right now, since we # can't control what DB it does into, once we can control this we # should move it back into the DB loop from django.core.management import call_command call_command('loaddata', 'initial_data', verbosity=verbosity) ->>>>>>> 2c764d3ff7cb665ec919d1f3e2977587752c6f2c:django/core/management/commands/syncdb.py diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 8420e49d95..8acbc9eef9 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -288,8 +288,6 @@ class Constraint(object): try: if self.field: params = self.field.get_db_prep_lookup(lookup_type, value) - # FIXME, we're using the global connection object here, once a - # WhereNode know's it's connection we should pass that through db_type = self.field.db_type(connection) else: # This branch is used at times when we add a comparison to NULL diff --git a/django/db/utils.py b/django/db/utils.py index 75fd7ed233..0b51648384 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -36,7 +36,11 @@ class ConnectionHandler(object): self.databases = databases self._connections = {} - def check_connection(self, alias): + def ensure_defaults(self, alias): + """ + Puts the defaults into the settings dictionary for a given connection + where no settings is provided. + """ conn = self.databases[alias] conn.setdefault('DATABASE_ENGINE', 'dummy') conn.setdefault('DATABASE_OPTIONS', {}) @@ -52,7 +56,7 @@ class ConnectionHandler(object): if alias in self._connections: return self._connections[alias] - self.check_connection(alias) + self.ensure_defaults(alias) db = self.databases[alias] backend = load_backend(db['DATABASE_ENGINE']) conn = backend.DatabaseWrapper(db) @@ -63,4 +67,4 @@ class ConnectionHandler(object): return iter(self.databases) def all(self): - return [self[alias] for alias in self.databases] + return [self[alias] for alias in self]