Skip to content

Instantly share code, notes, and snippets.

@dinarcon
Created October 27, 2021 22:16
Show Gist options
  • Save dinarcon/83101ae0f4e1f81364db25bec4374159 to your computer and use it in GitHub Desktop.
Save dinarcon/83101ae0f4e1f81364db25bec4374159 to your computer and use it in GitHub Desktop.
Run Drupal migrations on Pantheon
#!/bin/bash
# Script to run Drupal migrations on Pantheon.
# See also https://pantheon.io/blog/running-drupal-8-data-migrations-pantheon-through-drush
# Make sure the command is called with at least 3 arguments. Example:
# ./pantheon_drupal_migration.sh target_environment.multidev source_environment.multidev upgrade_d7_user upgrade_d7_file
[ $# -lt 3 ] && { echo "Usage: $0 [d9_target_site_id.env] [d6_source_site_id.env] [migration_ids]" ; exit 1; }
TARGET=$1
SOURCE=$2
# Accept a list of migration IDs separated by commas or spaces starting in the 3rd argument
IFS=', ' read -r -a MIGRATIONS <<< "${@:3}"
# Wake up Pantheon environments
terminus env:wake -q $SOURCE
terminus env:wake -q $TARGET
for MIGRATION in "${MIGRATIONS[@]}"
do
START=`date +%s`
UNPROCESSED_RECORDS="-1"
PREV_UNPROCESSED_RECORDS=`terminus remote:drush -- $TARGET migrate:status $MIGRATION --field=unprocessed`
# Before trying to import the migration, check if there is anything else to process.
if [[ $PREV_UNPROCESSED_RECORDS -eq 0 ]]; then
echo "No more records to import for $MIGRATION migration"
break
fi
echo "Starting import operation for $MIGRATION migration"
until [[ $UNPROCESSED_RECORDS -eq 0 ]]
do
terminus remote:drush -- $TARGET migrate:import $MIGRATION --feedback=100 --skip-progress-bar
if [[ $? -ne 0 ]]; then
echo "Import operation for $MIGRATION migration failed"
break
fi
# Prevent an infinite loop when migration records cannot be imported.
UNPROCESSED_RECORDS=`terminus remote:drush -- $TARGET migrate:status $MIGRATION --field=unprocessed`
if [[ $UNPROCESSED_RECORDS -eq $PREV_UNPROCESSED_RECORDS ]]; then
echo "$MIGRATION cannot process the remaining $UNPROCESSED_RECORDS records"
break
else
echo "$UNPROCESSED_RECORDS more records to process for the $MIGRATION migration"
terminus env:wake -q $SOURCE
fi
PREV_UNPROCESSED_RECORDS=$UNPROCESSED_RECORDS
done
END=`date +%s`
echo "$((END-START)) seconds processing the $MIGRATION migration"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment