Skip to content

Commit 8483c33

Browse files
Feat: add auto migrations and notifications (#1777)
* feat: add auto migration at backend startup * feat: add staging migration ping Notifies on discord when staging has a new migration * feat: add production migration ping Notifies on discord about new/no migration for the production deployment
1 parent c6f527b commit 8483c33

3 files changed

Lines changed: 116 additions & 3 deletions

File tree

.github/workflows/deploy-production.yaml

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
default: 'main'
1010

1111
jobs:
12-
deploy-production:
12+
start-notification:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Discord notification
@@ -19,6 +19,75 @@ jobs:
1919
with:
2020
args: "Deploying production Dashboard with tag `${{ github.event.inputs.tag }}`"
2121

22+
check-migrations:
23+
needs: start-notification
24+
runs-on: ubuntu-latest
25+
outputs:
26+
has_new_migrations: ${{ steps.check.outputs.has_new_migrations }}
27+
migration_list: ${{ steps.check.outputs.migration_list }}
28+
steps:
29+
- name: Checkout code
30+
uses: actions/checkout@v4
31+
with:
32+
fetch-depth: 0
33+
34+
- name: Check for new Django migrations
35+
id: check
36+
run: |
37+
# Compare against the previous tag; fall back to HEAD~1 if no tags exist
38+
PREV_TAG=$(git describe --abbrev=0 --tags HEAD^ 2>/dev/null || echo "")
39+
if [ -z "$PREV_TAG" ]; then
40+
echo "No previous tag found, comparing against HEAD~1"
41+
COMPARE_REF="HEAD~1"
42+
else
43+
echo "Comparing migrations against tag: $PREV_TAG"
44+
COMPARE_REF="$PREV_TAG"
45+
fi
46+
47+
NEW_MIGRATIONS=$(git diff --name-only "$COMPARE_REF" HEAD -- '**/migrations/*.py' \
48+
| grep -v '__init__.py' || true)
49+
50+
if [ -n "$NEW_MIGRATIONS" ]; then
51+
echo "has_new_migrations=true" >> $GITHUB_OUTPUT
52+
# Encode newlines for the output so it can be passed between jobs
53+
ENCODED=$(echo "$NEW_MIGRATIONS" | tr '\n' '|')
54+
echo "migration_list=$ENCODED" >> $GITHUB_OUTPUT
55+
echo "New migration files detected:"
56+
echo "$NEW_MIGRATIONS"
57+
else
58+
echo "has_new_migrations=false" >> $GITHUB_OUTPUT
59+
echo "migration_list=" >> $GITHUB_OUTPUT
60+
echo "No new migration files found"
61+
fi
62+
63+
notify-new-migrations:
64+
needs: check-migrations
65+
if: needs.check-migrations.outputs.has_new_migrations == 'true'
66+
runs-on: ubuntu-latest
67+
steps:
68+
- name: Discord notification - new migrations detected
69+
env:
70+
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
71+
uses: Ilshidur/action-discord@master
72+
with:
73+
args: "New Django migrations detected in production deployment of `${{ github.event.inputs.tag }}`:\n`${{ needs.check-migrations.outputs.migration_list }}`"
74+
75+
notify-no-migrations:
76+
needs: check-migrations
77+
if: needs.check-migrations.outputs.has_new_migrations == 'false'
78+
runs-on: ubuntu-latest
79+
steps:
80+
- name: Discord notification - no migrations detected
81+
env:
82+
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
83+
uses: Ilshidur/action-discord@master
84+
with:
85+
args: "No new Django migrations detected in production deployment of `${{ github.event.inputs.tag }}`"
86+
87+
deploy-production:
88+
needs: start-notification
89+
runs-on: ubuntu-latest
90+
steps:
2291
- name: Configure host authenticity
2392
run: |
2493
mkdir -p ~/.ssh/ && chmod 700 ~/.ssh/

.github/workflows/deploy-staging.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,49 @@ on:
44
workflow_call:
55

66
jobs:
7+
check-migrations:
8+
runs-on: ubuntu-latest
9+
outputs:
10+
has_new_migrations: ${{ steps.check.outputs.has_new_migrations }}
11+
migration_list: ${{ steps.check.outputs.migration_list }}
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Check for new Django migrations
19+
id: check
20+
run: |
21+
NEW_MIGRATIONS=$(git diff --name-only HEAD~1 HEAD -- '**/migrations/*.py' \
22+
| grep -v '__init__.py' || true)
23+
24+
if [ -n "$NEW_MIGRATIONS" ]; then
25+
echo "has_new_migrations=true" >> $GITHUB_OUTPUT
26+
ENCODED=$(echo "$NEW_MIGRATIONS" | tr '\n' '|')
27+
echo "migration_list=$ENCODED" >> $GITHUB_OUTPUT
28+
echo "New migration files detected:"
29+
echo "$NEW_MIGRATIONS"
30+
else
31+
echo "has_new_migrations=false" >> $GITHUB_OUTPUT
32+
echo "migration_list=" >> $GITHUB_OUTPUT
33+
echo "No new migration files found"
34+
fi
35+
36+
notify-new-migrations:
37+
needs: check-migrations
38+
if: needs.check-migrations.outputs.has_new_migrations == 'true'
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Discord notification - new migrations detected
42+
env:
43+
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
44+
uses: Ilshidur/action-discord@master
45+
with:
46+
args: "New Django migrations detected in this staging deployment: `${{ needs.check-migrations.outputs.migration_list }}`"
47+
748
deploy-staging:
49+
needs: check-migrations
850
runs-on: ubuntu-latest
951
steps:
1052
- name: Configure staging host authenticity

backend/utils/docker/backend_entrypoint.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ crond start
5555
chmod +x ./migrate-cache-db.sh
5656
./migrate-cache-db.sh
5757

58-
# To update the app db, run MANNUALLY:
59-
# docker compose run --rm backend sh -c "chmod +x ./migrate-app-db.sh && ./migrate-app-db.sh"
58+
# Update the MAIN db
59+
chmod +x ./migrate-app-db.sh
60+
./migrate-app-db.sh
61+
6062

6163
exec "$@"

0 commit comments

Comments
 (0)