Guest User

Untitled

a guest
Mar 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # run-migrations-singly
  3.  
  4. PROG=${0##*/}
  5. DIR=${0%/*}
  6.  
  7. export PATH=$PATH:$HOME/bin:$HOME/lib
  8. # see github.com/aks/bash-lib
  9. source talk-utils.sh
  10. source run-utils.sh
  11.  
  12. status_log=migration_statuses.log
  13.  
  14. usage() {
  15. cat 1>&2 <<EOF
  16. usage: $PROG [-hnv] [STARTDATE [ENDDATE]]
  17. Run rails migrations from STARTDATE until ENDDATE, if given. If a date is
  18. omitted, all available down migrations are used.
  19.  
  20. Options
  21. -h show this help
  22. -n no run; show commands but don't run them
  23. -v be verbose
  24. EOF
  25. exit
  26. }
  27.  
  28. migration_statuses() {
  29. talk "Fetching migration statuses in $status_log .."
  30. safe_run "bundle exec rake db:migrate:status"
  31. }
  32.  
  33. prompt_yes_or_no() {
  34. local ans=
  35. while [[ -z "$ans" ]]; do
  36. read -e -p "Run? [yNq]" ans
  37. [[ -z "$ans" ]] && ans=no
  38. case "$ans" in
  39. yes|y|YES|Y)
  40. return 0 ;;
  41. no|n|NO|n)
  42. return 1 ;;
  43. quit|q|QUIT|q)
  44. talk "Quit!"
  45. exit ;;
  46. *)
  47. ans=
  48. talk "Please answer yes or no" ;;
  49. esac
  50. done
  51. }
  52.  
  53. start_yet() {
  54. test_limit '>' "$@"
  55. }
  56.  
  57. not_done_yet() {
  58. test_limit '<' "$@"
  59. }
  60.  
  61. # test_limit ver lim op
  62. test_limit() {
  63. local op="$1" ver="$2" lim="$3"
  64. if eval "[[ -z \"$lim\" || ( \"$ver\" $op \"$lim\" || \"$ver\" = \"$lim\" ) ]]" ; then
  65. return 0
  66. else
  67. return 1
  68. fi
  69. }
  70.  
  71. skipped_line() {
  72. if (( skipping )) ; then
  73. if (( verbose )) ; then
  74. talkf "%s (skipped)\n" "$line"
  75. fi
  76. return 0
  77. fi
  78. skipping=1
  79. return 1
  80. }
  81.  
  82. skipping_msg() {
  83. (( verbose )) || return 0
  84. talk "$line"
  85. talk "$1"
  86. }
  87.  
  88.  
  89. # up 20180309183346 Add index to site instruction type
  90. # down 20180312232945 Change alternate formats table all ids to bigints stage 1
  91.  
  92. run_migrations_up_iteratively() {
  93. local startdate="$1"
  94. local enddate="$2"
  95. local line skipping=0
  96. local updown version message
  97. exec 3<$status_log # open fd 3 on the status log
  98. while read -u 3 line ; do
  99. if [[ "$line" =~ (down|up)\ *([0-9]{14})\ *([^\ ].*)$ ]] ; then
  100. updown="${BASH_REMATCH[1]}"
  101. version="${BASH_REMATCH[2]}"
  102. message="${BASH_REMATCH[2]}"
  103. case "$updown" in
  104. down)
  105. if start_yet "$version" "$startdate" ; then
  106. if not_done_yet "$version" "$enddate" ; then
  107. talk "$line"
  108. if prompt_yes_or_no ; then
  109. run_migration "$version"
  110. else
  111. talk "Skipped $version"
  112. fi
  113. else # done
  114. skipped_line && continue
  115. skipping_msg "Skipping this and remaining migrations" || break
  116. fi
  117. else # not started yet?
  118. skipped_line && continue
  119. skipping_msg "Skipping this and others until $startdate .." || break
  120. fi
  121. ;;
  122. up)
  123. (( verbose )) && talk "$line"
  124. ;;
  125. esac
  126. else
  127. (( verbose )) && talk "$line"
  128. fi
  129. done
  130. exec 3<&- # close fd 3
  131. }
  132.  
  133. run_migration() {
  134. run "bundle exec rake db:migrate:up VERSION=\"$1\""
  135. }
  136.  
  137. norun= verbose=
  138.  
  139. while getopts 'hnv' opt ; do
  140. case "$opt" in
  141. h) usage ;;
  142. n) norun=1 ;;
  143. v) verbose=1 ;;
  144. esac
  145. done
  146. shift $(( OPTIND - 1 ))
  147.  
  148. case $# in
  149. 2) startdate="$1" enddate="$2" ;;
  150. 1) startdate="$1" enddate= ;;
  151. 0) startdate= enddate= ;;
  152. esac
  153.  
  154. if [[ ! -e $status_log ]] || find $status_log -mtime +1 1 >& /dev/null ; then
  155. migration_statuses >$status_log
  156. fi
  157. run_migrations_up_iteratively "$startdate" "$enddate"
  158. exit
Add Comment
Please, Sign In to add comment