From 499c65a5f542968493cbb083cd215dc818b84dc5 Mon Sep 17 00:00:00 2001 From: Hannes Kirsman Date: Tue, 27 Feb 2024 16:53:35 +0200 Subject: [PATCH 1/6] #11 Add syncdb command. --- dist/.ddev/wunderio/core/_helpers.sh | 20 +++++++++++ .../wunderio/core/hooks-db-post-import.sh | 9 +++-- .../wunderio/core/hooks-host-post-start.sh | 0 .../wunderio/core/hooks-web-post-start.sh | 7 ++-- dist/.ddev/wunderio/core/tooling-syncdb.sh | 35 +++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100755 dist/.ddev/wunderio/core/_helpers.sh mode change 100644 => 100755 dist/.ddev/wunderio/core/hooks-host-post-start.sh create mode 100755 dist/.ddev/wunderio/core/tooling-syncdb.sh diff --git a/dist/.ddev/wunderio/core/_helpers.sh b/dist/.ddev/wunderio/core/_helpers.sh new file mode 100755 index 0000000..445b028 --- /dev/null +++ b/dist/.ddev/wunderio/core/_helpers.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +# +# Helper functions. +# + +set -eu +if [ -n "${WUNDERIO_DEBUG:-}" ]; then + set -x +fi +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin + +# Function to display status message +display_status_message() { + local color_green="\033[38;5;70m" + local color_reset="\033[0m" + local message="$1" + + printf "${color_green}${message}${color_reset}\n" +} diff --git a/dist/.ddev/wunderio/core/hooks-db-post-import.sh b/dist/.ddev/wunderio/core/hooks-db-post-import.sh index 72f33b0..bf70b29 100755 --- a/dist/.ddev/wunderio/core/hooks-db-post-import.sh +++ b/dist/.ddev/wunderio/core/hooks-db-post-import.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run posb-import db hook. @@ -10,4 +10,9 @@ if [ -n "${WUNDERIO_DEBUG:-}" ]; then fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin -cd $DDEV_COMPOSER_ROOT && drush cache:rebuild -y && drush @local user:login +source /var/www/html/.ddev/wunderio/core/_helpers.sh + +cd $DDEV_COMPOSER_ROOT && drush cache:rebuild -y && drush sqlsan -y + +uli_link=$(drush uli) +display_status_message "Drupal is working, running drush uli: $uli_link" diff --git a/dist/.ddev/wunderio/core/hooks-host-post-start.sh b/dist/.ddev/wunderio/core/hooks-host-post-start.sh old mode 100644 new mode 100755 diff --git a/dist/.ddev/wunderio/core/hooks-web-post-start.sh b/dist/.ddev/wunderio/core/hooks-web-post-start.sh index 37b7b0b..da097ac 100755 --- a/dist/.ddev/wunderio/core/hooks-web-post-start.sh +++ b/dist/.ddev/wunderio/core/hooks-web-post-start.sh @@ -10,6 +10,8 @@ if [ -n "${WUNDERIO_DEBUG:-}" ]; then fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin +source .ddev/wunderio/core/_helpers.sh + # Function to check if Drupal is working. is_drupal_working() { # Drush 11 and older. @@ -34,7 +36,6 @@ if is_drupal_working; then color_green="\033[38;5;70m" color_reset="\033[0m" - printf "${color_green}Drupal is working, running drush uli: " - drush uli - printf "${color_reset}" + uli_link=$(ddev drush uli) + display_status_message "Drupal is working, running drush uli: $uli_link" fi diff --git a/dist/.ddev/wunderio/core/tooling-syncdb.sh b/dist/.ddev/wunderio/core/tooling-syncdb.sh new file mode 100755 index 0000000..5ebe689 --- /dev/null +++ b/dist/.ddev/wunderio/core/tooling-syncdb.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# +# Synchronise local database with the production environment. +# +# Based on https://github.com/wunderio/unisport/blob/master/.lando/syncdb.sh +# + +set -eu +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then + set -x +fi +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin + +source .ddev/wunderio/core/_helpers.sh + +sql_file="prod-syncdb-$(date +'%Y-%m-%d').sql" + +# Read the production alias from the drush configuration. +# For some reason "ddev drush sql-sync @prod @local -y" does not work and times out. +prod_alias=$(ddev drush sa @prod) +prod_ssh_user=$(echo "$prod_alias" | grep -Po '(?<=user: )\S+') +prod_ssh_options=$(echo "$prod_alias" | grep -Po "(?<=options: ')[^']+(?=')") +prod_ssh_host=$(echo "$prod_alias" | grep -Po '(?<=host: )\S+') + +echo "Dropping local database." +ddev drush sql-drop -y +echo "Dumping production database to $sql_file." +ssh "$prod_ssh_user@$prod_ssh_host" "$prod_ssh_options" "drush sql-dump" > "$sql_file" +echo "Importing production database." +ddev import-db --file="$sql_file" +echo "Removing $sql_file." +rm "$sql_file" + +display_status_message "Sync complete!" From 94bb58050c3d44770336a4a45c6a2789333c1c28 Mon Sep 17 00:00:00 2001 From: Hannes Kirsman Date: Thu, 29 Feb 2024 12:27:22 +0200 Subject: [PATCH 2/6] #11 Skip db caches to make the dump lot smaller in syncdb command. --- dist/.ddev/wunderio/core/tooling-syncdb.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/.ddev/wunderio/core/tooling-syncdb.sh b/dist/.ddev/wunderio/core/tooling-syncdb.sh index 5ebe689..a7f60a8 100755 --- a/dist/.ddev/wunderio/core/tooling-syncdb.sh +++ b/dist/.ddev/wunderio/core/tooling-syncdb.sh @@ -26,7 +26,7 @@ prod_ssh_host=$(echo "$prod_alias" | grep -Po '(?<=host: )\S+') echo "Dropping local database." ddev drush sql-drop -y echo "Dumping production database to $sql_file." -ssh "$prod_ssh_user@$prod_ssh_host" "$prod_ssh_options" "drush sql-dump" > "$sql_file" +ssh "$prod_ssh_user@$prod_ssh_host" "$prod_ssh_options" "drush sql-dump --structure-tables-list=cache,cache_*,history,search_*,sessions" > "$sql_file" echo "Importing production database." ddev import-db --file="$sql_file" echo "Removing $sql_file." From 62f945bc6fbc458c0ae7e83aaa9347efdc0d0585 Mon Sep 17 00:00:00 2001 From: Hannes Kirsman Date: Fri, 1 Mar 2024 11:04:41 +0200 Subject: [PATCH 3/6] #11 Remove redundant db drop from syncdb, fix post start hook. --- dist/.ddev/wunderio/core/hooks-web-post-start.sh | 7 ++----- dist/.ddev/wunderio/core/tooling-syncdb.sh | 2 -- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/dist/.ddev/wunderio/core/hooks-web-post-start.sh b/dist/.ddev/wunderio/core/hooks-web-post-start.sh index da097ac..6ec53ed 100755 --- a/dist/.ddev/wunderio/core/hooks-web-post-start.sh +++ b/dist/.ddev/wunderio/core/hooks-web-post-start.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run web post-start commands. @@ -33,9 +33,6 @@ is_drupal_working() { # Commands to run if Drupal is working. if is_drupal_working; then - color_green="\033[38;5;70m" - color_reset="\033[0m" - - uli_link=$(ddev drush uli) + uli_link=$(drush uli) display_status_message "Drupal is working, running drush uli: $uli_link" fi diff --git a/dist/.ddev/wunderio/core/tooling-syncdb.sh b/dist/.ddev/wunderio/core/tooling-syncdb.sh index a7f60a8..86b50c9 100755 --- a/dist/.ddev/wunderio/core/tooling-syncdb.sh +++ b/dist/.ddev/wunderio/core/tooling-syncdb.sh @@ -23,8 +23,6 @@ prod_ssh_user=$(echo "$prod_alias" | grep -Po '(?<=user: )\S+') prod_ssh_options=$(echo "$prod_alias" | grep -Po "(?<=options: ')[^']+(?=')") prod_ssh_host=$(echo "$prod_alias" | grep -Po '(?<=host: )\S+') -echo "Dropping local database." -ddev drush sql-drop -y echo "Dumping production database to $sql_file." ssh "$prod_ssh_user@$prod_ssh_host" "$prod_ssh_options" "drush sql-dump --structure-tables-list=cache,cache_*,history,search_*,sessions" > "$sql_file" echo "Importing production database." From 3502a6c9b2728715a9d0b194f514e5a7ad2db7a3 Mon Sep 17 00:00:00 2001 From: Hannes Kirsman Date: Mon, 4 Mar 2024 10:11:11 +0200 Subject: [PATCH 4/6] #11 Add missing wunderio-core-syncdb.sh, change all scripts to bash. --- dist/.ddev/commands/host/wunderio-core-syncdb.sh | 8 ++++++++ dist/.ddev/wunderio/core/_helpers.sh | 4 ++-- dist/.ddev/wunderio/core/_run-scripts.sh | 4 ++-- dist/.ddev/wunderio/core/hooks-db-post-import.sh | 2 +- dist/.ddev/wunderio/core/hooks-host-post-start.sh | 4 ++-- dist/.ddev/wunderio/core/hooks-web-post-start-once.sh | 4 ++-- dist/.ddev/wunderio/core/hooks-web-post-start.sh | 2 +- dist/.ddev/wunderio/core/tooling-grumphp.sh | 4 ++-- dist/.ddev/wunderio/core/tooling-phpunit.sh | 4 ++-- .../wunderio/core/tooling-regenerate-phpunit-config.sh | 4 ++-- 10 files changed, 24 insertions(+), 16 deletions(-) create mode 100755 dist/.ddev/commands/host/wunderio-core-syncdb.sh diff --git a/dist/.ddev/commands/host/wunderio-core-syncdb.sh b/dist/.ddev/commands/host/wunderio-core-syncdb.sh new file mode 100755 index 0000000..367be51 --- /dev/null +++ b/dist/.ddev/commands/host/wunderio-core-syncdb.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +## Description: Synchronise local database with production. +## Usage: syncdb +## Example: "ddev syncdb" + + +.ddev/wunderio/core/_run-scripts.sh tooling-syncdb.sh "$@" diff --git a/dist/.ddev/wunderio/core/_helpers.sh b/dist/.ddev/wunderio/core/_helpers.sh index 445b028..7034184 100755 --- a/dist/.ddev/wunderio/core/_helpers.sh +++ b/dist/.ddev/wunderio/core/_helpers.sh @@ -1,11 +1,11 @@ -#!/bin/sh +#!/bin/bash # # Helper functions. # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/_run-scripts.sh b/dist/.ddev/wunderio/core/_run-scripts.sh index 082863b..f71dae7 100755 --- a/dist/.ddev/wunderio/core/_run-scripts.sh +++ b/dist/.ddev/wunderio/core/_run-scripts.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run other scripts and allow overriding them by having the @@ -6,7 +6,7 @@ # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/hooks-db-post-import.sh b/dist/.ddev/wunderio/core/hooks-db-post-import.sh index bf70b29..6310cbc 100755 --- a/dist/.ddev/wunderio/core/hooks-db-post-import.sh +++ b/dist/.ddev/wunderio/core/hooks-db-post-import.sh @@ -5,7 +5,7 @@ # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/hooks-host-post-start.sh b/dist/.ddev/wunderio/core/hooks-host-post-start.sh index 876fb21..2f118ee 100755 --- a/dist/.ddev/wunderio/core/hooks-host-post-start.sh +++ b/dist/.ddev/wunderio/core/hooks-host-post-start.sh @@ -1,11 +1,11 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run host post-start commands. # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin diff --git a/dist/.ddev/wunderio/core/hooks-web-post-start-once.sh b/dist/.ddev/wunderio/core/hooks-web-post-start-once.sh index 7560300..9d96576 100755 --- a/dist/.ddev/wunderio/core/hooks-web-post-start-once.sh +++ b/dist/.ddev/wunderio/core/hooks-web-post-start-once.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run web commands on first post start. @@ -8,7 +8,7 @@ # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/hooks-web-post-start.sh b/dist/.ddev/wunderio/core/hooks-web-post-start.sh index 6ec53ed..612679e 100755 --- a/dist/.ddev/wunderio/core/hooks-web-post-start.sh +++ b/dist/.ddev/wunderio/core/hooks-web-post-start.sh @@ -5,7 +5,7 @@ # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/tooling-grumphp.sh b/dist/.ddev/wunderio/core/tooling-grumphp.sh index 052ed40..e8f1d8e 100755 --- a/dist/.ddev/wunderio/core/tooling-grumphp.sh +++ b/dist/.ddev/wunderio/core/tooling-grumphp.sh @@ -1,11 +1,11 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run GrumPHP. # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/tooling-phpunit.sh b/dist/.ddev/wunderio/core/tooling-phpunit.sh index 5061201..23920d4 100755 --- a/dist/.ddev/wunderio/core/tooling-phpunit.sh +++ b/dist/.ddev/wunderio/core/tooling-phpunit.sh @@ -1,11 +1,11 @@ -#!/bin/sh +#!/bin/bash # # Helper script to run PHPUnit. # set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/var/www/html/vendor/bin diff --git a/dist/.ddev/wunderio/core/tooling-regenerate-phpunit-config.sh b/dist/.ddev/wunderio/core/tooling-regenerate-phpunit-config.sh index 8e3afe0..5340ae6 100755 --- a/dist/.ddev/wunderio/core/tooling-regenerate-phpunit-config.sh +++ b/dist/.ddev/wunderio/core/tooling-regenerate-phpunit-config.sh @@ -1,6 +1,6 @@ -#!/bin/sh +#!/bin/bash set -eu -if [ -n "${WUNDERIO_DEBUG:-}" ]; then +if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then set -x fi From fa912fe515781b7582ba465d7490bb6d8b14fda9 Mon Sep 17 00:00:00 2001 From: Hannes Kirsman Date: Mon, 4 Mar 2024 13:17:28 +0200 Subject: [PATCH 5/6] #11 Use yq tool inside syncdb script for best approach. --- dist/.ddev/commands/web/wunderio-core-yq.sh | 7 +++++++ dist/.ddev/wunderio/core/tooling-syncdb.sh | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100755 dist/.ddev/commands/web/wunderio-core-yq.sh diff --git a/dist/.ddev/commands/web/wunderio-core-yq.sh b/dist/.ddev/commands/web/wunderio-core-yq.sh new file mode 100755 index 0000000..c087b91 --- /dev/null +++ b/dist/.ddev/commands/web/wunderio-core-yq.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +## Description: Runs yq commands (yq is a lightweight and portable command-line YAML processor). +## Usage: yq +## Example: "ddev yq" + +yq "$@" diff --git a/dist/.ddev/wunderio/core/tooling-syncdb.sh b/dist/.ddev/wunderio/core/tooling-syncdb.sh index 86b50c9..dc76b4a 100755 --- a/dist/.ddev/wunderio/core/tooling-syncdb.sh +++ b/dist/.ddev/wunderio/core/tooling-syncdb.sh @@ -19,9 +19,9 @@ sql_file="prod-syncdb-$(date +'%Y-%m-%d').sql" # Read the production alias from the drush configuration. # For some reason "ddev drush sql-sync @prod @local -y" does not work and times out. prod_alias=$(ddev drush sa @prod) -prod_ssh_user=$(echo "$prod_alias" | grep -Po '(?<=user: )\S+') -prod_ssh_options=$(echo "$prod_alias" | grep -Po "(?<=options: ')[^']+(?=')") -prod_ssh_host=$(echo "$prod_alias" | grep -Po '(?<=host: )\S+') +prod_ssh_user=$(echo "$prod_alias" | ddev yq '.\"@self.prod\".user' ) +prod_ssh_options=$(echo "$prod_alias" | ddev yq '.\"@self.prod\".ssh.options' ) +prod_ssh_host=$(echo "$prod_alias" | ddev yq '.\"@self.prod\".host' ) echo "Dumping production database to $sql_file." ssh "$prod_ssh_user@$prod_ssh_host" "$prod_ssh_options" "drush sql-dump --structure-tables-list=cache,cache_*,history,search_*,sessions" > "$sql_file" From 15380be1fcc7e9b4250ca91ca4f86972189cd64e Mon Sep 17 00:00:00 2001 From: Hannes Kirsman Date: Mon, 4 Mar 2024 13:23:16 +0200 Subject: [PATCH 6/6] #11 Add debugging syntax for import section in syncdb. --- dist/.ddev/wunderio/core/tooling-syncdb.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dist/.ddev/wunderio/core/tooling-syncdb.sh b/dist/.ddev/wunderio/core/tooling-syncdb.sh index dc76b4a..7b90a98 100755 --- a/dist/.ddev/wunderio/core/tooling-syncdb.sh +++ b/dist/.ddev/wunderio/core/tooling-syncdb.sh @@ -23,11 +23,10 @@ prod_ssh_user=$(echo "$prod_alias" | ddev yq '.\"@self.prod\".user' ) prod_ssh_options=$(echo "$prod_alias" | ddev yq '.\"@self.prod\".ssh.options' ) prod_ssh_host=$(echo "$prod_alias" | ddev yq '.\"@self.prod\".host' ) -echo "Dumping production database to $sql_file." +set -x ssh "$prod_ssh_user@$prod_ssh_host" "$prod_ssh_options" "drush sql-dump --structure-tables-list=cache,cache_*,history,search_*,sessions" > "$sql_file" -echo "Importing production database." ddev import-db --file="$sql_file" -echo "Removing $sql_file." rm "$sql_file" +{ set +x; } 2>/dev/null display_status_message "Sync complete!"