|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Explode PostgreSQL database dump into several files, one per type |
| 4 | +# LICENCE: GPL 2 |
| 5 | +# AUTHOR: 3LIZ |
| 6 | + |
| 7 | + |
| 8 | +echo "# CHECK INPUT PARAMETERS service and schema" |
| 9 | +if [ -n "$1" ]; then |
| 10 | + echo "# POSTGRESQL SERVICE: $1" |
| 11 | + SERVICE=$1 |
| 12 | +else |
| 13 | + echo "ERROR: No PostgreSQL service given as second parameter"; |
| 14 | + exit; |
| 15 | +fi |
| 16 | +if [ -n "$2" ]; then |
| 17 | + echo "# GIVEN SCHEMA: $2" |
| 18 | + SCHEMA=$2 |
| 19 | +else |
| 20 | + SCHEMA="cartads" |
| 21 | + echo "# DEFAULT SCHEMA: $SCHEMA"; |
| 22 | +fi |
| 23 | +echo "" |
| 24 | + |
| 25 | +OUTDIR=$SCHEMA |
| 26 | +mkdir -p ./$OUTDIR |
| 27 | + |
| 28 | +# STRUCTURE |
| 29 | +# Dump database structure |
| 30 | +pg_dump service=$SERVICE --schema-only -n $SCHEMA --no-acl --no-owner -Fc -f "$OUTDIR/dump" |
| 31 | + |
| 32 | +# Loop through DB object types and extract SQL |
| 33 | +I=10 |
| 34 | +for ITEM in FUNCTION "TABLE|SEQUENCE|DEFAULT" VIEW INDEX TRIGGER CONSTRAINT COMMENT; do |
| 35 | + echo $ITEM |
| 36 | + # Extract list of objects for current item |
| 37 | + pg_restore --no-acl --no-owner -l $OUTDIR/dump | grep -E "$ITEM" > "$OUTDIR/$ITEM"; |
| 38 | + # Extract SQL for these objects |
| 39 | + pg_restore -f "$OUTDIR"/"$I"_"$ITEM".sql --no-acl --no-owner -L "$OUTDIR/$ITEM" "$OUTDIR/dump"; |
| 40 | + # Remove file containing list of objects |
| 41 | + rm "$OUTDIR/$ITEM"; |
| 42 | + # Simplify comments inside SQL files |
| 43 | + perl -i -0pe 's/\n--\n-- Name: (TABLE )?(COLUMN )?(.+); Type:.+\n--\n\n/\n-- $3\n/g' "$OUTDIR"/"$I"_"$ITEM".sql; |
| 44 | + # Remove audit trigger (added afterwards) |
| 45 | + if [ $ITEM = 'TRIGGER' ] |
| 46 | + then |
| 47 | + sed -i '/audit_trigger/d' "$OUTDIR"/"$I"_"$ITEM".sql; |
| 48 | + fi |
| 49 | + # Remove SET function to remove some compatibility issues between PostgreSQL versions |
| 50 | + sed -i "s#SET idle_in_transaction_session_timeout = 0;##g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 51 | + # Remove as integer for sequences, to keep compatibility |
| 52 | + sed -i -E "s# AS integer##g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 53 | + # Remove SET search_path |
| 54 | + sed -i "s#SELECT pg_catalog.set_config('search_path', '', false);##g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 55 | + # Remove default_table_access_method |
| 56 | + sed -i "s#SET default_table_access_method = heap##g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 57 | + # Remove SET transaction_timeout = 0; |
| 58 | + sed -i "s#SET transaction_timeout = 0;##g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 59 | + # Remove --- Dumped blah |
| 60 | + sed -i 's#-- Dumped.*$##g' "$OUTDIR"/"$I"_"$ITEM".sql; |
| 61 | + # Replace FOR EACH ROW EXECUTE FUNCTION (pg13) by FOR EACH ROW EXECUTE PROCEDURE (still ok for Pg13) |
| 62 | + sed -i "s#FOR EACH ROW EXECUTE FUNCTION#FOR EACH ROW EXECUTE PROCEDURE#g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 63 | + # Remove new pg_dump 17 restrict lines |
| 64 | + sed -i -E "s#\\\(un)?restrict .*##g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 65 | + # Replace public.geometry by geometry |
| 66 | + sed -i "s#public.geometry#geometry#g" "$OUTDIR"/"$I"_"$ITEM".sql; |
| 67 | + # Rename |
| 68 | + rename -f 's#\|#_#g' "$OUTDIR"/"$I"_"$ITEM".sql; |
| 69 | + # Increment I |
| 70 | + I=$(($I+10)); |
| 71 | +done |
| 72 | + |
| 73 | +# Remove dump |
| 74 | +rm "$OUTDIR/dump" |
| 75 | + |
| 76 | +# NOMENCLATURE |
| 77 | +echo "GLOSSARY" |
| 78 | +if [ $SCHEMA = 'cartads' ] |
| 79 | +then |
| 80 | + pg_dump service=$SERVICE --data-only --inserts --column-inserts -n $SCHEMA --no-acl --no-owner --table "$SCHEMA.glossary_*" -f "$OUTDIR"/90_GLOSSARY.sql |
| 81 | + sed -i "s#SET idle_in_transaction_session_timeout = 0;##g" "$OUTDIR"/"90_GLOSSARY.sql" |
| 82 | + sed -i "s#SELECT pg_catalog.set_config('search_path', '', false);##g" "$OUTDIR"/"90_GLOSSARY.sql" |
| 83 | + # Remove --- Dumped blah |
| 84 | + sed -i 's#-- Dumped.*$##g' "$OUTDIR"/"90_GLOSSARY.sql"; |
| 85 | + # Remove SET transaction_timeout = 0; |
| 86 | + sed -i "s#SET transaction_timeout = 0;##g" "$OUTDIR"/"90_GLOSSARY.sql"; |
| 87 | + # Remove new pg_dump 17 restrict lines |
| 88 | + sed -i -E "s#\\\(un)?restrict .*##g" "$OUTDIR"/"90_GLOSSARY.sql"; |
| 89 | +fi |
0 commit comments