diff --git a/commands/db.cmd b/commands/db.cmd index 0e848654..fbbc88ac 100644 --- a/commands/db.cmd +++ b/commands/db.cmd @@ -13,37 +13,17 @@ if (( ${#WARDEN_PARAMS[@]} == 0 )) || [[ "${WARDEN_PARAMS[0]}" == "help" ]]; the $WARDEN_BIN db --help || exit $? && exit $? fi -## load connection information for the mysql service +## load connection information for the database service DB_CONTAINER=$($WARDEN_BIN env ps -q db) if [[ ! ${DB_CONTAINER} ]]; then fatal "No container found for db service." fi -eval "$( - docker container inspect ${DB_CONTAINER} --format ' - {{- range .Config.Env }}{{with split . "=" -}} - {{- index . 0 }}='\''{{ range $i, $v := . }}{{ if $i }}{{ $v }}{{ end }}{{ end }}'\''{{println}} - {{- end }}{{ end -}} - ' | grep "^MYSQL_" -)" - -## sub-command execution -case "${WARDEN_PARAMS[0]}" in - connect) - "$WARDEN_BIN" env exec db \ - mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" --database="${MYSQL_DATABASE}" "${WARDEN_PARAMS[@]:1}" "$@" - ;; - import) - LC_ALL=C sed -E 's/DEFINER[ ]*=[ ]*`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' \ - | LC_ALL=C sed -E '/\@\@(GLOBAL\.GTID_PURGED|SESSION\.SQL_LOG_BIN)/d' \ - | "$WARDEN_BIN" env exec -T db \ - mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" --database="${MYSQL_DATABASE}" "${WARDEN_PARAMS[@]:1}" "$@" - ;; - dump) - "$WARDEN_BIN" env exec -T db \ - mysqldump -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" "${MYSQL_DATABASE}" "${WARDEN_PARAMS[@]:1}" "$@" - ;; - *) - fatal "The command \"${WARDEN_PARAMS[0]}\" does not exist. Please use --help for usage." +DB_CMD="mysql" +case ${WARDEN_DB_SYSTEM:-mysql} in + pgsql|postgres|postgresql) + DB_CMD="postgres" ;; esac + +source "${WARDEN_DIR}/commands/db.${DB_CMD}.cmd" diff --git a/commands/db.help b/commands/db.help index 1eba2e81..5b0f50d3 100755 --- a/commands/db.help +++ b/commands/db.help @@ -9,8 +9,8 @@ WARDEN_USAGE=$(cat <&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 + +eval "$( + docker container inspect ${DB_CONTAINER} --format ' + {{- range .Config.Env }}{{with split . "=" -}} + {{- index . 0 }}='\''{{ range $i, $v := . }}{{ if $i }}{{ $v }}{{ end }}{{ end }}'\''{{println}} + {{- end }}{{ end -}} + ' | grep "^MYSQL_" +)" + +## sub-command execution +case "${WARDEN_PARAMS[0]}" in + connect) + "$WARDEN_BIN" env exec db \ + mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" --database="${MYSQL_DB}" "${WARDEN_PARAMS[@]:1}" "$@" + ;; + import) + LC_ALL=C sed -E 's/DEFINER[ ]*=[ ]*`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' \ + | LC_ALL=C sed -E '/\@\@(GLOBAL\.GTID_PURGED|SESSION\.SQL_LOG_BIN)/d' \ + | "$WARDEN_BIN" env exec -T db \ + mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" --database="${MYSQL_DATABASE}" "${WARDEN_PARAMS[@]:1}" "$@" + ;; + dump) + "$WARDEN_BIN" env exec -T db \ + mysqldump -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" "${MYSQL_DATABASE}" "${WARDEN_PARAMS[@]:1}" "$@" + ;; + *) + fatal "The command \"${WARDEN_PARAMS[0]}\" does not exist. Please use --help for usage." + ;; +esac diff --git a/commands/db.postgres.cmd b/commands/db.postgres.cmd new file mode 100644 index 00000000..1621ce92 --- /dev/null +++ b/commands/db.postgres.cmd @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 + +eval "$( + docker container inspect ${DB_CONTAINER} --format ' + {{- range .Config.Env }}{{with split . "=" -}} + {{- index . 0 }}='\''{{ range $i, $v := . }}{{ if $i }}{{ $v }}{{ end }}{{ end }}'\''{{println}} + {{- end }}{{ end -}} + ' | grep "^POSTGRES_" +)" + +CONNECTION_STRING=postgresql://"$POSTGRES_USER":"$POSTGRES_PASSWORD"@127.0.0.1:5432/"$POSTGRES_DB" + +## sub-command execution +case "${WARDEN_PARAMS[0]}" in + connect) + "$WARDEN_BIN" env exec db \ + psql "$CONNECTION_STRING" "${WARDEN_PARAMS[@]:1}" "$@" + ;; + import) + LC_ALL=C sed -E 's/DEFINER[ ]*=[ ]*`[^`]+`@`[^`]+`/DEFINER=current_user/g' \ + | LC_ALL=C sed -E '/\@\@(GLOBAL\.GTID_PURGED|SESSION\.SQL_LOG_BIN)/d' \ + | "$WARDEN_BIN" env exec -T db \ + psql "$CONNECTION_STRING" "${WARDEN_PARAMS[@]:1}" "$@" + ;; + dump) + "$WARDEN_BIN" env exec -T db \ + pg_dump "$CONNECTION_STRING" "${WARDEN_PARAMS[@]:1}" "$@" + ;; + *) + fatal "The command \"${WARDEN_PARAMS[0]}\" does not exist. Please use --help for usage." + ;; +esac diff --git a/commands/env.cmd b/commands/env.cmd index 7f464e1a..c714319a 100644 --- a/commands/env.cmd +++ b/commands/env.cmd @@ -79,8 +79,23 @@ fi [[ ${WARDEN_NGINX} -eq 1 ]] \ && appendEnvPartialIfExists "nginx" -[[ ${WARDEN_DB} -eq 1 ]] \ - && appendEnvPartialIfExists "db" +if [[ ${WARDEN_DB} -eq 1 ]]; then + DB_PARTIAL=("db") + case "${WARDEN_DB_SYSTEM:-mysql}" in + pgsql|postgres|postgresql) + # Reset DB_PARTIAL array to only include PostgreSQL partial + DB_PARTIAL=("db.postgres") + ;; + mysql|mariadb) + DB_PARTIAL+=("db") + DB_PARTIAL+=("db.mysql") + ;; + esac + + for partial in "${DB_PARTIAL[@]}"; do + appendEnvPartialIfExists "${partial}" + done +fi [[ ${WARDEN_ELASTICSEARCH} -eq 1 ]] \ && appendEnvPartialIfExists "elasticsearch" diff --git a/environments/drupal/db.base.yml b/environments/drupal/db.mysql.base.yml similarity index 100% rename from environments/drupal/db.base.yml rename to environments/drupal/db.mysql.base.yml diff --git a/environments/drupal/db.postgres.base.yml b/environments/drupal/db.postgres.base.yml new file mode 100644 index 00000000..ba2547d7 --- /dev/null +++ b/environments/drupal/db.postgres.base.yml @@ -0,0 +1,7 @@ +version: "3.5" +services: + db: + environment: + - POSTGRES_PASSWORD=${DB_ROOT_PASSWORD:-drupal} + - POSTGRES_DB=${DB_DATABASE:-drupal} + - POSTGRES_USER=${DB_USER:-drupal} diff --git a/environments/drupal/init.env b/environments/drupal/init.env index 6b1756fc..8d873547 100644 --- a/environments/drupal/init.env +++ b/environments/drupal/init.env @@ -6,6 +6,7 @@ PHP_VERSION=8.2 PHP_XDEBUG_3=1 WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_RABBITMQ=0 WARDEN_REDIS=0 diff --git a/environments/includes/db.base.yml b/environments/includes/db.mysql.base.yml similarity index 100% rename from environments/includes/db.base.yml rename to environments/includes/db.mysql.base.yml diff --git a/environments/includes/db.postgres.base.yml b/environments/includes/db.postgres.base.yml new file mode 100644 index 00000000..9f4fb76f --- /dev/null +++ b/environments/includes/db.postgres.base.yml @@ -0,0 +1,18 @@ +version: "3.5" +services: + php-fpm: + depends_on: + - db + + db: + hostname: "${WARDEN_ENV_NAME}-postgres" + image: ${WARDEN_IMAGE_REPOSITORY}/${DB_DISTRIBUTION:-postgres}:${DB_DISTRIBUTION_VERSION:-${DB_VERSION:-16}} + environment: + - POSTGRES_PASSWORD=${DB_ROOT_PASSWORD:-app} + - POSTGRES_DB=${DB_DATABASE:-app} + - POSTGRES_USER=${DB_USER:-app} + volumes: + - dbdata:/var/lib/postgresql/data + +volumes: + dbdata: diff --git a/environments/laravel/db.base.yml b/environments/laravel/db.mysql.base.yml similarity index 100% rename from environments/laravel/db.base.yml rename to environments/laravel/db.mysql.base.yml diff --git a/environments/laravel/db.postgres.base.yml b/environments/laravel/db.postgres.base.yml new file mode 100644 index 00000000..8af85e4b --- /dev/null +++ b/environments/laravel/db.postgres.base.yml @@ -0,0 +1,7 @@ +version: "3.5" +services: + db: + environment: + - POSTGRES_PASSWORD=${DB_ROOT_PASSWORD:-laravel} + - POSTGRES_DB=${DB_DATABASE:-laravel} + - POSTGRES_USER=${DB_USER:-laravel} diff --git a/environments/laravel/init.env b/environments/laravel/init.env index 0c97ca6a..ddd09413 100644 --- a/environments/laravel/init.env +++ b/environments/laravel/init.env @@ -1,6 +1,6 @@ -MYSQL_DISTRIBUTION=mariadb -MYSQL_DISTRIBUTION_VERSION=10.4 +DB_DISTRIBUTION=mariadb +DB_DISTRIBUTION_VERSION=10.4 NODE_VERSION=12 COMPOSER_VERSION=1 PHP_VERSION=7.4 @@ -8,6 +8,7 @@ PHP_XDEBUG_3=1 REDIS_VERSION=5.0 WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_REDIS=1 ## Laravel Config diff --git a/environments/magento1/db.base.yml b/environments/magento1/db.mysql.base.yml similarity index 100% rename from environments/magento1/db.base.yml rename to environments/magento1/db.mysql.base.yml diff --git a/environments/magento1/init.env b/environments/magento1/init.env index 5b92aad6..76bc722c 100644 --- a/environments/magento1/init.env +++ b/environments/magento1/init.env @@ -1,9 +1,10 @@ WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_REDIS=1 -MYSQL_DISTRIBUTION=mariadb -MYSQL_DISTRIBUTION_VERSION=10.3 +DB_DISTRIBUTION=mariadb +DB_DISTRIBUTION_VERSION=10.3 NODE_VERSION=12 COMPOSER_VERSION=1 PHP_VERSION=7.2 @@ -18,4 +19,3 @@ BLACKFIRE_CLIENT_ID= BLACKFIRE_CLIENT_TOKEN= BLACKFIRE_SERVER_ID= BLACKFIRE_SERVER_TOKEN= - diff --git a/environments/magento2/db.base.yml b/environments/magento2/db.mysql.base.yml similarity index 100% rename from environments/magento2/db.base.yml rename to environments/magento2/db.mysql.base.yml diff --git a/environments/magento2/init.env b/environments/magento2/init.env index f60032c4..a575eef3 100644 --- a/environments/magento2/init.env +++ b/environments/magento2/init.env @@ -1,5 +1,6 @@ WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_ELASTICSEARCH=0 WARDEN_OPENSEARCH=1 WARDEN_ELASTICHQ=0 @@ -8,8 +9,8 @@ WARDEN_RABBITMQ=1 WARDEN_REDIS=1 OPENSEARCH_VERSION=2.5 -MYSQL_DISTRIBUTION=mariadb -MYSQL_DISTRIBUTION_VERSION=10.6 +DB_DISTRIBUTION=mariadb +DB_DISTRIBUTION_VERSION=10.6 NODE_VERSION=12 COMPOSER_VERSION=2.2 PHP_VERSION=8.1 @@ -34,4 +35,3 @@ BLACKFIRE_CLIENT_ID= BLACKFIRE_CLIENT_TOKEN= BLACKFIRE_SERVER_ID= BLACKFIRE_SERVER_TOKEN= - diff --git a/environments/shopware/db.base.yml b/environments/shopware/db.mysql.base.yml similarity index 100% rename from environments/shopware/db.base.yml rename to environments/shopware/db.mysql.base.yml diff --git a/environments/shopware/init.env b/environments/shopware/init.env index f0f867b2..bf255341 100644 --- a/environments/shopware/init.env +++ b/environments/shopware/init.env @@ -1,12 +1,13 @@ WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_REDIS=1 WARDEN_RABBITMQ=0 WARDEN_ELASTICSEARCH=0 WARDEN_VARNISH=0 -MYSQL_DISTRIBUTION=mariadb -MYSQL_DISTRIBUTION_VERSION=10.4 +DB_DISTRIBUTION=mariadb +DB_DISTRIBUTION_VERSION=10.4 NODE_VERSION=12 COMPOSER_VERSION=2 PHP_VERSION=7.4 @@ -14,4 +15,3 @@ PHP_XDEBUG_3=1 RABBITMQ_VERSION=3.8 REDIS_VERSION=5.0 VARNISH_VERSION=6.0 - diff --git a/environments/symfony/db.base.yml b/environments/symfony/db.mysql.base.yml similarity index 100% rename from environments/symfony/db.base.yml rename to environments/symfony/db.mysql.base.yml diff --git a/environments/symfony/db.postgres.base.yml b/environments/symfony/db.postgres.base.yml new file mode 100644 index 00000000..e06e574a --- /dev/null +++ b/environments/symfony/db.postgres.base.yml @@ -0,0 +1,7 @@ +version: "3.5" +services: + db: + environment: + - POSTGRES_PASSWORD=${DB_ROOT_PASSWORD:-symfony} + - POSTGRES_DB=${DB_DATABASE:-symfony} + - POSTGRES_USER=${DB_USER:-symfony} diff --git a/environments/symfony/init.env b/environments/symfony/init.env index f0f867b2..bf255341 100644 --- a/environments/symfony/init.env +++ b/environments/symfony/init.env @@ -1,12 +1,13 @@ WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_REDIS=1 WARDEN_RABBITMQ=0 WARDEN_ELASTICSEARCH=0 WARDEN_VARNISH=0 -MYSQL_DISTRIBUTION=mariadb -MYSQL_DISTRIBUTION_VERSION=10.4 +DB_DISTRIBUTION=mariadb +DB_DISTRIBUTION_VERSION=10.4 NODE_VERSION=12 COMPOSER_VERSION=2 PHP_VERSION=7.4 @@ -14,4 +15,3 @@ PHP_XDEBUG_3=1 RABBITMQ_VERSION=3.8 REDIS_VERSION=5.0 VARNISH_VERSION=6.0 - diff --git a/environments/wordpress/db.base.yml b/environments/wordpress/db.mysql.base.yml similarity index 100% rename from environments/wordpress/db.base.yml rename to environments/wordpress/db.mysql.base.yml diff --git a/environments/wordpress/init.env b/environments/wordpress/init.env index cf8c0218..6692fe67 100644 --- a/environments/wordpress/init.env +++ b/environments/wordpress/init.env @@ -1,12 +1,13 @@ -MYSQL_DISTRIBUTION=mariadb -MYSQL_DISTRIBUTION_VERSION=10.4 +DB_DISTRIBUTION=mariadb +DB_DISTRIBUTION_VERSION=10.4 NODE_VERSION=12 COMPOSER_VERSION=1 PHP_VERSION=7.4 PHP_XDEBUG_3=1 WARDEN_DB=1 +WARDEN_DB_SYSTEM=mysql WARDEN_REDIS=0 APP_ENV=local diff --git a/utils/env.sh b/utils/env.sh index 598021d6..000e9aad 100644 --- a/utils/env.sh +++ b/utils/env.sh @@ -134,6 +134,11 @@ function appendEnvPartialIfExists () { do if [[ -f "${PARTIAL_PATH}" ]]; then DOCKER_COMPOSE_ARGS+=("-f" "${PARTIAL_PATH}") + + if [[ "${PARTIAL_NAME}" = "db" ]]; then + echo -e "\033[33mWARNING\033[0m The 'db.base.yml' and 'db.${WARDEN_ENV_SUBT}.yml' files are deprecated." + echo -e "\033[33mWARNING\033[0m To resolve this rename them to 'db.mysql.base.yml' or 'db.mysql.${WARDEN_ENV_SBUT}.yml'." + fi fi done }