Skip to content

Commit 69ddcb7

Browse files
committed
Add a test that there are no outdated migrations.
If a branch merges with a migration that is timestamped earlier than other migrations already in master, that migration may get skipped.
1 parent e201632 commit 69ddcb7

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ branches:
3434
- master
3535

3636
before_install:
37+
# Travis does shallow clones, so there is no master branch present.
38+
# But test-no-outdated-migrations.sh needs to check diffs against master.
39+
# Fetch just the master branch from origin.
40+
- git fetch origin master
3741
# Github-PR-Status secret
3842
- openssl aes-256-cbc -K $encrypted_53b2630f0fb4_key -iv $encrypted_53b2630f0fb4_iv -in test/github-secret.json.enc -out test/github-secret.json -d || true
3943

test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ check_gofmt() {
178178
run_and_comment check_gofmt
179179
end_context #test/gofmt
180180

181+
start_context "test/migrations"
182+
run_and_comment ./test/test-no-outdated-migrations.sh
183+
end_context "test/migrations"
184+
181185
if [ "${TRAVIS}" == "true" ]; then
182186
./test/create_db.sh || die "unable to create the boulder database with test/create_db.sh"
183187
fi

test/db-common.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
# Common variables used by Goose-related scripts.
2-
set -o errexit
3-
set -o xtrace
4-
52
function die() {
63
if [ ! -z "$1" ]; then
74
echo $1 > /dev/stderr

test/migrate-up.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Run this script after pulling changes that have migrations, to migrate your
44
# local DB.
55
#
6+
set -o errexit
7+
set -o xtrace
68
cd $(dirname $0)/..
79

810
source test/db-common.sh
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
cd $(dirname $0)/..
3+
if [ ! -d .git ]; then
4+
echo "Not in a git repository, skipping out-of-order migration test."
5+
fi
6+
7+
source test/db-common.sh
8+
9+
NEW_FILES_LIST=$(mktemp)
10+
trap 'rm ${NEW_FILES_LIST}' EXIT
11+
12+
for svc in $SERVICES; do
13+
for dbenv in $DBENVS; do
14+
DB_DIR=$svc/_db/
15+
git diff --name-only master -- ${DB_DIR} > ${NEW_FILES_LIST}
16+
# Search for files in the migrations directory match the new files or come
17+
# lexically after them, then filter out the new files.
18+
GREP_OUT="$(ls ${DB_DIR}migrations/* | sort | \
19+
grep -A 1 -xf ${NEW_FILES_LIST} | \
20+
grep -vxf ${NEW_FILES_LIST})"
21+
if [ -n "${GREP_OUT}" ] ; then
22+
echo "--- New migrations on this branch: ---"
23+
cat $NEW_FILES_LIST
24+
echo "--- Existing migrations on master: ---"
25+
echo "${GREP_OUT}"
26+
echo
27+
echo "All migrations on a branch must be timestamped newer than"
28+
echo "migrations on master before they can be merged. Please"
29+
echo "rename migrations as appropriate."
30+
exit 1
31+
fi
32+
done
33+
done

0 commit comments

Comments
 (0)