From 1d4fbc1c7068b98561890d42dccc3e2d9ef8807c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Lauk=C3=B6tter?= Date: Wed, 9 Oct 2024 11:29:09 +0200 Subject: [PATCH] Create import script RISDEV-4929 --- Dockerfile | 9 +++++++++ src/import_from_migration.sh | 14 ++++++++++++++ src/import_from_migration.sql | 11 +++++++++++ 3 files changed, 34 insertions(+) create mode 100644 Dockerfile create mode 100644 src/import_from_migration.sh create mode 100644 src/import_from_migration.sql diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5842fbc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM cgr.dev/chainguard/wolfi-base:latest + +RUN apk update \ + && apk add --no-cache --update-cache postgresql-14-client; + +COPY src/import_from_migration.sql /import_from_migration.sql +COPY --chmod=111 src/import_from_migration.sh /import_from_migration.sh + +CMD ["/import_from_migration.sh"] diff --git a/src/import_from_migration.sh b/src/import_from_migration.sh new file mode 100644 index 0000000..74b8d2b --- /dev/null +++ b/src/import_from_migration.sh @@ -0,0 +1,14 @@ +echo "MIGRATION_STATS_SCHEMA=$MIGRATION_STATS_SCHEMA" + +LATEST_SUCCESSFUL_RUN="$(psql --tuples-only --csv --command="SELECT created_at FROM $MIGRATION_STATS_SCHEMA.migration_stats ORDER BY created_at DESC LIMIT 1")" +LATEST_SUCCESSFUL_RUN_DATE="${LATEST_SUCCESSFUL_RUN:0:10}" + +CURRENT_DATE="$(date "+%Y-%m-%d")" + +if [[ "$LATEST_SUCCESSFUL_RUN_DATE" != "$CURRENT_DATE" ]]; then + echo "NORMS_SCHEMA=$NORMS_SCHEMA" + echo "MIGRATION_SCHEMA=$MIGRATION_SCHEMA" + psql --echo-all --variable=NORMS_SCHEMA=$NORMS_SCHEMA --variable=MIGRATION_SCHEMA=$MIGRATION_SCHEMA --file=./import_from_migration.sql +else + echo "Found no successful migration that finished today. Last successful run date: $LATEST_SUCCESSFUL_RUN. Today: $CURRENT_DATE" +fi diff --git a/src/import_from_migration.sql b/src/import_from_migration.sql new file mode 100644 index 0000000..320410b --- /dev/null +++ b/src/import_from_migration.sql @@ -0,0 +1,11 @@ +DELETE FROM :NORMS_SCHEMA.announcements; +DELETE FROM :NORMS_SCHEMA.norms; + +INSERT INTO :NORMS_SCHEMA.norms (xml) SELECT ldml_xml.content FROM :MIGRATION_SCHEMA.migration_record + INNER JOIN :MIGRATION_SCHEMA.ldml ldml ON migration_record.id = ldml.migration_record_id + INNER JOIN :MIGRATION_SCHEMA.ldml_xml ldml_xml ON ldml.id = ldml_xml.ldml_id + LEFT OUTER JOIN :MIGRATION_SCHEMA.migration_error migration_error on migration_record.id = migration_error.migration_record_id + WHERE + migration_status = 'LEGALDOCML_TRANSFORMATION_SUCCEEDED' + AND xpath_exists('//akn:act[@name="regelungstext"]', ldml_xml.content, '{{akn,http://Inhaltsdaten.LegalDocML.de/1.7/}}') + AND migration_error.id IS NULL;