-
Notifications
You must be signed in to change notification settings - Fork 1
112 lines (97 loc) · 3.98 KB
/
migrate.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 }}"