diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6f25141e77..7e0916cbe4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -114,6 +114,23 @@ jobs: # - typecheck next (package pretypecheck emits SDK .d.ts; core resolves from src/) # - lint after typecheck (type-aware eslint reuses those declarations) # - build last (full emit; SDK JS built once via the root graph, still always-on as a ship gate) + migration-order: + name: Migration Order + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Check out repository code + uses: actions/checkout@v7 + with: + fetch-depth: 0 + + - name: Refresh main branch reference + run: git fetch --no-tags origin main + + - name: Check migration ordering + run: bash scripts/check-migration-order.sh + typecheck-and-build: name: Format, Typecheck, Lint and Build runs-on: ubuntu-latest @@ -268,6 +285,7 @@ jobs: if: always() needs: - detect-changes + - migration-order - typecheck-and-build - package-checks - chart-checks @@ -278,14 +296,16 @@ jobs: - name: Decide whether the needed jobs succeeded or failed env: DETECT_CHANGES: ${{ needs.detect-changes.result }} + MIGRATION_ORDER: ${{ needs.migration-order.result }} TYPECHECK_AND_BUILD: ${{ needs.typecheck-and-build.result }} PACKAGE_CHECKS: ${{ needs.package-checks.result }} CHART_CHECKS: ${{ needs.chart-checks.result }} STORE_TESTS: ${{ needs.store-tests.result }} run: | ok() { [ "$1" = success ] || [ "$1" = skipped ]; } - echo "detect-changes=$DETECT_CHANGES typecheck-and-build=$TYPECHECK_AND_BUILD package-checks=$PACKAGE_CHECKS chart-checks=$CHART_CHECKS store-tests=$STORE_TESTS" + echo "detect-changes=$DETECT_CHANGES migration-order=$MIGRATION_ORDER typecheck-and-build=$TYPECHECK_AND_BUILD package-checks=$PACKAGE_CHECKS chart-checks=$CHART_CHECKS store-tests=$STORE_TESTS" [ "$DETECT_CHANGES" = success ] || exit 1 + [ "$MIGRATION_ORDER" = success ] || exit 1 [ "$TYPECHECK_AND_BUILD" = success ] || exit 1 ok "$PACKAGE_CHECKS" || exit 1 ok "$CHART_CHECKS" || exit 1 diff --git a/scripts/check-migration-order.sh b/scripts/check-migration-order.sh new file mode 100644 index 0000000000..96a9bb78c5 --- /dev/null +++ b/scripts/check-migration-order.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +set -euo pipefail + +main_ref="${MIGRATION_BASE_REF:-origin/main}" +migration_dirs=( + "packages/trueforge/src/db/postgres/migrations" + "packages/trueforge/src/db/sqlite/migrations" +) + +latest_main_migration="$( + git ls-tree -r --name-only "$main_ref" -- "${migration_dirs[@]}" \ + | awk -F/ '{ print $NF }' \ + | sort \ + | tail -n 1 +)" + +added_migrations="$( + git diff --name-only --diff-filter=A "$main_ref...HEAD" -- "${migration_dirs[@]}" \ + | awk -F/ '{ print $NF }' \ + | sort +)" + +if [ -z "$added_migrations" ]; then + echo "No new migrations added relative to $main_ref" + exit 0 +fi + +invalid_migrations="$( + while IFS= read -r migration; do + [ -n "$migration" ] || continue + if [[ "$migration" < "$latest_main_migration" || "$migration" == "$latest_main_migration" ]]; then + echo "$migration" + fi + done <<< "$added_migrations" +)" + +if [ -z "$invalid_migrations" ]; then + echo "All added migrations sort after $main_ref ($latest_main_migration)" + exit 0 +fi + +echo "Latest migration on $main_ref: $latest_main_migration" >&2 +echo "Added migrations must sort strictly after it:" >&2 +while IFS= read -r migration; do + [ -n "$migration" ] || continue + echo "- $migration" >&2 +done <<< "$invalid_migrations" +exit 1