Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. # Instructions
  2. #
  3. # 1. Backup your repo somewhere!
  4. # 2. Make sure `git stash list` is empty - that'll help debugging in case things go wrong.
  5. # 3. Make sure `git status` shows that you've made no changes; that'll break things right away.
  6. # 4. Copy and paste the code below.
  7. # 5. Run the `update_timestamps` function and LET IT FINISH!
  8. # 6. Check the results, make sure they look reasonable, and commit!
  9. #
  10. # P.S. I take no responsibility for your use of the script; I recommend understanding what it does before running it.
  11.  
  12. function versionize() {
  13. local FILE=$1
  14. local COMMIT=$(git log -n 1 --pretty=format:%H -- $FILE)
  15.  
  16. # Find the correct version of Rails for that migration
  17. git checkout --quiet $COMMIT
  18. local RAILS_VERSION=$(grep "^ rails " Gemfile.lock | egrep -o "[0-9].[0-9]+")
  19.  
  20. # Substitute if necessary and create a stash if a substitution happens
  21. sed -i '' "s|.*Migration$|&[$RAILS_VERSION]|" $FILE
  22. git diff --quiet && return 1
  23. git stash --quiet && return 0
  24. }
  25.  
  26. function update_timestamps() {
  27. local COUNTER=0
  28. local ORIG_BRANCH=$(git rev-parse --abbrev-ref HEAD)
  29.  
  30. echo "Creating stashes for up to $(ls -d -1 db/migrate/* | wc -l | egrep -o "[0-9]+") migrations..."
  31. for filename in $(ls -d -1 db/migrate/*); do
  32. versionize $filename && ((COUNTER++))
  33.  
  34. # Go back to the tip to find the appropriate commit for the migration file
  35. git checkout --quiet $ORIG_BRANCH
  36. done
  37.  
  38. echo "${COUNTER} stashes created. Applying stashes..."
  39. until [ $COUNTER -lt 1 ]; do
  40. ((COUNTER--))
  41. git stash pop --quiet
  42. done
  43. }
Add Comment
Please, Sign In to add comment