Open2

GitHub Actionsでtblsを動かす(PostgreSQL + psqldef)

シロシロ

概要

DBのスキーマを変更した際、自動的にテーブル定義を作成しコミットする設定を行う

環境

ディレクトリ構成

db/
  db/schema/schema.sql
  db/dbdoc/
  db/tbls.yml

設定ファイル

  • .github/workflows/tbls.yml
name: tbls
on:
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
    paths:
      - db/schema/**
      - db/tbls.yml
    branches-ignore:
      - production
      - staging

jobs:
  tbls:
    name: generate-and-push
    runs-on: ubuntu-latest
    permissions:
      contents: write # github-actions[bot]のcommit&pushに必要
      pull-requests: write # pull requestのコメントを記載に必要
    services:
      postgres:
        image: postgres:15-alpine
        ports:
          - 5432:5432
        env:
          POSTGRES_HOST_AUTH_METHOD: trust
          POSTGRES_DB: sampledbname
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      TBLS_DSN: "postgres://postgres:@127.0.0.1:5432/sampledbname?sslmode=disable"
      TBLS_DOC_PATH: "db/dbdoc"
    steps:
      - uses: actions/checkout@v3
        with:
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}
      - uses: actions/setup-go@v3
        with:
          go-version: ^1.20
      - name: Execute migration
        run: |
          go install github.com/k0kubun/sqldef/cmd/psqldef@latest
          psqldef --file schema.sql sampledbname
        working-directory: ./db/schema
      - name: Remove dbdocs
        run: |
          rm -rf ./*.md ./*.svg
        working-directory: ./db/dbdoc
      - uses: k1low/setup-tbls@v1
      - name: Run tbls for generate database document
        run: |
          tbls doc -c ./db/tbls.yml -f
      - name: Count uncommit files
        id: check_diff
        run: |
          git status --porcelain | wc -l
          file_count=$(git status --porcelain | wc -l)
          echo "file_count=$file_count" >> $GITHUB_OUTPUT
        working-directory: ./db
      - name: Commit ER graph
        if: ${{ steps.check_diff.outputs.file_count != '0' }}
        uses: EndBug/add-and-commit@v9
        with:
          default_author: github_actions
          message: "CI: ER diagram and markdown update triggered by actions"
      - name: Report commit on pull request
        if: ${{ steps.check_diff.outputs.file_count != '0' }}
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'CI: ER diagrams and markdown auto-committed by Actions 🤖'
            })