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