#!/usr/bin/env bash
#
# Script to generate schemas for the various versions.
#
# Some setup is required, similar to the opportunistic tests.
#
# MySQL ->
#
# $ mysql -uroot
# MariaDB [(none)]> CREATE DATABASE keystone
# MariaDB [(none)]> GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY 'password';
# MariaDB [(none)]> quit;
#
# Postgres ->
#
# $ sudo -u postgres psql
# postgres=# create user keystone with createdb login password 'password';
# postgres=# create database keystone with owner keystone;
# postgres=# quit;
#
# Note that you may also have to configure 'pg_hba.conf' to use password-based
# auth instead of "ident", if you haven't done so already. You can locate this
# with 'locate pg_hba.conf'. More details at
# https://ubuntu.com/server/docs/databases-postgresql

set -o xtrace
set -e

source .tox/py38/bin/activate

INIT_VERSION=$(ls -1 keystone/common/sql/legacy_migrations/expand_repo/versions/ | head -1 | awk -F_ '{print $1}' | sed 's/^0*//')
INIT_VERSION=$(($INIT_VERSION-1))

echo "Detected init version of $INIT_VERSION"

mkdir -p /tmp/keystone-schemas
rm -f "/tmp/keystone-schemas/$INIT_VERSION-*.sql"

#
# functions
#

function sync () {
    DB_URL=$1

    python keystone/common/sql/legacy_migrations/expand_repo/manage.py version_control \
      --database "$DB_URL" \
      --version "$INIT_VERSION" \
      --repository keystone/common/sql/legacy_migrations/expand_repo/
    python keystone/common/sql/legacy_migrations/data_migration_repo/manage.py version_control \
      --database "$DB_URL" \
      --version "$INIT_VERSION" \
      --repository keystone/common/sql/legacy_migrations/data_migration_repo/
    python keystone/common/sql/legacy_migrations/contract_repo/manage.py version_control \
      --database "$DB_URL" \
      --version "$INIT_VERSION" \
      --repository keystone/common/sql/legacy_migrations/contract_repo/

    python keystone/common/sql/legacy_migrations/expand_repo/manage.py upgrade \
      --database "$DB_URL" \
      --repository keystone/common/sql/legacy_migrations/expand_repo/
    python keystone/common/sql/legacy_migrations/data_migration_repo/manage.py upgrade \
      --database "$DB_URL" \
      --repository keystone/common/sql/legacy_migrations/data_migration_repo/
    python keystone/common/sql/legacy_migrations/contract_repo/manage.py upgrade \
      --database "$DB_URL" \
      --repository keystone/common/sql/legacy_migrations/contract_repo/
}

#
# sqlite
#

# cleanup from previous runs

rm -f /tmp/keystone.db

# sync schema

sync 'sqlite:////tmp/keystone.db'

# dump the schema

sqlite3 /tmp/keystone.db << EOF
.output "/tmp/keystone-schemas/${INIT_VERSION}-sqlite.sql"
.schema
.quit
EOF

rm -f /tmp/keystone.db

#
# mysql
#

# cleanup from previous runs

mysql -u keystone -ppassword << EOF
DROP DATABASE IF EXISTS keystone;
CREATE DATABASE keystone;
EOF

# sync schema

sync 'mysql+pymysql://keystone:password@localhost/keystone'

# dump the schema

mysqldump --no-data --skip-comments -u keystone -ppassword \
    keystone > "/tmp/keystone-schemas/${INIT_VERSION}-mysql.sql"

mysql -u keystone -ppassword << EOF
DROP DATABASE IF EXISTS keystone;
EOF

#
# postgres
#

# cleanup from previous runs

sudo -u postgres dropdb --if-exists keystone
sudo -u postgres createdb --owner=keystone keystone

# sync to initial version

sync 'postgresql://keystone:password@localhost/keystone'

# dump the schema

pg_dump postgresql://keystone:password@localhost/keystone \
    --schema-only > "/tmp/keystone-schemas/${INIT_VERSION}-postgres.sql"

sudo -u postgres dropdb --if-exists keystone
