Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-47507: Add workflow to automatically generate migration scripts #53

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/migrate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Handle Migration Trigger

on:
repository_dispatch:
types: [migration]
workflow_dispatch:
inputs:
branch_name:
description: 'Branch name in sdm_schemas to migrate'
required: true

jobs:
generate-migration:
runs-on: ubuntu-latest

steps:
- name: Checkout consdb repo
uses: actions/checkout@v2
with:
path: consdb
fetch-depth: 0

- name: Setup python
uses: actions/setup-python@v2
with:
python-version: '3.12.7'

- name: Install dependencies
run: |
python -m pip install --upgrade pip uv
uv pip install --system lsst-felis testing.postgresql alembic sqlalchemy pyyaml black

- name: Determine branch name
run: |
if [ -n "${{ github.event.inputs.branch_name }}" ]; then
echo "BRANCH_NAME=${{ github.event.inputs.branch_name }}" >> $GITHUB_ENV
elif [ -n "${{ github.event.client_payload.source_branch }}" ]; then
echo "BRANCH_NAME=${{ github.event.client_payload.source_branch }}" >> $GITHUB_ENV
else
echo "Error: No branch name provided." >&2
exit 1
fi
echo "Using sdm_schemas branch: ${{ env.BRANCH_NAME }}"

- name: Checkout sdm_schemas repo
uses: actions/checkout@v2
with:
repository: 'github.com/lsst/sdm_schemas'
ref: ${{ env.BRANCH_NAME }}
path: sdm_schemas
fetch-depth: 0

- name: Set sdm_schemas path in environment
run: |
echo "SDM_SCHEMAS_DIR=${GITHUB_WORKSPACE}/sdm_schemas" >> $GITHUB_ENV

- name: Run migration script
run: |
cd $GITHUB_WORKSPACE/consdb
python alembic-autogenerate.py ${{ env.BRANCH_NAME }}

- name: Commit migration changes
run: |
cd $GITHUB_WORKSPACE/consdb
git checkout -b ${{ env.BRANCH_NAME }}-migrate
git config --global user.email "[email protected]"
git config --global user.name "Jeremy McCormick"
git add .
git commit -m "Migrate schema changes from ${{ env.BRANCH_NAME }}"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:${{ env.BRANCH_NAME }}-migrate

- name: Create PR for migration
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "Migrate schema changes from ${{ env.BRANCH_NAME }}"
body: "This PR migrates schema changes from ${{ env.BRANCH_NAME }} to the database."
branch: ${{ env.BRANCH_NAME }}-migrate
base: main
3 changes: 2 additions & 1 deletion alembic-autogenerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# 3. Heed the message at the end to revise your auto-generated code as needed.
#

import glob

Check failure on line 17 in alembic-autogenerate.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F401

'glob' imported but unused
import os
import re

Check failure on line 19 in alembic-autogenerate.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F401

're' imported but unused
import sys

from alembic.config import Config
Expand All @@ -42,7 +42,8 @@

# Loop over each of the instruments
pattern = os.environ["SDM_SCHEMAS_DIR"] + "/yml/cdb_*.yaml"
instruments = [re.search(r"cdb_(.+)\.yaml$", file).group(1) for file in glob.glob(pattern)]
# instruments = [re.search(r"cdb_(.+)\.yaml$", file).group(1) for file in glob.glob(pattern)]

Check failure on line 45 in alembic-autogenerate.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

W505

doc line too long (93 > 79 characters)
instruments = ["latiss", "lsstcam", "lsstcomcam", "lsstcomcamsim"]
for instrument in instruments:
# Set up a temporary PostgreSQL instance using testing.postgresql
with setup_postgres_test_db() as instance:
Expand Down
Loading