Skip to content

Commit 3b8f387

Browse files
committed
Fix generating comment about new DB revisions
1 parent a3e4adf commit 3b8f387

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

.github/workflows/migration-sql-comment.yml

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ jobs:
4242
id: get-data
4343
run: |
4444
echo "pull_id=$(</tmp/migration-sql-data/pull-request-id)" >> $GITHUB_OUTPUT
45-
if [ -f /tmp/migration-sql-data/upgrade.sql ]; then
46-
sql_command=$(</tmp/migration-sql-data/upgrade.sql)
47-
if [ ! -z "$sql_command" ] ; then
48-
echo "sql=$sql_command" >> $GITHUB_OUTPUT
49-
fi
45+
if [ -s </tmp/migration-sql-data/comment.md ]; then
46+
echo "has_data=true" >> $GITHUB_OUTPUT
47+
else
48+
echo "has_data=false" >> $GITHUB_OUTPUT
5049
fi
51-
5250
- name: Find previous comment
5351
uses: peter-evans/find-comment@v3
5452
id: find-comment
@@ -59,24 +57,15 @@ jobs:
5957

6058
- name: Create comment
6159
uses: peter-evans/create-or-update-comment@v4
62-
if: steps.get-data.outputs.sql
60+
if: steps.get-data.outputs.has_data
6361
with:
6462
issue-number: ${{ steps.get-data.outputs.pull_id }}
6563
comment-id: ${{ steps.find-comment.outputs.comment-id }}
6664
edit-mode: replace
67-
body: |
68-
This PR contains database changes. Before merging it, make sure to apply the migration in production:
69-
70-
```sql
71-
${{ steps.get-data.outputs.sql }}
72-
```
73-
74-
When reviewing the PR, make sure that the changes will not break the previously deployed
75-
version, i.e. any new column needs to have a `server_default` or be nullable.
76-
65+
body-file: /tmp/migration-sql-data/comment.md
7766
- name: Delete comment
7867
uses: peter-evans/create-or-update-comment@v4
79-
if: steps.find-comment.outputs.comment-id && !steps.get-data.outputs.sql
68+
if: steps.find-comment.outputs.comment-id && !steps.get-data.outputs.has_data
8069
with:
8170
issue-number: ${{ steps.get-data.outputs.pull_id }}
8271
comment-id: ${{ steps.find-comment.outputs.comment-id }}

.github/workflows/migration-sql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
run: |
5757
first_migration=$(echo '${{ steps.get-changed-files.outputs.files_created }}' | jq -r '.[0] // empty')
5858
latest_migration=$(echo '${{ steps.get-changed-files.outputs.files_created }}' | jq -r '.[-1] // empty')
59-
./ci/print_revision_sql.sh $first_migration $latest_migration > /tmp/migration-sql-data/upgrade.sql
59+
./ci/print_revision_sql.sh $first_migration $latest_migration > /tmp/migration-sql-data/comment.md
6060
6161
- name: Create artifact
6262
uses: actions/upload-artifact@v4

ci/print_revision_sql.sh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
#!/bin/bash
22
# Get all the SQL queries from the first migration file to the last, in order
3-
# and print them with escaped newlines
3+
# and print them surrounded by a markdown comment for github
44
set -e
55

6-
76
migration_file=$1
87
latest_migration=$2
98

10-
if [ ! -z ${migration_file} ] && [ ! -z ${latest_migration} ]; then
11-
down_revision=$(grep -Po "(?<=^down_revision = ').*(?=')" $migration_file )
12-
revision=$(grep -Po "(?<=^revision = ').*(?=')" $latest_migration )
9+
if [ ! -z ${migration_file} ] && [ ! -z ${latest_migration} ]; then
10+
down_revision=$(grep -Po "(?<=^down_revision = ').*(?=')" $migration_file)
11+
revision=$(grep -Po "(?<=^revision = ').*(?=')" $latest_migration)
12+
1313
sql_command=$(flask db upgrade --sql ${down_revision}:${revision})
14-
sql_command="${sql_command//'%'/'%25'}"
15-
sql_command="${sql_command//$'\n'/'%0A'}"
16-
sql_command="${sql_command//$'\r'/'%0D'}"
17-
echo $sql_command
18-
else
14+
[ -z "$sql_command" ] && exit 0
15+
16+
cat << EOF
17+
This PR contains database changes. Before merging it, make sure to apply the migration in production:
18+
19+
\`\`\`sql
20+
${sql_command}
21+
\`\`\`
22+
23+
When reviewing the PR, make sure that the changes will not break the previously deployed
24+
version, i.e. any new column needs to have a \`server_default\` or be nullable.
25+
EOF
26+
else
1927
exit 0
2028
fi

0 commit comments

Comments
 (0)