|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2018-2022 contributors to the Marquez project |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# A script used in CI to test database migrations by: |
| 7 | +# (1) Applying db migrations on latest Marquez release |
| 8 | +# (2) Take a backup of db from Step 1 |
| 9 | +# (3) Applying db migrations on latest Marquez build using backup |
| 10 | +# |
| 11 | +# Usage: $ ./db-migration.sh |
| 12 | + |
| 13 | +# Version of PostgreSQL |
| 14 | +readonly POSTGRES_VERSION="12.1" |
| 15 | +# Version of Marquez |
| 16 | +readonly MARQUEZ_VERSION="0.29.0" |
| 17 | +# Build version of Marquez |
| 18 | +readonly MARQUEZ_BUILD_VERSION="$(git log --pretty=format:'%h' -n 1)" # SHA1 |
| 19 | + |
| 20 | +readonly DB_MIGRATION_VOLUME="marquez_db-backup" |
| 21 | +readonly DB_MIGRATION_BACKUP="db-migration-backup" |
| 22 | +readonly DB_MIGRATION_QUERY=$(cat <<-END |
| 23 | + SELECT version,installed_on,checksum |
| 24 | + FROM flyway_schema_history |
| 25 | + WHERE version IS NOT NULL |
| 26 | + ORDER BY installed_on DESC LIMIT 1; |
| 27 | +END |
| 28 | +) |
| 29 | + |
| 30 | +log() { |
| 31 | + echo -e "\033[1m>>\033[0m ${1}" |
| 32 | +} |
| 33 | + |
| 34 | +error() { |
| 35 | + echo -e "\033[0;31merror: ${1}\033[0m" |
| 36 | +} |
| 37 | + |
| 38 | +exit_with_cause() { |
| 39 | + log "please view container logs for more details on cause:" |
| 40 | + docker-compose logs |
| 41 | + exit 1 |
| 42 | +} |
| 43 | + |
| 44 | +query_db_migration() { |
| 45 | + # Start db using backup |
| 46 | + [[ $(docker ps -f "name=${DB_MIGRATION_BACKUP}" --format '{{.Names}}') == "${DB_MIGRATION_BACKUP}" ]] || \ |
| 47 | + docker run -d --name "${DB_MIGRATION_BACKUP}" \ |
| 48 | + -v "${DB_MIGRATION_VOLUME}:/var/lib/postgresql/data" \ |
| 49 | + "postgres:${POSTGRES_VERSION}" |
| 50 | + # Query applied db migrations |
| 51 | + log "latest migration applied to db:" |
| 52 | + docker exec "${DB_MIGRATION_BACKUP}" \ |
| 53 | + psql -U marquez -c "${DB_MIGRATION_QUERY}" |
| 54 | +} |
| 55 | + |
| 56 | +# Change working directory to project root |
| 57 | +project_root=$(git rev-parse --show-toplevel) |
| 58 | +cd "${project_root}/" |
| 59 | + |
| 60 | +# (1) Apply db migrations on latest Marquez release |
| 61 | +log "start db with latest migrations (marquez=${MARQUEZ_VERSION}):" |
| 62 | +if ! ./docker/up.sh \ |
| 63 | + --args "--exit-code-from seed_marquez" \ |
| 64 | + --tag "${MARQUEZ_VERSION}" \ |
| 65 | + --no-web \ |
| 66 | + --seed > /dev/null; then |
| 67 | + error "failed to start db using backup!" |
| 68 | + exit_with_cause |
| 69 | +fi |
| 70 | + |
| 71 | +# Query, then display schema migration applied |
| 72 | +query_db_migration |
| 73 | + |
| 74 | +# (2) Apply db migrations on latest Marquez build using backup |
| 75 | +log "start db using backup (marquez=${MARQUEZ_BUILD_VERSION}):" |
| 76 | +if ! ./docker/up.sh \ |
| 77 | + --args "--exit-code-from seed_marquez" \ |
| 78 | + --no-web \ |
| 79 | + --no-volumes \ |
| 80 | + --build \ |
| 81 | + --seed > /dev/null; then |
| 82 | + error "failed to start db using backup!" |
| 83 | + exit_with_cause |
| 84 | +fi |
| 85 | + |
| 86 | +# Query, then display additional schema migration applied on backup (if any) |
| 87 | +query_db_migration |
| 88 | + |
| 89 | +log "DONE!" |
0 commit comments