Skip to content

Instantly share code, notes, and snippets.

@phcorp
Last active June 17, 2017 00:11
Show Gist options
  • Save phcorp/8379b472cac10af1c05fb134ee213b0d to your computer and use it in GitHub Desktop.
Save phcorp/8379b472cac10af1c05fb134ee213b0d to your computer and use it in GitHub Desktop.
Scripts that reverts entities changes to the state they were during migrations definitions
#!/bin/bash
safeRun() {
typeset cmnd="$*"
typeset ret_code
echo cmnd=$cmnd
eval $cmnd
ret_code=$?
if [ $ret_code != 0 ]; then
printf "Error : [%d] when executing command: '$cmnd'" $ret_code
exit $ret_code
fi
}
initial_commit="$(git log --pretty=format:%H|head -1)"
migrations_dir="app/DoctrineMigrations"
reset_paths="src/AppBundle"
echo "Initial commit: $initial_commit"
for file in app/DoctrineMigrations/*
do
echo "Processing $file"
# Version{timestamp}.php
timestamp="${file:30:14}"
day="${timestamp:0:4}-${timestamp:4:2}-${timestamp:6:2}T${timestamp:8:2}:${timestamp:10:2}:${timestamp:12:2}"
commit="$(git log --date=local --after="$day" --reverse --pretty=format:%H|head -1)"
echo "Checkout directories to $commit of $day"
IFS=';' read -ra PATHS <<< "$reset_paths"
for reset_path in "${PATHS[@]}"
do
safeRun "git checkout $commit -- $reset_path"
done
safeRun "php app/console -v doctrine:migrations:migrate --no-interaction"
done
echo "Reset entities directory"
safeRun "git stash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment