|
1 | | -name: Check Migration Order |
| 1 | +name: Migration order |
2 | 2 |
|
3 | 3 | on: |
4 | | - push: |
5 | | - branches: |
6 | | - - main |
7 | 4 | pull_request: |
8 | | - branches: |
9 | | - - main |
10 | | - types: [opened, synchronize] |
| 5 | + paths: |
| 6 | + - "db/migrations/**" |
| 7 | + push: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
11 | 12 |
|
12 | 13 | jobs: |
13 | 14 | check-migration-timestamp: |
14 | | - name: Verify Migration Timestamp |
15 | 15 | runs-on: ubuntu-latest |
16 | 16 | steps: |
17 | | - - name: Checkout code |
18 | | - uses: actions/checkout@v4 |
| 17 | + - uses: actions/checkout@v4 |
19 | 18 | with: |
20 | 19 | fetch-depth: 0 |
21 | 20 |
|
22 | 21 | - name: Verify that new migrations are the most recent |
| 22 | + shell: bash |
23 | 23 | run: | |
24 | | - set -e |
| 24 | + set -euo pipefail |
25 | 25 |
|
26 | 26 | MIGRATION_DIR="db/migrations/" |
27 | 27 |
|
28 | 28 | if [[ "${{ github.event_name }}" == "pull_request" ]]; then |
29 | 29 | BASE_REF="origin/${{ github.base_ref }}" |
30 | 30 | HEAD_REF="${{ github.sha }}" |
31 | | -
|
32 | | - echo "Running on PR. Comparing HEAD with base (${{ github.base_ref }})." |
33 | | - elif [[ "${{ github.event_name }}" == "push" ]]; then |
| 31 | + echo "PR: comparing HEAD with base (${BASE_REF})." |
| 32 | + else |
34 | 33 | BASE_REF="${{ github.before }}" |
35 | 34 | HEAD_REF="${{ github.sha }}" |
36 | | -
|
37 | 35 | if [[ "$BASE_REF" == "0000000000000000000000000000000000000000" ]]; then |
38 | | - echo "This is the first push. No base to compare against. Skipping check." |
| 36 | + echo "First push detected. Skipping migration order check." |
39 | 37 | exit 0 |
40 | 38 | fi |
41 | | -
|
42 | | - echo "Running on push. Comparing current commit with previous ($BASE_REF)." |
| 39 | + echo "Push: comparing ${HEAD_REF} with previous (${BASE_REF})." |
43 | 40 | fi |
44 | 41 |
|
45 | | - LATEST_BASE_TIMESTAMP=$(git ls-tree --name-only $BASE_REF $MIGRATION_DIR 2>/dev/null | xargs -n 1 basename | cut -d '_' -f1 | sort -nr | head -n 1 || echo "0") |
46 | | -
|
47 | | - if [ -z "$LATEST_BASE_TIMESTAMP" ]; then |
48 | | - LATEST_BASE_TIMESTAMP=0 |
49 | | - fi |
| 42 | + LATEST_BASE_TIMESTAMP="$(git ls-tree --name-only "$BASE_REF" "$MIGRATION_DIR" 2>/dev/null \ |
| 43 | + | xargs -n 1 basename \ |
| 44 | + | cut -d '_' -f1 \ |
| 45 | + | sort -nr \ |
| 46 | + | head -n 1 || true)" |
50 | 47 |
|
51 | | - echo "Latest migration timestamp on base branch is: $LATEST_BASE_TIMESTAMP" |
| 48 | + LATEST_BASE_TIMESTAMP="${LATEST_BASE_TIMESTAMP:-0}" |
| 49 | + echo "Latest base migration timestamp: $LATEST_BASE_TIMESTAMP" |
52 | 50 |
|
53 | | - ADDED_FILES=$(git diff --name-only --diff-filter=A $BASE_REF $HEAD_REF -- $MIGRATION_DIR) |
54 | | - if [ -z "$ADDED_FILES" ]; then |
55 | | - echo "No new migration files found. Check passed." |
| 51 | + ADDED_FILES="$(git diff --name-only --diff-filter=A "$BASE_REF" "$HEAD_REF" -- "$MIGRATION_DIR" || true)" |
| 52 | + if [[ -z "$ADDED_FILES" ]]; then |
| 53 | + echo "No new migration files added. OK." |
56 | 54 | exit 0 |
57 | 55 | fi |
58 | 56 |
|
59 | | - echo "Found new migration files to check:" |
| 57 | + echo "New migration files:" |
60 | 58 | echo "$ADDED_FILES" |
61 | 59 |
|
62 | 60 | HAS_ERROR=false |
63 | 61 |
|
64 | 62 | while IFS= read -r file; do |
65 | | - filename=$(basename "$file") |
66 | | - timestamp=$(echo "$filename" | cut -d'_' -f1) |
67 | | - echo "-> Checking '$filename' with timestamp '$timestamp'" |
| 63 | + [[ -z "$file" ]] && continue |
| 64 | + filename="$(basename "$file")" |
| 65 | + timestamp="$(echo "$filename" | cut -d'_' -f1)" |
| 66 | +
|
| 67 | + echo "Checking $filename (timestamp $timestamp)" |
68 | 68 |
|
69 | | - # 1. Check if the timestamp is a valid number |
70 | 69 | if ! [[ "$timestamp" =~ ^[0-9]+$ ]]; then |
71 | | - echo "::error file=$file::Invalid timestamp in filename. Prefix must be numeric." |
| 70 | + echo "::error file=$file::Invalid timestamp prefix; must be numeric." |
72 | 71 | HAS_ERROR=true |
73 | 72 | continue |
74 | 73 | fi |
75 | 74 |
|
76 | | - # 2. Check if the new timestamp is greater than the latest on the base branch |
77 | | - if [ "$timestamp" -le "$LATEST_BASE_TIMESTAMP" ]; then |
78 | | - echo "::error file=$file::Timestamp '$timestamp' is not greater than the latest on the base branch ('$LATEST_BASE_TIMESTAMP')." |
| 75 | + if [[ "$timestamp" -le "$LATEST_BASE_TIMESTAMP" ]]; then |
| 76 | + echo "::error file=$file::Timestamp '$timestamp' must be greater than base '$LATEST_BASE_TIMESTAMP'." |
79 | 77 | HAS_ERROR=true |
80 | 78 | fi |
81 | 79 | done <<< "$ADDED_FILES" |
82 | 80 |
|
83 | | - if [ "$HAS_ERROR" = true ]; then |
84 | | - exit 1 |
85 | | - fi |
| 81 | + $HAS_ERROR && exit 1 |
86 | 82 |
|
87 | | - echo "All new migration files have valid timestamps." |
| 83 | + echo "Migration order OK." |
0 commit comments