Repository navigation
95 lines (90 loc) · 4.15 KB
/
Copy pathcheck-db-migration-confict.yml
File metadata and controls
95 lines (90 loc) · 4.15 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
name: Check DB migration conflict
on:
push:
paths:
- "superset/migrations/**"
branches:
- "master"
- "[0-9].[0-9]*"
pull_request:
paths:
- "superset/migrations/**"
types: [synchronize, opened, reopened, ready_for_review]
# cancel previous workflow jobs for PRs
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
permissions:
contents: read
jobs:
check_db_migration_conflict:
name: Check DB migration conflict
runs-on: ubuntu-slim
permissions:
contents: read
pull-requests: write
steps:
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Check and notify
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ github.token }}
script: |
// API reference: https://octokit.github.io/rest.js
const currentBranch = context.ref.replace('refs/heads/', '');
// Find all pull requests to current branch
const opts = github.rest.pulls.list.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
base: context.ref,
state: 'open',
sort: 'updated',
per_page: 100,
});
const pulls = await github.paginate(opts);
if (pulls.length > 0) {
console.log(`Found ${pulls.length} open PRs for base branch "${currentBranch}"`)
}
for (const pull of pulls) {
const listFilesOpts = await github.rest.pulls.listFiles.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull.number,
});
const files = await github.paginate(listFilesOpts);
// Match on `filename`, not `contents_url`: the API percent-encodes
// path separators in `contents_url` (`/contents/superset%2Fmigrations%2F...`),
// so a substring test for `/contents/superset/migrations` never matches.
if (
files.some(x => x.filename.startsWith('superset/migrations/'))
) {
console.log(`PR #${pull.number} "${pull.title}" also added db migration`)
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull.number,
body:
`### ⚠️ Possible DB migration conflict\n` +
`\n` +
`@${pull.user.login} the base branch \`${currentBranch}\` picked up a new database ` +
'migration under `superset/migrations/` after this PR was opened, and this PR adds or ' +
'modifies migration files too. Alembic migrations form a single linear chain, so if both ' +
'branch off the same parent revision, merging this PR as-is could leave the migration ' +
'history with two heads instead of one, which makes `superset db upgrade` fail with ' +
'`Multiple head revisions are present`.\n' +
'\n' +
'To check and, if needed, fix this:\n' +
'\n' +
`1. Rebase this branch onto the latest \`${currentBranch}\`.\n` +
'2. Run `superset db heads`. If it lists more than one revision, you have a conflict.\n' +
"3. In your new migration's script, update `down_revision` (and the `Revises` comment) " +
'to point at the other head instead, so the chain has a single head again.\n' +
'\n' +
'See [Merging DB Migrations](https://superset.apache.org/docs/contributing/development#merging-db-migrations) ' +
'in the contributing guide for a worked example.',
});
}
}