#!/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 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

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

[ -x "$SCRIPT" ] || { echo "SKIP: $SCRIPT not installed"; exit 77; }
[ -r "$SEED" ]   || { echo "SKIP: $SEED not installed"; exit 77; }

MARIADB_DIR="$AUTOPKGTEST_TMP/db"
MARIADB_DATA="$MARIADB_DIR/data"
MARIADB_SOCK="$MARIADB_DIR/mysqld.sock"
MARIADB_PIDFILE="$MARIADB_DIR/mysqld.pid"

# --- Boot a private MariaDB (does not touch the system service) --------------
rm -rf "$MARIADB_DIR"
mkdir -p "$MARIADB_DATA"
chown -R mysql:mysql "$MARIADB_DIR"

echo "Setting up the database server ..."
mariadb-install-db --no-defaults --user=mysql --datadir="$MARIADB_DATA" \
    --auth-root-authentication-method=normal --skip-name-resolve >/dev/null 2>&1
/usr/sbin/mariadbd --no-defaults --user=mysql --datadir="$MARIADB_DATA" \
    --socket="$MARIADB_SOCK" --pid-file="$MARIADB_PIDFILE" \
    --bind-address=127.0.0.1 --port="$PORT" >/dev/null 2>&1 &
MARIADB_PID=$!

cleanup() {
    kill "$MARIADB_PID" 2>/dev/null || echo "Unable to kill mariadb-server"
    [ -f "$CONFIG.dep8-bak" ] && mv "$CONFIG.dep8-bak" "$CONFIG"
    return 0
}
trap cleanup EXIT

echo "Waiting for the database to start ..."
for _ in $(seq 1 60); do
    mariadb --no-defaults --socket="$MARIADB_SOCK" --user=root -e 'SELECT 1' >/dev/null 2>&1 && break
    sleep 1
done

# --- Create the control user and seed the schema from the package's SQL ------
echo "Setting up the control user, database and schema ..."
cat > "$AUTOPKGTEST_TMP/db_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
mariadb --no-defaults --socket="$MARIADB_SOCK" --user=root < "$AUTOPKGTEST_TMP/db_setup.sql"
mariadb --no-defaults --socket="$MARIADB_SOCK" --user=root < "$SEED"

# --- Point the upgrade script's fixed config path at the private instance -----
echo "Pointing $CONFIG at the private instance ..."
[ -f "$CONFIG" ] && cp "$CONFIG" "$CONFIG.dep8-bak"
mkdir -p "$(dirname "$CONFIG")"
cat > "$CONFIG" <<PHP
<?php
\$dbtype='mysql';
\$dbserver='127.0.0.1';
\$dbport='$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 "$SCRIPT"; then
    echo "PASS: upgrade script exited 0 on an already-migrated database"
else
    rc=$?
    echo "FAIL: upgrade script exited $rc; it must ignore the already-applied migration"
    exit 1
fi
