Skip to content

Commit

Permalink
[12785] Add DB-Tools helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmoozerd committed Nov 14, 2014
1 parent 00d86f0 commit 9e7c1ed
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 1 deletion.
2 changes: 2 additions & 0 deletions contrib/DB_Tools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
db_info.conf
75 changes: 75 additions & 0 deletions contrib/DB_Tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# The DB_Tools subdirectory
This directory is a place to store sql-scripts for database work and maintenance.

The core team will provide sql-files that will not change any content in any database, but will output data that is considered incorrect or obsolete.

We will use these subdirectories:
* WorldDB -- To output content we consider bad filled in the world-database (default: mangos)
* CharDB -- To output content we consider bad filled in the characters-database (default: characters)
* RealmDB -- To output content we consider bad filled in the realmd-database (default: realmd)

You can add additional Folders as you wish to store own sql-files for your own purposes.
These folders are also processable with the provided scripts.

You can use the sql-files just like any normal sql-files, however for easy use and batch-mode there are shipped two helper scripts:

1. UseDB_Tools.sh
2. UseDB_Tools_Interactive.sh

The only difference between these two scripts is, that the second will always run in "interactive" mode.

These scripts will help you executing the sql-files of the subdirectories.

## Introduction to the UseDB_Tools[_Interactive].sh helper scripts
The scripts will execute sql-scripts of the subfolders and log the output in files named
`<subfolder>.log`.

You can run them with the following parameters:
* -h display help
This will give a short information about the available options.
* -i for interactive mode
In the interactive mode you will be asked about which subfolders and which files to process.
* -f DIR to force processing only the subfolder DIR
This option allows you to use the script only on one of the given subfolders. The subfolder must be provided as plain name without additional path information.
* -q run in quiet mode
When this option is set, you will not be bothered with too much information.
* -d DB to force using the database DB instead of the one named in the configuration

## Configuration files in the subfolders
When the script is run it will try to guess the used database for a subfolder based on its name.
With a normal setup, you will not have to worry about this at all!

However in case that you use non default user or databasenames, or wish to use the tools on special databases, you can modify the files named

`db_info.conf`

located in each of a processed subfolder to your needs.

These config files are pretty clear, they only store information about database, database-user and the user's password.

This information is used by the UseDB_Tools[_Interactive].sh helper scripts for the subfolder, the content of these files is only stored on your side and not safed with git or similar.

Note however that storing sensible information is a security risk, so you should do so only on trusted systems (or when the information is non-critical)

## Suggested format of the sql-files
As the sql-files are processed automatically (with the mysql command), they must be proper sql syntax.

For automated use it is also suggested to follow these guidelines:
* First line should be a comment giving rough information about what the sql-commands in this file will do.
Note that the first line will always be displayed in the console as information about what is supposed to happen in the script.
* Additional comments that are emphasized with `-- --` will be output in non-quiet mode as additional information to the user.
* Further comments will be ignored
* The actual SQL statements should be placed at the end of the file.

An example file might look like:

-- This is an example. It will ensure that Hogger is spawned

-- -- This comment is displayed without the -q (quiet) option.
-- -- This SQL file ensures that the most important NPC is spawned in the world.
-- -- Is Hogger spawned?

-- This is a comment for the sql-file only, the script will not touch it.
-- The following query ensures that Hogger is spawned!
-- Do not forget the semicolon at the end
SELECT guid, id, position_x, position_y, position_z, map FROM creature WHERE id=448;
211 changes: 211 additions & 0 deletions contrib/DB_Tools/UseDB_Tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#!/bin/bash
# This tool is used to work upon sql files stored in subfolders of this folder
#
#
# Known options
# -i for interactive mode
# -h display help
# -f DIR Only process directory
# -q quiet mode
# d DB Work upon database DB
#
function show_help
{
echo "You can run this tool with the following options:"
echo " -i To enable interactive mode"
echo " -q To be quiet about much of the output"
echo " -f DIR To only work on the folder DIR"
echo " -h or -? To display this help"
echo " -d DB Force use database DB"
}

SCRIPT_PATH="${BASH_SOURCE[0]}"
SCRIPT_DIR=`dirname "$SCRIPT_PATH"`

MYSQL="mysql"
DATABASE=
DB_USER=
DB_PASS=

TMP_FILE="tmp_UseDBTools_$RANDOM"

#options
interactive=0
quiet=0
only_directory=""
force_database=""

while getopts "h?iqf:d:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
q) quiet=1
;;
f) only_directory=$OPTARG
;;
d) force_database=$OPTARG
;;
i) interactive=1
;;
esac
done

# $1 Must be a directory
function create_db_config
{
if [ "$interactive" = "1" ]
then
echo "Which DB should be operated within directory $1?"
read line
DATABASE=$line
echo "Which user should be used for database $DATABASE?"
read line
DB_USER=$line
echo "Which password should be used for database $DATABASE and user $DB_USER?"
read line
DB_PASS=$line
else
#Guess based on starting string
DB_USER="mangos"
DB_PASS="mangos"
case `basename "$1"` in
world* | World*)
DATABASE="mangos"
;;
char* | Char*)
DATABASE="characters"
;;
realm* | Realm*)
DATABASE="realmd"
;;
script* | Script*)
DATABASE="scriptdev2"
;;
*)
echo "Unable to guess a proper name for the database, use 'mangos' as default."
DATABASE="mangos"
;;
esac
fi

cat << EOF > "$1"/db_info.conf
DATABASE="$DATABASE"
DB_USER="$DB_USER"
DB_PASS="$DB_PASS"
EOF
}

# $1 Must be a subdirectory
function work_directory
{
log_file="${1}.log"

echo "UseDB_Tools: Start working on directory ${1}" | tee "$log_file"
echo "" | tee -a "$log_file"

#Create if needed and get DB-config
if [ ! -e "$1"/db_info.conf ]
then
create_db_config "$1"
fi

. "$1"/db_info.conf

if [ "$force_database" != "" ]
then
DATABASE="$force_database"
fi

for f in ${1}/*.sql
do
if [ ! -f "$f" ]; then break; fi

echo "Process file $f" | tee -a "$TMP_FILE"
echo "" | tee -a "$TMP_FILE"

if [ "$quiet" = "0" ]; then
head -n1 "$f" | tee -a "$TMP_FILE"
echo "" | tee -a "$TMP_FILE"
else
head -n1 "$f"
echo ""
fi

if [ "$quiet" = "0" ]
then
grep "\-\- \-\-" "$f" | tee -a "$TMP_FILE"
fi
# Interactive mode: ASK
process_file=1
if [ "$interactive" = "1" ]
then
process_file=0
echo "Shall I process this file? (y/n)"
read line
if [ "$line" = "y" ]
then
process_file=1
fi
fi

# DO the actual work
if [ "$process_file" = "1" ]
then
cat "$TMP_FILE" >> "$log_file"
echo "Output:" | tee -a "$log_file"
$MYSQL -u$DB_USER -p$DB_PASS $DATABASE < "$f" > "$TMP_FILE" 2>&1
if [ "$?" != "0" ]
then
cat "$TMP_FILE" | tee -a "$log_file"
exit 1
fi
cat "$TMP_FILE" | tee -a "$log_file"

echo "" | tee -a "$log_file"
echo "" | tee -a "$log_file"
fi

rm "$TMP_FILE"
done

echo "" | tee -a "$log_file"
echo "" | tee -a "$log_file"
echo "" | tee -a "$log_file"
}

if [ "$only_directory" = "" ]
then
OLD_IFS="$IFS"
IFS=$'\n'
for dir in `find "$SCRIPT_DIR" -type d`
do
#exclude self
if [ "$dir" != "$SCRIPT_DIR" ]
then
if [ "$interactive" = "1" ]
then
echo "Shall I process directory $dir? (y/n)"
read line
if [ "$line" = "y" ]
then
work_directory "$dir"
fi
else
# work it
work_directory "$dir"
fi
fi
done
IFS=$OLD_IFS
else
if [ -d "$SCRIPT_DIR/$only_directory" ]
then
work_directory "$SCRIPT_DIR/$only_directory"
else
echo "ERROR - passed parameter of -d option must be a subdirectory (without additional path)"
exit 1
fi
fi
4 changes: 4 additions & 0 deletions contrib/DB_Tools/UseDB_Tools_Interactive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
# Only simple wrapper to call in interactive mode
SCRIPT_PATH="${BASH_SOURCE[0]}"
./`dirname ${SCRIPT_PATH}`/UseDB_Tools.sh -i $@
2 changes: 1 addition & 1 deletion src/shared/revision_nr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "12784"
#define REVISION_NR "12785"
#endif // __REVISION_NR_H__

0 comments on commit 9e7c1ed

Please sign in to comment.