-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.sh
executable file
·72 lines (60 loc) · 1.51 KB
/
test.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
#!/bin/sh -e
export PGDATABASE=migration-test
THISDIR=$(dirname "$0")
reset() {
dropdb --if-exists "$PGDATABASE"
createdb "$PGDATABASE"
}
run_migrations() {
psql -q -v ON_ERROR_STOP=1 -1f "$1"
}
assert() {
temp=$(mktemp)
cat <<'EOF' > "$temp"
DO
$body$
BEGIN
EOF
cat >> "$temp"
cat <<'EOF' >> "$temp"
END
$body$;
EOF
psql -q -v ON_ERROR_STOP=1 -f "$temp"
}
fail() {
echo "TEST FAILED" >&2
exit 1;
}
announce() {
echo
echo "---------------------------------------------------"
echo "$@"
echo "---------------------------------------------------"
}
test_file=$(mktemp)
cat "$THISDIR/migrations.sql" > "$test_file"
reset
run_migrations "$test_file"
announce "Checking initial state"
assert <<'EOF'
ASSERT (EXISTS (SELECT FROM pg_catalog.pg_proc WHERE proname = 'apply_migration'));
ASSERT (NOT EXISTS (SELECT FROM pg_catalog.pg_tables WHERE tablename = 'applied_migrations' AND schemaname = 'public'));
EOF
announce "Migrating to create a simple table."
cat <<'EOF' >> $test_file
SELECT apply_migration('create_foo', $$
CREATE TABLE foo ();
$$);
EOF
run_migrations "$test_file"
announce "Checking migration ran and was recorded"
assert <<'EOF'
ASSERT (EXISTS (SELECT 1 FROM applied_migrations WHERE identifier = 'create_foo'));
ASSERT (EXISTS (SELECT FROM pg_catalog.pg_tables WHERE tablename = 'foo' AND schemaname = 'public'));
EOF
announce "Re-running to check idempotency"
run_migrations "$test_file"
assert <<'EOF'
ASSERT (1 = (SELECT COUNT(1) FROM applied_migrations));
EOF