Commit 2a5c750a authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Merge pull request #2726 from gchp/ticket-20550

Fixed #20550 -- Added ability to preserve test db between runs
parents 83e7555f b7aa7c4a
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -82,8 +82,10 @@ class PostGISCreation(DatabaseCreation):
                self.connection.ops.quote_name(self.template_postgis),)
        return ''

    def _create_test_db(self, verbosity, autoclobber):
        test_database_name = super(PostGISCreation, self)._create_test_db(verbosity, autoclobber)
    def _create_test_db(self, verbosity, autoclobber, keepdb=False):
        test_database_name = super(PostGISCreation, self)._create_test_db(verbosity, autoclobber, keepdb)
        if keepdb:
            return test_database_name
        if self.template_postgis is None:
            # Connect to the test database in order to create the postgis extension
            self.connection.close()
+7 −3
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ from django.db.backends.sqlite3.creation import DatabaseCreation

class SpatiaLiteCreation(DatabaseCreation):

    def create_test_db(self, verbosity=1, autoclobber=False):
    def create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
        """
        Creates a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
@@ -22,11 +22,15 @@ class SpatiaLiteCreation(DatabaseCreation):

        if verbosity >= 1:
            test_db_repr = ''
            action = 'Creating'
            if verbosity >= 2:
                test_db_repr = " ('%s')" % test_database_name
            print("Creating test database for alias '%s'%s..." % (self.connection.alias, test_db_repr))
            if keepdb:
                action = 'Using existing'
            print("%s test database for alias '%s'%s..." % (
                action, self.connection.alias, test_db_repr))

        self._create_test_db(verbosity, autoclobber)
        self._create_test_db(verbosity, autoclobber, keepdb)

        self.connection.close()
        self.connection.settings_dict["NAME"] = test_database_name
+19 −5
Original line number Diff line number Diff line
@@ -332,7 +332,7 @@ class BaseDatabaseCreation(object):
            ";",
        ]

    def create_test_db(self, verbosity=1, autoclobber=False):
    def create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
        """
        Creates a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
@@ -344,12 +344,21 @@ class BaseDatabaseCreation(object):

        if verbosity >= 1:
            test_db_repr = ''
            action = 'Creating'
            if verbosity >= 2:
                test_db_repr = " ('%s')" % test_database_name
            print("Creating test database for alias '%s'%s..." % (
                self.connection.alias, test_db_repr))
            if keepdb:
                action = "Using existing"

            print("%s test database for alias '%s'%s..." % (
                action, self.connection.alias, test_db_repr))

        self._create_test_db(verbosity, autoclobber)
        # We could skip this call if keepdb is True, but we instead
        # give it the keepdb param. This is to handle the case
        # where the test DB doesn't exist, in which case we need to
        # create it, then just not destroy it. If we instead skip
        # this, we will get an exception.
        self._create_test_db(verbosity, autoclobber, keepdb)

        self.connection.close()
        settings.DATABASES[self.connection.alias]["NAME"] = test_database_name
@@ -393,7 +402,7 @@ class BaseDatabaseCreation(object):
            return self.connection.settings_dict['TEST']['NAME']
        return TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']

    def _create_test_db(self, verbosity, autoclobber):
    def _create_test_db(self, verbosity, autoclobber, keepdb=False):
        """
        Internal implementation - creates the test db tables.
        """
@@ -409,6 +418,11 @@ class BaseDatabaseCreation(object):
                cursor.execute(
                    "CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
            except Exception as e:
                # if we want to keep the db, then no need to do any of the below,
                # just return and skip it all.
                if keepdb:
                    return test_database_name

                sys.stderr.write(
                    "Got an error creating the test database: %s\n" % e)
                if not autoclobber:
+5 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ class DatabaseCreation(BaseDatabaseCreation):
    def __init__(self, connection):
        super(DatabaseCreation, self).__init__(connection)

    def _create_test_db(self, verbosity=1, autoclobber=False):
    def _create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
        TEST_NAME = self._test_database_name()
        TEST_USER = self._test_database_user()
        TEST_PASSWD = self._test_database_passwd()
@@ -76,6 +76,10 @@ class DatabaseCreation(BaseDatabaseCreation):
            try:
                self._execute_test_db_creation(cursor, parameters, verbosity)
            except Exception as e:
                # if we want to keep the db, then no need to do any of the below,
                # just return and skip it all.
                if keepdb:
                    return
                sys.stderr.write("Got an error creating the test database: %s\n" % e)
                if not autoclobber:
                    confirm = input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_NAME)
+3 −1
Original line number Diff line number Diff line
@@ -52,8 +52,10 @@ class DatabaseCreation(BaseDatabaseCreation):
            return test_database_name
        return ':memory:'

    def _create_test_db(self, verbosity, autoclobber):
    def _create_test_db(self, verbosity, autoclobber, keepdb=False):
        test_database_name = self._get_test_db_name()
        if keepdb:
            return test_database_name
        if test_database_name != ':memory:':
            # Erase the old test database
            if verbosity >= 1:
Loading