forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_migration.sh
More file actions
executable file
·39 lines (31 loc) · 940 Bytes
/
add_migration.sh
File metadata and controls
executable file
·39 lines (31 loc) · 940 Bytes
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
#!/bin/bash
cd $(dirname "${BASH_SOURCE[0]}")/../migrations
set -e
if [ -z "$1" ]; then
echo "USAGE: $0 <name>"
exit 1
fi
# This simulates what "migrate create -ext sql -digits 10 -seq" does.
awkcmd='
BEGIN { FS="_" }
/^[0-9].*\.sql/ { n=$1 }
END {
gsub(/[^A-Za-z0-9]/, "_", name);
printf("%s_%s.up.sql\n", n + 1, name);
printf("%s_%s.down.sql\n", n + 1, name);
}
'
files=$(ls -1 | sort -n | awk -v name="$1" "$awkcmd")
for f in $files; do
cat > $f <<EOF
BEGIN;
-- Insert migration here. See README.md. Highlights:
-- * Always use IF EXISTS. eg: DROP TABLE IF EXISTS global_dep_private;
-- * All migrations must be backward-compatible. Old versions of Sourcegraph
-- need to be able to read/write post migration.
-- * Historically we advised against transactions since we thought the
-- migrate library handled it. However, it does not! /facepalm
COMMIT;
EOF
echo "Created migrations/$f"
done