Skip to content

Instantly share code, notes, and snippets.

@briantully
Forked from adamzimmermann/migrate.sh
Created January 6, 2022 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briantully/279174ab9300129e2fa31b8df96a2ed5 to your computer and use it in GitHub Desktop.
Save briantully/279174ab9300129e2fa31b8df96a2ed5 to your computer and use it in GitHub Desktop.
Migration Script with Limit/Looping
#!/bin/bash
migrate_loop()
{
# Better readability with separation.
echo "========================";
# Get the output of the drush status.
drush_output=$(drush ms "$1" --format string);
# Split output string into an array.
output=( $drush_output )
# Output the status items.
for index in "${!output[@]}"
do
if [ "$index" == "3" ]
then
echo "Migration: ${output[index]}";
fi
if [ "$index" == "4" ]
then
echo "Status: ${output[index]}";
fi
if [ "$index" == "5" ]
then
echo "Total: ${output[index]}";
fi
if [ "$index" == "6" ]
then
echo "Imported: ${output[index]}";
fi
if [ "$index" == "7" ]
then
echo "Remaining: ${output[index]}";
fi
done
# Check if all items were imported.
if [ "${output[7]}" == "0" ] || [ "${output[7]}" -lt "0" ]
then
echo "No items left to import.";
else
echo "There are ${output[7]} remaining ${output[3]} items to be imported.";
echo "Running command: drush migrate:import $1";
echo "...";
# Run the migration until it stops.
drush migrate:reset "$1";
drush migrate:import "$1" --feedback 1000 --limit 10000;
# Run the check on this migration again.
migrate_loop "$1";
fi
}
if [ "$1" == "reset" ]; then
printf "\\nResetting migration status...\\n\\n"
drush cr >/dev/null 2>&1
drush migrate:status --format=var_export | grep -o "\\w*example_\\w*" | grep -v "example_migration" | xargs -n1 drush migrate:stop
drush migrate:status --format=var_export | grep -o "\\w*example_\\w*" | grep -v "example_migration" | xargs -n1 drush migrate:reset-status
printf "\\n\\nMigration status reset. Run ./scripts/migrate.sh to start migrating data or ./scripts/migrate.sh rollback to rollback data as well.\\n\\n"
elif [ "$1" == "rollback" ]; then
drush migrate:status --format=var_export | grep -o "\\w*example_\\w*" | grep -v "example_migration" | xargs -n1 drush migrate:stop
drush migrate:status --format=var_export | grep -o "\\w*example_\\w*" | grep -v "example_migration" | xargs -n1 drush migrate:reset-status
drush migrate:status --format=var_export | grep -o "\\w*example_\\w*" | grep -v "example_migration" | xargs -n1 drush migrate:rollback
printf "\\n\\nMigration rolled back. Run ./scripts/migrate.sh to start migrating data.\\n\\n"
else
printf "Updating database and entity definitions...\\n"
drush updb -y >/dev/null 2>&1
drush entup -y >/dev/null 2>&1
migrate_loop example_migration
migrate_loop another_example_migration
migrate_loop my_example_migration
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment