Skip to content

Commit 2d1b4ef

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 2d1b4ef

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
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: 30 additions & 36 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,20 @@ 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}"
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
170171
# 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"
172+
tar cvf - --directory "${tmp_dir}" "db" "$( [ -n "${lstate_gz_file:-}" ] && [ -f "/${tmp_dir}/${lstate_gz_file}" ] && echo "${lstate_gz_file}" )" | tee "${tgz_file}.tmp" | \
173+
sha256sum | head -c 64 | sed -e "s/$/ ${tgz_file}\n/" > "${tgz_file}.sha256sum"
173174
mv "${tgz_file}.tmp" "${tgz_file}"
174175
rm "${recursive}" "${force}" "${tmp_dir}"
175176
if test "$(tar "${test}" --file "${tgz_file}")" ; then
176177
echo "Tar reports the snapshot file as being corrupt."
177178
echo "It is not safe to drop the database and restore using this file."
178179
exit 1
179-
fi
180+
fi
180181
echo "Created ${tgz_file} + .sha256sum"
181182
}
182183

@@ -185,24 +186,25 @@ function restore_snapshot {
185186
if test "${file_count}" -gt 0 ; then
186187
echo "Ledger state directory ($2) is not empty. Please empty it and then retry."
187188
exit 1
188-
fi
189+
fi
189190
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
190191
tar xvf "$1" --directory "$tmp_dir"
191192
if test -d "${tmp_dir}/db/" ; then
192193
# 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}"
194+
if [ -n "${lstate_gz_file:-}" ] ; then
195+
lstate_file=$(basename "${lstate_gz_file}" | sed 's/.gz$//')
196+
gunzip --to-stdout "${lstate_gz_file}" > "$2/${lstate_file}"
197+
fi
196198

197-
# Important: specify --schema=public below to skip over `create schema public`
198-
# statement generated by pg_dump
199+
# Important: specify --schema=public below to skip over `create schema public`
200+
# statement generated by pg_dump
199201
pg_restore \
200202
--schema=public \
201203
--jobs="${numcores}" \
202204
--format=directory \
203205
--dbname="${PGDATABASE}" \
204-
--no-owner \
205-
--exit-on-error \
206+
--no-owner \
207+
"$( [ -z "${SKIP_RESTORE_ERROR}" ] && echo " --exit-on-error" )" \
206208
"${tmp_dir}/db/"
207209
else
208210
# Old snapshot format produced by this script
@@ -228,7 +230,7 @@ function usage_exit {
228230
echo " $progname --dump-schema - Dump the schema of the database."
229231
echo
230232
echo " - Create a db-sync state snapshot"
231-
echo " $progname --create-snapshot <snapshot-file> <ledger-state-file>"
233+
echo " $progname --create-snapshot <snapshot-file> [<ledger-state-file>]"
232234
echo
233235
echo " - Restore a db-sync state snapshot."
234236
echo " $progname --restore-snapshot <snapshot-file> <ledger-state-dir>"
@@ -300,23 +302,19 @@ case "${1:-""}" in
300302
--create-snapshot)
301303
check_pgpass_file
302304
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."
305+
if test $# -lt 2 ; then
306+
echo "Expecting the snapshot file name (without extension) and optionally the ledger state file as arguments."
305307
exit 1
306-
fi
308+
fi
307309
if test -z "$2" ; then
308-
echo "Second argument should be the snapshot file name template."
310+
echo "Second argument should be the snapshot file name (without extension)."
309311
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."
312+
fi
313+
if test -n "${3:-}" && test -d "${3}" ; then
314+
echo "Third argument provided is a directory but expecting a file."
317315
exit 1
318-
fi
319-
create_snapshot "$2" "$3"
316+
fi
317+
create_snapshot "$2" "${3:-}"
320318
;;
321319
--restore-snapshot)
322320
check_pgpass_file
@@ -334,15 +332,11 @@ case "${1:-""}" in
334332
echo "Second argument should be the snapshot file."
335333
exit 1
336334
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
335+
if test -n "${3:-}" && test -f "${3}"; then
342336
echo "Third argument is a file and expecting a directory."
343337
exit 1
344338
fi
345-
restore_snapshot "$2" "$3"
339+
restore_snapshot "$2" "${3:-}"
346340
;;
347341
*)
348342
usage_exit

0 commit comments

Comments
 (0)