Skip to content

Commit 39f0e36

Browse files
authored
Merge pull request #50 from wunderio/bugfix/GH-46-syncdb
GH-46: Syncdb with #42
2 parents edb2afd + d86be36 commit 39f0e36

5 files changed

Lines changed: 43 additions & 44 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ This project uses [ddev-wunderio-drupal](https://github.com/wunderio/ddev-wunder
130130
```bash
131131
ddev syncdb <alias> # e.g. ddev syncdb prod
132132
ddev syncdb prod --backup # Back up local DB before overwriting
133-
ddev syncdb prod --deploy # Run drush deploy and drush uli after import
133+
ddev syncdb prod --skip-hooks # Skip ddev hooks
134134
ddev syncdb prod --keep-dump # Keep the downloaded dump file
135-
ddev syncdb prod --backup --deploy # Combine flags
135+
ddev syncdb prod --backup --skip-hooks # Combine flags
136136
```
137137

138138
- `yq`: Runs [yq](https://mikefarah.gitbook.io/yq) commands (YAML processor).

commands/host/wunderio-core-syncdb.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
## Description: Synchronise local database with a remote environment.
66
## Usage: syncdb
7-
## Example: "ddev syncdb prod" "ddev syncdb prod --backup --deploy"
7+
## Example: "ddev syncdb prod" "ddev syncdb prod --backup --skip-hooks"
88
## ExecRaw: true
99
## ProjectTypes: drupal9,drupal10,drupal11
1010

11-
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:$HOME/.ddev/wunderio/core/
11+
export PATH="$HOME/.ddev/wunderio/core/${PATH:+:$PATH}"
1212

1313
wdr-core.sh tooling syncdb.sh "$@"

wunderio/core/hooks/db-post-import.sh

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44
#
55
# Helper script to run post-import db hook.
66
#
7-
# Only sanitizes the database after import. Full deployment steps
8-
# (updatedb, config:import, cache:rebuild, deploy:hook) should be
9-
# run deliberately via `ddev drush deploy`.
10-
#
11-
# Rationale: running cache:rebuild before config:import is dangerous
12-
# when the imported DB has schema differences from the current codebase
13-
# (see https://github.com/wunderio/charts/pull/514). Keeping this hook
14-
# minimal also keeps `ddev import-db` fast for all use cases (restoring
15-
# local backups, debugging specific DB states, etc.).
16-
#
7+
# Sanitizes the database after import and executes full deployment.
178

189
set -eu
1910
if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then
@@ -29,7 +20,8 @@ if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then
2920
exit 0
3021
fi
3122

32-
# Sanitize imported database (remove sensitive data).
33-
drush sqlsan -y || { display_error_message "Database sanitization failed"; exit 1; }
23+
drush deploy -y || { display_error_message "Drupal deploy failed"; exit 1; }
3424

25+
uli_link=$(drush uli)
3526
display_status_message "Database imported and sanitized."
27+
display_status_message "One-time login link: $(drush uli)"

wunderio/core/tooling/syncdb.sh

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#
55
# Synchronise local database with a remote environment.
66
#
7-
# Based on https://github.com/wunderio/unisport/blob/master/.lando/syncdb.sh
8-
#
97

108
set -eu
119

@@ -20,11 +18,11 @@ source "$WUNDERIO_GLOBAL_SCRIPT_ROOT/_helpers.sh"
2018
# Check if an alias was provided as an argument.
2119
if [[ -z "${1:-}" ]]; then
2220
display_error_message "Error: No site alias name provided."
23-
display_warning_message "Usage: ddev syncdb <alias> [--keep-dump] [--backup] [--deploy]"
21+
display_warning_message "Usage: ddev syncdb <alias> [--keep-dump] [--backup] [--skip-hooks]"
2422
display_warning_message "Example: ddev syncdb prod"
25-
display_warning_message " --keep-dump Keep the downloaded dump file after import"
26-
display_warning_message " --backup Create a local database backup before overwriting"
27-
display_warning_message " --deploy Run drush deploy and drush uli after import"
23+
display_warning_message " --keep-dump Keep the downloaded dump file after import"
24+
display_warning_message " --backup Create a local database backup before overwriting"
25+
display_warning_message " --skip-hooks Skip DDEV post-import hooks during database import"
2826
exit 1
2927
fi
3028

@@ -38,12 +36,12 @@ shift 1
3836
# Parse flags
3937
KEEP_DUMP=false
4038
BACKUP=false
41-
DEPLOY=false
39+
SKIP_HOOKS=false
4240
for arg in "$@"; do
4341
case "$arg" in
44-
--keep-dump) KEEP_DUMP=true ;;
45-
--backup) BACKUP=true ;;
46-
--deploy) DEPLOY=true ;;
42+
--keep-dump) KEEP_DUMP=true ;;
43+
--backup) BACKUP=true ;;
44+
--skip-hooks) SKIP_HOOKS=true ;;
4745
esac
4846
done
4947

@@ -65,10 +63,9 @@ if ! grep -q "^${ALIAS_KEY}:" "$SITE_YML"; then
6563
exit 1
6664
fi
6765

68-
# --- 3. Warn about overwrite ---
66+
# --- 3. Prepare dumps directory and warn about overwrite ---
6967
display_warning_message "This will overwrite your local database with data from '$SITE_ALIAS'."
7068

71-
# --- 4. Prepare dumps directory ---
7269
DUMPS_DIR="$PROJECT_ROOT/database_dumps"
7370

7471
if [ ! -d "$DUMPS_DIR" ]; then
@@ -81,16 +78,16 @@ fi
8178
# Use .sql.gz extension for compressed dump.
8279
sql_file="$DUMPS_DIR/${ALIAS_KEY}-syncdb-$(date +'%Y-%m-%d').sql.gz"
8380

84-
# --- 5. Create local backup (if --backup) ---
81+
# --- 4. Create local backup (if --backup) ---
8582
if [[ "$BACKUP" == "true" ]]; then
8683
backup_file="$DUMPS_DIR/backup-$(date +'%Y-%m-%d-%H%M%S').sql.gz"
8784
display_status_message "Creating local database backup: $backup_file"
8885
ddev export-db --gzip --file="$backup_file"
8986
display_status_message "Backup saved."
9087
fi
9188

92-
# --- 6. Read remote alias details from Drush ---
93-
if ! alias_details=$(ddev drush sa "$SITE_ALIAS" 2>&1); then
89+
# --- 5. Read remote alias details from Drush ---
90+
if ! alias_details=$(ddev drush sa "$SITE_ALIAS" --format=yaml 2>&1); then
9491
display_error_message "Drush command failed."
9592
echo "--------------------------------------------------"
9693
echo "$alias_details"
@@ -103,9 +100,13 @@ fi
103100
# DDEV might be injecting some messages to output so clean the output.
104101
alias_details_clean=$(echo "$alias_details" | sed -n '/@self/,$p')
105102

106-
eval "$(ddev yq '."@self.'"$ALIAS_KEY"'" | "remote_ssh_user=\"" + .user + "\" remote_ssh_host=\"" + .host + "\" remote_ssh_options=\"" + .ssh.options + "\""' <<< "$alias_details_clean")"
103+
# Dynamically build the yq query path using the alias key ("main").
104+
alias_full="@self.${ALIAS_KEY}"
105+
read -r remote_ssh_user remote_ssh_host remote_ssh_options < <(
106+
ddev exec -- yq -r ".\"$alias_full\" | [.user, .host, .ssh.options] | @tsv" <<< "$alias_details_clean"
107+
)
107108

108-
# --- Validate parsed SSH details ---
109+
# --- 6. Validate parsed SSH details ---
109110
if [[ -z "$remote_ssh_user" || "$remote_ssh_user" == "null" ]]; then
110111
display_error_message "Missing or invalid SSH user for alias '$ALIAS_KEY'."
111112
display_warning_message "Check your drush/sites/self.site.yml configuration for the 'user' field."
@@ -139,21 +140,24 @@ display_status_message "Dumping database from '$SITE_ALIAS' (gzip compressed)...
139140

140141
display_status_message "Dump complete, starting import!"
141142

143+
# Build import-db command with conditional flags.
142144
# ddev import-db natively handles .gz files.
143-
ddev import-db --file="$sql_file"
145+
import_cmd=(ddev import-db --file="$sql_file")
146+
147+
# Full deployment steps can be ran seperatly.
148+
if [[ "$SKIP_HOOKS" == "true" ]]; then
149+
import_cmd+=(--skip-hooks)
150+
fi
151+
152+
"${import_cmd[@]}"
153+
144154
if [[ "$KEEP_DUMP" != "true" ]]; then
145155
rm "$sql_file"
146156
fi
147-
{ set +x; } 2>/dev/null
148157

149-
if [[ "$DEPLOY" == "true" ]]; then
150-
display_status_message "Running drush deploy..."
151-
ddev drush deploy -y || { display_error_message "drush deploy failed"; exit 1; }
152-
display_status_message "One-time login link: $(ddev drush uli)"
153-
fi
158+
# Sanitize imported database (remove sensitive data).
159+
ddev drush sqlsan -y || { display_error_message "Database sanitization failed"; exit 1; }
160+
161+
{ set +x; } 2>/dev/null
154162

155163
display_status_message "Sync with '$SITE_ALIAS' complete!"
156-
if [[ "$DEPLOY" != "true" ]]; then
157-
display_warning_message "Run 'ddev drush deploy' to apply database updates, import config, and rebuild caches."
158-
display_warning_message "Run 'ddev drush uli' to generate a one-time login link."
159-
fi

wunderio/core/wdr-core.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ else
3838
else
3939
PROJECT_ROOT="$PWD"
4040
fi
41+
# Ensure DDEV_APPROOT is set for any downstream consumers.
42+
DDEV_APPROOT="$PROJECT_ROOT"
4143
fi
44+
4245
export PROJECT_ROOT
4346

4447
# Remove the first argument (the method)

0 commit comments

Comments
 (0)