Skip to content

Commit 0bea766

Browse files
committed
Test and fixups
Replaced check for running postgres with failure in `detect_old_locale`, since the script would fail later anyway, so there's no reason to have extra code that does nothing. Fixed `psql` being run from the old pg bin directory. Wrote a test for the script.
1 parent 28591ac commit 0bea766

3 files changed

Lines changed: 159 additions & 65 deletions

File tree

‎bin/postgresql-setup.in‎

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -225,80 +225,55 @@ detect_old_cluster_locale()
225225
old_cluster_lc_ctype=""
226226
old_cluster_locale=""
227227

228-
debug "detecting locale from old cluster at $old_datadir"
229-
230-
# Try to get locale from running database using psql
231-
# First check if server is running
232-
local pidfile="$old_datadir/postmaster.pid"
233-
local server_was_running=false
234-
local temp_server_started=false
235-
236-
if [ -f "$pidfile" ]; then
237-
# Check if the PID in the file is actually running
238-
local pid=$(head -n 1 "$pidfile" 2>/dev/null)
239-
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
240-
server_was_running=true
241-
debug "old cluster server is running (PID: $pid), querying locale via psql"
242-
fi
228+
if old_data_in_use; then
229+
return 1
243230
fi
244231

245232
# If server is not running, try to start it temporarily
246-
if ! $server_was_running; then
247-
debug "old cluster server is not running, attempting temporary start to query locale"
248-
249-
# Create a temporary socket directory
250-
local temp_sockdir=$(mktemp -d 2>/dev/null)
251-
if [ $? -ne 0 ] || [ -z "$temp_sockdir" ]; then
252-
debug "failed to create temporary socket directory"
253-
return 1
254-
fi
255-
256-
local temp_log="$old_datadir/temp_locale_query.log"
257-
258-
# Try to start server with minimal configuration
259-
# Use a different port to avoid conflicts
260-
local temp_port=$((old_port + 10000))
261-
262-
# Start server in background with minimal settings
263-
# Use -m fast to speed up startup
264-
if "$old_bindir/pg_ctl" -D "$old_datadir" -o "-p $temp_port -c listen_addresses='' -c unix_socket_directories='$temp_sockdir' -c max_connections=5 -c shared_buffers=128kB" start -w -l "$temp_log" -m fast >/dev/null 2>&1; then
265-
temp_server_started=true
266-
debug "temporarily started old cluster server on port $temp_port"
267-
# Give it a moment to be ready
268-
sleep 2
269-
else
270-
debug "failed to start old cluster server temporarily (see $temp_log for details)"
271-
rm -rf "$temp_sockdir"
272-
rm -f "$temp_log"
273-
return 1
274-
fi
275-
fi
276-
277-
# Query locale from template1 database
278-
local query="SELECT datcollate, datctype FROM pg_catalog.pg_database WHERE datname = 'template1'"
279-
local result
280-
local query_port="$old_port"
281-
if $temp_server_started; then
282-
query_port="$temp_port"
233+
debug "old cluster server is not running, attempting temporary start to query locale"
234+
235+
# Create a temporary socket directory
236+
local temp_sockdir=$(mktemp -d 2>/dev/null)
237+
if [ $? -ne 0 ] || [ -z "$temp_sockdir" ]; then
238+
debug "failed to create temporary socket directory"
239+
return 1
283240
fi
284-
285-
# Use appropriate connection method
286-
local psql_exit_code=0
287-
if $temp_server_started; then
288-
result=$("$old_bindir/psql" -t -A -h "$temp_sockdir" -p "$query_port" -d template1 -c "$query" 2>/dev/null)
289-
psql_exit_code=$?
241+
242+
local temp_log="$old_datadir/temp_locale_query.log"
243+
244+
# Try to start server with minimal configuration
245+
# Use a different port to avoid conflicts
246+
local temp_port=$((old_port + 10000))
247+
248+
# Start server in background with minimal settings
249+
# Use -m fast to speed up startup
250+
if "$old_bindir/pg_ctl" -D "$old_datadir" -o "-p $temp_port -c listen_addresses='' -c unix_socket_directories='$temp_sockdir' -c max_connections=5 -c shared_buffers=128kB" start -w -l "$temp_log" -m fast >/dev/null 2>&1; then
251+
debug "temporarily started old cluster server on port $temp_port"
252+
# Give it a moment to be ready
253+
sleep 2
290254
else
291-
result=$("$old_bindir/psql" -t -A -p "$query_port" -d template1 -c "$query" 2>/dev/null)
292-
psql_exit_code=$?
293-
fi
294-
295-
# Stop temporary server if we started it
296-
if $temp_server_started; then
297-
"$old_bindir/pg_ctl" -D "$old_datadir" stop >/dev/null 2>&1
255+
debug "failed to start old cluster server temporarily (see $temp_log for details)"
298256
rm -rf "$temp_sockdir"
299257
rm -f "$temp_log"
258+
return 1
300259
fi
260+
301261

262+
# Query locale from template1 database
263+
local query="SELECT datcollate, datctype FROM pg_catalog.pg_database WHERE datname = 'template1'"
264+
local result
265+
local query_port="$temp_port"
266+
267+
# Perform query
268+
local psql_exit_code=0
269+
result=$(psql -t -A -h "$temp_sockdir" -p "$query_port" -d template1 -c "$query" 2>/dev/null)
270+
psql_exit_code=$?
271+
272+
# Stop temporary server
273+
"$old_bindir/pg_ctl" -D "$old_datadir" stop >/dev/null 2>&1
274+
rm -rf "$temp_sockdir"
275+
rm -f "$temp_log"
276+
302277
# Parse result
303278
if [ $psql_exit_code -eq 0 ] && [ -n "$result" ]; then
304279
# Parse result: datcollate|datctype
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
3+
4+
. /usr/share/beakerlib/beakerlib.sh || exit 1
5+
6+
rlJournalStart
7+
rlPhaseStartSetup
8+
rlRun "git clone \"$REPO_URL\" repo"
9+
rlRun "pushd repo"
10+
rlRun "git fetch origin \"$PR_HEAD\""
11+
rlRun "git checkout FETCH_HEAD"
12+
rlRun "cat /etc/fedora-release"
13+
rlRun "dnf -y install postgresql16-server"
14+
15+
rlRun "autoreconf -vfi" 0 "Building and installing postgresql-setup"
16+
rlRun "./configure --prefix=/usr"
17+
rlRun "make"
18+
rlRun "./bin/postgresql-setup --init" 0 "Initializing database dir"
19+
rlRun "systemctl start postgresql" 0 "Starting service"
20+
rlRun "systemctl is-active postgresql" 0 "Verifying service running"
21+
rlPhaseEnd
22+
23+
rlPhaseStartTest
24+
rlRun "su - postgres -c \"
25+
createdb testdb;
26+
psql -U postgres -d testdb -c \\\"create table users (id serial primary key, name text)\\\";
27+
psql -U postgres -d testdb -c \\\"insert into users (name) values ('Alice'), ('Bob'), ('Celine')\\\"
28+
\""
29+
rlRun "su - postgres -c '
30+
psql -U postgres -d testdb -c \"select * from users\"
31+
' > expected.txt"
32+
33+
echo "Expected:"
34+
cat expected.txt
35+
36+
# change locale (https://wiki.archlinux.org/title/Locale#Make_locale_changes_immediate)
37+
rlRun "localectl set-locale LANG=en_GB.UTF-8" 0 "Changing locale"
38+
rlRun "unset LANG"
39+
rlRun "source /etc/profile.d/lang.sh"
40+
rlRun "locale"
41+
42+
rlRun "dnf -y remove postgresql16*" 0 "Removing postgresql 16"
43+
rlRun "dnf -y install postgresql17-upgrade" 0 "Installing postgresql 17"
44+
rlRun "./bin/postgresql-setup --upgrade" 0 "Running upgrade"
45+
46+
rlRun "systemctl start postgresql" 0 "Starting service again"
47+
rlRun "systemctl is-active postgresql" 0 "Verifying service running"
48+
49+
rlRun "su - postgres -c '
50+
psql -U postgres -d testdb -c \"select * from users\"
51+
' > actual17.txt"
52+
53+
echo "Actual:"
54+
cat actual17.txt
55+
56+
rlAssertNotDiffer expected.txt actual17.txt
57+
58+
rlRun "localectl set-locale LC_COLLATE=en_US.UTF-8" 0 "Changing LC_COLLATE"
59+
rlRun "localectl set-locale LC_CTYPE=en_AU.UTF-8" 0 "Changing LC_CTYPE"
60+
rlRun "unset LANG"
61+
rlRun "source /etc/profile.d/lang.sh"
62+
rlRun "locale"
63+
64+
rlRun "dnf -y remove postgresql17*" 0 "Removing postgresql 17"
65+
rlRun "dnf -y install postgresql18-upgrade" 0 "Installing postgresql 18"
66+
# TODO: remove if functionality added to the script
67+
rlRun "/usr/lib64/pgsql/postgresql-17/bin/pg_checksums -e /var/lib/pgsql/data" 0 "Enabling data checksums for pg18"
68+
69+
rlRun "su - postgres -c '/usr/lib64/pgsql/postgresql-17/bin/pg_ctl -D /var/lib/pgsql/data -l /var/lib/pgsql/logfile start'" 0 "Starting postgres pre-upgrade"
70+
rlRun "su - postgres -c '/usr/lib64/pgsql/postgresql-17/bin/pg_ctl -D /var/lib/pgsql/data status'" 0 "Verifying postgres is running"
71+
72+
rlRun "./bin/postgresql-setup --upgrade --debug" 1 "Trying to run upgrade"
73+
rlRun "su - postgres -c '/usr/lib64/pgsql/postgresql-17/bin/pg_ctl -D /var/lib/pgsql/data stop'" 0 "Stopping old postgres"
74+
rlRun "./bin/postgresql-setup --upgrade --debug" 0 "Upgrading"
75+
76+
rlRun "systemctl start postgresql" 0 "Starting service again"
77+
rlRun "systemctl is-active postgresql" 0 "Verifying service running"
78+
79+
rlRun "su - postgres -c '
80+
psql -U postgres -d testdb -c \"select * from users\"
81+
' > actual18.txt"
82+
83+
echo "Actual:"
84+
cat actual18.txt
85+
86+
rlAssertNotDiffer actual17.txt actual18.txt
87+
rlPhaseEnd
88+
89+
rlPhaseStartCleanup
90+
rlRun "popd"
91+
rlRun "rm -rf repo"
92+
rlRun "echo 'LANG=en_US.UTF-8' > /etc/locale.conf" 0 "Resetting locale"
93+
rlRun "unset LANG"
94+
rlRun "source /etc/profile.d/lang.sh"
95+
rlRun "systemctl stop postgresql" 0 "Stopping postgresql service"
96+
rlRun "rm -rf /var/lib/pgsql" 0 "Removing database folder"
97+
rlRun "dnf -y remove postgresql*" 0 "Uninstalling postgresql"
98+
rlPhaseEnd
99+
rlJournalPrintText
100+
rlJournalEnd
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
summary: Check if upgrading a database works when using a different locale
2+
name: locale-change
3+
require:
4+
- make
5+
- m4
6+
- docbook-utils
7+
- help2man
8+
- elinks
9+
- coreutils
10+
- autoconf
11+
- automake
12+
- autoconf-archive
13+
- git
14+
framework: beakerlib
15+
contact: pkhartsk@redhat.com
16+
tag:
17+
- fedora-upgrade
18+
test: ./locale.sh
19+

0 commit comments

Comments
 (0)