-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-migration
executable file
·83 lines (63 loc) · 1.86 KB
/
validate-migration
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
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
set -euo pipefail
c_green=$'\033[0;32m'
c_red=$'\033[0;31m'
c_yellow=$'\033[1;33m'
c_reset=$'\033[0m'
initial_data_path=$TMPDIR/cala-db-initial-schema.txt
initial_schema_path=$TMPDIR/cala-db-before-migration.txt
after_migration_schema_path=$TMPDIR/cala-db-after-migration.txt
after_rollback_schema_path=$TMPDIR/cala-db-after-rollback.txt
migrate() {
# Swap this out for your own implementation if you're not using knex
$(npm bin)/knex migrate:latest --knexfile dist/knexfile.js
}
rollback() {
# Swap this out for your own implementation if you're not using knex
$(npm bin)/knex migrate:rollback --knexfile dist/knexfile.js
}
pre() {
echo -n "$@"
}
post() {
echo $c_green Done$c_reset
}
restore_data() {
pre 'Restoring initial schema... '
psql $DATABASE_URL < $initial_schema_path > /dev/null
post
pre 'Restoring initial data... '
psql $DATABASE_URL < $initial_data_path > /dev/null
post
}
fail_elegantly() {
echo $c_red"Unable to complete migration."$c_reset
restore_data
exit 1
}
run() {
pre 'Dumping initial data... '
pg_dump -c $DATABASE_URL > $initial_data_path
post
pre 'Dumping initial schema... '
pg_dump -csOx $DATABASE_URL > $initial_schema_path
post
pre 'Migrating... '
migrate > /dev/null || fail_elegantly
post
pre 'Dumping migrated schema... '
pg_dump -csOx $DATABASE_URL > $after_migration_schema_path
post
pre 'Rolling back... '
rollback > /dev/null || fail_elegantly
post
pre 'Dumping final schema... '
pg_dump -csOx $DATABASE_URL > $after_rollback_schema_path
post
restore_data
echo $c_yellow'Schema difference after migration (should be non-zero):'$c_reset
diff $initial_schema_path $after_migration_schema_path || true
echo $c_yellow'Schema difference after applying and rolling back (should be zero):'$c_reset
diff $initial_schema_path $after_rollback_schema_path
}
run "$@"