Skip to content

Commit af56012

Browse files
committed
Postgresql-setup script updates
docker-compose*.yml: Bump node version to 10.1.4 and dbsync version to 13.6.0.4 postgresql-setup.sh: - Parse psql output using csv instead of manipulating white chars or table seperators - Use strict checks for ${PGDATABASE} as there can be another database with a prefix/suffix attached - When creating/restoring a snapshot, existence of ledger_file should be optional, as there are dbsync config options that don't require lstate files, accordingly - make presence of ledger-state-file optional - When restoring a snapshot (eg: across postgres versions), there will be cases where an informational error may be expected. The presence of exit-on-error prevents use of these snapshots, especially when run from docker created by nix-ops (where it isnt as straightforward to override scripts)
1 parent cea19a3 commit af56012

File tree

3 files changed

+35
-40
lines changed

3 files changed

+35
-40
lines changed

docker-compose.example.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ services:
3636
max-file: "10"
3737

3838
cardano-node:
39-
image: ghcr.io/intersectmbo/cardano-node:9.0.0
39+
image: ghcr.io/intersectmbo/cardano-node:10.1.4
4040
environment:
4141
- NETWORK=${NETWORK:-mainnet}
4242
networks:
@@ -59,7 +59,7 @@ services:
5959
max-file: "10"
6060

6161
cardano-db-sync:
62-
image: ghcr.io/intersectmbo/cardano-db-sync:13.3.0.0
62+
image: ghcr.io/intersectmbo/cardano-db-sync:13.6.0.4
6363
environment:
6464
- NETWORK=${NETWORK:-mainnet}
6565
- POSTGRES_HOST=postgres

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ services:
3333
max-file: "10"
3434

3535
cardano-node:
36-
image: ghcr.io/intersectmbo/cardano-node:10.1.3
36+
image: ghcr.io/intersectmbo/cardano-node:10.1.4
3737
environment:
3838
- NETWORK=${NETWORK:-mainnet}
3939
volumes:

scripts/postgresql-setup.sh

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function check_connect_as_user {
9898

9999
function check_db_exists {
100100
set +e
101-
count=$(psql -l "${PGDATABASE}" | grep -c "${PGDATABASE} ")
101+
count=$(psql -t -l --csv "${PGDATABASE}" | grep -c "^${PGDATABASE},")
102102
if test "${count}" -lt 1 ; then
103103
echo
104104
echo "Error : No '${PGDATABASE}' database."
@@ -108,7 +108,7 @@ function check_db_exists {
108108
echo
109109
exit 1
110110
fi
111-
count=$(psql -l "${PGDATABASE}" | grep "${PGDATABASE} " | cut -d \| -f 3 | grep -c UTF8)
111+
count=$(psql -t -l --csv "${PGDATABASE}" | grep "^${PGDATABASE}," | grep -c UTF8)
112112
if test "${count}" -ne 1 ; then
113113
echo
114114
echo "Error : '${PGDATABASE}' database exists, but is not UTF8."
@@ -164,19 +164,21 @@ function create_snapshot {
164164
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
165165
echo $"Working directory: ${tmp_dir}"
166166
pg_dump --no-owner --schema=public --jobs="${numcores}" "${PGDATABASE}" --format=directory --file="${tmp_dir}/db/"
167-
lstate_gz_file=$(basename "${ledger_file}").gz
168-
gzip --to-stdout "${ledger_file}" > "${tmp_dir}/$(basename "${ledger_file}").gz"
169-
tree "${tmp_dir}"
170-
# Use plain tar here because the database dump files and the ledger state file are already gzipped.
171-
tar cvf - --directory "${tmp_dir}" "db" "${lstate_gz_file}" | tee "${tgz_file}.tmp" \
172-
| sha256sum | head -c 64 | sed -e "s/$/ ${tgz_file}\n/" > "${tgz_file}.sha256sum"
167+
if [ -n "${ledger_file}" ]; then
168+
lstate_gz_file=$(basename "${ledger_file}").gz
169+
gzip --to-stdout "${ledger_file}" > "${tmp_dir}/${lstate_gz_file}"
170+
fi
171+
# Use plain tar here because the database dump files and the ledger state file are already gzipped. Disable Shellcheck SC2046 to avoid empty '' getting added while quoting
172+
# shellcheck disable=SC2046
173+
tar cvf - --directory "${tmp_dir}" "db" $( [ -n "${lstate_gz_file:-}" ] && [ -f "/${tmp_dir}/${lstate_gz_file}" ] && echo "${lstate_gz_file}" ) | tee "${tgz_file}.tmp" | \
174+
sha256sum | head -c 64 | sed -e "s/$/ ${tgz_file}\n/" > "${tgz_file}.sha256sum"
173175
mv "${tgz_file}.tmp" "${tgz_file}"
174176
rm "${recursive}" "${force}" "${tmp_dir}"
175177
if test "$(tar "${test}" --file "${tgz_file}")" ; then
176178
echo "Tar reports the snapshot file as being corrupt."
177179
echo "It is not safe to drop the database and restore using this file."
178180
exit 1
179-
fi
181+
fi
180182
echo "Created ${tgz_file} + .sha256sum"
181183
}
182184

@@ -185,24 +187,25 @@ function restore_snapshot {
185187
if test "${file_count}" -gt 0 ; then
186188
echo "Ledger state directory ($2) is not empty. Please empty it and then retry."
187189
exit 1
188-
fi
190+
fi
189191
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
190192
tar xvf "$1" --directory "$tmp_dir"
191193
if test -d "${tmp_dir}/db/" ; then
192194
# New pg_dump format
193-
lstate_gz_file=$(find "${tmp_dir}/" -iname "*.lstate.gz")
194-
lstate_file=$(basename "${lstate_gz_file}" | sed 's/.gz$//')
195-
gunzip --to-stdout "${lstate_gz_file}" > "$2/${lstate_file}"
195+
if [ -n "${lstate_gz_file:-}" ] ; then
196+
lstate_file=$(basename "${lstate_gz_file}" | sed 's/.gz$//')
197+
gunzip --to-stdout "${lstate_gz_file}" > "$2/${lstate_file}"
198+
fi
196199

197-
# Important: specify --schema=public below to skip over `create schema public`
198-
# statement generated by pg_dump
200+
# Important: specify --schema=public below to skip over `create schema public`
201+
# statement generated by pg_dump
199202
pg_restore \
200203
--schema=public \
201204
--jobs="${numcores}" \
202205
--format=directory \
203206
--dbname="${PGDATABASE}" \
204-
--no-owner \
205-
--exit-on-error \
207+
--no-owner \
208+
"$( [ -z "${SKIP_RESTORE_ERROR}" ] && echo " --exit-on-error" )" \
206209
"${tmp_dir}/db/"
207210
else
208211
# Old snapshot format produced by this script
@@ -228,7 +231,7 @@ function usage_exit {
228231
echo " $progname --dump-schema - Dump the schema of the database."
229232
echo
230233
echo " - Create a db-sync state snapshot"
231-
echo " $progname --create-snapshot <snapshot-file> <ledger-state-file>"
234+
echo " $progname --create-snapshot <snapshot-file> [<ledger-state-file>]"
232235
echo
233236
echo " - Restore a db-sync state snapshot."
234237
echo " $progname --restore-snapshot <snapshot-file> <ledger-state-dir>"
@@ -300,23 +303,19 @@ case "${1:-""}" in
300303
--create-snapshot)
301304
check_pgpass_file
302305
check_db_exists
303-
if test $# -ne 3 ; then
304-
echo "Expecting exactly 2 more arguments, the snapshot file name template and the ledger state file."
306+
if test $# -lt 2 ; then
307+
echo "Expecting the snapshot file name (without extension) and optionally the ledger state file as arguments."
305308
exit 1
306-
fi
309+
fi
307310
if test -z "$2" ; then
308-
echo "Second argument should be the snapshot file name template."
311+
echo "Second argument should be the snapshot file name (without extension)."
309312
exit 1
310-
fi
311-
if test -z "$3" ; then
312-
echo "Third argument should be the ledger state file."
313-
exit 1
314-
fi
315-
if test -d "$3" ; then
316-
echo "Third argument is a directory and expecting a file."
313+
fi
314+
if test -n "${3:-}" && test -d "${3}" ; then
315+
echo "Third argument provided is a directory but expecting a file."
317316
exit 1
318-
fi
319-
create_snapshot "$2" "$3"
317+
fi
318+
create_snapshot "$2" "${3:-}"
320319
;;
321320
--restore-snapshot)
322321
check_pgpass_file
@@ -334,15 +333,11 @@ case "${1:-""}" in
334333
echo "Second argument should be the snapshot file."
335334
exit 1
336335
fi
337-
if test -z "$3" ; then
338-
echo "Third argument should be the ledger state directory."
339-
exit 1
340-
fi
341-
if test -f "$3" ; then
336+
if test -n "${3:-}" && test -f "${3}"; then
342337
echo "Third argument is a file and expecting a directory."
343338
exit 1
344339
fi
345-
restore_snapshot "$2" "$3"
340+
restore_snapshot "$2" "${3:-}"
346341
;;
347342
*)
348343
usage_exit

0 commit comments

Comments
 (0)