Skip to content

Commit

Permalink
Diff Schema locally (#3047)
Browse files Browse the repository at this point in the history
* work

* fix

* fix

* checkout current commit

* fix
  • Loading branch information
mgyucht authored Dec 20, 2023
1 parent 8191f6c commit 02b4d2c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 103 deletions.
107 changes: 4 additions & 103 deletions .github/workflows/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ on:
required: true

jobs:
compute_current:
name: "Generate current"
compute_diff:
runs-on: ubuntu-latest

steps:
- if: github.event_name == 'pull_request'
name: Checkout
uses: actions/checkout@v4
with:
# Checkout main branch to generate schema for current release
ref: master

- if: github.event_name == 'workflow_dispatch'
name: Checkout
Expand All @@ -44,102 +40,7 @@ jobs:
with:
terraform_wrapper: false

- run: make install

- name: Generate provider schema
shell: bash
run: |
set -ex
cd /tmp
cat > main.tf <<EOF
terraform {
required_providers {
databricks = {
source = "databricks/databricks"
}
}
}
EOF
terraform init
terraform providers schema -json > provider.json
- name: 'Upload provider schema'
uses: actions/upload-artifact@v3
with:
name: schema-current
path: /tmp/provider.json
retention-days: 1

compute_new:
name: "Generate new"
runs-on: ubuntu-latest

steps:
- if: github.event_name == 'pull_request'
name: Checkout
uses: actions/checkout@v4

- if: github.event_name == 'workflow_dispatch'
name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.head }}

- name: 'Setup Go'
uses: actions/setup-go@v4
with:
go-version: 1.21.x

- name: 'Setup Terraform'
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false

- run: make install

- name: Generate provider schema
shell: bash
run: |
set -ex
cd /tmp
cat > main.tf <<EOF
terraform {
required_providers {
databricks = {
source = "databricks/databricks"
}
}
}
EOF
terraform init
terraform providers schema -json > provider.json
- name: 'Upload provider schema'
uses: actions/upload-artifact@v3
with:
name: schema-new
path: /tmp/provider.json
retention-days: 1

diff:
needs: [compute_current, compute_new]

name: "Compute diff"
runs-on: ubuntu-latest

steps:
- name: 'Setup Go'
uses: actions/setup-go@v4
with:
go-version: 1.21.x
cache: false

- run: go install github.com/josephburnett/jd@latest

- name: 'Download provider schemas'
uses: actions/download-artifact@v3

- run: ls -l schema*/*
- name: 'Install jd'
run: go install github.com/josephburnett/jd@latest

- run: jd -color schema-current/provider.json schema-new/provider.json
continue-on-error: true
- run: make diff-schema
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ test-preview: install
docker-it:
docker build -t databricks-terrafrom/test -f scripts/Dockerfile .

schema:
@/bin/bash scripts/print-schema.sh

diff-schema:
@/bin/bash scripts/diff-schema.sh

.PHONY: build fmt python-setup docs vendor build fmt coverage test lint
30 changes: 30 additions & 0 deletions scripts/diff-schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

source scripts/libschema.sh

BASE_BRANCH="master"

checkout_branch() {
local branch=$1
echo "Checking out branch: $branch"
git checkout $branch
}

if [ -n "$(git status --porcelain)" ]; then
echo "There are uncommitted changes. Please commit them before running this script."
exit 1
fi

CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
NEW_SCHEMA=$(generate_schema)
checkout_branch $BASE_BRANCH
CURRENT_SCHEMA=$(generate_schema)
checkout_branch $CURRENT_BRANCH

set +e
jd -color "$CURRENT_SCHEMA" "$NEW_SCHEMA"
RES=$?
set -e
if [ $RES -eq 0 ]; then
echo "No schema changes detected."
fi
26 changes: 26 additions & 0 deletions scripts/libschema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

# Function to generate provider schema
generate_schema() {
>&2 make install
version=$(./terraform-provider-databricks version)

local TMPDIR=$(mktemp -d)
>&2 echo "Generating provider schema in $TMPDIR..."
>&2 pushd $TMPDIR
cat > main.tf <<EOF
terraform {
required_providers {
databricks = {
source = "databricks/databricks"
version = "$version"
}
}
}
EOF
>&2 terraform init
terraform providers schema -json > schema.json
>&2 popd
>&2 echo "Provider schema available in $TMPDIR/schema.json"
echo "$TMPDIR/schema.json"
}
5 changes: 5 additions & 0 deletions scripts/print-schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

source scripts/libschema.sh

cat $(generate_schema) | jq

0 comments on commit 02b4d2c

Please sign in to comment.