Skip to content

Commit 5c18664

Browse files
committed
+ dump.sh and run it to get schema.sql
@ sql
1 parent bbccbec commit 5c18664

4 files changed

Lines changed: 649 additions & 0 deletions

File tree

sql/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pgpass

sql/.pgpass

Whitespace-only changes.

sql/dump.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# https://mywiki.wooledge.org/BashFAQ/105
3+
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
4+
set -euxo pipefail
5+
6+
chmod 600 .pgpass
7+
export PGPASSFILE=.pgpass
8+
9+
filter_dump() {
10+
# https://stackoverflow.com/questions/11454343/pipe-output-to-bash-function/11455201#11455201
11+
cat |
12+
# https://stackoverflow.com/questions/41941527/removing-comments-from-pg-dump-output/41972898#41972898
13+
# https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed
14+
# https://dba.stackexchange.com/questions/315063/disable-toast-compression-for-all-columns
15+
sed -E '/(^(SET |--)|^$|SET STORAGE EXTERNAL;$)/d' |
16+
sed -E 's/^(ALTER TABLE) ONLY/\1/g' |
17+
# https://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string/26289#26289
18+
perl -p0e 's/\n (ADD CONSTRAINT)/ \1/g' |
19+
sed -e "/^SELECT pg_catalog.set_config('search_path', '', false);$/d"
20+
}
21+
dump_table() {
22+
local table=${2:?}
23+
pg_dump -U$username -sOxn \"$schema\" -t \"$table\" |
24+
filter_dump
25+
echo # https://unix.stackexchange.com/questions/690635/how-can-i-add-a-new-line-after-the-output-of-a-command
26+
}
27+
dump_db_and_schema() {
28+
# https://dba.stackexchange.com/questions/258183/pg-dump-table-dependencies-when-using-table/347112#347112
29+
pg_dump -U$username -sOx -T '*' |
30+
filter_dump
31+
echo # https://unix.stackexchange.com/questions/690635/how-can-i-add-a-new-line-after-the-output-of-a-command
32+
}
33+
34+
# https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash/16753536#16753536
35+
username=${1:?}
36+
schema=${2:?}
37+
dump_db_and_schema
38+
# https://dba.stackexchange.com/questions/292696/can-i-make-postgres-pg-dump-condense-the-alter-table-statements-in-the-create-t/292703#292703
39+
psql -U$username -XAtc "SELECT tablename FROM pg_tables WHERE schemaname = '$schema' ORDER BY tablename;" \
40+
| mapfile -tc1 -C dump_table

0 commit comments

Comments
 (0)