diff --git a/config.xhgui.yaml b/config.xhgui.yaml new file mode 100644 index 0000000..18c9e8b --- /dev/null +++ b/config.xhgui.yaml @@ -0,0 +1,11 @@ +#ddev-generated +hooks: + post-start: + # Create a new database called "xhgui" + - exec: | + if [[ "$DDEV_DATABASE_FAMILY" == "postgres" ]]; then + echo "SELECT 'CREATE DATABASE xhgui' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'xhgui')\gexec" | psql > /dev/null + else + mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS xhgui; GRANT ALL ON xhgui.* to 'db'@'%';" + fi + service: db diff --git a/docker-compose.xhgui.yaml b/docker-compose.xhgui.yaml index 33cd8f0..618eca0 100644 --- a/docker-compose.xhgui.yaml +++ b/docker-compose.xhgui.yaml @@ -17,35 +17,11 @@ services: - VIRTUAL_HOST=$DDEV_HOSTNAME - HTTP_EXPOSE=8142:80 - HTTPS_EXPOSE=8143:80 - - XHGUI_MONGO_HOSTNAME=xhgui-mongo - - XHGUI_MONGO_DATABASE=xhprof + - XHGUI_SAVE_HANDLER=pdo + - DDEV_DATABASE_FAMILY=${DDEV_DATABASE_FAMILY} + - XHGUI_PDO_USER=db + - XHGUI_PDO_PASS=db links: - - xhgui-mongo + - db depends_on: - - xhgui-mongo - - web: - links: - - xhgui - depends_on: - - xhgui - - xhgui-mongo: - # https://hub.docker.com/r/percona/percona-server-mongodb/tags - image: percona/percona-server-mongodb:6.0-multi - container_name: ddev-${DDEV_SITENAME}-xhgui-mongo - command: --storageEngine=wiredTiger - restart: always - labels: - com.ddev.site-name: ${DDEV_SITENAME} - com.ddev.approot: $DDEV_APPROOT - environment: - - MONGO_INITDB_DATABASE=xhprof - volumes: - - ./xhgui-mongo/mongo.init.d:/docker-entrypoint-initdb.d - - xhgui-mongo:/data/db - expose: - - "27017" - -volumes: - xhgui-mongo: + - db diff --git a/install.yaml b/install.yaml index 07b88f0..d935bd1 100644 --- a/install.yaml +++ b/install.yaml @@ -2,6 +2,7 @@ name: xhgui project_files: - docker-compose.xhgui.yaml +- config.xhgui.yaml - commands/host/xhgui - xhgui/Dockerfile - xhgui/xhgui.config.php @@ -9,4 +10,7 @@ project_files: - xhgui/collector/xhgui.collector.php - xhgui/nginx.conf - xhprof/xhprof_prepend.php -- xhgui-mongo/mongo.init.d + +removal_actions: +- if [[ "$DDEV_DATABASE_FAMILY" == "postgres" ]]; then ddev psql -U db -c "drop database xhgui"; fi +- if [[ "$DDEV_DATABASE_FAMILY" != "postgres" ]]; then ddev mysql -uroot -proot -e "DROP DATABASE IF EXISTS xhgui"; fi diff --git a/tests/test.bats b/tests/test.bats index 99dc921..9a0dcf9 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -2,15 +2,12 @@ setup() { set -eu -o pipefail export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.." - export TESTDIR=~/tmp/ddev-xhgui + export PROJNAME=test-ddev-xhgui + export TESTDIR=~/tmp/${PROJNAME} mkdir -p $TESTDIR - export PROJNAME=ddev-xhgui export DDEV_NON_INTERACTIVE=true ddev delete -Oy ${PROJNAME} || true cd "${TESTDIR}" - ddev config --project-name=${PROJNAME} - ddev start -y - echo "# ddev started at $(date)" >&3 } teardown() { @@ -41,6 +38,7 @@ collector_checks() { @test "install from directory" { set -eu -o pipefail cd ${TESTDIR} + ddev config --project-name=${PROJNAME} echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 ddev get ${DIR} ddev restart @@ -52,6 +50,7 @@ collector_checks() { @test "install from release" { set -eu -o pipefail cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 ) + ddev config --project-name=${PROJNAME} echo "# ddev get tyler36/ddev-xhgui with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 ddev get tyler36/ddev-xhgui ddev restart @@ -60,15 +59,71 @@ collector_checks() { health_checks } -@test "a site can be profiled" { +@test "it can profile using the default (mariadb) database" { set -eu -o pipefail cd ${TESTDIR} # Create test site echo "# Create a demo website at ${TESTDIR}" >&3 + ddev config --docroot=public --create-docroot + ddev composer require perftools/php-profiler + ddev composer install + echo "${TESTDIR}/public/index.php + + echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 + ddev get ${DIR} + ddev restart + + # Check service works + health_checks + collector_checks + + # Check it removes database on uninstall. 'mysql "name"' returns 1 if db exists, 0 if missing. + ddev mysql "xhgui" -e exit > /dev/null 2>&1 && echo "Database exists." | grep "exists" + ddev get --remove ${DIR} + ddev mysql "xhgui" -e exit > /dev/null 2>&1 && echo "Database exists." || echo "Database missing" | grep "missing" +} + +@test "it can profile using a MySQL database" { + set -eu -o pipefail + cd ${TESTDIR} + + # Create test site + echo "# Create a demo website at ${TESTDIR} using MySQL" >&3 + ddev config --docroot=public --create-docroot --database=mysql:8.0 + ddev composer require perftools/php-profiler + ddev composer install + echo "${TESTDIR}/public/index.php + + echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 + ddev get ${DIR} + ddev restart + + # Check service works + health_checks + collector_checks + + # Check it removes database on uninstall. 'mysql "name"' returns 1 if db exists, 0 if missing. + ddev mysql "xhgui" -e exit > /dev/null 2>&1 && echo "Database exists." | grep "exists" + ddev get --remove ${DIR} + ddev mysql "xhgui" -e exit > /dev/null 2>&1 && echo "Database exists." || echo "Database missing" | grep "missing" +} + +@test "it can profile using a Postres database" { + set -eu -o pipefail + cd ${TESTDIR} + + # Create test site + echo "# Create a demo website at ${TESTDIR} using Postgres" >&3 + ddev config --docroot=public --create-docroot --database=postgres:16 ddev composer require perftools/php-profiler ddev composer install - ddev config --docroot=public --create-docroot echo "${TESTDIR}/public/index.php # Check service works health_checks collector_checks + + # Check it removes database on uninstall. `psql "xhgui" -c '\q'` returns 1 if db exists, 0 if missing. + ddev psql "xhgui" -c '\q' > /dev/null 2>&1 && echo "Database exists." | grep "exists" + ddev get --remove ${DIR} + ddev psql "xhgui" -c '\q' > /dev/null 2>&1 && echo "Database exists." || echo "Database missing" | grep "missing" } diff --git a/xhgui-mongo/mongo.init.d/.gitmanaged b/xhgui-mongo/mongo.init.d/.gitmanaged deleted file mode 100644 index a8c84e0..0000000 --- a/xhgui-mongo/mongo.init.d/.gitmanaged +++ /dev/null @@ -1 +0,0 @@ -#ddev-generated diff --git a/xhgui/xhgui.config.php b/xhgui/xhgui.config.php index 190b1cd..f5ee2ae 100644 --- a/xhgui/xhgui.config.php +++ b/xhgui/xhgui.config.php @@ -7,6 +7,11 @@ * and return an array from there with your overriding settings. */ +$DDEV_DATABASE_FAMILY = getenv('DDEV_DATABASE_FAMILY', 'mysql'); +$XHGUI_PDO_DSN = $DDEV_DATABASE_FAMILY === 'postgres' + ? 'pgsql:host=db;dbname=xhgui' + : "$DDEV_DATABASE_FAMILY:host=db;dbname=xhgui"; + return [ // Which backend to use for Xhgui_Saver. // Must be one of 'mongodb', or 'pdo'. @@ -14,9 +19,9 @@ // Database options for PDO. 'pdo' => [ - 'dsn' => getenv('XHGUI_PDO_DSN') ?: null, - 'user' => getenv('XHGUI_PDO_USER') ?: null, - 'pass' => getenv('XHGUI_PDO_PASS') ?: null, + 'dsn' => $XHGUI_PDO_DSN, + 'user' => getenv('XHGUI_PDO_USER') ?: 'db', + 'pass' => getenv('XHGUI_PDO_PASS') ?: 'db', 'table' => getenv('XHGUI_PDO_TABLE') ?: 'results', 'tableWatch' => getenv('XHGUI_PDO_TABLE_WATCHES') ?: 'watches', ],