Changes in database for allowing IP parameters, changes in DB migration script, new...
[osm/RO.git] / database_utils / init_mano_db.sh
index c876ea4..8afd9c2 100755 (executable)
@@ -26,6 +26,7 @@ DBPASS=""
 DBHOST="localhost"
 DBPORT="3306"
 DBNAME="mano_db"
+CREATEDB=""
 
 # Detect paths
 MYSQL=$(which mysql)
@@ -41,11 +42,12 @@ function usage(){
     echo -e "     -p PASS  database password. 'No password' by default. Prompts if DB access fails"
     echo -e "     -P PORT  database port. '$DBPORT' by default"
     echo -e "     -h HOST  database host. '$DBHOST' by default"
-#    echo -e "     -d NAME  database name. '$DBNAME' by default.  Prompts if DB access fails"
+    echo -e "     -d NAME  database name. '$DBNAME' by default.  Prompts if DB access fails"
     echo -e "     --help   shows this help"
+    echo -e "     --createdb   forces the deletion and creation of the database"
 }
 
-while getopts ":u:p:P:h:-:" o; do
+while getopts ":u:p:P:d:h:-:" o; do
     case "${o}" in
         u)
             DBUSER="$OPTARG"
@@ -56,16 +58,21 @@ while getopts ":u:p:P:h:-:" o; do
         P)
             DBPORT="$OPTARG"
             ;;
-#        d)
-#            DBNAME="$OPTARG"
-#            ;;
+        d)
+            DBNAME="$OPTARG"
+            ;;
         h)
             DBHOST="$OPTARG"
             ;;
         -)
-            [ "${OPTARG}" == "help" ] && usage && exit 0
-            echo "Invalid option: --$OPTARG" >&2 && usage  >&2
-            exit 1
+            if [ "${OPTARG}" == "help" ]; then
+                usage && exit 0
+            elif [ "${OPTARG}" == "createdb" ]; then
+                CREATEDB="yes"
+            else
+                echo "Invalid option: --$OPTARG" >&2 && usage  >&2
+                exit 1
+            fi
             ;;
         \?)
             echo "Invalid option: -$OPTARG" >&2 && usage  >&2
@@ -104,9 +111,15 @@ do
         echo
 done
 
-#${DIRNAME}/quick_delete_db.sh $MUSER $MPASS $MDB $HOST $PORT
+if [ -n "${CREATEDB}" ]; then
+    echo "    deleting previous database ${DBNAME}"
+    echo "DROP DATABASE IF EXISTS ${DBNAME}" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_
+    echo "    creating database ${DBNAME}"
+    mysqladmin $DBUSER_ $DBPASS_ -s create ${DBNAME} || exit 1
+fi
+
 echo "    loading ${DIRNAME}/${DBNAME}_structure.sql"
-mysql  $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ < ${DIRNAME}/${DBNAME}_structure.sql
+mysql  $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME < ${DIRNAME}/mano_db_structure.sql
 
 echo "    migrage database version"
 ${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME