-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-patch-database.sh
More file actions
executable file
·110 lines (77 loc) · 2.58 KB
/
Copy pathmigrate-patch-database.sh
File metadata and controls
executable file
·110 lines (77 loc) · 2.58 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
110
#!/bin/bash
# This is the second step in the environment migration.
# Patches the specified DB dump file with the specified DB diff file. The
# resulting dump is saved in the specified file.
# Takes three arguments - DB dump, DB diff and the resulting dump file.
# full path to this script
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${DIR}/conf/config.sh"
source "${DIR}/utils/utils.sh"
source "${DIR}/utils/db-utils.sh"
function main {
if [[ "${#}" != "4" ]]
then
echo "4 parameters (env + 3 files) expected: originalenv dbdump dbdiff outputdump"
exit 1
fi
ENV_ORIGINAL="${1}"
DB_DUMP="${2}"
DB_DIFF="${3}"
OUT_DUMP="${4}"
remove_bom "${DB_DUMP}"
sed -i ".bak" "/CREATE SCHEMA/d" "${DB_DUMP}"
remove_bom "${DB_DIFF}"
HOST=`getparam "localhost" "host"`
PORT=`getparam "localhost" "dbport"`
USER=`getparam "localhost" "dbuser"`
USER_ADMIN=`getparam "localhost" "admin"`
DB=`getparam "localhost" "dbtestdatabase"`
SCHEMA=`getparam "localhost" "dbschema"`
COMMON="-h ${HOST} -p ${PORT} -d ${DB} --no-password --single-transaction"
echo -n "Flushing temp DB..."
# delete local test schema
${PSQL} ${COMMON} -U ${USER_ADMIN} -c "DROP SCHEMA IF EXISTS ${SCHEMA} CASCADE" &> /dev/null
echo "done."
echo -n "Creating DB..."
# create local test schema
${PSQL} ${COMMON} -U ${USER_ADMIN} -c "CREATE SCHEMA ${SCHEMA} AUTHORIZATION ${USER}" &> /dev/null
echo "done."
echo -n "Creating DB schema & data from the dump..."
# fill local test schema with data
${PSQL} ${COMMON} -U ${USER} -e -f ${DB_DUMP} &> /dev/null
echo "done."
# re-sync with the source
echo "Re-syncing with the source..."
# TODO move somehow to config
WORK="${DIR}/work"
mkdir -p "${WORK}"
RESYNC_DIFF="${WORK}/migrate-resync-diff.sql"
# TODO "test" should be parametrized
"${DIR}/db-diff.sh" "test" "${ENV_ORIGINAL}" > "${RESYNC_DIFF}"
echo "done."
echo -n "Applying re-sync diff..."
# try to apply the diff
${PSQL} ${COMMON} -U ${USER} -e -f ${RESYNC_DIFF} > /dev/null
echo "done."
echo -n "Applying diff..."
# try to apply the diff
${PSQL} ${COMMON} -U ${USER} -e -f ${DB_DIFF} > /dev/null
echo "done."
echo -n "Dumping the patched image..."
# dump the schema
"${PG_DUMP}" \
--host localhost \
--port "${PORT}" \
--username "${USER}" \
--no-password \
--format plain \
--create \
--inserts \
--no-tablespaces \
--file "${OUT_DUMP}" \
--schema "${SCHEMA}" "${DB}"
echo "done."
echo "Please continue with the next step of the migration (migrate-server)."
}
main "${@}"
exit 0