[autotest] Optionally clobber database when running setup.

This fixes setup_dev_autotest.sh not being re-entrant, and adds an
option to either drop the database or skip re-creating the database.

BUG=chromium-os:32854
TEST=run twice

Change-Id: I2d0cebce6a84682f75bd5fa44ec16b73d34c67a7
Reviewed-on: https://gerrit.chromium.org/gerrit/28981
Tested-by: Alex Miller <milleral@chromium.org>
Reviewed-by: Chris Masone <cmasone@chromium.org>
Commit-Ready: Alex Miller <milleral@chromium.org>
diff --git a/site_utils/setup_dev_autotest.sh b/site_utils/setup_dev_autotest.sh
index e88e200..f50b561 100755
--- a/site_utils/setup_dev_autotest.sh
+++ b/site_utils/setup_dev_autotest.sh
@@ -106,12 +106,7 @@
 fi
 echo -e "Done!\n"
 
-echo -n "Setting up Database: chromeos_autotest_db in MySQL..."
-SQL_COMMAND="create database chromeos_autotest_db; \
-grant all privileges on chromeos_autotest_db.* TO \
-'chromeosqa-admin'@'%' identified by '${PASSWD}'; \
-FLUSH PRIVILEGES;"
-
+echo "Setting up Database: chromeos_autotest_db in MySQL..."
 if mysql -u root -e ';' 2> /dev/null ; then
   PASSWD_STRING=
 elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
@@ -120,9 +115,33 @@
   PASSWD_STRING="-p"
 fi
 
+CLOBBERDB=
+EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
+FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
+if [ -n "${EXISTING_DATABASE}" ]; then
+  while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do
+    echo
+    if [[ -z ${CLOBBERDB} || $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]];
+    then
+      break
+    fi
+    echo "Please enter y or n."
+  done
+else
+  CLOBBERDB='y'
+fi
+
+SQL_COMMAND="drop database if exists chromeos_autotest_db; \
+create database chromeos_autotest_db; \
+grant all privileges on chromeos_autotest_db.* TO \
+'chromeosqa-admin'@'%' identified by '${PASSWD}'; \
+FLUSH PRIVILEGES;"
+
 set -e
 
-mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
+if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then
+  mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
+fi
 echo -e "Done!\n"
 
 AT_DIR=/usr/local/autotest