Commit afe77763 authored by Ville Skyttä's avatar Ville Skyttä Committed by Tim Graham
Browse files

Fixed #25196 -- Normalized database representations in test database messages.

Left over Oracle mostly as-is since it's more complicated.
parent 53e89ce2
Loading
Loading
Loading
Loading
+24 −16
Original line number Diff line number Diff line
@@ -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.
+3 −1
Original line number Diff line number Diff line
@@ -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:
+2 −2
Original line number Diff line number Diff line
@@ -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)
+3 −1
Original line number Diff line number Diff line
@@ -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)))
+6 −2
Original line number Diff line number Diff line
@@ -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: