github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/bin/migrate.sh (about) 1 # 2 # This file is part of the Smart Home 3 # Program complex distribution https://github.com/e154/smart-home 4 # Copyright (C) 2016-2023, Filippov Alex 5 # 6 # This library is free software: you can redistribute it and/or 7 # modify it under the terms of the GNU Lesser General Public 8 # License as published by the Free Software Foundation; either 9 # version 3 of the License, or (at your option) any later version. 10 # 11 # This library is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 # Library General Public License for more details. 15 # 16 # You should have received a copy of the GNU Lesser General Public 17 # License along with this library. If not, see 18 # <https://www.gnu.org/licenses/>. 19 20 #!/usr/bin/env bash 21 22 set -o errexit 23 24 main() { 25 26 case "${COMMAND}" in 27 new) 28 __new 29 ;; 30 up) 31 __up 32 ;; 33 down) 34 __down 35 ;; 36 status) 37 __status 38 ;; 39 gen) 40 __gen 41 ;; 42 help) 43 __help 44 ;; 45 *) 46 __help 47 exit 1 48 ;; 49 esac 50 51 } 52 53 BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 54 ENV=development 55 CONFIG=./conf/dbconfig.yml 56 57 MIGRATION_NAME=$2 58 COMMAND=$1 59 60 __new() { 61 echo $MIGRATION_NAME 62 63 NOW=$(date +"%Y%m%d_%H%M%S") 64 65 cat > $BASEDIR/../migrations/"${NOW}_${MIGRATION_NAME}.sql" <<EOF 66 -- +migrate Up 67 -- SQL in section 'Up' is executed when this migration is applied 68 69 -- +migrate Down 70 -- SQL section 'Down' is executed when this migration is rolled back 71 72 73 EOF 74 } 75 76 __up() { 77 sql-migrate up -config=$CONFIG -env=$ENV 78 } 79 80 __down() { 81 sql-migrate down -config=$CONFIG -env=$ENV 82 } 83 84 __status() { 85 sql-migrate status -config=$CONFIG -env=$ENV 86 } 87 88 __gen() { 89 90 BD=`which go-bindata` 91 92 if [ -z "$BD" ]; then 93 echo "Required tools are missing - check beginning of \"$0\" file for details." 94 echo "wait for installing go-bindta" 95 go get -u github.com/jteeuwen/go-bindata/... 96 fi 97 98 # go get -u github.com/jteeuwen/go-bindata/... 99 ${BD} -pkg database -o ${BASEDIR}/../system/migrations/assets/assets.go migrations 100 } 101 102 __help() { 103 cat <<EOF 104 Usage: migrate.sh [options] 105 106 OPTIONS: 107 108 new - new migration 109 up - up migration 110 down - down migration 111 status - reset migration 112 gen - generate migration sources 113 114 -h / --help - show this help text and exit 0 115 116 EOF 117 } 118 119 if [ -z `which sql-migrate` ]; then 120 echo "Required tools are missing - check beginning of \"$0\" file for details." 121 echo "wait for installing sql-migrate" 122 go get github.com/rubenv/sql-migrate/... 123 fi 124 125 main "$@"