Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default DDEV database #22

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.xhgui.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ddev-generated
hooks:
post-start:
# Create a new database called "xhgui"
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
- exec: |
if [[ "$DDEV_DATABASE_FAMILY" == "postgres" ]]; then
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
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
36 changes: 6 additions & 30 deletions docker-compose.xhgui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ name: xhgui

project_files:
- docker-compose.xhgui.yaml
- config.xhgui.yaml
- commands/host/xhgui
- xhgui/Dockerfile
- xhgui/xhgui.config.php
- xhgui/collector/xhgui.collector.config.php
- 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
74 changes: 67 additions & 7 deletions tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 "<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once '/mnt/ddev_config/xhgui/collector/xhgui.collector.php';
echo 'Demo website';" >${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 "<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once '/mnt/ddev_config/xhgui/collector/xhgui.collector.php';
echo 'Demo website';" >${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 "<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once '/mnt/ddev_config/xhgui/collector/xhgui.collector.php';
Expand All @@ -81,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"
}
1 change: 0 additions & 1 deletion xhgui-mongo/mongo.init.d/.gitmanaged

This file was deleted.

11 changes: 8 additions & 3 deletions xhgui/xhgui.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
* 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'.
'save.handler' => getenv('XHGUI_SAVE_HANDLER') ?: 'mongodb',

// 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',
],
Expand Down