migrate.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/bash
  2. LAST_NUM_STR=`find ./support/migrate/ -maxdepth 1 -name '*.go' | sort -t_ -nk2,2 | tail -n1 | awk -F/ '{print $NF}' | awk -F. '{print $NR}'`
  3. LAST_NUM_STR=`echo "$LAST_NUM_STR" | sed 's/^[0]*//g'`
  4. NEXT_NUM_INT=$(($LAST_NUM_STR + 1))
  5. NEXT_NUM_STR=`printf %09d $NEXT_NUM_INT`
  6. TARGET_FILE="./support/migrate/${NEXT_NUM_STR}.go"
  7. # Create new migration file
  8. echo "package migrate" > $TARGET_FILE
  9. echo "" >> $TARGET_FILE
  10. echo "import (" >> $TARGET_FILE
  11. echo " \"context\"" >> $TARGET_FILE
  12. echo "" >> $TARGET_FILE
  13. echo " \"golang-fave/engine/sqlw\"" >> $TARGET_FILE
  14. echo ")" >> $TARGET_FILE
  15. echo "" >> $TARGET_FILE
  16. echo "func Migrate_${NEXT_NUM_STR}(ctx context.Context, db *sqlw.DB, host string) error {" >> $TARGET_FILE
  17. echo " return nil" >> $TARGET_FILE
  18. echo "}" >> $TARGET_FILE
  19. # Update list
  20. LIST_FILE="./support/migrate/000000001.go"
  21. echo "package migrate" > $LIST_FILE
  22. echo "" >> $LIST_FILE
  23. echo "import (" >> $LIST_FILE
  24. echo " \"context\"" >> $LIST_FILE
  25. echo "" >> $LIST_FILE
  26. echo " \"golang-fave/engine/sqlw\"" >> $LIST_FILE
  27. echo ")" >> $LIST_FILE
  28. echo "" >> $LIST_FILE
  29. echo "var Migrations = map[string]func(context.Context, *sqlw.DB, string) error{" >> $LIST_FILE
  30. echo " \"000000000\": nil," >> $LIST_FILE
  31. echo " \"000000001\": nil," >> $LIST_FILE
  32. for i in `ls ./support/migrate/*.go | sort -V`; do
  33. IFILE=`echo "$i" | awk -F/ '{print $NF}' | awk -F. '{print $NR}'`
  34. if [ $IFILE == "000000000" ] || [ $IFILE == "000000001" ]; then
  35. continue
  36. fi
  37. echo " \"${IFILE}\": Migrate_${IFILE}," >> $LIST_FILE
  38. done
  39. echo "}" >> $LIST_FILE
  40. echo "New migration file created: ${TARGET_FILE}"