From 49fadfa7489421024e274bca69a587adb434fbc1 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Wed, 18 Dec 2024 12:41:12 +0700 Subject: [PATCH] atlasaction: `codeblock` with markdown --- atlasaction/action.go | 3 +-- atlasaction/action_test.go | 4 +-- atlasaction/comments/schema-plan.tmpl | 9 +------ .../testdata/github/schema-apply-envs.txtar | 27 ++++++++++++++----- .../testdata/github/schema-apply-lint.txtar | 9 +++++-- atlasaction/testdata/gitlab/schema-plan.txtar | 1 - 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/atlasaction/action.go b/atlasaction/action.go index e031d2bf..a3217cf8 100644 --- a/atlasaction/action.go +++ b/atlasaction/action.go @@ -1127,7 +1127,7 @@ var ( }, "join": strings.Join, "codeblock": func(lang, code string) string { - return fmt.Sprintf("
%s
", lang, code) + return fmt.Sprintf("\n\n```%s\n%s\n```\n\n", lang, strings.Trim(code, "\n")) }, "details": func(label, details string) string { return fmt.Sprintf("
%s%s
", label, details) @@ -1154,7 +1154,6 @@ var ( // clicking on the image to view the full size. return fmt.Sprintf(``, src, attrs), nil }, - "trimRight": strings.TrimRight, }). ParseFS(comments, "comments/*.tmpl"), ) diff --git a/atlasaction/action_test.go b/atlasaction/action_test.go index e2174783..157feae9 100644 --- a/atlasaction/action_test.go +++ b/atlasaction/action_test.go @@ -1728,7 +1728,7 @@ func TestApplyTemplateGeneration(t *testing.T) { End: must(time.Parse(time.RFC3339, "2024-06-16T15:27:38.963743+03:00")), }, // language=markdown - expected: "

Migration Passed

atlas migrate apply Summary:

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ParameterDetails
Migration Directorytestdata/migrations
Database URLsqlite://file?_fk=1&mode=memory
Migrate to Version\n 20221108173658\n
SQL Summary2 migration files, 3 statements passed
Total Time54.297ms

Version 20221108173626.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
225.765ms--
๐Ÿ“„ View SQL Statements
CREATE TABLE `dept_emp_latest_date` (`emp_no` int NOT NULL, `from_date` date NULL, `to_date` date NULL) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci COMMENT \"VIEW\";\nCREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

Version 20221108173658.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
123.4ms--
๐Ÿ“„ View SQL Statements
CREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
", + expected: "

Migration Passed

atlas migrate apply Summary:

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ParameterDetails
Migration Directorytestdata/migrations
Database URLsqlite://file?_fk=1&mode=memory
Migrate to Version\n 20221108173658\n
SQL Summary2 migration files, 3 statements passed
Total Time54.297ms

Version 20221108173626.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
225.765ms--
๐Ÿ“„ View SQL Statements\n\n```sql\nCREATE TABLE `dept_emp_latest_date` (`emp_no` int NOT NULL, `from_date` date NULL, `to_date` date NULL) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci COMMENT \"VIEW\";\nCREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;\n```\n\n

Version 20221108173658.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
123.4ms--
๐Ÿ“„ View SQL Statements\n\n```sql\nCREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;\n```\n\n
", }, { name: "2 files, 1 statement error", @@ -1794,7 +1794,7 @@ func TestApplyTemplateGeneration(t *testing.T) { Error: "sql/migrate: executing statement \"create Table Err?\" from version \"20240616125213\": Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1", }, // language=markdown - expected: "

Migration Failed

atlas migrate apply Summary:

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ParameterDetails
Migration Directorytestdata/migrations
Database URLmysql://localhost:3306/test?parseTime=true
Migrate from Version20221108143624
Migrate to Version\n 20221108173658\n
SQL Summary2 migration files, 2 statements passed, 1 failed
Total Time54.297ms

Version 20221108173626.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
125.765ms--
๐Ÿ“„ View SQL Statements
CREATE TABLE Persons ( PersonID int );

Version 20221108173658.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
123.4msError 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
๐Ÿ“„ View
create Table Err?
๐Ÿ“„ View SQL Statements
create Table Err?
", + expected: "

Migration Failed

atlas migrate apply Summary:

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ParameterDetails
Migration Directorytestdata/migrations
Database URLmysql://localhost:3306/test?parseTime=true
Migrate from Version20221108143624
Migrate to Version\n 20221108173658\n
SQL Summary2 migration files, 2 statements passed, 1 failed
Total Time54.297ms

Version 20221108173626.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
125.765ms--
๐Ÿ“„ View SQL Statements\n\n```sql\nCREATE TABLE Persons ( PersonID int );\n```\n\n

Version 20221108173658.sql:

\n\n \n \n \n \n \n \n \n \n \n
StatusExecuted StatementsExecution TimeErrorError Statement
123.4msError 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
๐Ÿ“„ View\n\n```sql\ncreate Table Err?\n```\n\n
๐Ÿ“„ View SQL Statements\n\n```sql\ncreate Table Err?\n```\n\n
", }, { name: "no work migration", diff --git a/atlasaction/comments/schema-plan.tmpl b/atlasaction/comments/schema-plan.tmpl index a9d72c44..f0871053 100644 --- a/atlasaction/comments/schema-plan.tmpl +++ b/atlasaction/comments/schema-plan.tmpl @@ -1,13 +1,6 @@

Atlas detected changes to the desired schema

Migration Plan {{- with .Plan.File.Link }} ({{- . | link "View on Atlas Cloud" -}}){{ end }}

- -{{/* Respect double new lines as they are skipped in
 blocks. */}}
-{{- with .Plan.File.Migration }}
-```sql
-{{ trimRight . "\n" }}
-```
-{{- end }}
-
+{{- with .Plan.File.Migration }}{{ codeblock "sql" . }}{{- end -}}
 

Atlas lint results

{{- template "schema-lint" . -}}
diff --git a/atlasaction/testdata/github/schema-apply-envs.txtar b/atlasaction/testdata/github/schema-apply-envs.txtar index 476f921c..a49f4ab4 100644 --- a/atlasaction/testdata/github/schema-apply-envs.txtar +++ b/atlasaction/testdata/github/schema-apply-envs.txtar @@ -61,9 +61,14 @@ _GitHubActionsFileCommandDelimeter_
1 234ยตs-- -
๐Ÿ“„ View Pre-planned SQL Statements
-- Add column "c2" to table: "t4"
+
๐Ÿ“„ View Pre-planned SQL Statements + +```sql +-- Add column "c2" to table: "t4" ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL; -
+``` + +

Migration Passed

atlas schema apply Summary:

@@ -96,9 +101,14 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL; -
Database URL
1 315ยตs--
๐Ÿ“„ View Pre-planned SQL Statements
-- Add column "c2" to table: "t4"
+
๐Ÿ“„ View Pre-planned SQL Statements + +```sql +-- Add column "c2" to table: "t4" ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL; -
+``` + +

Migration Passed

atlas schema apply Summary:

@@ -131,6 +141,11 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL; -
Database URL
1 221ยตs--
๐Ÿ“„ View Pre-planned SQL Statements
-- Add column "c2" to table: "t4"
+
๐Ÿ“„ View Pre-planned SQL Statements + +```sql +-- Add column "c2" to table: "t4" ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL; -
+``` + + diff --git a/atlasaction/testdata/github/schema-apply-lint.txtar b/atlasaction/testdata/github/schema-apply-lint.txtar index e61290a7..9d9ccdba 100644 --- a/atlasaction/testdata/github/schema-apply-lint.txtar +++ b/atlasaction/testdata/github/schema-apply-lint.txtar @@ -32,13 +32,18 @@ _GitHubActionsFileCommandDelimeter_ To Hash Rl4lBdMkvFoGQ4xu+3sYCeogTVnamJ7bmDoq9pMXcjw= - ErrorRejected by review policy: errors or warnings were found
๐Ÿ“„ View SQL Statements
-- Disable the enforcement of foreign-keys constraints
+  ErrorRejected by review policy: errors or warnings were found
๐Ÿ“„ View SQL Statements + +```sql +-- 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; -

Atlas lint results

+``` + +

Atlas lint results

diff --git a/atlasaction/testdata/gitlab/schema-plan.txtar b/atlasaction/testdata/gitlab/schema-plan.txtar index 396a09ac..e472676c 100644 --- a/atlasaction/testdata/gitlab/schema-plan.txtar +++ b/atlasaction/testdata/gitlab/schema-plan.txtar @@ -116,7 +116,6 @@ schema plan --format {{ json . }} --context {"repo":"my-project","branch":"test-

Atlas detected changes to the desired schema

Migration Plan (View on Atlas Cloud)

- ```sql -- Create "1" table CREATE TABLE `1` (
Status