#!/bin/bash

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

set -e

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

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

db1='tnb_account_tdb'
db2='tnb_content_tdb'

db11='tnb_account_rdb'
db21='tnb_content_rdb'

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

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

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

  # 还原表数据到数据库
  mysql -h${host} -u${username} -p${password} ${db11} < msg_email_account.sql
  mysql -h${host} -u${username} -p${password} ${db11} < user_registry.sql
  mysql -h${host} -u${username} -p${password} ${db11} < user_vip_plan.sql

  mysql -h${host} -u${username} -p${password} ${db21} < file_store_config.sql
  mysql -h${host} -u${username} -p${password} ${db21} < vod_video_category.sql
}

echo "create db..."
create_db

echo "init db..."
init_db
