Repository: ariga/atlas
Pull request #3026 (2024-08-05 20:48:35)

sql/sqlspec: export mightheredoc helper
need to use it in the cli


Patch:
diff --git a/sql/internal/specutil/convert.go b/sql/internal/specutil/convert.go
index ada57f79..904ef49c 100644
--- a/sql/internal/specutil/convert.go
+++ b/sql/internal/specutil/convert.go
@@ -5,7 +5,6 @@
 package specutil
 
 import (
-	"bytes"
 	"errors"
 	"fmt"
 	"slices"
@@ -683,7 +682,7 @@ func FromView(v *schema.View, colFn ViewColumnSpecFunc, idxFn IndexSpecFunc) (*s
 	}
 	// In case the view definition is multi-line,
 	// format it as indented heredoc with two spaces.
-	as := MightHeredoc(v.Def)
+	as := sqlspec.MightHeredoc(v.Def)
 	embed := &schemahcl.Resource{
 		Attrs: []*schemahcl.Attr{
 			schemahcl.StringAttr("as", as),
@@ -706,39 +705,6 @@ func FromView(v *schema.View, colFn ViewColumnSpecFunc, idxFn IndexSpecFunc) (*s
 	return spec, nil
 }
 
-// normalizeCRLF for heredoc strings that inspected and printed in the HCL as-is to
-// avoid having mixed endings in the printed file - Unix-like (default) and Windows-like.
-func normalizeCRLF(s string) string {
-	if strings.Contains(s, "\r\n") {
-		return strings.ReplaceAll(s, "\r\n", "\n")
-	}
-	return s
-}
-
-// MightHeredoc returns the string as an indented heredoc if it has multiple lines.
-func MightHeredoc(s string) string {
-	s = normalizeCRLF(strings.TrimSpace(s))
-	// In case the given definition is multi-line,
-	// format it as indented heredoc with two spaces.
-	if lines := strings.Split(s, "\n"); len(lines) > 1 {
-		var b bytes.Buffer
-		b.Grow(len(s))
-		b.WriteString("<<-SQL\n")
-		for _, l := range lines {
-			// Skip spaces-only lines, as editors stripped these spaces off,
-			// and HCL parser results a different string for them.
-			if strings.TrimSpace(l) != "" {
-				b.WriteString("  ")
-				b.WriteString(l)
-			}
-			b.WriteByte('\n')
-		}
-		b.WriteString("  SQL")
-		s = b.String()
-	}
-	return s
-}
-
 // dependsOn returns the depends_on attribute for the given objects.
 func dependsOn(realm *schema.Realm, objects []schema.Object) (*schemahcl.Attr, bool) {
 	var (
diff --git a/sql/sqlspec/sqlspec.go b/sql/sqlspec/sqlspec.go
index 4060da35..12bca2f3 100644
--- a/sql/sqlspec/sqlspec.go
+++ b/sql/sqlspec/sqlspec.go
@@ -5,6 +5,9 @@
 package sqlspec
 
 import (
+	"bytes"
+	"strings"
+
 	"ariga.io/atlas/schemahcl"
 
 	"github.com/zclconf/go-cty/cty"
@@ -192,3 +195,36 @@ func init() {
 	schemahcl.Register("sequence", &Sequence{})
 	schemahcl.Register("schema", &Schema{})
 }
+
+// normalizeCRLF for heredoc strings that inspected and printed in the HCL as-is to
+// avoid having mixed endings in the printed file - Unix-like (default) and Windows-like.
+func normalizeCRLF(s string) string {
+	if strings.Contains(s, "\r\n") {
+		return strings.ReplaceAll(s, "\r\n", "\n")
+	}
+	return s
+}
+
+// MightHeredoc returns the string as an indented heredoc if it has multiple lines.
+func MightHeredoc(s string) string {
+	s = normalizeCRLF(strings.TrimSpace(s))
+	// In case the given definition is multi-line,
+	// format it as indented heredoc with two spaces.
+	if lines := strings.Split(s, "\n"); len(lines) > 1 {
+		var b bytes.Buffer
+		b.Grow(len(s))
+		b.WriteString("<<-SQL\n")
+		for _, l := range lines {
+			// Skip spaces-only lines, as editors stripped these spaces off,
+			// and HCL parser results a different string for them.
+			if strings.TrimSpace(l) != "" {
+				b.WriteString("  ")
+				b.WriteString(l)
+			}
+			b.WriteByte('\n')
+		}
+		b.WriteString("  SQL")
+		s = b.String()
+	}
+	return s
+}


Test patch:
diff --git a/sql/internal/specutil/convert_test.go b/sql/internal/specutil/convert_test.go
index dabf339c..70dee7b3 100644
--- a/sql/internal/specutil/convert_test.go
+++ b/sql/internal/specutil/convert_test.go
@@ -180,49 +180,3 @@ func TestFromView(t *testing.T) {
 	require.NoError(t, err)
 	require.Equal(t, "<<-SQL\n  SELECT * FROM users\n   WHERE c NOT LIKE \"\\r\\n\"\n  SQL", s)
 }
-
-func TestMightHeredoc(t *testing.T) {
-	for _, tt := range []struct {
-		input    string
-		expected string
-	}{
-		{
-			input:    "SELECT * FROM users",
-			expected: "SELECT * FROM users",
-		},
-		{
-			input: `
-SELECT
-  *
-  FROM users
-  WHERE active`,
-			expected: `<<-SQL
-  SELECT
-    *
-    FROM users
-    WHERE active
-  SQL`,
-		},
-		{
-			input: `
--- The line below includes spaces.
-  
-	
-SELECT
-  *
-  FROM users
-  WHERE active`,
-			expected: `<<-SQL
-  -- The line below includes spaces.
-
-
-  SELECT
-    *
-    FROM users
-    WHERE active
-  SQL`,
-		},
-	} {
-		require.Equal(t, tt.expected, MightHeredoc(tt.input))
-	}
-}
diff --git a/sql/sqlspec/sqlspec_test.go b/sql/sqlspec/sqlspec_test.go
new file mode 100644
index 00000000..a8425236
--- /dev/null
+++ b/sql/sqlspec/sqlspec_test.go
@@ -0,0 +1,57 @@
+// Copyright 2021-present The Atlas Authors. All rights reserved.
+// This source code is licensed under the Apache 2.0 license found
+// in the LICENSE file in the root directory of this source tree.
+
+package sqlspec
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestMightHeredoc(t *testing.T) {
+	for _, tt := range []struct {
+		input    string
+		expected string
+	}{
+		{
+			input:    "SELECT * FROM users",
+			expected: "SELECT * FROM users",
+		},
+		{
+			input: `
+SELECT
+  *
+  FROM users
+  WHERE active`,
+			expected: `<<-SQL
+  SELECT
+    *
+    FROM users
+    WHERE active
+  SQL`,
+		},
+		{
+			input: `
+-- The line below includes spaces.
+  
+	
+SELECT
+  *
+  FROM users
+  WHERE active`,
+			expected: `<<-SQL
+  -- The line below includes spaces.
+
+
+  SELECT
+    *
+    FROM users
+    WHERE active
+  SQL`,
+		},
+	} {
+		require.Equal(t, tt.expected, MightHeredoc(tt.input))
+	}
+}