Commit 8f97413f authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #20968 -- Checked Spatialite metadata before migrations

Thanks Kenial S. Lee for the initial patch and Tim Graham for
the review.
parent 1aa41dd0
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -79,3 +79,12 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
            six.reraise(ImproperlyConfigured, ImproperlyConfigured(new_msg), sys.exc_info()[2])
        cur.close()
        return conn

    def prepare_database(self):
        super(DatabaseWrapper, self).prepare_database()
        # Check if spatial metadata have been initialized in the database
        with self.cursor() as cursor:
            cursor.execute("PRAGMA table_info(geometry_columns);")
            if cursor.fetchall() == []:
                arg = "1" if self.features.supports_initspatialmetadata_in_one_transaction else ""
                cursor.execute("SELECT InitSpatialMetaData(%s)" % arg)
+0 −8
Original line number Diff line number Diff line
@@ -30,11 +30,3 @@ class SpatiaLiteCreation(DatabaseCreation):
                              style.SQL_FIELD(gqn(f.column)) + ');')

        return output

    def _create_test_db_pre_migrate_sql(self):
        """
        Creates the spatial metadata tables.
        """
        cur = self.connection._cursor()
        arg = "1" if self.connection.features.supports_initspatialmetadata_in_one_transaction else ""
        cur.execute("SELECT InitSpatialMetaData(%s)" % arg)
+2 −0
Original line number Diff line number Diff line
@@ -64,6 +64,8 @@ class Command(BaseCommand):
        if options.get("list", False):
            return self.show_migration_list(connection, [options['app_label']] if options['app_label'] else None)

        # Hook for backends needing any database preparation
        connection.prepare_database()
        # Work out which apps have migrations and which do not
        executor = MigrationExecutor(connection, self.migration_progress_callback)

+7 −0
Original line number Diff line number Diff line
@@ -432,6 +432,13 @@ class BaseDatabaseWrapper(object):

    ##### Miscellaneous #####

    def prepare_database(self):
        """
        Hook to do any database check or preparation, generally called before
        migrating a project or an app.
        """
        pass

    @cached_property
    def wrap_database_errors(self):
        """
+0 −8
Original line number Diff line number Diff line
@@ -371,8 +371,6 @@ class BaseDatabaseCreation(object):
        settings.DATABASES[self.connection.alias]["NAME"] = test_database_name
        self.connection.settings_dict["NAME"] = test_database_name

        self._create_test_db_pre_migrate_sql()

        # We report migrate messages at one level lower than that requested.
        # This ensures we don't get flooded with messages during testing
        # (unless you really ask to be flooded).
@@ -398,12 +396,6 @@ class BaseDatabaseCreation(object):

        return test_database_name

    def _create_test_db_pre_migrate_sql(self):
        """
        Hook for databases to load SQL before creating the test DB.
        """
        pass

    def serialize_db_to_string(self):
        """
        Serializes all data in the database into a JSON string.
Loading