Skip to content

Commit

Permalink
Schema diff scripts/workflow (robherley#5)
Browse files Browse the repository at this point in the history
* scripts + workflow to detect schema changes

* test schema changes

* revert test schema changes
  • Loading branch information
robherley authored Apr 17, 2023
1 parent 06f6265 commit 9af0716
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Database Check

on:
pull_request:
paths:
- 'internal/db/schema.hcl'

permissions:
contents: read
pull-requests: write

jobs:
schema-diff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Setup atlas
run: |
script/atlas
- name: Check schema
run: |
echo '## Schema Diff' >> diff-output.md
echo '```sql' >> diff-output.md
script/schema-diff >> diff-output.md
echo '```' >> diff-output.md
- name: Add comment and label
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const body = fs.readFileSync('diff-output.md', {encoding:'utf8'});
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body
});
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['schema-diff']
});
11 changes: 11 additions & 0 deletions script/atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

BINDIR="$(git rev-parse --show-toplevel)"/bin
BINARY=$BINDIR/atlas
ATLAS_VERSION=v0.10.1

if [ ! -f "$BINARY" ]; then
GOBIN=$BINDIR go install ariga.io/atlas/cmd/atlas@${ATLAS_VERSION}
fi

$BINARY "$@"
15 changes: 15 additions & 0 deletions script/schema-diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

TOPDIR="$(git rev-parse --show-toplevel)"
SCRIPTDIR="$TOPDIR"/script
TMPDIR="$TOPDIR"/tmp
MAIN_SCHEMA="$TMPDIR"/schema.hcl
CURRENT_SCHEMA="$TOPDIR"/internal/db/schema.hcl

mkdir -p "$TMPDIR"
curl -sSL https://raw.githubusercontent.com/robherley/snips.sh/main/internal/db/schema.hcl > "$MAIN_SCHEMA"

"$SCRIPTDIR"/atlas schema diff \
--dev-url sqlite://file?mode=memory \
--from file://"$MAIN_SCHEMA" \
--to file://"$CURRENT_SCHEMA"

0 comments on commit 9af0716

Please sign in to comment.