#! /bin/bash set -e source ./scripts/common.sh revs=$(git log --format=%h | tac) first_commit="7371374" first_commit_n=$(echo "$revs" | grep -n "$first_commit" | awk -F: '{print $1}') revs=$(echo "empty" && (echo "$revs" | tail --lines "+$first_commit_n")) revs_ct=$(echo "$revs" | wc -l) do_i() { base=$(echo "$revs" | tail --lines "+$1" | head -n 1) if [[ -f "./migrations/${base}_skipped.sql" ]]; then echo "[$base]: skipped previously" return fi for j in $(seq "$(($1 + 1))" "$revs_ct"); do head=$(echo "$revs" | tail --lines "+$j" | head -n 1) migration="./migrations/${base}_to_${head}.sql" if [[ -f "$migration" ]]; then echo "[$migration]: exists" break else echo "[$migration]: building..." fi set +e out=$(./scripts/diff.sh "$base" "$head") status="$?" set -e if [[ "$status" = "1" && "$out" = *"head failed to build"* ]]; then echo "" > "./migrations/${head}_skipped.sql" echo "[$migration]: skipped, $head bad" continue elif [[ "$status" = "1" ]]; then echo "$out" exit 1 else echo "$out" > "$migration" echo "[$migration]: wrote" break fi done } for i in $(seq 1 "$revs_ct"); do do_i "$i" done