Skip to content

Commit

Permalink
Merge pull request #12 from wunderio/feature/11-Create-syncdb-task-to…
Browse files Browse the repository at this point in the history
…-sync-wunder-production-db-to-local

#11 Add syncdb command.
  • Loading branch information
hkirsman authored Mar 5, 2024
2 parents 4cd8816 + 15380be commit 2795d01
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 23 deletions.
8 changes: 8 additions & 0 deletions dist/.ddev/commands/host/wunderio-core-syncdb.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
7 changes: 7 additions & 0 deletions dist/.ddev/commands/web/wunderio-core-yq.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
20 changes: 20 additions & 0 deletions dist/.ddev/wunderio/core/_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

#
# 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"
}
4 changes: 2 additions & 2 deletions dist/.ddev/wunderio/core/_run-scripts.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/sh
#!/bin/bash

#
# Helper script to run other scripts and allow overriding them by having the
# same file in .ddev/wunderio/custom folder.
#

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
Expand Down
11 changes: 8 additions & 3 deletions dist/.ddev/wunderio/core/hooks-db-post-import.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/bin/sh
#!/bin/bash

#
# Helper script to run posb-import db hook.
#

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

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"
4 changes: 2 additions & 2 deletions dist/.ddev/wunderio/core/hooks-host-post-start.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions dist/.ddev/wunderio/core/hooks-web-post-start-once.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

#
# Helper script to run web commands on first post start.
Expand All @@ -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
Expand Down
14 changes: 6 additions & 8 deletions dist/.ddev/wunderio/core/hooks-web-post-start.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/bin/sh
#!/bin/bash

#
# Helper script to run web 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:/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.
Expand All @@ -31,10 +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"

printf "${color_green}Drupal is working, running drush uli: "
drush uli
printf "${color_reset}"
uli_link=$(drush uli)
display_status_message "Drupal is working, running drush uli: $uli_link"
fi
4 changes: 2 additions & 2 deletions dist/.ddev/wunderio/core/tooling-grumphp.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions dist/.ddev/wunderio/core/tooling-phpunit.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions dist/.ddev/wunderio/core/tooling-regenerate-phpunit-config.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
#!/bin/bash
set -eu
if [ -n "${WUNDERIO_DEBUG:-}" ]; then
if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then
set -x
fi

Expand Down
32 changes: 32 additions & 0 deletions dist/.ddev/wunderio/core/tooling-syncdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/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" | 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' )

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"
ddev import-db --file="$sql_file"
rm "$sql_file"
{ set +x; } 2>/dev/null

display_status_message "Sync complete!"

0 comments on commit 2795d01

Please sign in to comment.