From 5666a45369df64d87563e9ad3f6157262368f9fc Mon Sep 17 00:00:00 2001
From: Marcin Ickiewicz <mickiewicz@syncad.com>
Date: Tue, 7 Jan 2025 13:19:56 +0100
Subject: [PATCH] use drop function if exists instead create or replace fior
 hash functions

---
 src/hive_fork_manager/CMakeLists.txt              |  1 -
 src/hive_fork_manager/authorization.sql           | 10 +++++-----
 ...ive_fork_manager_update_script_generator.sh.in |  1 +
 src/hive_fork_manager/hived_api.sql               |  4 ++--
 .../table_schema_verification.sql                 | 15 ++++++++++-----
 tests/integration/functional/tools/common.sh      |  5 +++++
 tests/integration/functional/tools/test.sh        |  8 +++++---
 7 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/hive_fork_manager/CMakeLists.txt b/src/hive_fork_manager/CMakeLists.txt
index da737dd5c..d103c0c30 100644
--- a/src/hive_fork_manager/CMakeLists.txt
+++ b/src/hive_fork_manager/CMakeLists.txt
@@ -53,7 +53,6 @@ ADD_PSQL_EXTENSION(
             app_api_impl.sql
             hived_api.sql
             app_api.sql
-            table_schema_verification.sql
             api_helpers/block_api_support.sql
             authorization.sql
             get_impacted_accounts.sql
diff --git a/src/hive_fork_manager/authorization.sql b/src/hive_fork_manager/authorization.sql
index bb4dd3701..eabef59f6 100644
--- a/src/hive_fork_manager/authorization.sql
+++ b/src/hive_fork_manager/authorization.sql
@@ -194,12 +194,12 @@ GRANT EXECUTE ON FUNCTION
     , hafd._operation_from_jsonb(jsonb)
     , hafd.operation_to_jsontext(hafd.operation)
     , hafd.operation_from_jsontext(TEXT)
-    , hive.create_database_hash()
-    , hive.calculate_schema_hash()
+    --, hive.create_database_hash()
+    --, hive.calculate_schema_hash()
     , hive.all_indexes_have_status(_status hafd.index_status)
-    , hive.calculate_table_schema_hash(schema_name TEXT,_table_name TEXT)
-    , hive.calculate_state_provider_hash(_provider hafd.state_providers )
-    , hive.calculate_state_provider_hashes()
+    --, hive.calculate_table_schema_hash(schema_name TEXT,_table_name TEXT)
+    --, hive.calculate_state_provider_hash(_provider hafd.state_providers )
+    --, hive.calculate_state_provider_hashes()
     , hive.are_any_indexes_missing()
     , hive.are_indexes_restored()
     , hive.are_fk_dropped()
diff --git a/src/hive_fork_manager/hive_fork_manager_update_script_generator.sh.in b/src/hive_fork_manager/hive_fork_manager_update_script_generator.sh.in
index 11a740c95..a091517b1 100755
--- a/src/hive_fork_manager/hive_fork_manager_update_script_generator.sh.in
+++ b/src/hive_fork_manager/hive_fork_manager_update_script_generator.sh.in
@@ -96,6 +96,7 @@ verify_table_schema() {
   echo "Attempting to verify if existing table schema is correct..."
   psql_do -a -d postgres -c "CREATE DATABASE $DB_NAME_AFTER_UPDATE WITH OWNER $DB_ADMIN;"
   psql_do -a -d "$DB_NAME_AFTER_UPDATE" -c 'CREATE EXTENSION hive_fork_manager CASCADE;'
+  psql_do -d "$DB_NAME_AFTER_UPDATE" -q -t -A -f "$SCRIPTPATH/table_schema_verification.sql"
 
   BEFORE_UPDATE=$(psql_do -d "$DB_NAME" -t -A -c "SELECT schema_hash FROM hive.create_database_hash()")
   AFTER_UPDATE=$(psql_do -d "$DB_NAME_AFTER_UPDATE" -t -A -c "SELECT schema_hash FROM hive.create_database_hash()")
diff --git a/src/hive_fork_manager/hived_api.sql b/src/hive_fork_manager/hived_api.sql
index 578515a1e..0f6374743 100644
--- a/src/hive_fork_manager/hived_api.sql
+++ b/src/hive_fork_manager/hived_api.sql
@@ -451,7 +451,7 @@ BEGIN
     IF EXISTS ( SELECT 1 FROM hafd.events_queue WHERE id = hive.unreachable_event_id() LIMIT 1 ) THEN
         SELECT MAX(eq.id) + 1 FROM hafd.events_queue eq WHERE eq.id != hive.unreachable_event_id() INTO __events_id;
         PERFORM SETVAL( 'hafd.events_queue_id_seq', __events_id, false );
-        PERFORM hive.create_database_hash();
+        -- PERFORM hive.create_database_hash();
         RETURN;
     END IF;
 
@@ -463,7 +463,7 @@ BEGIN
 
     INSERT INTO hafd.fork(block_num, time_of_fork) VALUES( 1, '2016-03-24 16:05:00'::timestamp ) ON CONFLICT DO NOTHING;
 
-    PERFORM hive.create_database_hash();
+    -- PERFORM hive.create_database_hash();
 END;
 $BODY$
 ;
diff --git a/src/hive_fork_manager/table_schema_verification.sql b/src/hive_fork_manager/table_schema_verification.sql
index b7db0eef2..2c0358f61 100644
--- a/src/hive_fork_manager/table_schema_verification.sql
+++ b/src/hive_fork_manager/table_schema_verification.sql
@@ -1,4 +1,5 @@
-CREATE OR REPLACE FUNCTION hive.calculate_table_schema_hash(schema_name TEXT,_table_name TEXT)
+DROP FUNCTION IF EXISTS hive.calculate_table_schema_hash;
+CREATE FUNCTION hive.calculate_table_schema_hash(schema_name TEXT,_table_name TEXT)
     RETURNS hafd.verify_table_schema
     LANGUAGE plpgsql
     STABLE
@@ -90,7 +91,8 @@ BEGIN
 END;
 $BODY$;
 
-CREATE OR REPLACE FUNCTION hive.calculate_state_provider_hash(_provider hafd.state_providers )
+DROP FUNCTION IF EXISTS hive.calculate_state_provider_hash;
+CREATE FUNCTION hive.calculate_state_provider_hash(_provider hafd.state_providers )
     RETURNS TEXT --md5 of start_provider function
     LANGUAGE plpgsql
     STABLE
@@ -108,7 +110,8 @@ BEGIN
 END;
 $BODY$;
 
-CREATE OR REPLACE FUNCTION hive.calculate_state_provider_hashes()
+DROP FUNCTION IF EXISTS hive.calculate_state_provider_hashes;
+CREATE FUNCTION hive.calculate_state_provider_hashes()
     RETURNS SETOF hafd.state_provider_and_hash
     LANGUAGE plpgsql
     STABLE
@@ -126,7 +129,8 @@ $BODY$;
 
 
 -- calculate hafd schema hash
-CREATE OR REPLACE FUNCTION hive.calculate_schema_hash()
+DROP FUNCTION IF EXISTS hive.calculate_schema_hash;
+CREATE FUNCTION hive.calculate_schema_hash()
     RETURNS SETOF hafd.verify_table_schema
     LANGUAGE plpgsql
     STABLE
@@ -142,7 +146,8 @@ END;
 $BODY$
 ;
 
-CREATE OR REPLACE FUNCTION hive.create_database_hash()
+DROP FUNCTION IF EXISTS hive.create_database_hash();
+CREATE FUNCTION hive.create_database_hash()
     RETURNS SETOF hafd.table_schema
     LANGUAGE plpgsql
     VOLATILE
diff --git a/tests/integration/functional/tools/common.sh b/tests/integration/functional/tools/common.sh
index f16ede693..846db08a2 100755
--- a/tests/integration/functional/tools/common.sh
+++ b/tests/integration/functional/tools/common.sh
@@ -39,6 +39,7 @@ setup_test_database() {
   setup_scripts_dir_path="$1"
   postgres_port="$2"
   test_path="$3"
+  extension_path="$4"
 
   test_directory=$(dirname "${test_path}");
   sql_setup_fixture="./${test_directory}/fixture.sql";
@@ -58,6 +59,10 @@ setup_test_database() {
     exit 1
   fi
 
+  # ATTENTION: normally the extension does not contain hash functions
+  # so the db is little different than production state, but these are functional tests so IMO it is acceptable
+  psql -p "${postgres_port}" -d "${DB_NAME}" -a -v ON_ERROR_STOP=on -f "${extension_path}/table_schema_verification.sql"
+
   # TODO(mickiewicz@syncad.com): remove when releasing on pg16 where 'public' schema is not accessible by default
   if ! psql -p "${postgres_port}" -d "${DB_NAME}" -a -v ON_ERROR_STOP=on -c "REVOKE CREATE ON SCHEMA public FROM PUBLIC;";
   then
diff --git a/tests/integration/functional/tools/test.sh b/tests/integration/functional/tools/test.sh
index a3f22a286..eefde30ca 100755
--- a/tests/integration/functional/tools/test.sh
+++ b/tests/integration/functional/tools/test.sh
@@ -8,7 +8,7 @@ script_to_execute_after_testfun="$5"
 
 . ./tools/common.sh
 
-setup_test_database "$setup_scripts_dir_path" "$postgres_port" "$test_path"
+setup_test_database "$setup_scripts_dir_path" "$postgres_port" "$test_path" "$extension_path"
 
 trap on_exit EXIT;
 
@@ -66,9 +66,11 @@ for testfun in ${tests}; do
   done
 
   if [ -n "${script_to_execute_after_testfun}" ]; then
-    pg_call="-p $postgres_port -d $DB_NAME -v ON_ERROR_STOP=on -c"
-    psql ${pg_call} "UPDATE pg_extension SET extversion = '1.0' WHERE extname = 'hive_fork_manager';"
+    pg_call="-p $postgres_port -d $DB_NAME -v ON_ERROR_STOP=on"
+    psql ${pg_call} -c "UPDATE pg_extension SET extversion = '1.0' WHERE extname = 'hive_fork_manager';"
     sudo "${script_to_execute_after_testfun}" --haf-db-name="$DB_NAME";
+    # for testing hash functions we ned to add them after update which remove them
+    psql ${pg_call} -f "${extension_path}/table_schema_verification.sql"
   fi
 done
 
-- 
GitLab