feat(engine+infra): Implement DB migrations with alembic #6
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check database changes | |
on: | |
pull_request: | |
branches: ["main"] | |
paths: | |
- tracecat/db/** | |
- .github/workflows/check-db-changes.yml | |
permissions: | |
pull-requests: write | |
jobs: | |
check: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python 3.12 | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
cache: "pip" | |
cache-dependency-path: pyproject.toml | |
- name: Run environment setup script | |
run: bash env.sh | |
- name: Spin up latest version database | |
run: docker compose -f docker-compose.yml up --build --no-deps -d api postgres_db | |
- name: Install dependencies | |
run: | | |
pip install alembic==1.13.2 alembic_utils==0.8.4 alembic-postgresql-enum==1.3.0 | |
pip install sqlmodel==0.0.18 | |
pip install fastapi==0.111.0 "fastapi-users[sqlalchemy,oauth]==13.0.0" | |
pip install python-slugify==8.0.4 | |
- name: Check for potential database changes | |
id: db-check | |
env: | |
TRACECAT__POSTGRES_USER: postgres | |
TRACECAT__POSTGRES_PASSWORD: postgres | |
run: | | |
export TRACECAT__DB_URL=postgresql+psycopg://${TRACECAT__POSTGRES_USER}:${TRACECAT__POSTGRES_PASSWORD}@postgres_db:5432/postgres | |
output=$(alembic check) | |
echo "$output" > alembic_check_output.txt | |
if grep -q "No new upgrade operations detected" alembic_check_output.txt; then | |
echo "changes=false" >> $GITHUB_OUTPUT | |
else | |
echo "changes=true" >> $GITHUB_OUTPUT | |
fi | |
- name: Comment PR | |
if: steps.db-check.outputs.changes == 'true' | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const fs = require('fs') | |
const checkOutput = fs.readFileSync('alembic_check_output.txt', 'utf8') | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: 'Alembic has detected potential database changes:\n\n```\n' + checkOutput + '\n```\nPlease review these changes and create a migration if necessary.' | |
}) |