-
Notifications
You must be signed in to change notification settings - Fork 5
/
pg_schema_dump.sh
executable file
·35 lines (33 loc) · 1.48 KB
/
pg_schema_dump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source $DIR/.env
SCHEMAFN=$DIR/sql/schema.sql
RSCHEMAFN=$DIR/sql/schema-reconstructed.sql
mkdir -p sql/schema && \
pg_dump --schema-only --no-owner $DBURI > $SCHEMAFN && \
cd sql/schema/ && \
(rm * ||:) && \
csplit $SCHEMAFN -f '' -b '%02d' '/^\-\- Name: .*/' '{*}' && \
for FN in `ls -1v *` ; do
echo $FN
NF="$(egrep -o "Name: ([^;]+); Type: ([^;]+)" $FN | sed -E 's/(Name: |Type: |; |\(|\)| )/./g' | sed -E 's/(\.+)/./g' | sed -E 's/$/.sql/' | sed -E "s/^\.//g" | sed -E 's/character\.varying([^\.]*)\./cv\./g' | sed -E 's/timestamp.with.time.zone/tstz/g')";
#mv -n $FN $FN$NF ;
(
#echo '-- SPLIT '$FN ; # disabled to not trigger train diffs
cat $FN
echo "$NF" >> order.txt
#echo $NF ;
) | sed -E "s/ '"$JWTSECRET"'/ current_setting('app.jwt_secret')/" > $NF && rm $FN
done
[ "$(wc -l < order.txt)" -eq "$(sort -u order.txt | wc -l)" ] || (echo 'FILENAMES NOT UNIQUE!' ; exit 1)
cd -
echo '* reconstructing'
# to reconstruct a single file schema:
cd sql/schema && (echo 00 ; cat order.txt) | xargs cat > $RSCHEMAFN ;
diff -Nuar $SCHEMAFN $RSCHEMAFN || (echo "DUMPS $SCHEMAFN && $RSCHEMAFN NOT EQUAL!" ; exit 2) &&
echo '* erasing monodumps' && \
rm $SCHEMAFN $RSCHEMAFN && \
echo '* rewriting view creation' && \
sed -E -i 's/^CREATE VIEW/CREATE OR REPLACE VIEW/g' *sql && \
echo '* all done!'
cd -