feat(database): 优化开发环境数据库连接配置逻辑
- 添加 database_url_targets_local_port 函数验证数据库URL端口匹配 - 新增 database_url_matches_container 变量标识数据库URL与容器匹配状态 - 更新 postgres_port 变量获取容器实际端口值 - 重构数据库URL配置条件判断逻辑 - 添加容器凭证刷新提示信息 - 优化Docker数据库创建条件判断流程
This commit is contained in:
+46
-6
@@ -65,6 +65,35 @@ postgres_host_port() {
|
||||
fi
|
||||
}
|
||||
|
||||
database_url_targets_local_port() {
|
||||
local database_url="$1"
|
||||
local expected_port="$2"
|
||||
local authority host_port port
|
||||
|
||||
case "$database_url" in
|
||||
postgres://*|postgresql://*) ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
|
||||
authority="${database_url#*://}"
|
||||
authority="${authority%%/*}"
|
||||
host_port="${authority##*@}"
|
||||
|
||||
case "$host_port" in
|
||||
localhost|127.0.0.1|\[::1\])
|
||||
port=5432
|
||||
;;
|
||||
localhost:*|127.0.0.1:*|\[::1\]:*)
|
||||
port="${host_port##*:}"
|
||||
;;
|
||||
*)
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
[[ "$port" == "$expected_port" ]]
|
||||
}
|
||||
|
||||
stop_stale_api_processes() {
|
||||
local api_port="${HTTP_ADDR:-:8088}"
|
||||
api_port="${api_port##*:}"
|
||||
@@ -109,12 +138,19 @@ stop_stale_api_processes() {
|
||||
|
||||
database_url_was_configured=0
|
||||
pg_container_was_configured=0
|
||||
database_url_matches_container=0
|
||||
[[ -n "${AI_GATEWAY_DATABASE_URL:-}" ]] && database_url_was_configured=1
|
||||
[[ -n "${AI_GATEWAY_PG_CONTAINER:-}" ]] && pg_container_was_configured=1
|
||||
|
||||
postgres_container="$(find_postgres_container || true)"
|
||||
postgres_port=5432
|
||||
if [[ -n "$postgres_container" ]]; then
|
||||
export AI_GATEWAY_PG_CONTAINER="$postgres_container"
|
||||
postgres_port="$(postgres_host_port "$postgres_container")"
|
||||
if [[ "$database_url_was_configured" == "1" ]] \
|
||||
&& database_url_targets_local_port "$AI_GATEWAY_DATABASE_URL" "$postgres_port"; then
|
||||
database_url_matches_container=1
|
||||
fi
|
||||
elif [[ "$pg_container_was_configured" == "1" ]]; then
|
||||
echo "[ai-gateway] configured PostgreSQL container is unavailable; using the database URL without Docker database creation" >&2
|
||||
fi
|
||||
@@ -129,20 +165,24 @@ if [[ -z "${AI_GATEWAY_PG_PASSWORD:-}" && -n "$postgres_container" ]]; then
|
||||
fi
|
||||
export AI_GATEWAY_PG_PASSWORD="${AI_GATEWAY_PG_PASSWORD:-easyai2025}"
|
||||
export AI_GATEWAY_DATABASE_NAME="${AI_GATEWAY_DATABASE_NAME:-easyai_ai_gateway}"
|
||||
if [[ "$database_url_was_configured" == "0" ]]; then
|
||||
postgres_port=5432
|
||||
if [[ -n "$postgres_container" ]]; then
|
||||
postgres_port="$(postgres_host_port "$postgres_container")"
|
||||
fi
|
||||
if [[ "$database_url_was_configured" == "0" || "$database_url_matches_container" == "1" ]]; then
|
||||
export AI_GATEWAY_DATABASE_URL="postgresql://${AI_GATEWAY_PG_USER}:${AI_GATEWAY_PG_PASSWORD}@localhost:${postgres_port}/${AI_GATEWAY_DATABASE_NAME}?sslmode=disable"
|
||||
fi
|
||||
|
||||
echo "[ai-gateway] using configured local database connection (credentials redacted)"
|
||||
if [[ "$database_url_matches_container" == "1" ]]; then
|
||||
echo "[ai-gateway] refreshed credentials from local PostgreSQL container: ${postgres_container}"
|
||||
fi
|
||||
|
||||
if [[ "${AI_GATEWAY_SKIP_DB_CREATE:-}" == "1" ]]; then
|
||||
echo "[ai-gateway] skipping Docker database creation"
|
||||
elif [[ -n "$postgres_container" && ( "$database_url_was_configured" == "0" || "$pg_container_was_configured" == "1" ) ]]; then
|
||||
elif [[ -n "$postgres_container" \
|
||||
&& ( "$database_url_was_configured" == "0" \
|
||||
|| "$pg_container_was_configured" == "1" \
|
||||
|| "$database_url_matches_container" == "1" ) ]]; then
|
||||
scripts/create-database.sh
|
||||
elif [[ -n "$postgres_container" ]]; then
|
||||
echo "[ai-gateway] preserving configured database URL; skipping Docker database creation"
|
||||
else
|
||||
echo "[ai-gateway] no matching Docker database container selected; skipping database creation"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user