Skip to content

Instantly share code, notes, and snippets.

@darth30joker
Created September 1, 2015 08:18
Show Gist options
  • Save darth30joker/b4654fcf6894c6f18516 to your computer and use it in GitHub Desktop.
Save darth30joker/b4654fcf6894c6f18516 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
###################################################################################
# #
# Copyright 2010-2011 Ning, Inc. #
# #
# Ning licenses this file to you under the Apache License, version 2.0 #
# (the "License"); you may not use this file except in compliance with the #
# License. You may obtain a copy of the License at: #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT #
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the #
# License for the specific language governing permissions and limitations #
# under the License. #
# #
###################################################################################
# set -x
HERE=`cd \`dirname $0\`; pwd`
TOP=$HERE/..
POM="$TOP/pom.xml"
ACTION=
HOST="localhost"
DATABASE="killbill"
USER="root"
PWD="root"
DRIVER="mysql"
TEST_ALSO=
OUTPUT_FILE=
DDL_FILE=
CLEAN_FILE=
# Egrep like for skipping some modules until they are ready
SKIP="(server)"
ARGS=`getopt -o "a:d:h:u:p:t:f:" -l "action:,driver:,database:,host:,user:,password:,test:,file:,port:,help" -n "db-helper" -- "$@"`
eval set -- "${ARGS}"
function usage() {
echo -n "./db_helper"
echo -n " -a|--action <create|clean|dump>"
echo -n " --driver <mysql|postgres> (default = mysql)"
echo -n " -h|--host host (default = localhost)"
echo -n " --port port"
echo -n " -d|--database database_name (default = killbill)"
echo -n " -u|--user user_name (default = root)"
echo -n " -p|--password password (default = root)"
echo -n " -t|--test (also include test ddl)"
echo -n " -f|--file file (output file, for dump only)"
echo -n " --help this message"
echo
exit 1
}
function get_modules() {
local modules=`grep module $POM | grep -v modules | cut -d '>' -f 2 | cut -d '<' -f 1 | egrep -v "$SKIP"`
echo $modules
}
function find_test_ddl() {
local modules=`get_modules`
local ddl_test=
local cur_ddl=
for m in $modules; do
cur_ddl=`find $TOP/$m/src/test/resources/ -name ddl_test.sql 2>/dev/null`
ddl_test="$ddl_test $cur_ddl"
done
echo "$ddl_test"
}
function find_src_ddl() {
local modules=`get_modules`
local ddl_src=
local cur_ddl=
for m in $modules; do
cur_ddl=`find $TOP/$m/src/main/resources/ -name ddl.sql 2>/dev/null`
ddl_src="$ddl_src $cur_ddl"
done
echo "$ddl_src"
}
function create_clean_file() {
local ddl_file=$1
local tables=`cat $ddl_file | grep -i "create table" | awk ' { print $3 } '`
local tmp="/tmp/clean-$DATABASE.$$"
echo "/*! use $DATABASE; */" >> $tmp
echo "" >> $tmp
for t in $tables; do
echo "truncate $t;" >> $tmp
done
echo $tmp
}
function create_ddl_file() {
local ddls=`find_src_ddl`
local test_ddls=
if [ ! -z $TEST_ALSO ]; then
test_ddls=`find_test_ddl`
ddls="$ddls $test_ddls"
fi
local tmp="/tmp/ddl-$DATABASE.$$"
touch $tmp
echo "/*! use $DATABASE; */" >> $tmp
echo "" >> $tmp
for d in $ddls; do
cat $d >> $tmp
echo "" >> $tmp
done
echo $tmp
}
function cleanup() {
rm -f "/tmp/*.$$"
}
while true; do
case "$1" in
-a|--action) ACTION=$2; shift 2;;
--driver) DRIVER=$2; shift 2;;
-d|--database) DATABASE=$2; shift 2;;
-h|--host) HOST=$2; shift 2;;
-u|--user) USER=$2; shift 2;;
-p|--password) PWD=$2; shift 2;;
-t|--test) TEST_ALSO=1; shift 2;;
-f|--file) OUTPUT_FILE=$2; shift 2;;
--help) usage; shift;;
--) shift; break;;
esac
done
if [ -z $ACTION ]; then
echo "Need to specify an action <CREATE|CLEAN|DUMP>"
usage
fi
if [ $DRIVER != "mysql" ] && [ $DRIVER != "postgres" ]; then
echo "Only support driver <mysql> or <postgres>"
usage
fi
if [ $ACTION == "dump" ]; then
DDL_FILE=`create_ddl_file`
if [ -z $OUTPUT_FILE ]; then
cat $DDL_FILE
else
cat $DDL_FILE > $OUTPUT_FILE
fi
fi
if [ $ACTION == "create" ]; then
DDL_FILE=`create_ddl_file`
echo "Applying new schema to database $DATABASE"
if [ $DRIVER == "mysql" ]; then
mysql -h $HOST -u $USER --password=$PWD < $DDL_FILE
else
psql -h $HOST -U $USER -d $DATABASE < $DDL_FILE
fi
fi
if [ $ACTION == "clean" ]; then
DDL_FILE=`create_ddl_file`
CLEAN_FILE=`create_clean_file $DDL_FILE`
echo "Cleaning db tables on database $DATABASE"
if [ $DRIVER == "mysql" ]; then
mysql -h $HOST -u $USER --password=$PWD < $DDL_FILE
else
psql -h $HOST -U $USER -d $DATABASE < $DDL_FILE
fi
fi
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment