|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) HashiCorp, Inc. |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | + |
| 6 | +die() { |
| 7 | + echo "$@" |
| 8 | + exit 255 |
| 9 | +} |
| 10 | + |
| 11 | +which pg_dump &> /dev/null || die "pg_dump must be installed" |
| 12 | +which pg_restore &> /dev/null || die "pg_restore must be installed" |
| 13 | +which pg_isready &> /dev/null || die "pg_isready must be installed" |
| 14 | +which awk &> /dev/null || die "awk must be installed" |
| 15 | +which git &> /dev/null || die "git must be installed" |
| 16 | +which make &> /dev/null || die "make must be installed" |
| 17 | + |
| 18 | +set -e |
| 19 | + |
| 20 | +SQL_TEST_DB_PORT=${SQL_TEST_DB_PORT:=5432} |
| 21 | +export SQL_TEST_DB_PORT |
| 22 | + |
| 23 | +DB_HOST=${DB_HOST:=127.0.0.1} |
| 24 | + |
| 25 | +base_branch=${1} |
| 26 | +if [[ -z "${base_branch}" ]]; then |
| 27 | + base_branch="main" |
| 28 | +fi |
| 29 | +base_commit=$(git rev-parse "${base_branch}") |
| 30 | + |
| 31 | +new_branch=$(git rev-parse --abbrev-ref HEAD) |
| 32 | +new_commit=$(git rev-parse HEAD) |
| 33 | + |
| 34 | +tmp_dir=".schema-diff" |
| 35 | + |
| 36 | +extract() { |
| 37 | + local suffix=$1 |
| 38 | + local dump="${tmp_dir}/${suffix}.dump" |
| 39 | + |
| 40 | + mkdir -p \ |
| 41 | + "${tmp_dir}/funcs_${suffix}" \ |
| 42 | + "${tmp_dir}/tables_${suffix}" \ |
| 43 | + "${tmp_dir}/views_${suffix}" \ |
| 44 | + "${tmp_dir}/triggers_${suffix}" \ |
| 45 | + "${tmp_dir}/indexes_${suffix}" |
| 46 | + |
| 47 | + echo "extracting function definitions from ${dump}" |
| 48 | + while read -r f; do |
| 49 | + fname="${f%(*}" |
| 50 | + pg_restore -s -O -P "${f}" -f - "${dump}" | tr '[:upper:]' '[:lower:]' > "${tmp_dir}/funcs_${suffix}/${fname}.sql" |
| 51 | + done < <(pg_restore -l "${dump}" -f - | awk '$4 == "FUNCTION" {for(i=6;i<NF;i++) printf $i" "; print ""}') |
| 52 | + |
| 53 | + echo "extracting table definitions from ${dump}" |
| 54 | + while read -r t; do |
| 55 | + tname="${t%(*}" |
| 56 | + pg_restore -s -O -t "${t}" -f - "${dump}" | tr '[:upper:]' '[:lower:]' > "${tmp_dir}/tables_${suffix}/${tname}.sql" |
| 57 | + done < <(pg_restore -l "${dump}" -f - | awk '$4 == "TABLE" {for(i=6;i<NF;i++) printf $i" "; print ""}') |
| 58 | + |
| 59 | + echo "extracting view definitions from ${dump}" |
| 60 | + while read -r v; do |
| 61 | + vname="${v%(*}" |
| 62 | + pg_restore -s -O -t "${v}" -f - "${dump}" | tr '[:upper:]' '[:lower:]' > "${tmp_dir}/views_${suffix}/${vname}.sql" |
| 63 | + done < <(pg_restore -l "${dump}" -f - | awk '$4 == "VIEW" {for(i=6;i<NF;i++) printf $i" "; print ""}') |
| 64 | + |
| 65 | + echo "extracting trigger definitions from ${dump}" |
| 66 | + while read -r t; do |
| 67 | + tname="${t%(*}" |
| 68 | + pg_restore -s -O -T "${t}" -f - "${dump}" | tr '[:upper:]' '[:lower:]' > "${tmp_dir}/triggers_${suffix}/${tname}.sql" |
| 69 | + done < <(pg_restore -l "${dump}" -f - | awk '$4 == "TRIGGER" {for(i=6;i<NF;i++) printf $i" "; print ""}') |
| 70 | + |
| 71 | + echo "extracting index definitions from ${dump}" |
| 72 | + while read -r d; do |
| 73 | + dname="${d%(*}" |
| 74 | + pg_restore -s -O -I "${d}" -f - "${dump}" | tr '[:upper:]' '[:lower:]' > "${tmp_dir}/indexes_${suffix}/${dname}.sql" |
| 75 | + done < <(pg_restore -l "${dump}" -f - | awk '$4 == "INDEX" {for(i=6;i<NF;i++) printf $i" "; print ""}') |
| 76 | + |
| 77 | + echo "extracting post-data from ${dump}" |
| 78 | + pg_restore --section=post-data -O -f - "${dump}" | tr '[:upper:]' '[:lower:]' > "${tmp_dir}/post_data_${suffix}.sql" |
| 79 | +} |
| 80 | + |
| 81 | +dump() { |
| 82 | + local suffix=$1 |
| 83 | + local dump="${tmp_dir}/${suffix}.dump" |
| 84 | + |
| 85 | + mkdir -p "${tmp_dir}" |
| 86 | + |
| 87 | + make -C internal/db/sqltest clean |
| 88 | + make -C internal/db/sqltest database-up |
| 89 | + max=120 |
| 90 | + c=0 |
| 91 | + until pg_isready -h "${DB_HOST}" -p "${SQL_TEST_DB_PORT}"; do |
| 92 | + ((c+=1)) |
| 93 | + if [[ $c -ge $max ]]; then |
| 94 | + docker logs boundary-sql-tests |
| 95 | + make -C internal/db/sqltest clean |
| 96 | + die "timeout waiting for database, likely an error in a migration" |
| 97 | + fi |
| 98 | + sleep 1 |
| 99 | + done |
| 100 | + |
| 101 | + echo "dumping to ${dump}" |
| 102 | + pg_dump -Fc -h "${DB_HOST}" -U boundary -f "${dump}" |
| 103 | + |
| 104 | + make -C internal/db/sqltest clean |
| 105 | +} |
| 106 | + |
| 107 | +rm -rf "${tmp_dir}" |
| 108 | + |
| 109 | +echo "Comparing schema between ${new_branch}@${new_commit} ${base_branch}@${base_commit}" |
| 110 | + |
| 111 | +dump "${new_commit}" |
| 112 | +extract "${new_commit}" |
| 113 | + |
| 114 | +git checkout "${base_commit}" |
| 115 | + |
| 116 | +dump "${base_commit}" |
| 117 | +extract "${base_commit}" |
| 118 | + |
| 119 | +if [[ "${new_branch}" == "HEAD" ]]; then |
| 120 | + git checkout "${new_commit}" |
| 121 | +else |
| 122 | + git checkout "${new_branch}" |
| 123 | +fi |
| 124 | + |
| 125 | +for t in "funcs" "tables" "views" "triggers" "indexes"; do |
| 126 | + git diff --no-index "${tmp_dir}/${t}_${base_commit}" "${tmp_dir}/${t}_${new_commit}" | tee "${tmp_dir}/${t}.diff" |
| 127 | +done |
| 128 | + |
| 129 | +git diff --no-index "${tmp_dir}/post_data_${base_commit}.sql" "${tmp_dir}/post_data_${new_commit}.sql" | tee "${tmp_dir}/post_data.diff" |
0 commit comments