|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 4 | +DEFAULT_BIN="${SCRIPT_DIR}/../../target/debug/pgdog" |
| 5 | +PGDOG_BIN=${PGDOG_BIN:-$DEFAULT_BIN} |
| 6 | + |
| 7 | +pushd ${SCRIPT_DIR} |
| 8 | +docker-compose down && docker-compose up -d |
| 9 | + |
| 10 | +# Give it a second to boot up. |
| 11 | +# It restarts the DB during initialization. |
| 12 | +sleep 2 |
| 13 | + |
| 14 | +for port in 15432 15433 15434 15435; do |
| 15 | + echo "Waiting for database on port ${port}..." |
| 16 | + until PGPASSWORD=pgdog pg_isready -h 127.0.0.1 -p "${port}" -U pgdog -d postgres; do |
| 17 | + sleep 1 |
| 18 | + done |
| 19 | +done |
| 20 | + |
| 21 | +${PGDOG_BIN} & |
| 22 | +PGDOG_PID="$!" |
| 23 | + |
| 24 | +export PGPASSWORD=pgdog |
| 25 | +export PGHOST=127.0.0.1 |
| 26 | +export PGPORT=6432 |
| 27 | +export PGUSER=pgdog |
| 28 | + |
| 29 | +until psql source -c 'SELECT 1' 2> /dev/null; do |
| 30 | + sleep 1 |
| 31 | +done |
| 32 | + |
| 33 | +pgbench -f pgbench.sql -P 1 source -c 5 -t 1000000 & |
| 34 | +PGBENCH_PID="$!" |
| 35 | + |
| 36 | +sleep 10 |
| 37 | + |
| 38 | +psql admin -c 'COPY_DATA source destination pgdog' |
| 39 | + |
| 40 | +sleep 10 |
| 41 | + |
| 42 | +kill -TERM ${PGBENCH_PID} |
| 43 | + |
| 44 | +replace_copy_with_replicate() { |
| 45 | + local table="$1" |
| 46 | + local column="$2" |
| 47 | + |
| 48 | + psql source -c "UPDATE ${table} SET ${column} = regexp_replace(${column}, '-copy$', '-replicate') WHERE ${column} LIKE '%-copy';" |
| 49 | +} |
| 50 | + |
| 51 | +replace_copy_with_replicate tenants name |
| 52 | +replace_copy_with_replicate accounts full_name |
| 53 | +replace_copy_with_replicate projects name |
| 54 | +replace_copy_with_replicate tasks title |
| 55 | +replace_copy_with_replicate task_comments body |
| 56 | +replace_copy_with_replicate settings name |
| 57 | + |
| 58 | +wait_for_no_copy_rows() { |
| 59 | + local table="$1" |
| 60 | + local column="$2" |
| 61 | + |
| 62 | + while true; do |
| 63 | + count=$(psql -d destination -tAc "SELECT COUNT(*) FROM ${table} WHERE ${column} LIKE '%-copy'") |
| 64 | + if [ "${count}" -eq 0 ]; then |
| 65 | + echo "${table}.${column}: replication caught up" |
| 66 | + break |
| 67 | + fi |
| 68 | + |
| 69 | + echo "${table}.${column}: waiting for ${count} rows ending in -copy" |
| 70 | + sleep 1 |
| 71 | + done |
| 72 | +} |
| 73 | + |
| 74 | +wait_for_no_copy_rows tenants name |
| 75 | +wait_for_no_copy_rows accounts full_name |
| 76 | +wait_for_no_copy_rows projects name |
| 77 | +wait_for_no_copy_rows tasks title |
| 78 | +wait_for_no_copy_rows task_comments body |
| 79 | +wait_for_no_copy_rows settings name |
| 80 | + |
| 81 | +check_row_count_matches() { |
| 82 | + local table="$1" |
| 83 | + local source_count |
| 84 | + local destination_count |
| 85 | + |
| 86 | + source_count=$(psql -d source -tAc "SELECT COUNT(*) FROM ${table}") |
| 87 | + destination_count=$(psql -d destination -tAc "SELECT COUNT(*) FROM ${table}") |
| 88 | + |
| 89 | + if [ "${source_count}" -ne "${destination_count}" ]; then |
| 90 | + echo "MISMATCH ${table}: source=${source_count} destination=${destination_count}" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + echo "OK ${table}: ${source_count} rows" |
| 95 | +} |
| 96 | + |
| 97 | +check_row_count_matches tenants |
| 98 | +check_row_count_matches accounts |
| 99 | +check_row_count_matches projects |
| 100 | +check_row_count_matches tasks |
| 101 | +check_row_count_matches task_comments |
| 102 | +check_row_count_matches settings |
| 103 | + |
| 104 | +kill -TERM ${PGDOG_PID} |
| 105 | +docker-compose down |
0 commit comments