From 7be73edcb51a9f13808283b7959c4a8f85083876 Mon Sep 17 00:00:00 2001
From: Marcin Ickiewicz <mickiewicz@syncad.com>
Date: Fri, 3 Jan 2025 09:59:02 +0100
Subject: [PATCH] hash db functions without schema name parameter

There is no sense to pass schema parameter to hash db computation,
at the end there is a list of tables to take into computation and all of
them belongs to hafd schema.
---
 src/hive_fork_manager/Readme.md                  |  8 ++++----
 src/hive_fork_manager/authorization.sql          |  4 ++--
 ...ve_fork_manager_update_script_generator.sh.in | 16 ++++++++--------
 src/hive_fork_manager/hived_api.sql              |  4 ++--
 .../table_schema_verification.sql                | 13 +++++++------
 .../shared_lib/calculate_schema_hash.sql         |  2 +-
 .../hive_fork_manager/state_providers/hash.sql   |  4 ++--
 7 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/src/hive_fork_manager/Readme.md b/src/hive_fork_manager/Readme.md
index 0704858e1..3b14e197d 100644
--- a/src/hive_fork_manager/Readme.md
+++ b/src/hive_fork_manager/Readme.md
@@ -594,10 +594,10 @@ Disables triggers attached to a register table. It is useful for processing irre
 ##### hive.get_impacted_accounts( operation_body )
 Returns list of accounts ( their names ) impacted by the operation. 
 
-###### hive.calculate_schema_hash( schema_name )
-Calculates hash for group of tables, used by hive.create_database_hash.
-###### hive.create_database_hash( schema_name )
-Used in update procedure, creates database hash using table schema.
+###### hive.calculate_schema_hash()
+Calculates hash for group of tables in hafd schema, used by hive.create_database_hash.
+###### hive.create_database_hash()
+Used in update procedure, creates database hash.
 
 ## Known Problems
 1. FOREIGN KEY constraints must be DEFERRABLE, otherwise we cannot guarantee success rewinding changes - the process may temporarily violate tables constraints.
diff --git a/src/hive_fork_manager/authorization.sql b/src/hive_fork_manager/authorization.sql
index feda4f4d7..bb4dd3701 100644
--- a/src/hive_fork_manager/authorization.sql
+++ b/src/hive_fork_manager/authorization.sql
@@ -194,8 +194,8 @@ GRANT EXECUTE ON FUNCTION
     , hafd._operation_from_jsonb(jsonb)
     , hafd.operation_to_jsontext(hafd.operation)
     , hafd.operation_from_jsontext(TEXT)
-    , hive.create_database_hash(schema_name TEXT)
-    , hive.calculate_schema_hash(schema_name TEXT)
+    , 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 )
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 780650010..65a9799c7 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
@@ -81,14 +81,14 @@ POSTGRES_EXTENSION_DIR='@POSTGRES_SHAREDIR@/extension'
 DB_NAME_AFTER_UPDATE="upd_${DB_NAME}" # truncate before postgres will do this
 DB_NAME_AFTER_UPDATE="${DB_NAME_AFTER_UPDATE:0:63}"
 save_table_schema() {
-  psql_do -d "$DB_NAME" -o before_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive.calculate_schema_hash('hafd')"
-  psql_do -d "$DB_NAME" -o before_update_constraints.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive.calculate_schema_hash('hafd')"
-  psql_do -d "$DB_NAME" -o before_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive.calculate_schema_hash('hafd')"
+  psql_do -d "$DB_NAME" -o before_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive.calculate_schema_hash()"
+  psql_do -d "$DB_NAME" -o before_update_constraints.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive.calculate_schema_hash()"
+  psql_do -d "$DB_NAME" -o before_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive.calculate_schema_hash()"
   psql_do -d "$DB_NAME" -o before_update_providers.txt -q -t -A -c "SELECT provider, hash FROM hive.calculate_state_provider_hashes()"
 
-  psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive.calculate_schema_hash('hafd')"
-  psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_constraings.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive.calculate_schema_hash('hafd')"
-  psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive.calculate_schema_hash('hafd')"
+  psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive.calculate_schema_hash()"
+  psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_constraings.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive.calculate_schema_hash()"
+  psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive.calculate_schema_hash()"
   psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_providers.txt -q -t -A -c "SELECT provider, hash FROM hive.calculate_state_provider_hashes()"
 }
 
@@ -97,8 +97,8 @@ verify_table_schema() {
   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;'
 
-  BEFORE_UPDATE=$(psql_do -d "$DB_NAME" -t -A -c "SELECT schema_hash FROM hive.create_database_hash('hafd')")
-  AFTER_UPDATE=$(psql_do -d "$DB_NAME_AFTER_UPDATE" -t -A -c "SELECT schema_hash FROM hive.create_database_hash('hafd')")
+  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()")
   if [ "$BEFORE_UPDATE" = "$AFTER_UPDATE" ]; then
     echo "The table schema is correct, verification completed."
     echo "Dropping temporary database"
diff --git a/src/hive_fork_manager/hived_api.sql b/src/hive_fork_manager/hived_api.sql
index 1692e24ca..578515a1e 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('hafd');
+        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('hafd');
+    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 49a44c8fe..f2ec7b6db 100644
--- a/src/hive_fork_manager/table_schema_verification.sql
+++ b/src/hive_fork_manager/table_schema_verification.sql
@@ -125,7 +125,8 @@ $BODY$;
 
 
 
-CREATE OR REPLACE FUNCTION hive.calculate_schema_hash(schema_name TEXT)
+-- calculate hafd schema hash
+CREATE OR REPLACE FUNCTION hive.calculate_schema_hash()
     RETURNS SETOF hafd.verify_table_schema
     LANGUAGE plpgsql
     VOLATILE
@@ -162,7 +163,7 @@ verified_tables_list = ARRAY[
 
     FOR _table_name IN SELECT UNNEST( verified_tables_list ) as _table_name
     LOOP
-        RETURN NEXT hive.calculate_table_schema_hash( schema_name, _table_name);
+        RETURN NEXT hive.calculate_table_schema_hash( 'hafd', _table_name);
     END LOOP;
 
     RETURN;
@@ -171,7 +172,7 @@ END;
 $BODY$
 ;
 
-CREATE OR REPLACE FUNCTION hive.create_database_hash(schema_name TEXT)
+CREATE OR REPLACE FUNCTION hive.create_database_hash()
     RETURNS SETOF hafd.table_schema
     LANGUAGE plpgsql
     VOLATILE
@@ -184,14 +185,14 @@ DECLARE
 BEGIN
     TRUNCATE hafd.table_schema;
 
-    SELECT string_agg(table_schema, ' | ') FROM hive.calculate_schema_hash(schema_name) INTO _tmp;
+    SELECT string_agg(table_schema, ' | ') FROM hive.calculate_schema_hash() INTO _tmp;
 
     SELECT string_agg(provider || hash, ' | ') FROM hive.calculate_state_provider_hashes() INTO _provider_hashes;
 
     _tmp = _tmp || _provider_hashes;
-    INSERT INTO hafd.table_schema VALUES (schema_name, MD5(_tmp)::uuid);
+    INSERT INTO hafd.table_schema VALUES ('hafd', MD5(_tmp)::uuid);
 
-    ts.schema_name := schema_name;
+    ts.schema_name := 'hafd';
     ts.schema_hash := MD5(_tmp)::uuid;
 RETURN NEXT ts;
 END;
diff --git a/tests/integration/functional/hive_fork_manager/shared_lib/calculate_schema_hash.sql b/tests/integration/functional/hive_fork_manager/shared_lib/calculate_schema_hash.sql
index cd1b0e72e..eecca8829 100644
--- a/tests/integration/functional/hive_fork_manager/shared_lib/calculate_schema_hash.sql
+++ b/tests/integration/functional/hive_fork_manager/shared_lib/calculate_schema_hash.sql
@@ -6,7 +6,7 @@ DECLARE
   _row TEXT;
 BEGIN
 FOR _row in
-  SELECT (ROW(f.table_name, f.table_schema_hash, f.columns_hash, f.constraints_hash, f.indexes_hash)::text) FROM hive.calculate_schema_hash('hafd') AS f
+  SELECT (ROW(f.table_name, f.table_schema_hash, f.columns_hash, f.constraints_hash, f.indexes_hash)::text) FROM hive.calculate_schema_hash() AS f
   EXCEPT SELECT unnest(ARRAY[
     '(blocks,6943f52d-ec57-ed27-b2e3-d8ba4b3288ca,4397b404-c56c-84e1-952e-a73d29745394,4c7b832d-5d52-83fe-fd2b-7e7a69416fae,2b354f61-618a-da7d-3380-3e12c45a3f30)',
     '(irreversible_data,dd1812c6-cabd-4382-a4bf-c355276b3839,53114e1c-c6e5-867b-6c67-1d55865180fe,77ed7932-7dab-20e3-b506-4a2d3fccfe75,f40cac4c-2fae-a597-11c8-8cc0f329e18f)',
diff --git a/tests/integration/functional/hive_fork_manager/state_providers/hash.sql b/tests/integration/functional/hive_fork_manager/state_providers/hash.sql
index f1622a7e2..d8b64bd0a 100644
--- a/tests/integration/functional/hive_fork_manager/state_providers/hash.sql
+++ b/tests/integration/functional/hive_fork_manager/state_providers/hash.sql
@@ -27,7 +27,7 @@ BEGIN
     SELECT STRING_AGG( hash, '|') FROM hive.calculate_state_provider_hashes() INTO __all_before_hashes;
     SELECT * FROM hive.calculate_state_provider_hash( 'KEYAUTH'::hafd.state_providers ) INTO __keyauth_before_hash;
 
-    SELECT schema_hash FROM hive.create_database_hash('hafd')  INTO __database_hash_before;
+    SELECT schema_hash FROM hive.create_database_hash()  INTO __database_hash_before;
 
     EXECUTE format( 'CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.context_name )
     RETURNS TEXT[]
@@ -43,7 +43,7 @@ BEGIN
 
     SELECT STRING_AGG( hash, '|') FROM hive.calculate_state_provider_hashes() INTO __all_after_hashes;
     SELECT * FROM hive.calculate_state_provider_hash( 'KEYAUTH'::hafd.state_providers ) INTO __keyauth_after_hash;
-    SELECT schema_hash FROM hive.create_database_hash('hafd')  INTO __database_hash_after;
+    SELECT schema_hash FROM hive.create_database_hash()  INTO __database_hash_after;
 
     ASSERT __all_after_hashes != __all_before_hashes, 'Hashes not changed after modification';
     ASSERT __keyauth_after_hash != __keyauth_before_hash, 'Hash not changed after modification';
-- 
GitLab