Skip to content

Commit

Permalink
Create import script
Browse files Browse the repository at this point in the history
RISDEV-4929
  • Loading branch information
malte-laukoetter committed Oct 9, 2024
1 parent beb785a commit 1d4fbc1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
14 changes: 14 additions & 0 deletions src/import_from_migration.sh
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions src/import_from_migration.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1d4fbc1

Please sign in to comment.