diff --git a/.github/workflows/migrate.yaml b/.github/workflows/migrate.yaml new file mode 100644 index 00000000..ca598205 --- /dev/null +++ b/.github/workflows/migrate.yaml @@ -0,0 +1,112 @@ +name: Generate migration scripts + +on: + repository_dispatch: + types: [migration] + workflow_dispatch: + inputs: + branch_name: + description: 'Branch name in sdm_schemas to migrate' + required: true + commit_sha: + description: 'Commit SHA in sdm_schemas to migrate' + required: true + +jobs: + generate-migration: + runs-on: ubuntu-latest + + steps: + - name: Checkout consdb repo + uses: actions/checkout@v4 + 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 psycopg2-binary + + - name: Determine branch name and commit SHA + run: | + if [ "${{ github.event_name }}" == "repository_dispatch" ]; then + echo "BRANCH_NAME=${{ github.event.client_payload.branch_name }}" >> $GITHUB_ENV + echo "COMMIT_SHA=${{ github.event.client_payload.commit_sha }}" >> $GITHUB_ENV + elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then + echo "BRANCH_NAME=${{ github.event.inputs.branch_name }}" >> $GITHUB_ENV + echo "COMMIT_SHA=${{ github.event.inputs.commit_sha }}" >> $GITHUB_ENV + fi + echo "Branch name: ${{ env.BRANCH_NAME }}" + echo "Commit SHA: ${{ env.COMMIT_SHA }}" + if [ -z "${{ env.BRANCH_NAME }}" ] || [ -z "${{ env.COMMIT_SHA }}" ]; then + echo "Error: Branch name and commit SHA must be provided." >&2 + exit 1 + fi + + - name: Check branch name + run: | + if [[ ! "${{ env.BRANCH_NAME }}" =~ ^tickets/DM-[0-9]+(-[a-zA-Z0-9-]*)?$ ]]; then + echo "Bad branch name: ${{ env.BRANCH_NAME }}" >&2 + echo "Error: Migrations are only generated for branches matching the pattern 'tickets/DM-[number](-[alphanumeric-]*)'." >&2 + exit 1 + fi + + - name: Set ticket name + run: | + TICKET_NAME=$(echo ${{ env.BRANCH_NAME }} | sed -E 's/tickets\/(DM-[0-9]+).*/\1/') + echo "TICKET_NAME=${TICKET_NAME}" >> $GITHUB_ENV + if [[ ! "${{ env.TICKET_NAME }}" =~ ^DM-[0-9]+$ ]]; then + echo "Error: TICKET_NAME does not match the expected pattern 'DM-[number]'." >&2 + exit 1 + fi + + - name: Checkout sdm_schemas repo + uses: actions/checkout@v4 + with: + repository: lsst/sdm_schemas + ref: ${{ env.COMMIT_SHA }} + path: sdm_schemas + + - name: Set sdm_schemas path in environment + run: | + echo "SDM_SCHEMAS_DIR=${GITHUB_WORKSPACE}/sdm_schemas" >> $GITHUB_ENV + + - name: Generate the migration scripts + working-directory: $GITHUB_WORKSPACE/consdb + run: | + python alembic-autogenerate.py ${{ env.TICKET_NAME }} + if git diff --quiet; then + echo "No changes detected." + exit 0 + fi + + - name: Create PR for the migration + id: cpr + uses: peter-evans/create-pull-request@v3 + with: + path: consdb + token: ${{ secrets.GITHUB_TOKEN }} + title: "${{ env.TICKET_NAME }}: Migrate schema changes" + commit-message: Migrate schema changes from ${{ env.TICKET_NAME }} + body: | + This PR migrates schema changes from [${{ env.TICKET_NAME }}](https://ls.st/${{ env.TICKET_NAME }}) to the database. + + ## Checklist + + - [ ] Verified the automatically generated migration scripts + branch: ${{ env.BRANCH_NAME }}-migrate + base: main + draft: true + labels: migration + + - name: Check PR outputs + if: ${{ steps.cpr.outputs.pull-request-number }} + run: | + echo "Pull Request Number: ${{ steps.cpr.outputs.pull-request-number }}" + echo "Pull Request URL: ${{ steps.cpr.outputs.pull-request-url }}"