cacode commited on
Commit
3cfa99e
·
verified ·
1 Parent(s): 37be533

Upload hf-entrypoint.sh

Browse files
Files changed (1) hide show
  1. hf-entrypoint.sh +65 -19
hf-entrypoint.sh CHANGED
@@ -56,32 +56,78 @@ if command -v psql >/dev/null 2>&1; then
56
  fi
57
 
58
  # sub2api AutoSetup currently uses a hard 60s migration context.
59
- # Migration 033 may exceed this on remote managed PostgreSQL.
60
- # Pre-apply 033 once (without recording schema_migrations) so the real runner
61
- # can re-run it quickly and record checksum in its own flow.
62
- if command -v psql >/dev/null 2>&1 && command -v curl >/dev/null 2>&1; then
63
- SCHEMA_MIGRATIONS_EXISTS=$(psql "dbname=${DATABASE_DBNAME}" -tAc "SELECT to_regclass('public.schema_migrations') IS NOT NULL" 2>/dev/null | tr -d '[:space:]' || true)
64
- if [ "${SCHEMA_MIGRATIONS_EXISTS}" = "true" ]; then
65
- MIG_033_APPLIED=$(psql "dbname=${DATABASE_DBNAME}" -tAc "SELECT 1 FROM schema_migrations WHERE filename='033_ops_monitoring_vnext.sql' LIMIT 1" 2>/dev/null | tr -d '[:space:]' || true)
 
 
 
 
 
 
 
 
 
 
 
66
  else
67
- MIG_033_APPLIED=""
68
  fi
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  if [ "${MIG_033_APPLIED}" != "1" ]; then
71
- MIG_033_URL="${MIG_033_URL:-https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/backend/migrations/033_ops_monitoring_vnext.sql}"
72
- MIG_033_SQL="/tmp/033_ops_monitoring_vnext.sql"
73
- if curl -fsSL "${MIG_033_URL}" -o "${MIG_033_SQL}" >/dev/null 2>&1; then
74
- echo "[boot] pre-applying migration 033 to avoid AutoSetup timeout..."
75
- if PGOPTIONS="-c statement_timeout=0 -c lock_timeout=0" psql "dbname=${DATABASE_DBNAME}" -v ON_ERROR_STOP=1 -f "${MIG_033_SQL}" >/dev/null 2>&1; then
76
- echo "[boot] pre-apply 033 done"
77
- else
78
- echo "[warn] pre-apply 033 failed; AutoSetup will continue with built-in migration flow"
79
- fi
80
- rm -f "${MIG_033_SQL}" || true
81
  else
82
- echo "[warn] failed to download migration 033 from ${MIG_033_URL}"
 
 
83
  fi
84
  fi
 
 
 
 
 
 
 
 
 
 
 
 
85
  fi
86
 
87
  DATABASE_QUERY="sslmode=${DATABASE_SSLMODE}&sslrootcert=${DATABASE_SSLROOTCERT}"
 
56
  fi
57
 
58
  # sub2api AutoSetup currently uses a hard 60s migration context.
59
+ # Pre-apply selected heavy migrations when needed to reduce timeout risk.
60
+ is_true() {
61
+ case "${1:-}" in
62
+ 1|t|T|true|TRUE|yes|YES|y|Y|on|ON) return 0 ;;
63
+ *) return 1 ;;
64
+ esac
65
+ }
66
+
67
+ migration_applied() {
68
+ local mig_name="$1"
69
+ local out=""
70
+ if ! is_true "${SCHEMA_MIGRATIONS_EXISTS:-}"; then
71
+ echo "0"
72
+ return 0
73
+ fi
74
+ out=$(psql "dbname=${DATABASE_DBNAME}" -tAc "SELECT 1 FROM schema_migrations WHERE filename='${mig_name}' LIMIT 1" 2>/dev/null | tr -d '[:space:]' || true)
75
+ if [ "${out}" = "1" ]; then
76
+ echo "1"
77
  else
78
+ echo "0"
79
  fi
80
+ }
81
+
82
+ preapply_migration() {
83
+ local mig_name="$1"
84
+ local mig_url="$2"
85
+ local mig_sql="/tmp/${mig_name}"
86
+ if curl -fsSL "${mig_url}" -o "${mig_sql}" >/dev/null 2>&1; then
87
+ echo "[boot] pre-applying migration ${mig_name} ..."
88
+ if PGOPTIONS="-c statement_timeout=0 -c lock_timeout=0" psql "dbname=${DATABASE_DBNAME}" -v ON_ERROR_STOP=1 -f "${mig_sql}" >/dev/null 2>&1; then
89
+ echo "[boot] pre-apply ${mig_name} done"
90
+ rm -f "${mig_sql}" || true
91
+ return 0
92
+ fi
93
+ echo "[warn] pre-apply ${mig_name} failed; AutoSetup will continue with built-in migration flow"
94
+ rm -f "${mig_sql}" || true
95
+ return 1
96
+ fi
97
+ echo "[warn] failed to download migration ${mig_name} from ${mig_url}"
98
+ return 1
99
+ }
100
+
101
+ if command -v psql >/dev/null 2>&1 && command -v curl >/dev/null 2>&1; then
102
+ SCHEMA_MIGRATIONS_EXISTS=$(psql "dbname=${DATABASE_DBNAME}" -tAc "SELECT to_regclass('public.schema_migrations') IS NOT NULL" 2>/dev/null | tr -d '[:space:]' || true)
103
+ OPS_CORE_EXISTS=$(psql "dbname=${DATABASE_DBNAME}" -tAc "SELECT to_regclass('public.ops_error_logs') IS NOT NULL" 2>/dev/null | tr -d '[:space:]' || true)
104
+
105
+ MIG_033_APPLIED="$(migration_applied "033_ops_monitoring_vnext.sql")"
106
+ MIG_062_APPLIED="$(migration_applied "062_add_scheduler_and_usage_composite_indexes_notx.sql")"
107
+ MIG_065_APPLIED="$(migration_applied "065_add_search_trgm_indexes.sql")"
108
 
109
  if [ "${MIG_033_APPLIED}" != "1" ]; then
110
+ # If ops core tables already exist, avoid re-running destructive 033 pre-apply.
111
+ if is_true "${OPS_CORE_EXISTS}"; then
112
+ echo "[boot] skip pre-apply 033: ops schema already present"
 
 
 
 
 
 
 
113
  else
114
+ preapply_migration \
115
+ "033_ops_monitoring_vnext.sql" \
116
+ "${MIG_033_URL:-https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/backend/migrations/033_ops_monitoring_vnext.sql}" || true
117
  fi
118
  fi
119
+
120
+ if [ "${MIG_062_APPLIED}" != "1" ]; then
121
+ preapply_migration \
122
+ "062_add_scheduler_and_usage_composite_indexes_notx.sql" \
123
+ "${MIG_062_URL:-https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/backend/migrations/062_add_scheduler_and_usage_composite_indexes_notx.sql}" || true
124
+ fi
125
+
126
+ if [ "${MIG_065_APPLIED}" != "1" ]; then
127
+ preapply_migration \
128
+ "065_add_search_trgm_indexes.sql" \
129
+ "${MIG_065_URL:-https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/backend/migrations/065_add_search_trgm_indexes.sql}" || true
130
+ fi
131
  fi
132
 
133
  DATABASE_QUERY="sslmode=${DATABASE_SSLMODE}&sslrootcert=${DATABASE_SSLROOTCERT}"