Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ This project uses [ddev-wunderio-drupal](https://github.com/wunderio/ddev-wunder
```bash
ddev syncdb <alias> # e.g. ddev syncdb prod
ddev syncdb prod --backup # Back up local DB before overwriting
ddev syncdb prod --deploy # Run drush deploy and drush uli after import
ddev syncdb prod --skip-hooks # Skip ddev hooks
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

ddev syncdb prod --keep-dump # Keep the downloaded dump file
ddev syncdb prod --backup --deploy # Combine flags
ddev syncdb prod --backup --skip-hooks # Combine flags
```

- `yq`: Runs [yq](https://mikefarah.gitbook.io/yq) commands (YAML processor).
Expand Down
4 changes: 2 additions & 2 deletions commands/host/wunderio-core-syncdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

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

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

wdr-core.sh tooling syncdb.sh "$@"
16 changes: 4 additions & 12 deletions wunderio/core/hooks/db-post-import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
#
# Helper script to run post-import db hook.
#
# Only sanitizes the database after import. Full deployment steps
# (updatedb, config:import, cache:rebuild, deploy:hook) should be
# run deliberately via `ddev drush deploy`.
#
# Rationale: running cache:rebuild before config:import is dangerous
# when the imported DB has schema differences from the current codebase
# (see https://github.com/wunderio/charts/pull/514). Keeping this hook
# minimal also keeps `ddev import-db` fast for all use cases (restoring
# local backups, debugging specific DB states, etc.).
#
# Sanitizes the database after import and executes full deployment.

set -eu
if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then
Expand All @@ -29,7 +20,8 @@ if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then
exit 0
fi

# Sanitize imported database (remove sensitive data).
drush sqlsan -y || { display_error_message "Database sanitization failed"; exit 1; }
drush deploy -y || { display_error_message "Drupal deploy failed"; exit 1; }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be on by default. Let's introduce --deploy flag instead with drush deploy and drush uli included.

Copy link
Copy Markdown
Contributor Author

@zHelmet zHelmet Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the --skip-hooks these are not executed so no need for flag on it, i rather would keep this since on a normal workflow i want to have dev at the latest configs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

95% of time I also need to import configs and update db right after import so I would enable deploy by default too. In rare cases I need to debug production db locally, there's the --skip-hooks

Only thing against deploy is the the fact that it relies on drush version. It seems in rare cases running clear cache first can cause db errors and that's done in 12.x
https://www.drush.org/12.x/deploycommand/
In newer versions config import is first:
https://www.drush.org/14.x/deploycommand/

If we can't agree on this here, then maybe meeting is needed.


uli_link=$(drush uli)
display_status_message "Database imported and sanitized."
display_status_message "One-time login link: $(drush uli)"
60 changes: 32 additions & 28 deletions wunderio/core/tooling/syncdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#
# Synchronise local database with a remote environment.
#
# Based on https://github.com/wunderio/unisport/blob/master/.lando/syncdb.sh
#

set -eu

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

Expand All @@ -38,12 +36,12 @@ shift 1
# Parse flags
KEEP_DUMP=false
BACKUP=false
DEPLOY=false
SKIP_HOOKS=false
for arg in "$@"; do
case "$arg" in
--keep-dump) KEEP_DUMP=true ;;
--backup) BACKUP=true ;;
--deploy) DEPLOY=true ;;
--keep-dump) KEEP_DUMP=true ;;
--backup) BACKUP=true ;;
--skip-hooks) SKIP_HOOKS=true ;;
esac
done

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

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

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

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

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

# --- 6. Read remote alias details from Drush ---
if ! alias_details=$(ddev drush sa "$SITE_ALIAS" 2>&1); then
# --- 5. Read remote alias details from Drush ---
if ! alias_details=$(ddev drush sa "$SITE_ALIAS" --format=yaml 2>&1); then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

display_error_message "Drush command failed."
echo "--------------------------------------------------"
echo "$alias_details"
Expand All @@ -103,9 +100,13 @@ fi
# DDEV might be injecting some messages to output so clean the output.
alias_details_clean=$(echo "$alias_details" | sed -n '/@self/,$p')

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

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

display_status_message "Dump complete, starting import!"

# Build import-db command with conditional flags.
# ddev import-db natively handles .gz files.
ddev import-db --file="$sql_file"
import_cmd=(ddev import-db --file="$sql_file")

# Full deployment steps can be ran seperatly.
if [[ "$SKIP_HOOKS" == "true" ]]; then
import_cmd+=(--skip-hooks)
fi

"${import_cmd[@]}"

if [[ "$KEEP_DUMP" != "true" ]]; then
rm "$sql_file"
fi
{ set +x; } 2>/dev/null

if [[ "$DEPLOY" == "true" ]]; then
display_status_message "Running drush deploy..."
ddev drush deploy -y || { display_error_message "drush deploy failed"; exit 1; }
display_status_message "One-time login link: $(ddev drush uli)"
fi
# Sanitize imported database (remove sensitive data).
ddev drush sqlsan -y || { display_error_message "Database sanitization failed"; exit 1; }

{ set +x; } 2>/dev/null

display_status_message "Sync with '$SITE_ALIAS' complete!"
if [[ "$DEPLOY" != "true" ]]; then
display_warning_message "Run 'ddev drush deploy' to apply database updates, import config, and rebuild caches."
display_warning_message "Run 'ddev drush uli' to generate a one-time login link."
fi
3 changes: 3 additions & 0 deletions wunderio/core/wdr-core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ else
else
PROJECT_ROOT="$PWD"
fi
# Ensure DDEV_APPROOT is set for any downstream consumers.
DDEV_APPROOT="$PROJECT_ROOT"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

fi

export PROJECT_ROOT

# Remove the first argument (the method)
Expand Down