Guest User

Untitled

a guest
Oct 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ############################################################
  4. # Program: migrate.sh #
  5. ############################################################
  6.  
  7. ## BEGIN SCRIPT
  8. usage()
  9. {
  10. cat << EOF
  11. usage: $(basename $0) [OPTIONS] <COMMAND>
  12.  
  13. OPTIONS can have:
  14. -h Show this message
  15. -v Verbose
  16.  
  17. COMMAND can be one of:
  18. up
  19. all
  20.  
  21. You can either write the db credentials in a file named 'config'
  22. located in the same dir as this script, or pass them like:
  23. user=someuser host=somehost pass=somepass db=somedbname $(basename $0) up.
  24.  
  25. Also, any variables written in the config file will overwrite those
  26. in the command line.
  27. EOF
  28. }
  29.  
  30. debug()
  31. {
  32. [ -z $VERBOSE ] && echo $1
  33. }
  34.  
  35. EXIT_INVALID_ARGS=1
  36. EXIT_SQL_ERROR=2
  37.  
  38. VERBOSE=0
  39.  
  40. # Check options passed in.
  41. while getopts "hv" OPTION
  42. do
  43. case $OPTION in
  44. h)
  45. usage
  46. exit EXIT_INVALID_ARGS
  47. ;;
  48. v)
  49. VERBOSE=1
  50. ;;
  51. ?)
  52. usage
  53. exit EXIT_INVALID_ARGS
  54. ;;
  55. esac
  56. done
  57.  
  58. shift $(($OPTIND-1))
  59.  
  60. # Checks for custom config file path
  61. [ -z "$cfg" ] && cfg="./config"
  62. if [ -e "$cfg" ]
  63. then
  64. source "$cfg"
  65. fi
  66.  
  67. # Show usage when we can't find or understand the mandatory parameters.
  68. if [ $# -ne 1 ] \
  69. || ! (echo $1 | grep -x 'up\|all' &>/dev/null) \
  70. || [ -z "$user" ] || [ -z "$host" ]
  71. then
  72. usage
  73. exit EXIT_INVALID_ARGS
  74. else
  75. cmd=$1
  76. fi
  77.  
  78. # If no basedir was informed, use current dir
  79. [ -z "$basedir" ] && basedir=$(pwd)
  80.  
  81. for sql_file in "$basedir"/db/*.sql
  82. do
  83. debug "Checking filename format for '$sql_file'"
  84. # If the filename is not in the format YYYYMMDD-HH:MM:SS_description.sql we skip it
  85. if (echo $sql_file | grep -x '[0-9]\{8\}-\([0-9]\{2\}:\)\{2\}[0-9]\{2\}_[a-zA-Z0-9_]\+\.sql')
  86. then
  87. # If this file exists, we have some previously executed migration
  88. if [ -e "$basedir"/db/.last_migration ]
  89. then
  90. last_migration=$(cat "$basedir"/db/.last_migration)
  91. current_filename=$(basename "$sql_file")
  92. # Check if the current $sql_file is newer than the last one
  93. if [[ $(date -d "$(echo ${last_migration:0:17} | sed 's/-/ /')" +%s) \
  94. > $(date -d "$(echo ${current_filename:0:17} | sed 's/-/ /')" +%s) ]]
  95. then
  96. debug "File '$sql_file' is older than last sucessfully executed file" \
  97. "'$(cat $basedir/db/.last_migration). Skipping..."
  98. continue
  99. fi
  100. fi
  101. debug "Executing SQL file $sql_file"
  102. # TODO: implement SQL Server branch
  103. mysql_out=$(mysql -u"$user" -p"$pass" -h"$host" "$db" < $sql_file)
  104. debug "MySQL exit code: $?"
  105. if [ $? -ne 0 ]
  106. then
  107. debug $mysql_out
  108. exit $EXIT_SQL_ERROR
  109. fi
  110. else
  111. debug "Filename '$sql_file' cannot be recognized. Skipping..."
  112. continue
  113. fi
  114.  
  115. echo "$sql_file" > $basedir/db/.last_migration
  116.  
  117. if [[ $cmd = "up" ]]
  118. then
  119. break
  120. fi
  121. done
  122.  
  123. exit 0
Add Comment
Please, Sign In to add comment