Skip to content

Commit 02b4d2c

Browse files
authored
Diff Schema locally (#3047)
* work * fix * fix * checkout current commit * fix
1 parent 8191f6c commit 02b4d2c

File tree

5 files changed

+71
-103
lines changed

5 files changed

+71
-103
lines changed

.github/workflows/schema.yml

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ on:
1616
required: true
1717

1818
jobs:
19-
compute_current:
20-
name: "Generate current"
19+
compute_diff:
2120
runs-on: ubuntu-latest
2221

2322
steps:
2423
- if: github.event_name == 'pull_request'
2524
name: Checkout
2625
uses: actions/checkout@v4
27-
with:
28-
# Checkout main branch to generate schema for current release
29-
ref: master
3026

3127
- if: github.event_name == 'workflow_dispatch'
3228
name: Checkout
@@ -44,102 +40,7 @@ jobs:
4440
with:
4541
terraform_wrapper: false
4642

47-
- run: make install
48-
49-
- name: Generate provider schema
50-
shell: bash
51-
run: |
52-
set -ex
53-
cd /tmp
54-
cat > main.tf <<EOF
55-
terraform {
56-
required_providers {
57-
databricks = {
58-
source = "databricks/databricks"
59-
}
60-
}
61-
}
62-
EOF
63-
terraform init
64-
terraform providers schema -json > provider.json
65-
66-
- name: 'Upload provider schema'
67-
uses: actions/upload-artifact@v3
68-
with:
69-
name: schema-current
70-
path: /tmp/provider.json
71-
retention-days: 1
72-
73-
compute_new:
74-
name: "Generate new"
75-
runs-on: ubuntu-latest
76-
77-
steps:
78-
- if: github.event_name == 'pull_request'
79-
name: Checkout
80-
uses: actions/checkout@v4
81-
82-
- if: github.event_name == 'workflow_dispatch'
83-
name: Checkout
84-
uses: actions/checkout@v4
85-
with:
86-
ref: ${{ github.event.inputs.head }}
87-
88-
- name: 'Setup Go'
89-
uses: actions/setup-go@v4
90-
with:
91-
go-version: 1.21.x
92-
93-
- name: 'Setup Terraform'
94-
uses: hashicorp/setup-terraform@v2
95-
with:
96-
terraform_wrapper: false
97-
98-
- run: make install
99-
100-
- name: Generate provider schema
101-
shell: bash
102-
run: |
103-
set -ex
104-
cd /tmp
105-
cat > main.tf <<EOF
106-
terraform {
107-
required_providers {
108-
databricks = {
109-
source = "databricks/databricks"
110-
}
111-
}
112-
}
113-
EOF
114-
terraform init
115-
terraform providers schema -json > provider.json
116-
117-
- name: 'Upload provider schema'
118-
uses: actions/upload-artifact@v3
119-
with:
120-
name: schema-new
121-
path: /tmp/provider.json
122-
retention-days: 1
123-
124-
diff:
125-
needs: [compute_current, compute_new]
126-
127-
name: "Compute diff"
128-
runs-on: ubuntu-latest
129-
130-
steps:
131-
- name: 'Setup Go'
132-
uses: actions/setup-go@v4
133-
with:
134-
go-version: 1.21.x
135-
cache: false
136-
137-
- run: go install github.com/josephburnett/jd@latest
138-
139-
- name: 'Download provider schemas'
140-
uses: actions/download-artifact@v3
141-
142-
- run: ls -l schema*/*
43+
- name: 'Install jd'
44+
run: go install github.com/josephburnett/jd@latest
14345

144-
- run: jd -color schema-current/provider.json schema-new/provider.json
145-
continue-on-error: true
46+
- run: make diff-schema

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ test-preview: install
8181
docker-it:
8282
docker build -t databricks-terrafrom/test -f scripts/Dockerfile .
8383

84+
schema:
85+
@/bin/bash scripts/print-schema.sh
86+
87+
diff-schema:
88+
@/bin/bash scripts/diff-schema.sh
89+
8490
.PHONY: build fmt python-setup docs vendor build fmt coverage test lint

scripts/diff-schema.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/libschema.sh
4+
5+
BASE_BRANCH="master"
6+
7+
checkout_branch() {
8+
local branch=$1
9+
echo "Checking out branch: $branch"
10+
git checkout $branch
11+
}
12+
13+
if [ -n "$(git status --porcelain)" ]; then
14+
echo "There are uncommitted changes. Please commit them before running this script."
15+
exit 1
16+
fi
17+
18+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
19+
NEW_SCHEMA=$(generate_schema)
20+
checkout_branch $BASE_BRANCH
21+
CURRENT_SCHEMA=$(generate_schema)
22+
checkout_branch $CURRENT_BRANCH
23+
24+
set +e
25+
jd -color "$CURRENT_SCHEMA" "$NEW_SCHEMA"
26+
RES=$?
27+
set -e
28+
if [ $RES -eq 0 ]; then
29+
echo "No schema changes detected."
30+
fi

scripts/libschema.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Function to generate provider schema
4+
generate_schema() {
5+
>&2 make install
6+
version=$(./terraform-provider-databricks version)
7+
8+
local TMPDIR=$(mktemp -d)
9+
>&2 echo "Generating provider schema in $TMPDIR..."
10+
>&2 pushd $TMPDIR
11+
cat > main.tf <<EOF
12+
terraform {
13+
required_providers {
14+
databricks = {
15+
source = "databricks/databricks"
16+
version = "$version"
17+
}
18+
}
19+
}
20+
EOF
21+
>&2 terraform init
22+
terraform providers schema -json > schema.json
23+
>&2 popd
24+
>&2 echo "Provider schema available in $TMPDIR/schema.json"
25+
echo "$TMPDIR/schema.json"
26+
}

scripts/print-schema.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/libschema.sh
4+
5+
cat $(generate_schema) | jq

0 commit comments

Comments
 (0)