Skip to content

Commit ad17f97

Browse files
author
Lukas Wingerberg
committed
adopt more intelligent schema diff logic
1 parent 7f67fbd commit ad17f97

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

rootfs/container/entrypoint.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ sync_mysql_schema() {
9696

9797
log "Database is not empty. Checking for schema differences..."
9898

99-
# <-- CHANGED: Wrapped diff in an 'if' to capture exit code and prevent 'set -e' exit
10099
if ! mysqldump -h"$PDNS_GMYSQL_HOST" -P"$PDNS_GMYSQL_PORT" -u"$PDNS_GMYSQL_USER" -p"$PDNS_GMYSQL_PASSWORD" --no-data "$PDNS_GMYSQL_DBNAME" | diff - "$schema_file" > "$temp_diff_file"; then
101100
log "Detected differences in MySQL schema. Diff output:"
102101
cat "$temp_diff_file"
@@ -128,7 +127,6 @@ sync_mysql_schema() {
128127
sync_pgsql_schema() {
129128
local schema_file="/etc/pdns/pgsql_schema.sql"
130129
local temp_diff_file="/tmp/schema_diff.sql"
131-
# <-- CHANGED: Schema name is hardcoded to 'pdns' as requested
132130
local schema_name="pdns"
133131

134132
# Verify PostgreSQL-related environment variables
@@ -150,7 +148,6 @@ sync_pgsql_schema() {
150148
export PGPASSWORD="$PDNS_GPGSQL_PASSWORD"
151149

152150
# Check if the target schema is empty or non-existent
153-
# <-- CHANGED: Check count in 'pdns' schema
154151
local table_count
155152
log "Checking table count in '$schema_name' schema..."
156153
table_count=$(psql -h "$PDNS_GPGSQL_HOST" -p "$PDNS_GPGSQL_PORT" -U "$PDNS_GPGSQL_USER" -d "$PDNS_GPGSQL_DBNAME" -t -c \
@@ -167,7 +164,6 @@ sync_pgsql_schema() {
167164
log "No tables found in '$schema_name' schema. Database appears to be empty."
168165
log "Initializing database from $schema_file (forcing schema '$schema_name')..."
169166

170-
# <-- CHANGED: Wrap schema import to force 'pdns' schema and search_path
171167
(
172168
echo "CREATE SCHEMA IF NOT EXISTS \"$schema_name\";"
173169
echo "SET search_path = \"$schema_name\";"
@@ -185,9 +181,16 @@ sync_pgsql_schema() {
185181
log "Database is not empty ($table_count tables found in '$schema_name'). Checking for schema differences..."
186182
log "Comparing PostgreSQL schema '$schema_name' with local schema.sql file..."
187183

188-
# <-- CHANGED: Wrapped diff in 'if' and added '-n "$schema_name"' to pg_dump
189-
# We only dump the specific schema_name for a clean diff
190-
if ! pg_dump -h "$PDNS_GPGSQL_HOST" -p "$PDNS_GPGSQL_PORT" -U "$PDNS_GPGSQL_USER" -d "$PDNS_GPGSQL_DBNAME" --schema-only -n "$schema_name" | diff - "$schema_file" > "$temp_diff_file"; then
184+
if ! pg_dump -h "$PDNS_GPGSQL_HOST" -p "$PDNS_GPGSQL_PORT" -U "$PDNS_GPGSQL_USER" -d "$PDNS_GPGSQL_DBNAME" --schema-only -n "$schema_name" --no-owner --no-privileges \
185+
| sed -e "s/$schema_name\.//g" \
186+
-e '/^SET /d' \
187+
-e '/^SELECT pg_catalog.set_config/d' \
188+
-e '/^--/d' \
189+
-e '/^CREATE SCHEMA/d' \
190+
-e '/^ALTER SCHEMA/d' \
191+
-e '/^$/d' \
192+
| diff -Bw - "$schema_file" > "$temp_diff_file"; then
193+
191194
log "Detected differences in PostgreSQL schema '$schema_name'. Diff output:"
192195
cat "$temp_diff_file"
193196
log "Error: Schema mismatch. Please apply migrations manually."

0 commit comments

Comments
 (0)