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

Feature/add postgresql support #754

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 7 additions & 27 deletions commands/db.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions commands/db.help
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ WARDEN_USAGE=$(cat <<EOF
-h, --help Display this help menu

\033[33mCommands:\033[0m
connect Launches an interactive mysql session within the current project environment
import Reads data from stdin and loads it into the current project\047s mysql database
dump Dumps database from db container via mysqldump
connect Launches an interactive database session within the current project environment
import Reads data from stdin and loads it into the current project\047s database
dump Dumps database from db container via database\047s dump utility
EOF
)
31 changes: 31 additions & 0 deletions commands/db.mysql.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/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 "^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
33 changes: 33 additions & 0 deletions commands/db.postgres.cmd
Original file line number Diff line number Diff line change
@@ -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
19 changes: 17 additions & 2 deletions commands/env.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions environments/drupal/db.postgres.base.yml
Original file line number Diff line number Diff line change
@@ -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}
1 change: 1 addition & 0 deletions environments/drupal/init.env
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions environments/includes/db.postgres.base.yml
Original file line number Diff line number Diff line change
@@ -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:
7 changes: 7 additions & 0 deletions environments/laravel/db.postgres.base.yml
Original file line number Diff line number Diff line change
@@ -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}
5 changes: 3 additions & 2 deletions environments/laravel/init.env
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

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
REDIS_VERSION=5.0

WARDEN_DB=1
WARDEN_DB_SYSTEM=mysql
WARDEN_REDIS=1

## Laravel Config
Expand Down
6 changes: 3 additions & 3 deletions environments/magento1/init.env
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,4 +19,3 @@ BLACKFIRE_CLIENT_ID=
BLACKFIRE_CLIENT_TOKEN=
BLACKFIRE_SERVER_ID=
BLACKFIRE_SERVER_TOKEN=

6 changes: 3 additions & 3 deletions environments/magento2/init.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

WARDEN_DB=1
WARDEN_DB_SYSTEM=mysql
WARDEN_ELASTICSEARCH=0
WARDEN_OPENSEARCH=1
WARDEN_ELASTICHQ=0
Expand All @@ -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
Expand All @@ -34,4 +35,3 @@ BLACKFIRE_CLIENT_ID=
BLACKFIRE_CLIENT_TOKEN=
BLACKFIRE_SERVER_ID=
BLACKFIRE_SERVER_TOKEN=

6 changes: 3 additions & 3 deletions environments/shopware/init.env
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

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
PHP_XDEBUG_3=1
RABBITMQ_VERSION=3.8
REDIS_VERSION=5.0
VARNISH_VERSION=6.0

7 changes: 7 additions & 0 deletions environments/symfony/db.postgres.base.yml
Original file line number Diff line number Diff line change
@@ -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}
6 changes: 3 additions & 3 deletions environments/symfony/init.env
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

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
PHP_XDEBUG_3=1
RABBITMQ_VERSION=3.8
REDIS_VERSION=5.0
VARNISH_VERSION=6.0

5 changes: 3 additions & 2 deletions environments/wordpress/init.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions utils/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}