From 44bd8ebefc3db197128fa85483c497975d932720 Mon Sep 17 00:00:00 2001
From: 0xawaz <amina.waddiz@gmail.com>
Date: Mon, 25 Nov 2024 14:03:35 +0100
Subject: [PATCH] fix: use COPY for large binary files

---
 fhevm-engine/coprocessor/docker-compose.yml |  2 +-
 fhevm-engine/fhevm-db/initialize_db.sh      | 37 ++++++++-------------
 2 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml
index 2067eaea..aed104ef 100644
--- a/fhevm-engine/coprocessor/docker-compose.yml
+++ b/fhevm-engine/coprocessor/docker-compose.yml
@@ -20,7 +20,7 @@ services:
 
   migration:
       container_name: migration
-      image: ghcr.io/zama-ai/fhevm-db-migration:v1
+      image: ghcr.io/zama-ai/fhevm-db-migration:v1.1
       environment:
         DATABASE_URL: postgresql://postgres:postgres@db:5432/coprocessor
       depends_on:
diff --git a/fhevm-engine/fhevm-db/initialize_db.sh b/fhevm-engine/fhevm-db/initialize_db.sh
index 90285436..20400c40 100644
--- a/fhevm-engine/fhevm-db/initialize_db.sh
+++ b/fhevm-engine/fhevm-db/initialize_db.sh
@@ -9,32 +9,23 @@ echo "Running migrations..."
 sqlx migrate run --source /migrations || { echo "Failed to run migrations."; exit 1; }
 
 # 3. Insert test tenant with keys
-echo "Running Insert test tenant..."
+echo "Start preparing tenant query..."
 TENANT_API_KEY=a1503fb6-d79b-4e9e-826d-44cf262f3e05
+CHAIN_ID=12345
 ACL_CONTRACT_ADDRESS=0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2
 INPUT_VERIFIER_ADDRESS=0x69dE3158643e738a0724418b21a35FAA20CBb1c5
+PKS_FILE="/fhevm-keys/pks"
+SKS_FILE="/fhevm-keys/sks"
+PUBLIC_PARAMS_FILE="/fhevm-keys/pp"
 
-PKS="$(cat /fhevm-keys/pks | xxd -p | tr -d '\n')"
-SKS="$(cat /fhevm-keys/sks | xxd -p | tr -d '\n')"
-PUBLIC_PARAMS="$(cat /fhevm-keys/pp | xxd -p | tr -d '\n')"
-CKS="$(cat /fhevm-keys/cks | xxd -p | tr -d '\n')"
-
-QUERY="
-INSERT INTO tenants(tenant_api_key, chain_id, acl_contract_address, verifying_contract_address, pks_key, sks_key, public_params, cks_key)
-        VALUES (
-            '${TENANT_API_KEY}',
-            12345,
-            '${ACL_CONTRACT_ADDRESS}',
-            '${INPUT_VERIFIER_ADDRESS}',
-            decode('${PKS}', 'hex'),
-            decode('${SKS}', 'hex'),
-            decode('${PUBLIC_PARAMS}', 'hex'),
-            decode('${CKS}', 'hex')
-        )
-"
-
-echo $QUERY | psql $DATABASE_URL
-
-echo 'Test tenant insertion done'
+TMP_CSV="/tmp/tenant_data.csv"
+echo "tenant_api_key,chain_id,acl_contract_address,verifying_contract_address,pks_key,sks_key,public_params" > $TMP_CSV
+
+echo "$TENANT_API_KEY,$CHAIN_ID,$ACL_CONTRACT_ADDRESS,$INPUT_VERIFIER_ADDRESS,\"\\x$(cat $PKS_FILE | xxd -p | tr -d '\n')\",\"\\x$(cat $SKS_FILE | xxd -p | tr -d '\n')\",\"\\x$(cat $PUBLIC_PARAMS_FILE | xxd -p | tr -d '\n')\"" >> $TMP_CSV
+
+echo "Inserting tenant data using \COPY..."
+psql $DATABASE_URL -c "\COPY tenants (tenant_api_key, chain_id, acl_contract_address, verifying_contract_address, pks_key, sks_key, public_params) FROM '$TMP_CSV' CSV HEADER;"
+
+rm -f $TMP_CSV
 
 echo "Database initialization complete."
\ No newline at end of file