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

atlasaction: update schema lint format #226

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .github/workflows/ci-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_TOKEN }}
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
Expand Down
19 changes: 5 additions & 14 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,11 @@ func (a *Actions) SchemaPlan(ctx context.Context) error {
a.SetOutput("plan", plan.File.URL)
a.SetOutput("status", plan.File.Status)
// Report the schema plan to the user and add a comment to the PR.
summary, err := schemaPlanComment(plan, params.Env, tc.RerunCmd)
summary, err := RenderTemplate("schema-plan.tmpl", map[string]any{
"Plan": plan,
"EnvName": params.Env,
"RerunCommand": tc.RerunCmd,
})
if err != nil {
return fmt.Errorf("failed to generate schema plan comment: %w", err)
}
Expand Down Expand Up @@ -925,19 +929,6 @@ func (a *Actions) addSuggestions(lint *atlasexec.SummaryReport, fn func(*Suggest
return nil
}

func schemaPlanComment(payload *atlasexec.SchemaPlan, envName, rerun string) (string, error) {
data := map[string]any{
"Plan": payload,
"EnvName": envName,
"RerunCommand": rerun,
}
var buf bytes.Buffer
if err := commentsTmpl.ExecuteTemplate(&buf, "schema-plan.tmpl", data); err != nil {
return "", err
}
return buf.String(), nil
}

func execTime(start, end time.Time) string {
return end.Sub(start).String()
}
Expand Down
10 changes: 5 additions & 5 deletions atlasaction/action_test.go

Large diffs are not rendered by default.

35 changes: 0 additions & 35 deletions atlasaction/comments/lint-report.tmpl
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
{{- define "lint-report" -}}
<table>
<thead>
<tr>
<th>Status</th>
<th>Step</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
{{- block "lint-first-step" . -}}
{{- template "lint-check" "success.svg" -}}
<td>
{{- $fileCount := len .Files -}}
{{- if eq $fileCount 1 -}}
1 new migration file detected
{{- else if $fileCount -}}
{{- $fileCount }} new migration files detected
{{- else -}}
No migration files detected
{{- end -}}
</td>
<td>
{{- with .Files -}}
{{- range $i, $f := . -}}
{{- if $i -}}<br/>{{- end -}}{{- $f.Name -}}
{{- end -}}
{{- else -}}
&nbsp;
{{- end -}}
</td>
{{- end -}}
</tr>
{{- with .URL -}}
<tr>
{{- template "lint-check" "success.svg" -}}
Expand Down Expand Up @@ -74,8 +41,6 @@
{{- with .URL -}}
<tr><td colspan="4"><div align="center">Read the full linting report on {{ . | link "Atlas Cloud" -}}</div></td></tr>
{{- end -}}
</tbody>
</table>
{{- end -}}
{{- define "lint-check" -}}
<td><div align="center">{{- assetsImage . | image "20px" -}}</div></td>
Expand Down
33 changes: 32 additions & 1 deletion atlasaction/comments/migrate-lint.tmpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
<code>atlas migrate lint</code> on <strong>{{- .Env.Dir }}</strong>
<code>atlas migrate lint</code> on <strong>{{- .Env.Dir }}</strong><table>
<thead>
<tr>
<th>Status</th>
<th>Step</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
{{- template "lint-check" "success.svg" -}}
<td>
{{- $fileCount := len .Files -}}
{{- if eq $fileCount 1 -}}
1 new migration file detected
{{- else if $fileCount -}}
{{- $fileCount }} new migration files detected
{{- else -}}
No migration files detected
{{- end -}}
</td>
<td>
{{- with .Files -}}
{{- range $i, $f := . -}}
{{- if $i -}}<br/>{{- end -}}{{- $f.Name -}}
{{- end -}}
{{- else -}}
&nbsp;
{{- end -}}
</td>
</tr>
{{- template "lint-report" . -}}
</body></table>
2 changes: 1 addition & 1 deletion atlasaction/comments/schema-apply.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{{- .File.Migration | codeblock "sql" | details (printf "📄 View %s Statements" $kind) -}}
{{- with .Lint -}}
<h4>Atlas lint results</h4>
{{- template "lint-report" . -}}
{{- template "schema-lint" $ -}}
{{- end -}}
{{/* Fallback to the old output */}}
{{- else with .Changes -}}
Expand Down
32 changes: 30 additions & 2 deletions atlasaction/comments/schema-plan.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h4>SQL Plan</h4>
{{- .Plan.File.Migration | codeblock "sql" -}}
<h4>Atlas lint results</h4>
{{- template "lint-report" .Plan.Lint -}}
{{- template "schema-lint" . -}}
<hr>
<details>
<summary>📝 To edit this plan, follow these steps</summary>
Expand All @@ -14,4 +14,32 @@
<li>Re-trigger this action using <code>{{ .RerunCommand }}</code></li>
{{- end -}}
</ol>
</details>
</details>
{{- define "schema-lint" -}}
<table>
<thead>
<tr>
<th>Status</th>
<th>Step</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
{{- template "lint-check" "success.svg" -}}
{{- with .Plan.File -}}
<td>Detect schema changes</td><td>
{{- $stmtCount := len .Stmts -}}
{{- if eq $stmtCount 1 -}}
giautm marked this conversation as resolved.
Show resolved Hide resolved
1 new statement file detected
{{- else if $stmtCount -}}
{{- $stmtCount }} new statements detected
{{- else -}}
No statements detected
{{- end -}}
</td>
</tr>
{{- end -}}
{{- template "lint-report" .Plan.Lint -}}
</tbody></table>
{{- end -}}
18 changes: 9 additions & 9 deletions atlasaction/testdata/github/schema-apply-envs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Abort: The plan "From" hash does not match the current state hash (passed with -
+ R1cGcSfo1oWYK4dz+7WvgCtE/QppFo9lKFEqEDzoS4o= (current hash)

-- schema-apply/1/stdout --
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-bu.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-12T21:31:20.339663+07:00","End":"2024-09-12T21:31:20.351603+07:00","Applied":{"Name":"20240910173744.sql","Version":"20240910173744","Start":"2024-09-12T21:31:20.350607+07:00","End":"2024-09-12T21:31:20.351228+07:00","Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]},"Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-bu.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"File":{"Name":"20240910173744","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=","Migration":"-- Add column \"c2\" to table: \"t4\"\nALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;\n","URL":"file://20240910173744.plan.hcl"}},"Changes":{"Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]}}
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-pi.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-12T21:31:20.354074+07:00","End":"2024-09-12T21:31:20.35764+07:00","Applied":{"Name":"20240910173744.sql","Version":"20240910173744","Start":"2024-09-12T21:31:20.356221+07:00","End":"2024-09-12T21:31:20.356755+07:00","Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]},"Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-pi.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"File":{"Name":"20240910173744","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=","Migration":"-- Add column \"c2\" to table: \"t4\"\nALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;\n","URL":"file://20240910173744.plan.hcl"}},"Changes":{"Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]}}
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-su.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-12T21:31:20.360863+07:00","End":"2024-09-12T21:31:20.368395+07:00","Applied":{"Name":"20240910173744.sql","Version":"20240910173744","Start":"2024-09-12T21:31:20.364331+07:00","End":"2024-09-12T21:31:20.365086+07:00","Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]},"Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-su.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"File":{"Name":"20240910173744","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=","Migration":"-- Add column \"c2\" to table: \"t4\"\nALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;\n","URL":"file://20240910173744.plan.hcl"}},"Changes":{"Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]}}
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-bu.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-18T11:19:23.232253+07:00","End":"2024-09-18T11:19:23.238022+07:00","Applied":{"Name":"20240910173744.sql","Version":"20240910173744","Start":"2024-09-18T11:19:23.237559+07:00","End":"2024-09-18T11:19:23.237793+07:00","Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]},"Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-bu.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"File":{"Name":"20240910173744","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=","Migration":"-- Add column \"c2\" to table: \"t4\"\nALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;\n","Stmts":[{"Pos":34,"Text":"ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;","Comments":["-- Add column \"c2\" to table: \"t4\"\n"]}],"URL":"file://20240910173744.plan.hcl"}},"Changes":{"Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]}}
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-pi.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-18T11:19:23.239729+07:00","End":"2024-09-18T11:19:23.242458+07:00","Applied":{"Name":"20240910173744.sql","Version":"20240910173744","Start":"2024-09-18T11:19:23.241765+07:00","End":"2024-09-18T11:19:23.24208+07:00","Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]},"Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-pi.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"File":{"Name":"20240910173744","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=","Migration":"-- Add column \"c2\" to table: \"t4\"\nALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;\n","Stmts":[{"Pos":34,"Text":"ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;","Comments":["-- Add column \"c2\" to table: \"t4\"\n"]}],"URL":"file://20240910173744.plan.hcl"}},"Changes":{"Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]}}
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-su.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-18T11:19:23.244225+07:00","End":"2024-09-18T11:19:23.247165+07:00","Applied":{"Name":"20240910173744.sql","Version":"20240910173744","Start":"2024-09-18T11:19:23.246708+07:00","End":"2024-09-18T11:19:23.246929+07:00","Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]},"Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local-su.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"File":{"Name":"20240910173744","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=","Migration":"-- Add column \"c2\" to table: \"t4\"\nALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;\n","Stmts":[{"Pos":34,"Text":"ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;","Comments":["-- Add column \"c2\" to table: \"t4\"\n"]}],"URL":"file://20240910173744.plan.hcl"}},"Changes":{"Applied":["ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;"]}}
-- output.txt --
error<<_GitHubActionsFileCommandDelimeter_
Abort: The plan "From" hash does not match the current state hash (passed with --from): - iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE= (plan value) + R1cGcSfo1oWYK4dz+7WvgCtE/QppFo9lKFEqEDzoS4o= (current hash)
Expand All @@ -49,7 +49,7 @@ _GitHubActionsFileCommandDelimeter_
<td><code>file://20240910173744.plan.hcl</code></td>
</tr><tr>
<td>Total Time</td>
<td>11.94ms</td>
<td>5.769ms</td>
</tr></table><h4>Version 20240910173744.sql:</h4>
<table>
<tr>
Expand All @@ -60,7 +60,7 @@ _GitHubActionsFileCommandDelimeter_
<th>Error Statement</th>
</tr>
<tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture></div></td><td>1</td>
<td>621µs</td><td>-</td><td>-</td></tr>
<td>234µs</td><td>-</td><td>-</td></tr>
</table><details><summary>📄 View Pre-planned SQL Statements</summary><pre lang="sql"><code>-- Add column "c2" to table: "t4"
ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</code></pre></details>
Expand All @@ -84,7 +84,7 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
<td><code>file://20240910173744.plan.hcl</code></td>
</tr><tr>
<td>Total Time</td>
<td>3.566ms</td>
<td>2.729ms</td>
</tr></table><h4>Version 20240910173744.sql:</h4>
<table>
<tr>
Expand All @@ -95,7 +95,7 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
<th>Error Statement</th>
</tr>
<tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture></div></td><td>1</td>
<td>534µs</td><td>-</td><td>-</td></tr>
<td>315µs</td><td>-</td><td>-</td></tr>
</table><details><summary>📄 View Pre-planned SQL Statements</summary><pre lang="sql"><code>-- Add column "c2" to table: "t4"
ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</code></pre></details>
Expand All @@ -119,7 +119,7 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
<td><code>file://20240910173744.plan.hcl</code></td>
</tr><tr>
<td>Total Time</td>
<td>7.532ms</td>
<td>2.94ms</td>
</tr></table><h4>Version 20240910173744.sql:</h4>
<table>
<tr>
Expand All @@ -130,7 +130,7 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
<th>Error Statement</th>
</tr>
<tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture></div></td><td>1</td>
<td>755µs</td><td>-</td><td>-</td></tr>
<td>221µs</td><td>-</td><td>-</td></tr>
</table><details><summary>📄 View Pre-planned SQL Statements</summary><pre lang="sql"><code>-- Add column "c2" to table: "t4"
ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</code></pre></details>
Loading
Loading