Skip to content

Commit a33f9a3

Browse files
committed
feat: Add migration fixer script (for branches)
1 parent 8a6635b commit a33f9a3

File tree

1 file changed

+95
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)