#!/bin/sh
# Regression test for the dbconfig-common upgrade script
#   /usr/share/dbconfig-common/scripts/phpmyadmin/upgrade/mysql/4:4.5.0.1-1
#
# The script re-applies a migration that phpMyAdmin may already have performed,
# so its "ALTER TABLE pma__column_info ADD ..." is expected to fail with a
# duplicate-column error, which must be ignored. Since PHP 8.1 mysqli throws
# exceptions by default, the unhandled error makes the script exit non-zero,
# which aborts the dbconfig-common upgrade and leaves the package
# half-configured (dpkg: "post-installation script subprocess returned error
# exit status 1").
#
# A private MariaDB is booted in $AUTOPKGTEST_TMP (see debian/tests/lib/mariadb.sh)
# and the control database is seeded from the SQL shipped by the package itself
# (sql/create_tables.sql), which already contains the input_transformation
# columns, so this reproduces the exact failing situation. The upgrade script
# then connects as a dedicated control user, exactly like a real phpMyAdmin
# dbconfig setup.
set -eu

# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/helpers.sh"
# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/mariadb.sh"

SCRIPT="/usr/share/dbconfig-common/scripts/phpmyadmin/upgrade/mysql/4:4.5.0.1-1"
SEED="/usr/share/phpmyadmin/sql/create_tables.sql"
CONFIG="$AUTOPKGTEST_TMP/config-db.php"
DB="phpmyadmin"
DBUSER="pma_dep8"
DBPASS="pma_dep8_secret"

if [ ! -x "$SCRIPT" ]; then
    dep8_fail "$SCRIPT not installed"
fi
if [ ! -r "$SEED" ]; then
    dep8_fail "$SEED not installed"
fi

dep8_require mariadb mariadb-install-db

cleanup() {
    mariadb_stop
    return 0
}
trap cleanup EXIT

mariadb_start_private

# --- Create the control user and seed the schema from the package's SQL ------
echo "Setting up the control user, database and schema ..."
setup_sql="$AUTOPKGTEST_TMP/db_setup.sql"
cat > "$setup_sql" <<SQL
CREATE USER IF NOT EXISTS '$DBUSER'@'127.0.0.1' IDENTIFIED BY '$DBPASS';
GRANT SELECT, INSERT, DELETE, UPDATE, ALTER ON \`$DB\`.* TO '$DBUSER'@'127.0.0.1';
SQL
dep8_run "$DEP8_MARIADB_LOG" mariadb_sql < "$setup_sql"
dep8_run "$DEP8_MARIADB_LOG" mariadb_sql < "$SEED"

# --- Run the shipped script from a copy that reads a local configuration ------
# The script requires /etc/phpmyadmin/config-db.php by absolute path, so a copy is
# made in $AUTOPKGTEST_TMP with that one line rewritten. Everything else -- the
# SQL and the error handling being tested -- is the shipped code, and nothing
# outside the temporary directory is written, so no root is needed.
SCRIPT_COPY="$AUTOPKGTEST_TMP/upgrade-script"
sed "s|'/etc/phpmyadmin/config-db.php'|'$CONFIG'|" "$SCRIPT" > "$SCRIPT_COPY"
if ! grep -q "'$CONFIG'" "$SCRIPT_COPY"; then
    dep8_fail "could not point the upgrade script at $CONFIG"
fi

echo "Writing a configuration for the private instance ..."
cat > "$CONFIG" <<PHP
<?php
\$dbtype='mysql';
\$dbserver='127.0.0.1';
\$dbport='$DEP8_MARIADB_PORT';
\$dbname='$DB';
\$dbuser='$DBUSER';
\$dbpass='$DBPASS';
PHP

# --- Run the shipped upgrade script against the already-migrated schema -------
echo "Running the upgrade script against the already-migrated schema ..."
if php "$SCRIPT_COPY"; then
    dep8_pass "upgrade script exited 0 on an already-migrated database"
else
    rc=$?
    dep8_fail "upgrade script exited $rc; it must ignore the already-applied migration"
fi
