chore(sql): add export/import clean schema scripts for pglogical-safe sync

This commit is contained in:
root 2025-10-08 20:35:01 +08:00
parent 61c9fe82ce
commit 9ee3ee54b0
3 changed files with 64 additions and 0 deletions

View File

@ -1,3 +1,5 @@
sudo -u postgres psql -d account -c "GRANT USAGE ON SCHEMA pglogical TO PUBLIC;"
-- 登录 postgres
sudo -u postgres psql -d account
@ -11,5 +13,7 @@ GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO shenlan;
-- 授权 pglogical schema 使用权限(防止混用)
GRANT USAGE ON SCHEMA pglogical TO shenlan;
\q

View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euo pipefail
DB_URL="postgres://shenlan:password@127.0.0.1:5432/account?sslmode=disable"
OUT="/tmp/schema_clean.sql"
echo ">>> Exporting clean schema (PostgreSQL 16 compatible)"
pg_dump \
--schema-only \
--no-owner \
--no-privileges \
--exclude-schema=pglogical \
"$DB_URL" \
| grep -v -i "EXTENSION pglogical" \
| grep -v -i "COMMENT ON EXTENSION pglogical" \
| grep -v -i "SCHEMA pglogical" \
> "$OUT"
echo "✅ Schema exported to $OUT"
grep -i pglogical "$OUT" || echo "✅ pglogical completely removed"

View File

@ -0,0 +1,39 @@
#!/usr/bin/env bash
#
# scripts/import_schema_clean.sh
# ---------------------------------------------
# Import a cleaned schema.sql file into PostgreSQL.
# Safe for re-run (幂等导入)
# ---------------------------------------------
set -euo pipefail
# ====== Configuration ======
DB_URL=${1:-"postgres://shenlan:password@127.0.0.1:5432/account?sslmode=disable"}
IN_FILE=${2:-"/tmp/schema_clean.sql"}
# ====== Validation ======
if [ ! -f "$IN_FILE" ]; then
echo "❌ File not found: $IN_FILE"
echo "💡 请先运行 export_schema_clean.sh 导出 schema"
exit 1
fi
if ! command -v psql >/dev/null 2>&1; then
echo "❌ 未检测到 psql请先安装 PostgreSQL 客户端"
exit 1
fi
# ====== Import schema ======
echo ">>> Importing schema into database"
echo "---------------------------------------------"
echo "Database: $DB_URL"
echo "Schema: $IN_FILE"
echo "---------------------------------------------"
psql "$DB_URL" -v ON_ERROR_STOP=1 -f "$IN_FILE"
echo ""
echo "✅ Schema import completed successfully"
echo "---------------------------------------------"