From afe777639cc1c591df32591e196d4cda5783c615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 30 Jul 2015 13:56:54 +0300 Subject: [PATCH] Fixed #25196 -- Normalized database representations in test database messages. Left over Oracle mostly as-is since it's more complicated. --- django/db/backends/base/creation.py | 40 ++++++++++++++--------- django/db/backends/mysql/creation.py | 4 ++- django/db/backends/oracle/creation.py | 4 +-- django/db/backends/postgresql/creation.py | 4 ++- django/db/backends/sqlite3/creation.py | 8 +++-- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/django/db/backends/base/creation.py b/django/db/backends/base/creation.py index 888dc792fe..bca8376368 100644 --- a/django/db/backends/base/creation.py +++ b/django/db/backends/base/creation.py @@ -39,15 +39,14 @@ class BaseDatabaseCreation(object): test_database_name = self._get_test_db_name() if verbosity >= 1: - test_db_repr = '' action = 'Creating' - if verbosity >= 2: - test_db_repr = " ('%s')" % test_database_name if keepdb: action = "Using existing" - print("%s test database for alias '%s'%s..." % ( - action, self.connection.alias, test_db_repr)) + print("%s test database for alias %s..." % ( + action, + self._get_database_display_str(verbosity, test_database_name), + )) # We could skip this call if keepdb is True, but we instead # give it the keepdb param. This is to handle the case @@ -132,6 +131,15 @@ class BaseDatabaseCreation(object): for obj in serializers.deserialize("json", data, using=self.connection.alias): obj.save() + def _get_database_display_str(self, verbosity, database_name): + """ + Return display string for a database for use in various actions. + """ + return "'%s'%s" % ( + self.connection.alias, + (" ('%s')" % database_name) if verbosity >= 2 else '', + ) + def _get_test_db_name(self): """ Internal implementation - returns the name of the test DB that will be @@ -173,8 +181,9 @@ class BaseDatabaseCreation(object): if autoclobber or confirm == 'yes': try: if verbosity >= 1: - print("Destroying old test database '%s'..." - % self.connection.alias) + print("Destroying old test database for alias %s..." % ( + self._get_database_display_str(verbosity, test_database_name), + )) cursor.execute( "DROP DATABASE %s" % qn(test_database_name)) cursor.execute( @@ -197,13 +206,13 @@ class BaseDatabaseCreation(object): source_database_name = self.connection.settings_dict['NAME'] if verbosity >= 1: - test_db_repr = '' action = 'Cloning test database' - if verbosity >= 2: - test_db_repr = " ('%s')" % source_database_name if keepdb: action = 'Using existing clone' - print("%s for alias '%s'%s..." % (action, self.connection.alias, test_db_repr)) + print("%s for alias %s..." % ( + action, + self._get_database_display_str(verbosity, source_database_name), + )) # We could skip this call if keepdb is True, but we instead # give it the keepdb param. See create_test_db for details. @@ -241,14 +250,13 @@ class BaseDatabaseCreation(object): test_database_name = self.get_test_db_clone_settings(number)['NAME'] if verbosity >= 1: - test_db_repr = '' action = 'Destroying' - if verbosity >= 2: - test_db_repr = " ('%s')" % test_database_name if keepdb: action = 'Preserving' - print("%s test database for alias '%s'%s..." % ( - action, self.connection.alias, test_db_repr)) + print("%s test database for alias %s..." % ( + action, + self._get_database_display_str(verbosity, test_database_name), + )) # if we want to preserve the database # skip the actual destroying piece. diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py index a992e65952..43857f07ea 100644 --- a/django/db/backends/mysql/creation.py +++ b/django/db/backends/mysql/creation.py @@ -30,7 +30,9 @@ class DatabaseCreation(BaseDatabaseCreation): return try: if verbosity >= 1: - print("Destroying old test database '%s'..." % self.connection.alias) + print("Destroying old test database for alias %s..." % ( + self._get_database_display_str(target_database_name, verbosity), + )) cursor.execute("DROP DATABASE %s" % qn(target_database_name)) cursor.execute("CREATE DATABASE %s" % qn(target_database_name)) except Exception as e: diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py index afe965d664..7f92108524 100644 --- a/django/db/backends/oracle/creation.py +++ b/django/db/backends/oracle/creation.py @@ -48,7 +48,7 @@ class DatabaseCreation(BaseDatabaseCreation): "Type 'yes' to delete it, or 'no' to cancel: " % parameters['user']) if autoclobber or confirm == 'yes': if verbosity >= 1: - print("Destroying old test database '%s'..." % self.connection.alias) + print("Destroying old test database for alias '%s'..." % self.connection.alias) try: self._execute_test_db_destruction(cursor, parameters, verbosity) except DatabaseError as e: @@ -149,7 +149,7 @@ class DatabaseCreation(BaseDatabaseCreation): sys.exit(2) try: if verbosity >= 1: - print("Destroying old test database '%s'..." % self.connection.alias) + print("Destroying old test database for alias '%s'..." % self.connection.alias) self._execute_test_db_destruction(cursor, parameters, verbosity) except Exception as e: sys.stderr.write("Got an error destroying the test database: %s\n" % e) diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py index dcb0430f1b..2a886bae3c 100644 --- a/django/db/backends/postgresql/creation.py +++ b/django/db/backends/postgresql/creation.py @@ -32,7 +32,9 @@ class DatabaseCreation(BaseDatabaseCreation): return try: if verbosity >= 1: - print("Destroying old test database '%s'..." % self.connection.alias) + print("Destroying old test database for alias %s..." % ( + self._get_database_display_str(target_database_name, verbosity), + )) cursor.execute("DROP DATABASE %s" % qn(target_database_name)) cursor.execute("CREATE DATABASE %s WITH TEMPLATE %s" % ( qn(target_database_name), qn(source_database_name))) diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py index d52349e57f..a6e3155aa8 100644 --- a/django/db/backends/sqlite3/creation.py +++ b/django/db/backends/sqlite3/creation.py @@ -30,7 +30,9 @@ class DatabaseCreation(BaseDatabaseCreation): if not self.connection.is_in_memory_db(test_database_name): # Erase the old test database if verbosity >= 1: - print("Destroying old test database '%s'..." % self.connection.alias) + print("Destroying old test database for alias %s..." % ( + self._get_database_display_str(verbosity, test_database_name), + )) if os.access(test_database_name, os.F_OK): if not autoclobber: confirm = input( @@ -69,7 +71,9 @@ class DatabaseCreation(BaseDatabaseCreation): if keepdb: return if verbosity >= 1: - print("Destroying old test database '%s'..." % target_database_name) + print("Destroying old test database for alias %s..." % ( + self._get_database_display_str(verbosity, target_database_name), + )) try: os.remove(target_database_name) except Exception as e: