Skip to content

Instantly share code, notes, and snippets.

@mz0
Created March 16, 2020 15:12
Show Gist options
  • Save mz0/a3c52b5c4a3f1322c6c444abd7201b87 to your computer and use it in GitHub Desktop.
Save mz0/a3c52b5c4a3f1322c6c444abd7201b87 to your computer and use it in GitHub Desktop.
#!/bin/sh
DB=tmp42
DBu=db42u
DBp='passwurt42'
DBH=localhost
DBP=3306
usage() {
echo "This script needs one argument: an SQL script to run, e.g."
echo " $(basename $0) foo/bar.sql"
exit 1
}
mkdroptables() {
cat > $1 << Eof
SET FOREIGN_KEY_CHECKS = 0;
SET GROUP_CONCAT_MAX_LEN=32768;
SET @tables = NULL;
SELECT GROUP_CONCAT('\`', table_name, '\`') INTO @tables
FROM information_schema.tables
WHERE table_schema = (SELECT DATABASE());
SELECT IFNULL(@tables,'dummy') INTO @tables;
SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
Eof
}
runscript() {
echo Running $1 ...
if [ ! -s $1 ] || [ -d $1 ]; then echo "script $2 not found, or zero length."; exit 3; fi
mysql -h$DBH -P$DBP -u$DBu -p$DBp $DB < $1
}
dumpDB() {
mysqldump -h$DBH -P$DBP -u$DBu -p$DBp $DB -r dump.sql --extended-insert=FALSE
}
redump() {
STARTDIR=$PWD
cd $1 ; echo Working in $PWD
if [ ! -s $2 ] || [ -d $2 ]; then echo "script $2 not found. Need absolute path. Try \$PWD/$2"; exit 2; fi
mkdroptables droptables.sql
runscript droptables.sql
rm -f droptables.sql
runscript dump.sql
runscript $2
dumpDB
cd $STARTDIR
}
if [ -z $1 ]; then usage ; fi
for f in $(find src/functionalTest/testResources/parseTest -type f -name dump.sql); do redump $(dirname $f) $1; done
# pre-requsites:
# 1. create a temp DB & maybe a user
# mysql> create database tmp42;
# mysql> grant all on 'tmp42.*' to db42u@localhost identified by 'passwurt42';
# 2. create a script to run on temp DB
# echo "SELECT 1;" > foo.sql
# 3. call script and give it an absolute path to this script
# redump $PWD/foo.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment