Skip to content

Commit

Permalink
atlasaction: added new markdown templates (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Dec 19, 2024
1 parent 9be454d commit 47c2e51
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 0 deletions.
42 changes: 42 additions & 0 deletions atlasaction/comments/bitbucket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{- define "migrate-lint/md" -}}
`atlas migrate lint` on **{{ if eq .Env.Dir "." }}working directory{{ else }}{{ .Env.Dir }}{{ end }}**

| Status | Step | Result |
| :----: | :--- | :----- |
| {{ template "lint-check/md" "success.svg" }} | {{ filesDetected .Files }} | {{ join (fileNames .Files) "<br/>" }} |
{{ template "lint-report/md" . }}
{{- end -}}
{{- define "schema-plan/md" -}}
### Atlas detected changes to the desired schema

{{ with .File -}}
#### Migration Plan {{- with .Link }} ([View on Atlas Cloud]({{- . -}})){{ end }}
{{- with .Migration -}}{{- codeblock "sql" . -}}{{- end }}
{{- end -}}
#### Atlas lint results

| Status | Step | Result |
| :----: | :--- | :----- |
| {{ template "lint-check/md" "success.svg" }} | {{ with .File }}Detect schema changes | {{ stmtsDetected . }}{{ else -}} | {{ end }} |
{{ template "lint-report/md" .Lint -}}
{{- with .File }}

---

##### 📝 Steps to edit this migration plan

{{ template "plan-modify/md" . }}
{{- end -}}
{{- end -}}
{{- define "lint-report/md" -}}
{{- with .URL -}}
| {{ template "lint-check/md" "success.svg" }} | ERD and visual diff generated | [View Visualization]({{- printf "%s#erd" . -}}) |
{{ end }}
{{- with (.Steps | filterIssues) -}}
{{ range $step := . -}}
| {{ template "lint-check/md" (or (and ($step | stepIsError) "error.svg") "warning.svg") }} | {{ stepSummary $step | nl2br }} | {{ stepDetails $step | nl2br }} |
{{ end -}}
{{- else -}}
| {{ template "lint-check/md" "success.svg" }} | No issues found | {{ with .URL -}}[View Report]({{- . -}}){{- end }} |
{{- end -}}
{{- end -}}
3 changes: 3 additions & 0 deletions atlasaction/comments/commons.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ the database with the desired state. Otherwise, Atlas will report a schema drift
atlas schema plan push --pending --file {{ .Name }}.plan.hcl
```
{{- end -}}
{{- define "lint-check/md" -}}
![]({{- assetsImage . }})
{{- end -}}
{{- define "lint-check" -}}
{{- assetsImage . | image "20px" -}}
{{- end -}}
71 changes: 71 additions & 0 deletions atlasaction/testdata/templates/migrate-lint-md.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# no errors
render-lint migrate-lint/md data-0.json
cmp stdout golden-0.md

# file with 2 issues
render-lint migrate-lint/md data-1.json
cmp stdout golden-1.md

# 2 files, 1 with error, 1 with issue
render-lint migrate-lint/md data-2.json
cmp stdout golden-2.md

# 1 checksum error
render-lint migrate-lint/md data-3.json
cmp stdout golden-3.md

# non linear history error
render-lint migrate-lint/md data-4.json
cmp stdout golden-4.md

-- data-0.json --
{"URL":"https://migration-lint-report-url","Env":{"Dir":"testdata/migrations"},"Schema":{},"Steps":[{"Name":"Migration Integrity Check","Text":"File atlas.sum is valid"},{"Name":"Detect New Migration Files","Text":"Found 1 new migration files (from 1 total)"}],"Files":[{"Name":"20230925192914.sql"}]}
-- golden-0.md --
`atlas migrate lint` on **testdata/migrations**

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | 1 new migration file detected | 20230925192914.sql |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | ERD and visual diff generated | [View Visualization](https://migration-lint-report-url#erd) |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | No issues found | [View Report](https://migration-lint-report-url) |
-- data-1.json --
{"URL":"https://migration-lint-report-url","Env":{"Dir":"."},"Schema":{},"Steps":[{"Name":"Migration Integrity Check","Text":"File atlas.sum is valid"},{"Name":"Detect New Migration Files","Text":"Found 1 new migration files (from 1 total)"},{"Name":"Analyze 20230925192914.sql","Text":"2 reports were found in analysis","Result":{"Name":"20230925192914.sql","Text":"CREATE UNIQUE INDEX idx_unique_fullname ON Persons (FirstName, LastName);\nALTER TABLE Persons ADD City varchar(255) NOT NULL;\n","Reports":[{"Text":"data dependent changes detected","Diagnostics":[{"Pos":0,"Text":"Adding a unique index \"idx_unique_fullname\" on table \"Persons\" might fail in case columns \"FirstName\", \"LastName\" contain duplicate entries","Code":"MF101"},{"Pos":0,"Text":"Adding a non-nullable \"varchar\" column \"City\" on table \"Persons\" without a default value implicitly sets existing rows with \"\"","Code":"MY101"}]}]}}],"Files":[{"Name":"20230925192914.sql","Reports":[{"Text":"","Diagnostics":[{"Pos":0,"Text":"Add unique index to existing column","Code":"MF101"},{"Pos":0,"Text":"Adding a non-nullable column to a table without a DEFAULT","Code":"MY101"}]}]}]}
-- golden-1.md --
`atlas migrate lint` on **working directory**

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | 1 new migration file detected | 20230925192914.sql |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | ERD and visual diff generated | [View Visualization](https://migration-lint-report-url#erd) |
| ![](https://release.ariga.io/images/assets/warning.svg?v=1) | Analyze 20230925192914.sql<br/>2 reports were found in analysis | **Data dependent changes detected**<br/>Adding a unique index "idx_unique_fullname" on table "Persons" might fail in case columns "FirstName", "LastName" contain duplicate entries [(MF101)](https://atlasgo.io/lint/analyzers#MF101)<br/>Adding a non-nullable "varchar" column "City" on table "Persons" without a default value implicitly sets existing rows with "" [(MY101)](https://atlasgo.io/lint/analyzers#MY101) |
-- data-2.json --
{"URL":"https://migration-lint-report-url","Env":{"Dir":"testdata/migrations"},"Schema":{},"Steps":[{"Name":"Migration Integrity Check","Text":"File atlas.sum is valid"},{"Name":"Detect New Migration Files","Text":"Found 1 new migration files (from 1 total)"},{"Name":"Analyze 20230925192914.sql","Text":"1 reports were found in analysis","Result":{"Name":"20230925192914.sql","Text":"CREATE UNIQUE INDEX idx_unique_fullname ON Persons (FirstName, LastName);","Reports":[{"Text":"data dependent changes detected","Diagnostics":[{"Pos":0,"Text":"Adding a unique index \"idx_unique_fullname\" on table \"Persons\" might fail in case columns \"FirstName\", \"LastName\" contain duplicate entries","Code":"MF101"}]}]}},{"Name":"Analyze 20240625104520_destructive.sql","Text":"1 reports were found in analysis","Result":{"Name":"20240625104520_destructive.sql","Text":"DROP TABLE Persons;\n\n","Reports":[{"Text":"destructive changes detected","Diagnostics":[{"Pos":0,"Text":"Dropping table \"Persons\"","Code":"DS102"}]}],"Error":"Destructive changes detected"}}],"Files":[{"Name":"20230925192914.sql","Error":"Destructive changes detected"},{"Name":"20230925192915.sql","Reports":[{"Text":"","Diagnostics":[{"Pos":0,"Text":"Missing the CONCURRENTLY in index creation","Code":"PG101"}]}]}]}
-- golden-2.md --
`atlas migrate lint` on **testdata/migrations**

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | 2 new migration files detected | 20230925192914.sql<br/>20230925192915.sql |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | ERD and visual diff generated | [View Visualization](https://migration-lint-report-url#erd) |
| ![](https://release.ariga.io/images/assets/warning.svg?v=1) | Analyze 20230925192914.sql<br/>1 reports were found in analysis | **Data dependent changes detected**<br/>Adding a unique index "idx_unique_fullname" on table "Persons" might fail in case columns "FirstName", "LastName" contain duplicate entries [(MF101)](https://atlasgo.io/lint/analyzers#MF101) |
| ![](https://release.ariga.io/images/assets/error.svg?v=1) | Analyze 20240625104520_destructive.sql<br/>1 reports were found in analysis | **Destructive changes detected**<br/>Dropping table "Persons" [(DS102)](https://atlasgo.io/lint/analyzers#DS102) |
-- data-3.json --
{"URL":"https://migration-lint-report-url","Env":{"Dir":"testdata/migrations"},"Schema":{},"Steps":[{"Name":"Migration Integrity Check","Text":"File atlas.sum is invalid","Error":"checksum mismatch"}],"Files":[{"Name":"20230925192914.sql","Error":"checksum mismatch"}]}
-- golden-3.md --
`atlas migrate lint` on **testdata/migrations**

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | 1 new migration file detected | 20230925192914.sql |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | ERD and visual diff generated | [View Visualization](https://migration-lint-report-url#erd) |
| ![](https://release.ariga.io/images/assets/error.svg?v=1) | Migration Integrity Check<br/>File atlas.sum is invalid | checksum mismatch |
-- data-4.json --
{"URL":"https://migration-lint-report-url","Env":{"Dir":"testdata/migrations"},"Schema":{},"Steps":[{"Name":"Migration Integrity Check","Text":"File atlas.sum is valid"},{"Name":"Detected 1 non-additive change","Text":"Pulling the the latest git changes might fix this warning","Result":{"Reports":[{"Text":"","Diagnostics":[{"Pos":0,"Text":"File 20240613102407.sql is missing or has been removed. Changes that have already been applied will not be reverted","Code":""}]}]}}]}
-- golden-4.md --
`atlas migrate lint` on **testdata/migrations**

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | No migration files detected | |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | ERD and visual diff generated | [View Visualization](https://migration-lint-report-url#erd) |
| ![](https://release.ariga.io/images/assets/warning.svg?v=1) | Detected 1 non-additive change<br/>Pulling the the latest git changes might fix this warning | File 20240613102407.sql is missing or has been removed. Changes that have already been applied will not be reverted |
92 changes: 92 additions & 0 deletions atlasaction/testdata/templates/schema-plan-md.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
render-schema-plan schema-plan/md data-1.json
cmp stdout golden-1.md

render-schema-plan schema-plan/md data-2.json
cmp stdout golden-2.md

render-schema-plan schema-plan/md data-3.json
cmp stdout golden-3.md
-- data-1.json --
{"Env":{"Driver":"sqlserver","URL":{"Scheme":"sqlserver","Opaque":"","User":{},"Host":"host.docker.internal:39445","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"database=master\u0026mode=database","Fragment":"","RawFragment":"","Schema":""}},"Repo":"bitbucket-e2e","Lint":{"Env":{"Driver":"sqlserver","URL":{"Scheme":"sqlserver","Opaque":"","User":{},"Host":"host.docker.internal:39445","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"database=master\u0026mode=database","Fragment":"","RawFragment":"","Schema":""}},"Schema":{},"Steps":[{"Name":"Analyze pr-1-VwDs28An.sql","Text":"0 reports were found in analysis","Result":{"Name":"pr-1-VwDs28An.sql","Text":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;\n"}}],"Files":[{"Name":"pr-1-VwDs28An.sql","Text":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;\n"}]},"File":{"Name":"pr-1-VwDs28An","FromHash":"VwDs28An8dyAzzfEHiqF9k9XiotM5uDQxXSXp0mKWFA=","ToHash":"ae2Xr0Ir2B5ztK//Jdtg7x/CvffsebUWBZL25wdE4dc=","Migration":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;\n","Stmts":[{"Pos":24,"Text":"ALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;","Comments":["-- Modify \"Image\" table\n"]}],"URL":"atlas://bitbucket-e2e/plans/pr-1-VwDs28An","Link":"https://ariga-atlas.atlasgo.cloud/schemas/141733923334/plans/210453398560","Status":"PENDING"}}
-- golden-1.md --
### Atlas detected changes to the desired schema

#### Migration Plan ([View on Atlas Cloud](https://ariga-atlas.atlasgo.cloud/schemas/141733923334/plans/210453398560))

```sql
-- Modify "Image" table
ALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;
```

#### Atlas lint results

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | Detect schema changes | 1 new statement detected |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | No issues found | |

---

##### 📝 Steps to edit this migration plan

1\. Run the following command to pull the generated plan to your local workstation:
```bash
atlas schema plan pull --url "atlas://bitbucket-e2e/plans/pr-1-VwDs28An" > pr-1-VwDs28An.plan.hcl
```

2\. Open `pr-1-VwDs28An` in your editor and modify it as needed. Note that the result of the plan should align
the database with the desired state. Otherwise, Atlas will report a schema drift.

3\. Push the updated plan to the registry using the following command:
```bash
atlas schema plan push --pending --file pr-1-VwDs28An.plan.hcl
```
-- data-2.json --
{"Env":{"Driver":"sqlserver","URL":{"Scheme":"sqlserver","Opaque":"","User":{},"Host":"host.docker.internal:39445","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"database=master\u0026mode=database","Fragment":"","RawFragment":"","Schema":""}},"Repo":"bitbucket-e2e","Lint":{"Env":{"Driver":"sqlserver","URL":{"Scheme":"sqlserver","Opaque":"","User":{},"Host":"host.docker.internal:39445","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"database=master\u0026mode=database","Fragment":"","RawFragment":"","Schema":""}},"Schema":{},"Steps":[{"Name":"Analyze pr-1-VwDs28An.sql","Text":"0 reports were found in analysis","Result":{"Name":"pr-1-VwDs28An.sql","Text":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;\n"}}],"Files":[{"Name":"pr-1-VwDs28An.sql","Text":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] ADD [StorageKey] nvarchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;\n"}]},"File":null}
-- golden-2.md --
### Atlas detected changes to the desired schema

#### Atlas lint results

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | | |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | No issues found | |
-- data-3.json --
{"Env":{"Driver":"sqlserver","URL":{"Scheme":"sqlserver","Opaque":"","User":{},"Host":"host.docker.internal:35283","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"database=master\u0026mode=database","Fragment":"","RawFragment":"","Schema":""}},"Repo":"bitbucket-e2e","Lint":{"Env":{"Driver":"sqlserver","URL":{"Scheme":"sqlserver","Opaque":"","User":{},"Host":"host.docker.internal:35283","Path":"","RawPath":"","OmitHost":false,"ForceQuery":false,"RawQuery":"database=master\u0026mode=database","Fragment":"","RawFragment":"","Schema":""}},"Schema":{},"Steps":[{"Name":"Analyze pr-3-ae2Xr0Ir.sql","Text":"1 reports were found in analysis","Result":{"Name":"pr-3-ae2Xr0Ir.sql","Text":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] DROP COLUMN [Description];\n-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] DROP COLUMN [StorageKey];\n","Reports":[{"Text":"destructive changes detected","Diagnostics":[{"Pos":24,"Text":"Dropping non-virtual column \"Description\"","Code":"DS103","SuggestedFixes":[{"Message":"Add a pre-migration check to ensure column \"Description\" is NULL before dropping it"}]},{"Pos":106,"Text":"Dropping non-virtual column \"StorageKey\"","Code":"DS103","SuggestedFixes":[{"Message":"Add a pre-migration check to ensure column \"StorageKey\" is NULL before dropping it"}]}]}],"Error":"destructive changes detected"}}],"Files":[{"Name":"pr-3-ae2Xr0Ir.sql","Text":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] DROP COLUMN [Description];\n-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] DROP COLUMN [StorageKey];\n","Reports":[{"Text":"destructive changes detected","Diagnostics":[{"Pos":24,"Text":"Dropping non-virtual column \"Description\"","Code":"DS103","SuggestedFixes":[{"Message":"Add a pre-migration check to ensure column \"Description\" is NULL before dropping it"}]},{"Pos":106,"Text":"Dropping non-virtual column \"StorageKey\"","Code":"DS103","SuggestedFixes":[{"Message":"Add a pre-migration check to ensure column \"StorageKey\" is NULL before dropping it"}]}]}],"Error":"destructive changes detected"}]},"File":{"Name":"pr-3-ae2Xr0Ir","FromHash":"ae2Xr0Ir2B5ztK//Jdtg7x/CvffsebUWBZL25wdE4dc=","ToHash":"DntscM4aNcMzI+ZL/0DLj29fGfIjTaYaUYydNFa4cqE=","Migration":"-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] DROP COLUMN [Description];\n-- Modify \"Image\" table\nALTER TABLE [MySchema].[Image] DROP COLUMN [StorageKey];\n","Stmts":[{"Pos":24,"Text":"ALTER TABLE [MySchema].[Image] DROP COLUMN [Description];","Comments":["-- Modify \"Image\" table\n"]},{"Pos":106,"Text":"ALTER TABLE [MySchema].[Image] DROP COLUMN [StorageKey];","Comments":["-- Modify \"Image\" table\n"]}],"URL":"atlas://bitbucket-e2e/plans/pr-3-ae2Xr0Ir","Link":"https://ariga-atlas.atlasgo.cloud/schemas/141733923334/plans/210453398623","Status":"PENDING"}}
-- golden-3.md --
### Atlas detected changes to the desired schema

#### Migration Plan ([View on Atlas Cloud](https://ariga-atlas.atlasgo.cloud/schemas/141733923334/plans/210453398623))

```sql
-- Modify "Image" table
ALTER TABLE [MySchema].[Image] DROP COLUMN [Description];
-- Modify "Image" table
ALTER TABLE [MySchema].[Image] DROP COLUMN [StorageKey];
```

#### Atlas lint results

| Status | Step | Result |
| :----: | :--- | :----- |
| ![](https://release.ariga.io/images/assets/success.svg?v=1) | Detect schema changes | 2 new statements detected |
| ![](https://release.ariga.io/images/assets/error.svg?v=1) | Analyze pr-3-ae2Xr0Ir.sql<br/>1 reports were found in analysis | **Destructive changes detected**<br/>Dropping non-virtual column "Description" [(DS103)](https://atlasgo.io/lint/analyzers#DS103)<br/>Dropping non-virtual column "StorageKey" [(DS103)](https://atlasgo.io/lint/analyzers#DS103) |


---

##### 📝 Steps to edit this migration plan

1\. Run the following command to pull the generated plan to your local workstation:
```bash
atlas schema plan pull --url "atlas://bitbucket-e2e/plans/pr-3-ae2Xr0Ir" > pr-3-ae2Xr0Ir.plan.hcl
```

2\. Open `pr-3-ae2Xr0Ir` in your editor and modify it as needed. Note that the result of the plan should align
the database with the desired state. Otherwise, Atlas will report a schema drift.

3\. Push the updated plan to the registry using the following command:
```bash
atlas schema plan push --pending --file pr-3-ae2Xr0Ir.plan.hcl
```

0 comments on commit 47c2e51

Please sign in to comment.