From bffbc4665a919c13a91f2e646a32298d9485b7e6 Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 26 Jun 2025 22:54:00 +0800 Subject: [PATCH] . --- supabase/chatdesk/init.sh | 37 +++++++++++++++++++++++++-------- supabase/chatdesk/init.sh.old | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 supabase/chatdesk/init.sh.old diff --git a/supabase/chatdesk/init.sh b/supabase/chatdesk/init.sh index 77cfa1c..70fd41e 100644 --- a/supabase/chatdesk/init.sh +++ b/supabase/chatdesk/init.sh @@ -4,6 +4,7 @@ set -e # 标记文件路径(使用系统级目录) FLAG_DIR="/var/lib/db-init" FLAG_FILE="$FLAG_DIR/.db_initialized" +LOG_FILE="/var/log/postgres-init.log" # 数据库连接配置 export PGUSER=supabase_admin @@ -12,28 +13,46 @@ export PGHOST=127.0.0.1 export PGPORT=5432 export PGDATABASE=postgres -# 提前创建标记目录 +# 创建标记目录和日志目录 mkdir -p "$FLAG_DIR" +mkdir -p "$(dirname "$LOG_FILE")" # 如果标记已存在,跳过执行 if [ -f "$FLAG_FILE" ]; then - echo "✅ Database has already been initialized, skipping." + echo "✅ Database has already been initialized, skipping." | tee -a "$LOG_FILE" exit 0 fi -echo "🚀 Starting database initialization..." +echo "🚀 Starting database initialization..." | tee -a "$LOG_FILE" + +# 封装重试逻辑 +retry_until_success() { + local sql_file="$1" + + while true; do + echo " ▶ Executing $sql_file" | tee -a "$LOG_FILE" + + if psql -v ON_ERROR_STOP=1 -f "$sql_file" >> "$LOG_FILE" 2>&1; then + echo " ✅ Success: $sql_file" | tee -a "$LOG_FILE" + break + else + echo " ❌ ERROR in $sql_file" | tee -a "$LOG_FILE" + echo " 🔁 Retrying in 5 seconds..." | tee -a "$LOG_FILE" + sleep 5 + fi + done +} # 执行 migrations -echo "📂 Running migrations..." +echo "📂 Running migrations..." | tee -a "$LOG_FILE" for file in $(ls /supabase/chatdesk/supabase/migrations/*.sql | sort); do - echo " ▶ Executing $file" - psql -f "$file" + retry_until_success "$file" done # 执行 seed.sql -echo "🌱 Running seed.sql..." -psql -f /supabase/chatdesk/supabase/seed.sql +echo "🌱 Running seed.sql..." | tee -a "$LOG_FILE" +retry_until_success /supabase/chatdesk/supabase/seed.sql # 写入初始化标记 touch "$FLAG_FILE" -echo "✅ Database initialization complete. Marked as initialized at $FLAG_FILE" +echo "✅ Database initialization complete. Marked as initialized at $FLAG_FILE" | tee -a "$LOG_FILE" diff --git a/supabase/chatdesk/init.sh.old b/supabase/chatdesk/init.sh.old new file mode 100644 index 0000000..77cfa1c --- /dev/null +++ b/supabase/chatdesk/init.sh.old @@ -0,0 +1,39 @@ +#!/bin/bash +set -e + +# 标记文件路径(使用系统级目录) +FLAG_DIR="/var/lib/db-init" +FLAG_FILE="$FLAG_DIR/.db_initialized" + +# 数据库连接配置 +export PGUSER=supabase_admin +export PGPASSWORD=postgres +export PGHOST=127.0.0.1 +export PGPORT=5432 +export PGDATABASE=postgres + +# 提前创建标记目录 +mkdir -p "$FLAG_DIR" + +# 如果标记已存在,跳过执行 +if [ -f "$FLAG_FILE" ]; then + echo "✅ Database has already been initialized, skipping." + exit 0 +fi + +echo "🚀 Starting database initialization..." + +# 执行 migrations +echo "📂 Running migrations..." +for file in $(ls /supabase/chatdesk/supabase/migrations/*.sql | sort); do + echo " ▶ Executing $file" + psql -f "$file" +done + +# 执行 seed.sql +echo "🌱 Running seed.sql..." +psql -f /supabase/chatdesk/supabase/seed.sql + +# 写入初始化标记 +touch "$FLAG_FILE" +echo "✅ Database initialization complete. Marked as initialized at $FLAG_FILE"