Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/bin-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
python src/manage.py migrate
src/manage.py loaddata demodata
SCRIPTPATH=bin DUMP_FILE=dump.sql bin/dump_data.sh --combined
SCRIPTPATH=bin TAR_FILE=dump.tar bin/dump_data.sh --csv
env:
DB_PASSWORD: ""
DB_USER: postgres
Expand All @@ -56,3 +57,12 @@ jobs:
run: |
createdb -h localhost -U postgres test
psql -v ON_ERROR_STOP=1 -h localhost -U postgres -d test -f dump.sql

- name: validate csv dump
run: |
tar -xf dump.tar
test -f core_object.csv || exit 1
! test -f auth_group.csv || exit 1
grep "id,uuid,object_type_id,created_on,modified_on" core_object.csv
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Floris272 could you check if we can upgrade the pg_dump version inside the image to 17? I am running into this issue when trying to dump data with the docker compose setup:

pg_dump: error: aborting because of server version mismatch
pg_dump: detail: server version: 17.5 (Debian 17.5-1.pgdg110+1); pg_dump version: 15.14 (Debian 15.14-0+deb12u1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slim-bookworm does not know 17, it could be done by adding the postgres apt repo or by updating to slim-trixie (libpcre3 seems to be deprecated).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm that's annoying, but I'd rather not have our docs stating that we support up to postgres 17 and then get complaints when people try this command with postgres 17.

Can you check if we actually need libpcre3 and if so if there is a replacement for it (apparently it's needed for uwsgi logging)? Since trixie is the current stable release of debian I think we could upgrade to that anyway



83 changes: 59 additions & 24 deletions bin/dump_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
# or --combined which appends the data dump to the schema dump.
# The schema dump could not use -t to filter tables because this excludes extensions like postgis in the dump.
# pg_dump also does not add related tables automatically, so `dump_data.sh` does not add related data from accounts to the dump.

#
# with --csv a csv dump can be created for all tables in the given components. The csv files will be generated in the temporary directory csv_dumps
# and combined into a single TAR archive csv_dumps.

set -e

Expand All @@ -27,32 +29,38 @@ SCRIPTPATH=$(dirname "$SCRIPT")

${SCRIPTPATH}/wait_for_db.sh

DUMP_FILE=${DUMP_FILE:-"dump_$(date +'%Y-%m-%d_%H-%M-%S').sql"}
DEFAULT_FILE_NAME="dump_$(date +'%Y-%m-%d_%H-%M-%S')"
DUMP_FILE=${DUMP_FILE:-"$DEFAULT_FILE_NAME.sql"}
TAR_FILE=${TAR_FILE:-"$DEFAULT_FILE_NAME.tar"}
CSV_OUTPUT_DIR="csv_dumps"

CSV=false
SCHEMA=true
DATA=true
COMBINED=false

for arg in "$@"; do
case "$arg" in
case "$arg" in
--csv) CSV=true ;;
--schema-only) DATA=false ;;
--data-only) SCHEMA=false ;;
--combined) COMBINED=true ;;
--data-only) SCHEMA=false ;;
--combined) COMBINED=true ;;
--*)
echo "Unknown flag: $arg"
exit 1
;;
echo "Unknown flag: $arg"
exit 1
;;
*)
APPS+=("$arg") ;;
esac
APPS+=("$arg")
;;
esac
done

# export given apps or export DEFAULT_APPS
if [ "${#APPS[@]}" -eq 0 ]; then
APPS=("${DEFAULT_APPS[@]}")
APPS=("${DEFAULT_APPS[@]}")
fi

>&2 echo "exporting: ${APPS[*]}"
echo >&2 "exporting: ${APPS[*]}"

# create -t flags for each app
INCLUDES=()
Expand All @@ -61,32 +69,59 @@ for app in "${APPS[@]}"; do
done

dump_schema() {
echo "Dumping schema to $1..."
pg_dump --schema-only -f "$1"
echo "Dumping schema to $1..."
pg_dump --schema-only -f "$1"
}

dump_data() {
echo "Dumping data to $1..."
pg_dump "${INCLUDES[@]}" --disable-triggers --data-only > "$1"
echo "Dumping data to $1..."
pg_dump "${INCLUDES[@]}" --disable-triggers --data-only >"$1"
}

append_data() {
echo "Appending data to $1..."
pg_dump "${INCLUDES[@]}" --disable-triggers --data-only \
| sed '/^SET\|^SELECT pg_catalog.set_config/d' >> "$1"
echo "Appending data to $1..."
pg_dump "${INCLUDES[@]}" --disable-triggers --data-only |
sed '/^SET\|^SELECT pg_catalog.set_config/d' >>"$1"
}

dump_csv() {
mkdir -p $CSV_OUTPUT_DIR
echo "Dumping data to csv..."

WHERE_CLAUSE=""
for app in "${APPS[@]}"; do
if [ -n "$WHERE_CLAUSE" ]; then
WHERE_CLAUSE+=" OR "
fi
WHERE_CLAUSE+="tablename LIKE '${app}_%'"
done

TABLES=$(psql -Atc "SELECT tablename FROM pg_tables WHERE schemaname='public' AND ($WHERE_CLAUSE);")

for table in $TABLES; do
echo "dumping $table..."
psql -c "\copy $table TO '$CSV_OUTPUT_DIR/$table.csv' WITH CSV HEADER"
done

tar -cf "$TAR_FILE" -C "$CSV_OUTPUT_DIR" .
rm -rf "$CSV_OUTPUT_DIR"
}

if $CSV; then
dump_csv
exit 0
fi

if $COMBINED; then
dump_schema "$DUMP_FILE"
append_data "$DUMP_FILE"
exit 0
dump_schema "$DUMP_FILE"
append_data "$DUMP_FILE"
exit 0
fi

if $SCHEMA; then
dump_schema "schema__$DUMP_FILE"
dump_schema "schema__$DUMP_FILE"
fi

if $DATA; then
dump_data "data__$DUMP_FILE"
dump_data "data__$DUMP_FILE"
fi
7 changes: 5 additions & 2 deletions docs/manual/scripts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Scripts
Dump data
---------

Met het script ``dump_data.sh`` kan de data van alle componenten (core) worden geëxporteerd naar een sql bestand.
Met het script ``dump_data.sh`` kan de data van alle componenten (core) worden geëxporteerd naar een sql of csv bestand(en).

Dit script is niet bedoeld voor een data migratie naar een andere Objects Api of Objecttypes Api instantie.

Expand All @@ -17,12 +17,14 @@ Om alleen specifieke data te exporteren kunnen de gewenste component namen worde

.. code-block:: shell

./dump_data.sh core
/dump_data.sh core

.. note::

om een postgres 17 db te exporteren is de package postgres-client-17 vereist.

Met de flag ``--csv`` worden alle tabellen in de meegegeven componenten geëxporteerd naar csv bestanden. Deze bestanden worden tijdelijk in ``csv_dumps`` geplaatst en gecombineerd in een TAR bestand.

Environment variabelen
----------------------

Expand All @@ -32,6 +34,7 @@ Environment variabelen
* DB_NAME (objects/objecttypes)
* DB_PASSWORD ("")
* DUMP_FILE ("dump_$(date +'%Y-%m-%d_%H-%M-%S').sql")
* TAR_FILE ("dump_$(date +'%Y-%m-%d_%H-%M-%S').tar")

.. code-block:: shell

Expand Down
Loading