This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmigrate-db.sh
More file actions
executable file
·109 lines (87 loc) · 2.36 KB
/
migrate-db.sh
File metadata and controls
executable file
·109 lines (87 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
accountScriptsDir=$DIR/database/account/sql/
todoScriptsDir=$DIR/database/todo/sql/
accountHost=$1
accountPort=$2
accountUser=$3
accountPassword=$4
shift 4
todoHost=$1
todoPort=$2
todoUser=$3
todoPassword=$4
shift 4
function logFlywayMessage() {
CYAN='\033[0;36m'
NC='\033[0m'
printf "📝 ${CYAN}$1${NC}\n"
}
function flywayInstalled() {
command -v flyway > /dev/null
return $?
}
function dockerInstalled() {
command -v docker > /dev/null
return $?
}
LOCAL_BINARY=1
DOCKER_BINARY=2
function getMode() {
flywayInstalled
isFlywayInstalled=$?
if [[ $isFlywayInstalled == 0 ]]; then
return $LOCAL_BINARY
fi
return $DOCKER_BINARY
}
function getBinaryToExecute() {
scriptsPath=$1
getMode
mode=$?
if [[ $mode == $LOCAL_BINARY ]]; then
echo "flyway -locations=filesystem:$scriptsPath"
else
echo "docker run --net=host --rm -v $scriptsPath:/flyway/sql boxfuse/flyway"
fi
}
function translateHost() {
if [ "$(uname -s)" = "Darwin" ]; then
# running in MacOS, localhost translation needs to happen
if [[ "$1" == "localhost" || "$1" == "127.0.0.1" ]]; then
echo "host.docker.internal"
else
echo "$1"
fi
else
# Linux does not need localhost translation
echo "$1"
fi
}
set +e
if ! flywayInstalled && ! dockerInstalled; then
logFlywayMessage "You must have Flyway or Docker installed to be able to migrate the database"
exit 1
fi
getMode
mode=$?
if [[ $mode == $DOCKER_BINARY ]]; then
accountHost=$(translateHost $accountHost)
todoHost=$(translateHost $todoHost)
docker pull boxfuse/flyway > /dev/null 2>&1
fi
set -e
echo
echo
logFlywayMessage "Starting migration of 'Account' database"
flyway=$(getBinaryToExecute $accountScriptsDir)
eval "$flyway migrate -url=\"jdbc:sqlserver://$accountHost:$accountPort;databaseName=spawndemoaccount\" -user=\"$accountUser\" -password=\"$accountPassword\" -mixed=true"
echo
echo
logFlywayMessage "Starting migration of 'Todo' database"
flyway=$(getBinaryToExecute $todoScriptsDir)
eval "$flyway migrate -url=\"jdbc:postgresql://$todoHost:$todoPort/spawndemotodo\" -user=\"$todoUser\" -password=\"$todoPassword\""
echo
echo
logFlywayMessage "Finished migrating both databases"