-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration-diff.yaml
More file actions
135 lines (122 loc) · 5.07 KB
/
migration-diff.yaml
File metadata and controls
135 lines (122 loc) · 5.07 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# migration-diff.yaml — Post schema diff as a PR comment when migrations change.
#
# Copy this file to your repository's .github/workflows/ directory and
# adjust the "Generate schema" steps to match your migration tooling
# (e.g. pg_dump, Rails db:schema:dump, Flyway, etc.).
#
# If you also want an HTML artifact, add a second relune/action step
# with format: html and a separate output-path, then upload it with
# actions/upload-artifact.
name: Schema Diff
on:
pull_request:
paths:
- "migrations/**"
# Adjust this path to match your migration directory.
# Required for posting PR comments and reading the repository.
permissions:
contents: read
pull-requests: write
jobs:
schema-diff:
runs-on: ubuntu-latest
steps:
# ----------------------------------------------------------------
# 1. Checkout both base and head so we can generate schemas from
# each ref.
# ----------------------------------------------------------------
- name: Checkout HEAD
uses: actions/checkout@v6
- name: Checkout base ref
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
# ----------------------------------------------------------------
# 2. Generate the final schema from each ref.
#
# Replace the commands below with whatever produces a single
# SQL DDL file representing your full database schema.
#
# Examples:
# Rails: bin/rails db:schema:dump SCHEMA=schema.sql
# Flyway: flyway migrate && pg_dump -s > schema.sql
# Manual: cat migrations/*.sql > schema.sql
# ----------------------------------------------------------------
- name: Generate schema (base)
working-directory: base
run: |
# TODO: Replace with your schema generation command.
cat migrations/*.sql > ../base-schema.sql
- name: Generate schema (head)
run: |
# TODO: Replace with your schema generation command.
cat migrations/*.sql > head-schema.sql
# ----------------------------------------------------------------
# 3. Run relune diff
# ----------------------------------------------------------------
- name: Diff schemas (Markdown)
id: diff
uses: mhiro2/relune/action@54221dc4b373100b19f3e8a5d302cfe580844630 # v0.11.0
with:
before: base-schema.sql
after: head-schema.sql
format: markdown
# ----------------------------------------------------------------
# 4. Post PR comment (only when changes are detected)
# ----------------------------------------------------------------
- name: Post diff comment
if: steps.diff.outputs.has-changes == 'true'
uses: actions/github-script@v9
with:
script: |
const fs = require('fs');
const body = fs.readFileSync('${{ steps.diff.outputs.output-path }}', 'utf8');
const marker = '<!-- relune-schema-diff -->';
const fullBody = marker + '\n' + body;
// Find an existing comment with our marker
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c => c.body?.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: fullBody,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: fullBody,
});
}
# ----------------------------------------------------------------
# 5. (Optional) Upload HTML visual diff as an artifact
# ----------------------------------------------------------------
# - name: Diff schemas (HTML)
# id: diff-html
# uses: mhiro2/relune/action@54221dc4b373100b19f3e8a5d302cfe580844630 # v0.11.0
# with:
# before: base-schema.sql
# after: head-schema.sql
# format: html
#
# - name: Upload HTML diff
# if: steps.diff-html.outputs.has-changes == 'true'
# uses: actions/upload-artifact@v4
# with:
# name: schema-diff
# path: ${{ steps.diff-html.outputs.output-path }}
# NOTE: This workflow uses `on: pull_request`, which runs in the context
# of the head branch. For fork PRs the GITHUB_TOKEN has read-only access
# to the base repository, so the comment step will fail.
#
# If you need to support fork PRs, consider splitting into two workflows:
# one that generates the diff artifact (pull_request) and one that posts
# the comment (workflow_run with pull-requests: write permission).