#!/bin/bash

###############################################################################
# 初始化数据库, 包括创建数据库和初始化数据库表
###############################################################################

set -e

base_dir=`pwd`
cd ${base_dir}/schema

host='127.0.0.1'
username='dev'
password='Dev@123456'

db1='reghao_bnt_tdb'

db11='reghao_bnt_rdb'

create_db() {
sql1="CREATE DATABASE ${db1} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"

mysql -h${host} -u${username} -p${password} << EOF
 ${sql1}
EOF
}

init_db() {
  # 还原整个数据库结构(不包含数据)
  mysql -h${host} -u${username} -p${password} ${db11} < ${db1}.sql

  # 还原表数据到数据库
  mysql -h${host} -u${username} -p${password} ${db11} < oss_store_node.sql
  mysql -h${host} -u${username} -p${password} ${db11} < oss_store_volumes.sql
  mysql -h${host} -u${username} -p${password} ${db11} < oss_upload_channel.sql
  mysql -h${host} -u${username} -p${password} ${db11} < oss_user_key.sql
  mysql -h${host} -u${username} -p${password} ${db11} < oss_user_node.sql
  mysql -h${host} -u${username} -p${password} ${db11} < sys_menu.sql
  mysql -h${host} -u${username} -p${password} ${db11} < sys_role.sql
  mysql -h${host} -u${username} -p${password} ${db11} < sys_role_menu.sql
  mysql -h${host} -u${username} -p${password} ${db11} < sys_user.sql
}

echo "create db..."
create_db

echo "init db..."
init_db
