forked from MarquezProject/marquez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-migration.sh
executable file
·89 lines (77 loc) · 2.43 KB
/
db-migration.sh
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
#!/bin/bash
#
# Copyright 2018-2023 contributors to the Marquez project
# SPDX-License-Identifier: Apache-2.0
#
# A script used in CI to test database migrations by:
# (1) Applying db migrations on latest Marquez release
# (2) Taking a backup of db from Step 1
# (3) Applying db migrations on latest Marquez build using backup
#
# Usage: $ ./db-migration.sh
# Version of PostgreSQL
readonly POSTGRES_VERSION="14"
# Version of Marquez
readonly MARQUEZ_VERSION=0.43.1
# Build version of Marquez
readonly MARQUEZ_BUILD_VERSION="$(git log --pretty=format:'%h' -n 1)" # SHA1
readonly DB_MIGRATION_VOLUME="marquez_db-backup"
readonly DB_MIGRATION_BACKUP="db-migration-backup"
readonly DB_MIGRATION_QUERY=$(cat <<-END
SELECT version,installed_on,checksum
FROM flyway_schema_history
WHERE version IS NOT NULL
ORDER BY installed_on DESC LIMIT 1;
END
)
log() {
echo -e "\033[1m>>\033[0m ${1}"
}
error() {
echo -e "\033[0;31merror: ${1}\033[0m"
}
exit_with_cause() {
log "please view container logs for more details on cause:"
docker-compose logs
exit 1
}
query_db_migration() {
# Start db using backup
[[ $(docker ps -f "name=${DB_MIGRATION_BACKUP}" --format '{{.Names}}') == "${DB_MIGRATION_BACKUP}" ]] || \
docker run -d --name "${DB_MIGRATION_BACKUP}" \
-v "${DB_MIGRATION_VOLUME}:/var/lib/postgresql/data" \
"postgres:${POSTGRES_VERSION}"
# Query applied db migrations
log "latest migration applied to db:"
docker exec "${DB_MIGRATION_BACKUP}" \
psql -U marquez -c "${DB_MIGRATION_QUERY}"
}
# Change working directory to project root
project_root=$(git rev-parse --show-toplevel)
cd "${project_root}/"
# (1) Apply db migrations on latest Marquez release
log "start db with latest migrations (marquez=${MARQUEZ_VERSION}):"
if ! ./docker/up.sh \
--args "--exit-code-from seed_marquez" \
--tag "${MARQUEZ_VERSION}" \
--no-web \
--seed > /dev/null; then
error "failed to start db using backup!"
exit_with_cause
fi
# Query, then display schema migration applied
query_db_migration
# (2) Apply db migrations on latest Marquez build using backup
log "start db using backup (marquez=${MARQUEZ_BUILD_VERSION}):"
if ! ./docker/up.sh \
--args "--exit-code-from seed_marquez" \
--no-web \
--no-volumes \
--build \
--seed > /dev/null; then
error "failed to start db using backup!"
exit_with_cause
fi
# Query, then display additional schema migration applied on backup (if any)
query_db_migration
log "DONE!"