Add a GitHub workflow to comment the SQL generated by a migration in PRs #25
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
# 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-latest | |
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 | |
git diff --name-only origin/main -- alembic/versions | xargs pdm run python -c ' | |
import importlib.util | |
import sys | |
spec = importlib.util.spec_from_file_location("rev", sys.argv[1]) | |
module = importlib.util.module_from_spec(spec) | |
spec.loader.exec_module(module) | |
print(f"{module.revision},{module.down_revision}") | |
' | pdm run python -c ' | |
import sys | |
down_revs = set() | |
revs = set() | |
lines = (line.split(",") for line in sys.stdin) | |
for revision, down_revision in lines: | |
down_revs.add(down_revision) | |
revs.add(revision) | |
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 }} | |
\`\`\` | |
` | |
}) |