Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema diff scripts/workflow #5

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"