#!/bin/bash

# Common DB
#
# Shared script to set various database-related variables
#
# @author    nystudio107
# @copyright Copyright (c) 2017 nystudio107
# @link      https://nystudio107.com/
# @package   craft-scripts
# @since     1.1.0
# @license   MIT

# Tables to exclude from the mysqldump
EXCLUDED_TABLES=(
            "assetindexdata"
            "assettransformindex"
            "cache"
            "sessions"
            "templatecaches"
            "templatecachecriteria"
            "templatecacheelements"
            )

IGNORED_TABLES_STRING=""
for TABLE in "${EXCLUDED_TABLES[@]}"
do
   IGNORED_TABLES_STRING+="--ignore-table=${LOCAL_DB_NAME}.${GLOBAL_DB_TABLE_PREFIX}${TABLE} "
done

# Additional arguments for mysqldump
MYSQLDUMP_ADDITIONAL_ARGS=""
MYSQLDUMP_ADDITIONAL_ARGS+="--add-drop-table "
MYSQLDUMP_ADDITIONAL_ARGS+="--comments "
MYSQLDUMP_ADDITIONAL_ARGS+="--create-options "
MYSQLDUMP_ADDITIONAL_ARGS+="--dump-date "
MYSQLDUMP_ADDITIONAL_ARGS+="--no-autocommit "
MYSQLDUMP_ADDITIONAL_ARGS+="--routines "
MYSQLDUMP_ADDITIONAL_ARGS+="--set-charset "
MYSQLDUMP_ADDITIONAL_ARGS+="--triggers "

# Arguments to dump just the schema
MYSQLDUMP_SCHEMA_ARGS=""
MYSQLDUMP_SCHEMA_ARGS+="--single-transaction "
MYSQLDUMP_SCHEMA_ARGS+="--no-data "
MYSQLDUMP_SCHEMA_ARGS+=$MYSQLDUMP_ADDITIONAL_ARGS

# Arguments to dump just the data
MYSQLDUMP_DATA_ARGS=""
MYSQLDUMP_DATA_ARGS+="--no-create-info "
MYSQLDUMP_DATA_ARGS+=$IGNORED_TABLES_STRING
MYSQLDUMP_DATA_ARGS+=$MYSQLDUMP_ADDITIONAL_ARGS

# Build the remote mysql credentials
REMOTE_DB_CREDS=""
if [ "${REMOTE_DB_USER}" != "" ] ; then
    REMOTE_DB_CREDS+="--user=${REMOTE_DB_USER} "
fi
if [ "${REMOTE_DB_PASSWORD}" != "" ] ; then
    REMOTE_DB_CREDS+="--password=${REMOTE_DB_PASSWORD} "
fi
if [ "${REMOTE_DB_HOST}" != "" ] ; then
    REMOTE_DB_CREDS+="--host=${REMOTE_DB_HOST} "
fi
if [ "${REMOTE_DB_PORT}" != "" ] ; then
    REMOTE_DB_CREDS+="--port=${REMOTE_DB_PORT} "
fi
# Use login-path if they have it set instead
if [ "${REMOTE_DB_LOGIN_PATH}" != "" ] ; then
    REMOTE_DB_CREDS="--login-path=${REMOTE_DB_LOGIN_PATH} "
fi
REMOTE_DB_CREDS+="${REMOTE_DB_NAME}"

# Build the local mysql credentials
LOCAL_DB_CREDS=""
if [ "${LOCAL_DB_USER}" != "" ] ; then
    LOCAL_DB_CREDS+="--user=${LOCAL_DB_USER} "
fi
if [ "${LOCAL_DB_PASSWORD}" != "" ] ; then
    LOCAL_DB_CREDS+="--password=${LOCAL_DB_PASSWORD} "
fi
if [ "${LOCAL_DB_HOST}" != "" ] ; then
    LOCAL_DB_CREDS+="--host=${LOCAL_DB_HOST} "
fi
if [ "${LOCAL_DB_PORT}" != "" ] ; then
    LOCAL_DB_CREDS+="--port=${LOCAL_DB_PORT} "
fi
# Use login-path if they have it set instead
if [ "${LOCAL_DB_LOGIN_PATH}" != "" ] ; then
    LOCAL_DB_CREDS="--login-path=${LOCAL_DB_LOGIN_PATH} "
fi
LOCAL_DB_CREDS+="${LOCAL_DB_NAME}"
