-
Notifications
You must be signed in to change notification settings - Fork 0
GH-46: Syncdb with #42 #50
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
Changes from all commits
1479a5c
30e3571
fa08d07
91f5081
7ad3149
1748924
90c2eb8
84d3a6d
45cfe3a
d86be36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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; } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be on by default. Let's introduce
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 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)" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| display_error_message "Drush command failed." | ||
| echo "--------------------------------------------------" | ||
| echo "$alias_details" | ||
|
|
@@ -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." | ||
|
|
@@ -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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,10 @@ else | |
| else | ||
| PROJECT_ROOT="$PWD" | ||
| fi | ||
| # Ensure DDEV_APPROOT is set for any downstream consumers. | ||
| DDEV_APPROOT="$PROJECT_ROOT" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| fi | ||
|
|
||
| export PROJECT_ROOT | ||
|
|
||
| # Remove the first argument (the method) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.