Skip to content

Commit e00a614

Browse files
authored
[PATCH] View name contains space, backup fails (#3)
* Improved unit tests * Migrated to latest shunit2 & now using github as source * Added test cases & improved tests * Bug fix for `View name contains space` * Added exit codes & added tests
1 parent adda452 commit e00a614

40 files changed

+867
-49
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ before_script: |
3434
before_install:
3535
- sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('testbench'), host='%', password_last_changed=FROM_UNIXTIME(1523829600) where User='root'; update user set plugin='mysql_native_password'; delete from user where User != 'root' OR host != '%'; delete from user where User = 'sys'; FLUSH PRIVILEGES;"
3636
- sudo service mysql restart
37-
- "[ -f shunit2-2.0.3.tgz ] || wget http://downloads.sourceforge.net/shunit2/shunit2-2.0.3.tgz -O shunit2-2.0.3.tgz"
38-
- tar zxf shunit2-2.0.3.tgz && rm shunit2-2.0.3.tgz
39-
- chmod +x ./shunit2-2.0.3/src/shell/shunit2
37+
- wget https://github.com/kward/shunit2/archive/v2.1.7.tar.gz -O shunit2-2.1.7.tar.gz && tar zxf shunit2-2.1.7.tar.gz && rm shunit2-2.1.7.tar.gz
38+
- chmod +x ./shunit2-2.1.7/shunit2
4039

4140
script:
4241
- kcov --include-pattern=backup.sh,tests.sh --exclude-pattern=coverage coverage ./tests.sh

backup.sh

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ if [ ! -z "${SKIP_DATABASES}" ]; then
9595
# Split on ,
9696
SKIP_DATABASES=$(echo -e "${SKIP_DATABASES}" | tr "," "\n")
9797
for DB in ${SKIP_DATABASES} ; do
98-
VIEW_LIST_SQL="${VIEW_LIST_SQL}'${DB}'," ;
98+
VIEW_LIST_SQL="${VIEW_LIST_SQL}'${DB}'," ;
9999
done
100100
VIEW_LIST_SQL="${VIEW_LIST_SQL: : -1}"
101101
VIEW_LIST_SQL="${VIEW_LIST_SQL});"
@@ -107,65 +107,73 @@ fi
107107
VIEWS_LIST=$(mysql ${MYSQL_CONN} -ANe"${VIEW_LIST_SQL}")
108108

109109
if [ "$?" -ne 0 ]; then
110-
exitWithMsg 204 "Views listing failed"
110+
exitWithMsg 205 "Views listing failed"
111111
fi
112112

113-
VIEW_IGNORE_ARG=""
113+
VIEW_IGNORE_ARG=()
114114
# Split on :!
115115
VIEWS=$(echo -e "${VIEWS_LIST}" | tr ":!" "\n")
116+
# echo -e "${VIEWS}"
116117

118+
oldIFS=$IFS
119+
IFS=$'\n'
117120
for VIEW in $VIEWS; do # Concat ignore command
118-
VIEW_IGNORE_ARG="${VIEW_IGNORE_ARG} --ignore-table=${VIEW}"
121+
# Replace ` in ${VIEW}, does not work with ` for --ignore-table
122+
VIEW="${VIEW//\`/}"
123+
#VIEW=$(printf '%q' "${VIEW}")
124+
VIEW_IGNORE_ARG+=(--ignore-table=${VIEW} )
119125
done
120-
# Replace ` in ${VIEW_IGNORE_ARG}, does not work with ` in table/database names
121-
VIEW_IGNORE_ARG=${VIEW_IGNORE_ARG//\`/}
122-
# echo -e "${VIEW_IGNORE_ARG}"
123-
126+
IFS=$oldIFS
127+
# echo "${VIEW_IGNORE_ARG[@]}";
124128
echo "Structure..."
125-
mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=FALSE --no-data ${VIEW_IGNORE_ARG} --databases ${DBS} > ${BACKUP_DIR}/structure.sql
129+
mysqldump ${MYSQLDUMP_DEFAULTS} --skip-add-drop-table --routines=FALSE --triggers=FALSE --events=FALSE --no-data "${VIEW_IGNORE_ARG[@]}" --databases ${DBS} > ${BACKUP_DIR}/structure.sql
126130

127131
if [ "$?" -ne 0 ]; then
128-
exitWithMsg 205 "Structure dump failed"
132+
exitWithMsg 207 "Structure dump failed"
129133
fi
130134

131135
echo "Data ..."
132-
mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=FALSE --no-create-info ${VIEW_IGNORE_ARG} --databases ${DBS} > ${BACKUP_DIR}/database.sql
136+
mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=FALSE --no-create-info "${VIEW_IGNORE_ARG[@]}" --databases ${DBS} > ${BACKUP_DIR}/database.sql
133137

134138
if [ "$?" -ne 0 ]; then
135-
exitWithMsg 205 "Data dump failed"
139+
exitWithMsg 208 "Data dump failed"
136140
fi
137141

138142
echo "Routines ..."
139143
mysqldump ${MYSQLDUMP_DEFAULTS} --routines=TRUE --triggers=FALSE --events=FALSE --no-create-info --no-data --no-create-db --databases ${DBS} > ${BACKUP_DIR}/routines.sql
140144

141145
if [ "$?" -ne 0 ]; then
142-
exitWithMsg 205 "Routines dump failed"
146+
exitWithMsg 209 "Routines dump failed"
143147
fi
144148

145149
echo "Triggers ..."
146150
mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=TRUE --events=FALSE --no-create-info --no-data --no-create-db --databases ${DBS} > ${BACKUP_DIR}/triggers.sql
147151

148152
if [ "$?" -ne 0 ]; then
149-
exitWithMsg 205 "Triggers dump failed"
153+
exitWithMsg 210 "Triggers dump failed"
150154
fi
151155

152156
echo "Events ..."
153157
mysqldump ${MYSQLDUMP_DEFAULTS} --routines=FALSE --triggers=FALSE --events=TRUE --no-create-info --no-data --no-create-db --databases ${DBS} > ${BACKUP_DIR}/events.sql
154158

155159
if [ "$?" -ne 0 ]; then
156-
exitWithMsg 205 "Events dump failed"
160+
exitWithMsg 211 "Events dump failed"
157161
fi
158162

159163
echo "Views ..."
160164
VIEWS_SHOW_SQL=""
165+
oldIFS=$IFS
166+
IFS=$'\n'
161167
for VIEW in $VIEWS; do # Concat SHOW CREATE VIEW command
162168
VIEWS_SHOW_SQL="${VIEWS_SHOW_SQL}SHOW CREATE VIEW ${VIEW};"
163169
done
170+
IFS=$oldIFS
171+
164172
# echo -e "${VIEWS_SHOW_SQL}"
165173
echo ${VIEWS_SHOW_SQL} | sed 's/;/\\G/g' | mysql ${MYSQL_CONN} > ${BACKUP_DIR}/views.sql
166174

167175
if [ "$?" -ne 0 ]; then
168-
exitWithMsg 205 "Views dump failed"
176+
exitWithMsg 212 "Views dump failed"
169177
fi
170178

171179
#Keeps lines starting with Create
@@ -185,7 +193,7 @@ echo "Users ..."
185193
mysqldump ${MYSQLDUMP_DEFAULTS} mysql --no-create-info --complete-insert --tables user db > ${BACKUP_DIR}/users.sql
186194

187195
if [ "$?" -ne 0 ]; then
188-
exitWithMsg 205 "Users dump failed"
196+
exitWithMsg 213 "Users dump failed"
189197
fi
190198

191199
echo "Grants ..."
@@ -195,7 +203,7 @@ GRANTS_LIST=$(mysql ${MYSQL_CONN} -ANe"${GRANTS_SQL}")
195203
echo ${GRANTS_LIST} | mysql --default-character-set=utf8 --skip-comments ${MYSQL_CONN} | sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 --/;/--/{x;p;x;}' > ${BACKUP_DIR}/grants.sql
196204

197205
if [ "$?" -ne 0 ]; then
198-
exitWithMsg 205 "Grants dump failed"
206+
exitWithMsg 214 "Grants dump failed"
199207
fi
200208

201209
# Removes double backslashes > \\

samples/empty/create.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE DATABASE testbench CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

samples/empty/createuser.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE USER 'grantfail'@'%' IDENTIFIED BY 'testbench';

samples/empty/deleteuser.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP USER 'grantfail'@'%';

samples/empty/destroy.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP DATABASE testbench;

samples/empty/grantToDB.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GRANT ALL PRIVILEGES ON mysql.db TO 'grantfail'@'%';

samples/empty/grantToTestBench.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GRANT ALL PRIVILEGES ON testbench.* TO 'grantfail'@'%';

samples/empty/grantToUsers.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GRANT ALL PRIVILEGES ON mysql.user TO 'grantfail'@'%';

samples/withdata0/create.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE DATABASE testbench CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
2+
USE testbench;
3+
4+
CREATE TABLE `table hérétique ! @*:` (
5+
`ma première colonne` varchar(64) CHARACTER SET utf8mb4 NOT NULL,
6+
`qui utilise encore du latin sérieux` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL
7+
) ENGINE=InnoDB DEFAULT CHARSET=big5 COMMENT="Une table horrible";
8+
9+
ALTER TABLE `table hérétique ! @*:` ADD PRIMARY KEY (`ma première colonne`);
10+
ALTER TABLE `table hérétique ! @*:` ADD INDEX(`qui utilise encore du latin sérieux`);
11+
ALTER TABLE `table hérétique ! @*:` ADD UNIQUE(`ma première colonne`);
12+
ALTER TABLE `table hérétique ! @*:` ADD KEY `les cons` (`qui utilise encore du latin sérieux`) USING BTREE;

0 commit comments

Comments
 (0)