Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit aaa07ab

Browse files
committed
Issue #5 - Add a script to generate a single SQL file, allowing to set:
* The MySQL version to generate for (56 or 57) * The user that should be the owner of the objects * Whether to include or exclude sql_log_bin For an example, to create an RDS SQL file: ./generate_sql_file.sh -v 56 -b -u CURRENT_USER
1 parent a8a79a6 commit aaa07ab

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

generate_sql_file.sh

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3+
#
4+
# This program is free software; you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation; version 2 of the License.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
OS=`uname`
18+
19+
# Grab the current sys version
20+
SYSVERSIONTMP=`cat ./before_setup.sql | grep sys_version | awk '{print $8}'`
21+
SYSVERSION=`echo "${SYSVERSIONTMP//\'}"`
22+
23+
MYSQLUSER="'root'@'localhost'"
24+
25+
USAGE="
26+
Options:
27+
================
28+
29+
v: The version of MySQL to build the sys schema for, either '56' or '57'
30+
31+
b: Whether to omit any lines that deal with sql_log_bin (useful for RDS)
32+
33+
u: The user to set as the owner of the objects (useful for RDS)
34+
35+
Examples:
36+
================
37+
38+
Generate a MySQL 5.7 SQL file that uses the 'mark'@'localhost' user:
39+
40+
$0 -v 57 -u "'mark'@'localhost'"
41+
42+
Generate a MySQL 5.6 SQL file for RDS:
43+
44+
$0 -v 56 -b -u CURRENT_USER
45+
"
46+
47+
# Grab options
48+
while getopts ":v:hbu:" opt; do
49+
case $opt in
50+
b)
51+
SKIPBINLOG=true
52+
;;
53+
h)
54+
echo $"$USAGE"
55+
exit 0
56+
;;
57+
u)
58+
MYSQLUSER="${OPTARG}"
59+
;;
60+
v)
61+
if [ $OPTARG == "56" ] || [ $OPTARG == "57" ] ;
62+
then
63+
MYSQLVERSION=$OPTARG
64+
else
65+
echo "Invalid -v option, please run again with either '-v 56' or '-v 57'"
66+
exit 1
67+
fi
68+
;;
69+
\?)
70+
echo "Invalid option: -$OPTARG" >&2
71+
echo $"$USAGE"
72+
exit 1
73+
;;
74+
:)
75+
echo "Option -$OPTARG requires an argument." >&2
76+
echo $"$USAGE"
77+
exit 1
78+
;;
79+
esac
80+
done
81+
82+
# Check required options
83+
if [[ -z "$MYSQLVERSION" ]] ;
84+
then
85+
echo " -v (MySQL Version) parameter required, please run again with either '-v 56' or '-v 57'"
86+
exit 1
87+
fi
88+
89+
# Create output file name
90+
OUTPUTFILE="sys_${SYSVERSION}_${MYSQLVERSION}_inline.sql"
91+
92+
# Create the initial output file
93+
if [ $OS == "Darwin" ] ;
94+
then
95+
cat "./sys_$MYSQLVERSION.sql" | tr -d '\r' | grep 'SOURCE' | sed -E 's .{8} ' | sed 's/^/./' | \
96+
xargs sed -e "s/'root'@'localhost'/$MYSQLUSER/g" > "temp_${OUTPUTFILE}"
97+
else
98+
cat "./sys_$MYSQLVERSION.sql" | tr -d '\r' | grep 'SOURCE' | sed -r 's .{8} ' | sed 's/^/./' | \
99+
xargs sed -e "s/'root'@'localhost'/$MYSQLUSER/g" > "temp_${OUTPUTFILE}"
100+
fi
101+
102+
# Strip copyrights, retaining the first one
103+
head -n 15 "temp_${OUTPUTFILE}" > $OUTPUTFILE
104+
sed -e '/Copyright/,/51 Franklin St/d' "temp_${OUTPUTFILE}" >> $OUTPUTFILE
105+
rm "temp_${OUTPUTFILE}"
106+
107+
# Check if sql_log_bin lines should be removed
108+
if [[ ! -z "$SKIPBINLOG" ]] ;
109+
then
110+
sed -e "/sql_log_bin/d" $OUTPUTFILE > "temp_${OUTPUTFILE}"
111+
mv "temp_${OUTPUTFILE}" $OUTPUTFILE
112+
SKIPBINLOG="disabled"
113+
else
114+
SKIPBINLOG="enabled"
115+
fi
116+
117+
# Print summary
118+
echo $"
119+
Wrote: $OUTPUTFILE
120+
User: $MYSQLUSER
121+
sql_log_bin: $SKIPBINLOG
122+
"

0 commit comments

Comments
 (0)