Skip to content

Commit bd0fd75

Browse files
author
Roel Van de Paar
authored
Merge pull request #299 from ibrarahmad/master
Issue (#298) - System is not generating proper SQL file, compatible w…
2 parents a14310f + 4a69674 commit bd0fd75

File tree

1 file changed

+91
-14
lines changed

1 file changed

+91
-14
lines changed

postgres_test_to_sql.sh

+91-14
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,109 @@
11
#!/bin/bash
22
# Created by Ramesh Sivaraman, Percona LLC
3+
# Special thansks to unix.com
4+
# https://www.unix.com/shell-programming-and-scripting/80633-sed-combining-multiple-lines-into-one.html
35

46
echoit(){
5-
echo "[$(date +'%T')] $1"
7+
echo "[$(date +'%T')] $1"
68
}
79

810
# User configurable variables
9-
BASEDIR="/home/vagrant/postgresql-10.3" # Postgress source directory (should contain src directory as a first level subdirectory, see TESTS_PATH)
1011

11-
FINAL_SQL=/tmp/ptr_to_sql.sql # pquery final SQL grammar (i.e. file name for output generated by this script)
12+
# Postgress source directory
13+
# should contain src directory as a first level subdirectory, see TESTS_PATH.
14+
BASEDIR="/home/vagrant/postgresql"
15+
16+
# PostgreSQL lib directlry, conatains the PostgreSQL modules and libraries so's
17+
PG_LIBDIR="$(pg_config --libdir)"
18+
19+
# pquery final SQL grammar (i.e. file name for output generated by this script)
20+
FINAL_SQL=/tmp/ptr_to_sql.sql
21+
1222

1323
TESTS_PATH=$(echo ${BASEDIR} | sed 's|$|/src|;s|//||g')
1424

1525
if [[ $(ls $TESTS_PATH/test/regress/sql/*.sql 2>/dev/null | wc -l ) -eq 0 ]]; then
16-
echoit "Assert: this script cannot locate sql files in ${TESTS_PATH}/test/regress/sql"
17-
exit 1
26+
echoit "Assert: this script cannot locate sql files in ${TESTS_PATH}/test/regress/sql"
27+
exit 1
1828
fi
1929

2030
echoit "> Generating SQL"
2131

22-
# with thanks https://www.unix.com/shell-programming-and-scripting/80633-sed-combining-multiple-lines-into-one.html
23-
cat $TESTS_PATH/test/*/*.sql $TESTS_PATH/test/*/*/*.sql $TESTS_PATH/test/*/*/*/*.sql | \
24-
sed '/^[ \t]*$/d' | \
25-
sed '/^--.*/d' | \
26-
sed 's|;.*.\-\-.*|;|' | \
27-
sed '/^\\d/d' | \
28-
sed -e :x -e '/;$/b' -e N -e 's/\n//' -e bx | \
29-
sed 's|/\*.*.\*/||' | \
30-
sed 's|\\echo.*.\\quit||' > $FINAL_SQL
32+
rm -f "$FINAL_SQL"
33+
34+
generate_sql()
35+
{
36+
f=$1
37+
input_encoding=" "
38+
39+
# Need to convert the multibye file to UTF-8 encoding, before concatinating to main SQL file.
40+
if [[ $(echo $f | grep -c "/mb/") -gt 0 ]];
41+
then
42+
input_encoding="-f "
43+
if [[ $(echo $f | grep -c "mule_internal.sql$") -gt 0 ]];
44+
then
45+
input_encoding+="CSPC858MULTILINGUAL"
46+
elif [[ $(echo $f | grep -c "sjis.sql$") -gt 0 ]];
47+
then
48+
input_encoding+="SJIS-OPEN"
49+
else
50+
# Pickup encoding from the filename
51+
input_encoding+="$(echo $f | sed "s:.*/::g" | sed "s:\.sql::g" | sed "s:\(-\|\_\)::g" | awk '{print toupper($0)}')"
52+
fi
53+
fi
54+
55+
# We cannot execute module files directly, CREATE EXTENSION
56+
# command execute all the sql files in sequence.
57+
if [[ $(echo $f | grep -c "/modules/") -gt 0 ]];
58+
then
59+
echo "-- $f" >> $FINAL_SQL
60+
echo "create extension if not exists $(dirname $f | sed 's:.*/modules/::g' | cut -f1 -d"/");" >> $FINAL_SQL
61+
62+
# Currently we don't support COPY command syntax, which involve external file.
63+
elif [[ $(echo $f | grep -c ".*/copy.*.sql$") -gt 0 ]];
64+
then
65+
echo "-- Ignoring file $f" >> $FINAL_SQL
66+
67+
# The xml.sql file contain lot of comments and special characters and difficult to parse.
68+
elif [[ $(echo $f | grep -c "xml.sql$") -gt 0 ]];
69+
then
70+
echo "-- Ignoring file $f" >> $FINAL_SQL
71+
else
72+
echo "-- $f" >> $FINAL_SQL
73+
iconv $input_encoding -t UTF-8 $f | \
74+
sed "s|-->|==>|g" | \
75+
sed "s|HH24--text--MI--text--SS|HH24textMItextSS|g" | \
76+
sed '/^[ \t]*$/d' | \
77+
sed '/^ *--.*/d' | \
78+
sed "s: *--.*$::g" | \
79+
sed 's|;.*.\-\-.*|;|' | \
80+
sed '/^\\d/d' | \
81+
sed -e :x -e '/;$/b' -e N -e 's/\n/ /' -e bx | \
82+
sed "s|==>|-->|g" | \
83+
sed "s|HH24textMItextSS|HH24--text--MI--text--SS|g" | \
84+
sed 's|\\echo.*.\\quit||' | \
85+
sed "s|\\gset|\\gset\n|g" | \
86+
sed "s|set QUIET true|set QUIET true\n|g" | \
87+
sed "s|set QUIET false|set QUIET false\n|g" | \
88+
sed "s|set VERBOSITY terse|set VERBOSITY terse\n|g" | \
89+
awk '{ if ( $0 ~ /\\\./ ) { gsub(/\\./, "\\.\n"); print; } else print;}' | \
90+
awk '{ if ( $0 ~ /\\\./ ) { gsub(" ", "\n"); print; } else print;}' | \
91+
sed 's|^//|-- |g' >> $FINAL_SQL
92+
fi
93+
}
94+
95+
# Traverse whole PostgreSQL's test directory, except the regress.
96+
for f in $(find $TESTS_PATH/test/*/*.sql $TESTS_PATH/test/*/*/*.sql $TESTS_PATH/test/*/*/*/*.sql| grep -v regress)
97+
do
98+
generate_sql $f
99+
done
100+
101+
# Read serial_scheduale file for the file listing for sequence execution.
102+
while read p; do
103+
cmd="$(echo "$p" | awk '{print $2}')"
104+
f="$TESTS_PATH/test/regress/sql/$cmd.sql"
105+
generate_sql $f
106+
done <$TESTS_PATH/test/regress/serial_schedule
31107

32108
echoit "Done! Generated ${FINAL_SQL}"
109+

0 commit comments

Comments
 (0)