Skip to content

Add a GitHub workflow to comment the SQL generated by a migration in PRs #26

Add a GitHub workflow to comment the SQL generated by a migration in PRs

Add a GitHub workflow to comment the SQL generated by a migration in PRs #26

# Generate migration as SQL if necessary
# https://github.com/actions/github-script#comment-on-an-issue
name: "Migration as SQL"
on:
workflow_dispatch:
pull_request:
paths:
- alembic/versions/**
jobs:
comment:
runs-on: ubuntu-22.04
permissions:
pull-requests: write
steps:
- name: "Checkout repository"
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
# needed to diff with the main branch later
fetch-depth: 0
- name: "Setup PDM"
uses: pdm-project/setup-pdm@568ddd69406b30de1774ec0044b73ae06e716aa4 # v4
with:
python-version: "3.12"
cache: true
- name: "Install dependencies"
run: pdm install --dev
- name: "Generate SQL"
id: "sql"
run: |
#!/bin/bash
set -euo pipefail
# we need to find the down revision of the first migration made in this PR,
# which should be the head of main. to do this, we are going to import the
# migration file which will set globals that we can easily read
#
# see https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
# input will be newline separated paths to revision files
git diff --name-only origin/main -- alembic/versions | pdm run python -c '
import importlib.util
import sys
down_revs = set()
revs = set()
# git diff outputs an extra newline at the end
for i, path in enumerate(sys.stdin.read().rstrip().split("\n")):
spec = importlib.util.spec_from_file_location(f"rev", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
down_revs.add(module.down_revision)
revs.add(module.revision)
# the previous head revision is the down revision that does not appear
# in the list of revisions changed in this PR
head ,= down_revs - revs
print(head)
' > phr
previous_head_revision=$(cat phr)
# `alembic upgrade --sql` outputs logs to stderr and the sql to stdout, so
# make temp files to get both parts
pdm run alembic upgrade --sql $previous_head_revision:head > sql 2> summary
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "MIGRATION<<$EOF" >> $GITHUB_OUTPUT
echo "$(cat summary sql)" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
pdm run alembic downgrade --sql head:$previous_head_revision > sql 2> summary
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "DOWN_MIGRATION<<$EOF" >> $GITHUB_OUTPUT
echo "$(cat summary sql)" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
- name: "Comment on PR"
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # 7.0.1
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## \`alembic upgrade --sql $prev_head:head\`
\`\`\`
${{ steps.sql.outputs.MIGRATION }}
\`\`\`
## \`alembic downgrade --sql head:$prev_head\`
\`\`\`
${{ steps.sql.outputs.DOWN_MIGRATION }}
\`\`\`
`
})