From 882127fee92fb22a61ab66d0fc1ed6a52088864e Mon Sep 17 00:00:00 2001
From: Marcin Ickiewicz <mickiewicz@syncad.com>
Date: Thu, 9 Jan 2025 10:42:39 +0100
Subject: [PATCH] eliminate state providers tables (and their shadows) from
 hash computations

Currently state providers creates tables in hafd schema. All tables in
hafd schemas are taken to db hash computation, but hashes for state
providers are computed differently, ans should not affect hafd hash
schema.
---
 src/hive_fork_manager/update.sql                      | 11 +++++++----
 .../hive_fork_manager/test_update_script.sh           |  9 +++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/hive_fork_manager/update.sql b/src/hive_fork_manager/update.sql
index 4b44c8ac8..40842d021 100644
--- a/src/hive_fork_manager/update.sql
+++ b/src/hive_fork_manager/update.sql
@@ -326,10 +326,13 @@ AS
 $BODY$
 BEGIN
 
-    RETURN QUERY SELECT (hive_update.calculate_table_schema_hash( 'hafd', table_name)).*
-    FROM information_schema.tables
-    WHERE table_schema = 'hafd'
-    AND table_type = 'BASE TABLE';
+    RETURN QUERY SELECT (hive_update.calculate_table_schema_hash( 'hafd', ist.table_name)).*
+    FROM information_schema.tables ist
+    LEFT JOIN hafd.state_providers_registered spr ON ist.table_name ILIKE ANY( spr.tables )
+    WHERE ist.table_schema = 'hafd'
+    AND ist.table_type = 'BASE TABLE'
+    AND spr.tables IS NULL
+    AND ist.table_name NOT ILIKE 'shadow_hafd_%'; -- shadow tables, only possible for state providers
 END;
 $BODY$
 ;
diff --git a/tests/integration/functional/hive_fork_manager/test_update_script.sh b/tests/integration/functional/hive_fork_manager/test_update_script.sh
index 0834cbe5a..58a39c0f3 100755
--- a/tests/integration/functional/hive_fork_manager/test_update_script.sh
+++ b/tests/integration/functional/hive_fork_manager/test_update_script.sh
@@ -276,4 +276,13 @@ exec_sql "CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.
              ;"
 failswith 1 update_database
 
+
+printf "\nTEST: Use state provider. This should pass\n"
+prepare_database
+exec_sql "CREATE SCHEMA workaround;
+          SELECT hive.app_create_context('workaround', 'workaround');
+          SELECT hive.app_state_provider_import( 'KEYAUTH', 'workaround' );
+          "
+update_database
+
 echo "Succeeded"
-- 
GitLab