From 1a961ceee36e9d616ecbe9df5d13ce7de3de401d Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:46:46 +0900 Subject: [PATCH 1/7] Use default DDEV database --- docker-compose.xhgui.yaml | 36 ++++++------------------------------ xhgui/xhgui.config.php | 6 +++--- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/docker-compose.xhgui.yaml b/docker-compose.xhgui.yaml index 33cd8f0..818c55e 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 + - XHGUI_PDO_DSN=${DDEV_DATABASE_FAMILY}:host=db;dbname=db + - 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/xhgui/xhgui.config.php b/xhgui/xhgui.config.php index 190b1cd..8a3f28d 100644 --- a/xhgui/xhgui.config.php +++ b/xhgui/xhgui.config.php @@ -14,9 +14,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' => getenv('XHGUI_PDO_DSN') ?: 'mysql:host=db;dbname=db', + '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', ], From 5a85b33e85b5b9d843d37bc8fd1f81596797fc8f Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:46:46 +0900 Subject: [PATCH 2/7] remove mongo files --- install.yaml | 1 - xhgui-mongo/mongo.init.d/.gitmanaged | 1 - 2 files changed, 2 deletions(-) delete mode 100644 xhgui-mongo/mongo.init.d/.gitmanaged diff --git a/install.yaml b/install.yaml index 07b88f0..1a46a7b 100644 --- a/install.yaml +++ b/install.yaml @@ -9,4 +9,3 @@ project_files: - xhgui/collector/xhgui.collector.php - xhgui/nginx.conf - xhprof/xhprof_prepend.php -- xhgui-mongo/mongo.init.d 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 From 2690434b8c2a5969828a4b3342bc76e53bc831ba Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:46:46 +0900 Subject: [PATCH 3/7] use isolated database --- config.xhgui.yaml | 5 +++++ docker-compose.xhgui.yaml | 2 +- install.yaml | 1 + xhgui/xhgui.config.php | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 config.xhgui.yaml diff --git a/config.xhgui.yaml b/config.xhgui.yaml new file mode 100644 index 0000000..3ebe6b1 --- /dev/null +++ b/config.xhgui.yaml @@ -0,0 +1,5 @@ +#ddev-generated +hooks: + post-start: + - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS xhgui; GRANT ALL ON xhgui.* to 'db'@'%';" + service: db diff --git a/docker-compose.xhgui.yaml b/docker-compose.xhgui.yaml index 818c55e..fca9074 100644 --- a/docker-compose.xhgui.yaml +++ b/docker-compose.xhgui.yaml @@ -18,7 +18,7 @@ services: - HTTP_EXPOSE=8142:80 - HTTPS_EXPOSE=8143:80 - XHGUI_SAVE_HANDLER=pdo - - XHGUI_PDO_DSN=${DDEV_DATABASE_FAMILY}:host=db;dbname=db + - XHGUI_PDO_DSN=${DDEV_DATABASE_FAMILY}:host=db;dbname=xhgui - XHGUI_PDO_USER=db - XHGUI_PDO_PASS=db links: diff --git a/install.yaml b/install.yaml index 1a46a7b..cef4579 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 diff --git a/xhgui/xhgui.config.php b/xhgui/xhgui.config.php index 8a3f28d..5a55ebf 100644 --- a/xhgui/xhgui.config.php +++ b/xhgui/xhgui.config.php @@ -14,7 +14,7 @@ // Database options for PDO. 'pdo' => [ - 'dsn' => getenv('XHGUI_PDO_DSN') ?: 'mysql:host=db;dbname=db', + 'dsn' => getenv('XHGUI_PDO_DSN') ?: 'mysql:host=db;dbname=xhgui', 'user' => getenv('XHGUI_PDO_USER') ?: 'db', 'pass' => getenv('XHGUI_PDO_PASS') ?: 'db', 'table' => getenv('XHGUI_PDO_TABLE') ?: 'results', From 7329fffdb257aee8619d04d519df5408801595a5 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:46:46 +0900 Subject: [PATCH 4/7] create DB when using postgres --- config.xhgui.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config.xhgui.yaml b/config.xhgui.yaml index 3ebe6b1..18c9e8b 100644 --- a/config.xhgui.yaml +++ b/config.xhgui.yaml @@ -1,5 +1,11 @@ #ddev-generated hooks: post-start: - - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS xhgui; GRANT ALL ON xhgui.* to 'db'@'%';" + # 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 From cd50c9252ca72600f42f3272863e34e0889c23dc Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:46:46 +0900 Subject: [PATCH 5/7] add helper comment for postgres --- docker-compose.xhgui.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.xhgui.yaml b/docker-compose.xhgui.yaml index fca9074..baba1ea 100644 --- a/docker-compose.xhgui.yaml +++ b/docker-compose.xhgui.yaml @@ -19,6 +19,8 @@ services: - HTTPS_EXPOSE=8143:80 - XHGUI_SAVE_HANDLER=pdo - XHGUI_PDO_DSN=${DDEV_DATABASE_FAMILY}:host=db;dbname=xhgui + # If using pgsql database, use the below line instead and remove "#ddev-generated" from top of file + # - XHGUI_PDO_DSN=pgsql:host=db;dbname=xhgui - XHGUI_PDO_USER=db - XHGUI_PDO_PASS=db links: From 6650acd7c78817680ddbf946aee6c5dda3a1edd4 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 26 Apr 2024 17:34:07 +0900 Subject: [PATCH 6/7] rework postgres support to be dynamic --- docker-compose.xhgui.yaml | 4 +-- tests/test.bats | 59 ++++++++++++++++++++++++++++++++++----- xhgui/xhgui.config.php | 7 ++++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/docker-compose.xhgui.yaml b/docker-compose.xhgui.yaml index baba1ea..618eca0 100644 --- a/docker-compose.xhgui.yaml +++ b/docker-compose.xhgui.yaml @@ -18,9 +18,7 @@ services: - HTTP_EXPOSE=8142:80 - HTTPS_EXPOSE=8143:80 - XHGUI_SAVE_HANDLER=pdo - - XHGUI_PDO_DSN=${DDEV_DATABASE_FAMILY}:host=db;dbname=xhgui - # If using pgsql database, use the below line instead and remove "#ddev-generated" from top of file - # - XHGUI_PDO_DSN=pgsql:host=db;dbname=xhgui + - DDEV_DATABASE_FAMILY=${DDEV_DATABASE_FAMILY} - XHGUI_PDO_USER=db - XHGUI_PDO_PASS=db links: diff --git a/tests/test.bats b/tests/test.bats index 99dc921..a1d4b58 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,61 @@ 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 +} + +@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 +} + +@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 " [ - 'dsn' => getenv('XHGUI_PDO_DSN') ?: 'mysql:host=db;dbname=xhgui', + 'dsn' => $XHGUI_PDO_DSN, 'user' => getenv('XHGUI_PDO_USER') ?: 'db', 'pass' => getenv('XHGUI_PDO_PASS') ?: 'db', 'table' => getenv('XHGUI_PDO_TABLE') ?: 'results', From e1469af156c3b890db2f2be4c0c53c9e6c449010 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Mon, 27 May 2024 18:01:30 +0900 Subject: [PATCH 7/7] remove xhgui database on uninstall --- install.yaml | 4 ++++ tests/test.bats | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/install.yaml b/install.yaml index cef4579..d935bd1 100644 --- a/install.yaml +++ b/install.yaml @@ -10,3 +10,7 @@ project_files: - xhgui/collector/xhgui.collector.php - xhgui/nginx.conf - xhprof/xhprof_prepend.php + +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 a1d4b58..9a0dcf9 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -80,6 +80,11 @@ echo 'Demo website';" >${TESTDIR}/public/index.php # 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" { @@ -103,6 +108,11 @@ echo 'Demo website';" >${TESTDIR}/public/index.php # 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" { @@ -126,4 +136,9 @@ echo 'Demo website';" >${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" }