Skip to content

Commit

Permalink
chore: test script for schema/apply action
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Sep 15, 2024
1 parent 640ee19 commit a0b3507
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
136 changes: 136 additions & 0 deletions atlasaction/testdata/github/schema-apply-envs.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Mock the atlas command outputs
mock-atlas $WORK/schema-apply
# Setup the action input variables
env INPUT_ENV=test
env INPUT_PLAN=file://20240910173744.plan.hcl
# Run the action
! atlas-action schema/apply
stdout '"atlas schema apply" completed successfully on the target "sqlite://local-bu.db"'
stdout '"atlas schema apply" completed successfully on the target "sqlite://local-pi.db"'
stdout '"atlas schema apply" completed successfully on the target "sqlite://local-su.db"'

summary summary.html
output output.txt

-- schema-apply/1/args --
schema apply --format {{ json . }} --env test --plan file://20240910173744.plan.hcl
-- schema-apply/1/stderr --
Abort: The plan "From" hash does not match the current state hash (passed with --from):

- iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE= (plan value)
+ 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;"]}}
-- 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)
_GitHubActionsFileCommandDelimeter_
-- summary.html --
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture> Migration Passed</h2><h4><code>atlas schema apply</code> Summary:</h4><table>
<tr>
<td>Database URL</td>
<td><code>sqlite://local-bu.db</code></td>
</tr><tr>
<td>Plan Name</td>
<td><code>20240910173744</code></td>
</tr>
<tr>
<td>From Hash</td>
<td><code>iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=</code></td>
</tr>
<tr>
<td>To Hash</td>
<td><code>Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=</code></td>
</tr><tr>
<td>Plan URL</td>
<td><code>file://20240910173744.plan.hcl</code></td>
</tr><tr>
<td>Total Time</td>
<td>11.94ms</td>
</tr></table><h4>Version 20240910173744.sql:</h4>
<table>
<tr>
<th>Status</th>
<th>Executed Statements</th>
<th>Execution Time</th>
<th>Error</th>
<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>
</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>
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture> Migration Passed</h2><h4><code>atlas schema apply</code> Summary:</h4><table>
<tr>
<td>Database URL</td>
<td><code>sqlite://local-pi.db</code></td>
</tr><tr>
<td>Plan Name</td>
<td><code>20240910173744</code></td>
</tr>
<tr>
<td>From Hash</td>
<td><code>iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=</code></td>
</tr>
<tr>
<td>To Hash</td>
<td><code>Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=</code></td>
</tr><tr>
<td>Plan URL</td>
<td><code>file://20240910173744.plan.hcl</code></td>
</tr><tr>
<td>Total Time</td>
<td>3.566ms</td>
</tr></table><h4>Version 20240910173744.sql:</h4>
<table>
<tr>
<th>Status</th>
<th>Executed Statements</th>
<th>Execution Time</th>
<th>Error</th>
<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>
</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>
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture> Migration Passed</h2><h4><code>atlas schema apply</code> Summary:</h4><table>
<tr>
<td>Database URL</td>
<td><code>sqlite://local-su.db</code></td>
</tr><tr>
<td>Plan Name</td>
<td><code>20240910173744</code></td>
</tr>
<tr>
<td>From Hash</td>
<td><code>iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=</code></td>
</tr>
<tr>
<td>To Hash</td>
<td><code>Cp8xCVYilZuwULkggsfJLqIQHaxYcg/IpU+kgjVUBA4=</code></td>
</tr><tr>
<td>Plan URL</td>
<td><code>file://20240910173744.plan.hcl</code></td>
</tr><tr>
<td>Total Time</td>
<td>7.532ms</td>
</tr></table><h4>Version 20240910173744.sql:</h4>
<table>
<tr>
<th>Status</th>
<th>Executed Statements</th>
<th>Execution Time</th>
<th>Error</th>
<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>
</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>
57 changes: 57 additions & 0 deletions atlasaction/testdata/github/schema-apply-lint.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Mock the atlas command outputs
mock-atlas $WORK/schema-apply
# Setup the action input variables
env INPUT_ENV=test
env INPUT_AUTO-APPROVE=true
# Run the action
! atlas-action schema/apply

summary summary.html
output output.txt

-- schema-apply/1/args --
schema apply --format {{ json . }} --env test --auto-approve
-- schema-apply/1/stderr --
Rejected by review policy: errors or warnings were found
-- schema-apply/1/stdout --
{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"},"Start":"2024-09-12T17:47:58.815235+07:00","End":"0001-01-01T00:00:00Z","Error":"Rejected by review policy: errors or warnings were found","Plan":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"local.db","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Schema":"main"}},"Lint":{"Env":{"Driver":"sqlite3","URL":{"Scheme":"sqlite","Opaque":"","User":null,"Host":"file","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"mode=memory","Fragment":"","RawFragment":"","Schema":"main"}},"Schema":{},"Steps":[{"Name":"Analyze 20240912104758.sql","Text":"1 reports were found in analysis","Result":{"Name":"20240912104758.sql","Text":"-- Disable the enforcement of foreign-keys constraints\nPRAGMA foreign_keys = off;\n-- Drop \"t4\" table\nDROP TABLE `t4`;\n-- Enable back the enforcement of foreign-keys constraints\nPRAGMA foreign_keys = on;\n","Reports":[{"Text":"destructive changes detected","Diagnostics":[{"Pos":101,"Text":"Dropping table \"t4\"","Code":"DS102","SuggestedFixes":[{"Message":"Add a pre-migration check to ensure table \"t4\" is empty before dropping it"}]}]}],"Error":"destructive changes detected"}}],"Files":[{"Name":"20240912104758.sql","Text":"-- Disable the enforcement of foreign-keys constraints\nPRAGMA foreign_keys = off;\n-- Drop \"t4\" table\nDROP TABLE `t4`;\n-- Enable back the enforcement of foreign-keys constraints\nPRAGMA foreign_keys = on;\n","Reports":[{"Text":"destructive changes detected","Diagnostics":[{"Pos":101,"Text":"Dropping table \"t4\"","Code":"DS102","SuggestedFixes":[{"Message":"Add a pre-migration check to ensure table \"t4\" is empty before dropping it"}]}]}],"Error":"destructive changes detected"}]},"File":{"Name":"20240912104758","FromHash":"iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=","ToHash":"Rl4lBdMkvFoGQ4xu+3sYCeogTVnamJ7bmDoq9pMXcjw=","Migration":"-- Disable the enforcement of foreign-keys constraints\nPRAGMA foreign_keys = off;\n-- Drop \"t4\" table\nDROP TABLE `t4`;\n-- Enable back the enforcement of foreign-keys constraints\nPRAGMA foreign_keys = on;\n"}},"Changes":{}}
-- output.txt --
error<<_GitHubActionsFileCommandDelimeter_
Rejected by review policy: errors or warnings were found
_GitHubActionsFileCommandDelimeter_
-- summary.html --
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/error.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/error.svg?v=1"/></picture> Migration Failed</h2><h4><code>atlas schema apply</code> Summary:</h4><table>
<tr>
<td>Database URL</td>
<td><code>sqlite://local.db</code></td>
</tr><tr>
<td>Plan Name</td>
<td><code>20240912104758</code></td>
</tr>
<tr>
<td>From Hash</td>
<td><code>iHZMQ1EoarAXt/KU0KQbBljbbGs8gVqX2ZBXefePSGE=</code></td>
</tr>
<tr>
<td>To Hash</td>
<td><code>Rl4lBdMkvFoGQ4xu+3sYCeogTVnamJ7bmDoq9pMXcjw=</code></td>
</tr><tr><td>Error</td><td>Rejected by review policy: errors or warnings were found</td></tr></table><details><summary>📄 View SQL Statements</summary><pre lang="sql"><code>-- Disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- Drop "t4" table
DROP TABLE `t4`;
-- Enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;
</code></pre></details><h4>Atlas lint results</h4><table>
<thead>
<tr>
<th>Status</th>
<th>Step</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<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 new migration file detected</td>
<td>20240912104758.sql</td></tr><tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/error.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/error.svg?v=1"/></picture></div></td><td>Analyze 20240912104758.sql<br/>1 reports were found in analysis</td>
<td><b>Destructive changes detected</b><br/>Dropping table "t4"&nbsp;<a href="https://atlasgo.io/lint/analyzers#DS102" target="_blank">(DS102)</a><br/></td>
</tr></tbody>
</table>
30 changes: 30 additions & 0 deletions atlasaction/testdata/github/schema-apply-local.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Setup the action input variables
env INPUT_ENV=test
env INPUT_AUTO-APPROVE=true
# Run the action
atlas-action schema/apply
stdout '"atlas schema apply" completed successfully on the target "sqlite://local.db"'

summary summary.html
! output

-- atlas.hcl --
env {
name = atlas.env
url = "sqlite://local.db"
src = "file://schema.lt.hcl"
}
-- schema.lt.hcl --
table "t1" {
schema = schema.main
column "c1" {
null = true
type = int
}
}
schema "main" {
}
-- summary.html --
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture> Migration Passed</h2><h4><code>atlas schema apply</code> Summary:</h4><details><summary>📄 Succeeded SQL Applied</summary><pre lang="sql"><code>CREATE TABLE `t1` (
`c1` int NULL
)</code></pre></details>

0 comments on commit a0b3507

Please sign in to comment.