Skip to content

Commit 0c2b432

Browse files
authored
feat: Add migration fixer script (for branches) (#6466)
1 parent fd02f73 commit 0c2b432

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
5+
6+
list_migrations() {
7+
branch=$1
8+
git ls-tree -r --name-only "${branch}" -- . | grep -E '[0-9]{6}.*' | sort -n
9+
}
10+
11+
main() {
12+
cd "${SCRIPT_DIR}"
13+
14+
origin=$(git remote -v | grep "github.com[:/]coder/coder.*(fetch)" | cut -f1)
15+
16+
echo "Fetching ${origin}/main..."
17+
git fetch -u "${origin}" main
18+
19+
curr_num=$(
20+
set -e
21+
list_migrations "${origin}"/main | grep '^[0-9]' | tail -n1
22+
)
23+
echo "Last migration (main): ${curr_num}"
24+
next_num=$(("1${curr_num:0:6}" - 1000000 + 1))
25+
curr_num=$(printf "%06d" "${next_num}")
26+
echo "Next migration number: ${curr_num}"
27+
28+
main_files="$(
29+
set -e
30+
list_migrations "${origin}"/main
31+
)"
32+
head_files="$(
33+
set -e
34+
list_migrations HEAD
35+
)"
36+
37+
declare -A prefix_map=()
38+
declare -a git_add_files=()
39+
40+
# Renumber migrations part of this branch (as compared to main)
41+
diff_files="$(diff -u <(echo "${main_files}") <(echo "${head_files}") | sed -E -ne 's;^\+(0.*);\1;p' | sort -n || true)"
42+
if [[ -z "${diff_files}" ]]; then
43+
echo "No migrations to rename, exiting."
44+
return
45+
fi
46+
while read -r file; do
47+
old_file="${file}"
48+
dir=$(dirname "${file}")
49+
file=$(basename "${file}")
50+
num="${file:0:6}"
51+
set +u
52+
new_num="${prefix_map["${num}"]}"
53+
set -u
54+
if [[ -z "${new_num}" ]]; then
55+
new_num="${curr_num}"
56+
prefix_map["${num}"]="${new_num}"
57+
next_num=$((next_num + 1))
58+
curr_num=$(printf "%06d" "${next_num}")
59+
fi
60+
name="${file:7:-4}"
61+
new_file="${new_num}_${name}.sql"
62+
echo "Renaming ${old_file} to ${new_file}"
63+
mv "${old_file}" "${new_file}"
64+
git_add_files+=("${new_file}" "${old_file}")
65+
done <<<"${diff_files}"
66+
67+
# Renumber fixtures if there's a matching migration in this branch (as compared to main).
68+
diff_files="$(diff -u <(echo "${main_files}") <(echo "${head_files}") | sed -E -ne 's;^\+(testdata/[^/]*/0.*);\1;p' | sort -n || true)"
69+
if [[ -z "${diff_files}" ]]; then
70+
echo "No testdata fixtures to rename, skipping."
71+
return
72+
fi
73+
while read -r file; do
74+
old_file="${file}"
75+
dir=$(dirname "${file}")
76+
file=$(basename "${file}")
77+
num="${file:0:6}"
78+
set +u
79+
new_num="${prefix_map["${num}"]}"
80+
set -u
81+
if [[ -z "${new_num}" ]]; then
82+
echo "Skipping ${old_file}, no matching migration in ${SCRIPT_DIR}"
83+
continue
84+
fi
85+
name="${file:7:-4}"
86+
new_file="${dir}/${new_num}_${name}.sql"
87+
echo "Renaming ${old_file} to ${new_file}"
88+
mv "${old_file}" "${new_file}"
89+
git_add_files+=("${new_file}" "${old_file}")
90+
done <<<"${diff_files}"
91+
92+
git add "${git_add_files[@]}"
93+
git status
94+
echo "Run 'git commit' to commit the changes."
95+
}
96+
97+
(main "$@")

0 commit comments

Comments
 (0)