Repository: ariga/atlas
Pull request #1002 (2022-08-03 07:34:25)

sql/migrate: only take .sql files into the sumfile 


Patch:
diff --git a/sql/migrate/dir.go b/sql/migrate/dir.go
index 198e8aa9..e63520c0 100644
--- a/sql/migrate/dir.go
+++ b/sql/migrate/dir.go
@@ -13,7 +13,6 @@ import (
 	"fmt"
 	"io"
 	"io/fs"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
@@ -226,40 +225,22 @@ func HashSum(dir Dir) (HashFile, error) {
 		hs HashFile
 		h  = sha256.New()
 	)
-	err := fs.WalkDir(dir, "", func(path string, d fs.DirEntry, err error) error {
-		if err != nil {
-			return err
+	files, err := dir.Files()
+	if err != nil {
+		return nil, err
+	}
+	for _, f := range files {
+		if _, err = h.Write([]byte(f.Name())); err != nil {
+			return nil, err
 		}
-		// If this is the integrity sum file do not include it into the sum.
-		if filepath.Base(path) == HashFileName {
-			return nil
+		// Check if this file contains an "atlas:sum" directive and if so, act to it.
+		if mode, ok := directive(string(f.Bytes()), directiveSum); ok && mode == sumModeIgnore {
+			continue
 		}
-		if !d.IsDir() {
-			f, err := dir.Open(path)
-			if err != nil {
-				return err
-			}
-			defer f.Close()
-			if _, err := h.Write([]byte(path)); err != nil {
-				return err
-			}
-			c, err := ioutil.ReadAll(f)
-			if err != nil {
-				return err
-			}
-			// Check if this file contains an "atlas:sum" directive and if so, act to it.
-			if mode, ok := directive(string(c), directiveSum); ok && mode == sumModeIgnore {
-				return nil
-			}
-			if _, err := h.Write(c); err != nil {
-				return err
-			}
-			hs = append(hs, struct{ N, H string }{path, base64.StdEncoding.EncodeToString(h.Sum(nil))})
+		if _, err = h.Write(f.Bytes()); err != nil {
+			return nil, err
 		}
-		return nil
-	})
-	if err != nil {
-		return nil, err
+		hs = append(hs, struct{ N, H string }{f.Name(), base64.StdEncoding.EncodeToString(h.Sum(nil))})
 	}
 	return hs, nil
 }


Test patch:
diff --git a/cmd/atlas/internal/cmdapi/migrate_test.go b/cmd/atlas/internal/cmdapi/migrate_test.go
index c1fa260b..6ed4dfb8 100644
--- a/cmd/atlas/internal/cmdapi/migrate_test.go
+++ b/cmd/atlas/internal/cmdapi/migrate_test.go
@@ -328,6 +328,9 @@ func TestMigrate_New(t *testing.T) {
 	require.FileExists(t, filepath.Join(p, "atlas.sum"))
 	require.Equal(t, 3, countFiles(t, p))
 
+	_, err = runCmd(Root, "migrate", "hash", "--dir", "file://"+p, "--force", "--dir-format", formatGolangMigrate)
+	require.NoError(t, err)
+	MigrateFlags.Force = false
 	s, err = runCmd(Root, "migrate", "new", "golang-migrate", "--dir", "file://"+p, "--dir-format", formatGolangMigrate)
 	require.Zero(t, s)
 	require.NoError(t, err)
@@ -335,6 +338,9 @@ func TestMigrate_New(t *testing.T) {
 	require.FileExists(t, filepath.Join(p, v+"_golang-migrate.down.sql"))
 	require.Equal(t, 5, countFiles(t, p))
 
+	s, err = runCmd(Root, "migrate", "hash", "--dir", "file://"+p, "--force", "--dir-format", formatGoose)
+	require.NoError(t, err)
+	MigrateFlags.Force = false
 	s, err = runCmd(Root, "migrate", "new", "goose", "--dir", "file://"+p, "--dir-format", formatGoose)
 	require.Zero(t, s)
 	require.NoError(t, err)
diff --git a/sql/migrate/dir_test.go b/sql/migrate/dir_test.go
index 405203eb..c9975415 100644
--- a/sql/migrate/dir_test.go
+++ b/sql/migrate/dir_test.go
@@ -41,25 +41,38 @@ func TestHashSum(t *testing.T) {
 	require.Equal(t, 1, countFiles(t, d))
 	requireFileEqual(t, d, v+"_plan.sql", "cmd;\n")
 
+	// Files not ending with .sql get ignored.
+	p = t.TempDir()
+	d, err = migrate.NewLocalDir(p)
+	require.NoError(t, err)
+	pl = migrate.NewPlanner(nil, d)
+	require.NotNil(t, pl)
+	require.NoError(t, os.WriteFile(filepath.Join(p, "include.sql"), nil, 0600))
+	require.NoError(t, os.WriteFile(filepath.Join(p, "exclude.txt"), nil, 0600))
+	require.NoError(t, pl.WritePlan(plan))
+	require.Equal(t, 4, countFiles(t, d))
+	c, err := os.ReadFile(filepath.Join(p, "atlas.sum"))
+	require.NoError(t, err)
+	require.Contains(t, string(c), "include.sql")
+	require.NotContains(t, string(c), "exclude.txt")
+
 	// Files with directive in first line get ignored.
 	p = t.TempDir()
 	d, err = migrate.NewLocalDir(p)
 	require.NoError(t, err)
 	pl = migrate.NewPlanner(nil, d)
 	require.NotNil(t, pl)
-	require.NoError(t, os.WriteFile(filepath.Join(p, "include"), []byte("//atlas:sum\nfoo"), 0600))
-	require.NoError(t, os.WriteFile(filepath.Join(p, "exclude_1"), []byte("//atlas:sum ignore bar"), 0600))
-	require.NoError(t, os.WriteFile(filepath.Join(p, "exclude_2"), []byte("//atlas:sum ignore\nbar"), 0600))
-	require.NoError(t, os.WriteFile(filepath.Join(p, "exclude_3"), []byte("atlas:sum ignore"), 0600))
+	require.NoError(t, os.WriteFile(filepath.Join(p, "include.sql"), []byte("//atlas:sum\nfoo"), 0600))
+	require.NoError(t, os.WriteFile(filepath.Join(p, "exclude_1.sql"), []byte("//atlas:sum ignore\nbar"), 0600))
+	require.NoError(t, os.WriteFile(filepath.Join(p, "exclude_2.sql"), []byte("atlas:sum ignore"), 0600))
 	require.NoError(t, pl.WritePlan(plan))
-	require.Equal(t, 6, countFiles(t, d))
+	require.Equal(t, 5, countFiles(t, d))
 	requireFileEqual(t, d, v+"_plan.sql", "cmd;\n")
-	c, err := os.ReadFile(filepath.Join(p, "atlas.sum"))
+	c, err = os.ReadFile(filepath.Join(p, "atlas.sum"))
 	require.NoError(t, err)
 	require.Contains(t, string(c), "include")
-	require.NotContains(t, string(c), "exclude_2")
-	require.NotContains(t, string(c), "exclude_2")
-	require.NotContains(t, string(c), "exclude_3")
+	require.NotContains(t, string(c), "exclude_1.sql")
+	require.NotContains(t, string(c), "exclude_2.sql")
 }
 
 //go:embed testdata/migrate/atlas.sum
diff --git a/sql/migrate/testdata/migrate/atlas.sum b/sql/migrate/testdata/migrate/atlas.sum
index 23c4fc95..7660de17 100644
--- a/sql/migrate/testdata/migrate/atlas.sum
+++ b/sql/migrate/testdata/migrate/atlas.sum
@@ -1,6 +1,3 @@
-h1:rXkukFq7O3f2xgfZZi97Mh5Yd6MYRy6XmMYMV+Z9S9c=
+h1:M74RrNK69S2pj6C541LR1ew5O32/i0WoyNgsJmyuiUk=
 1_initial.down.sql h1:0zypK43rgPbgvVUgVJABGN25VgM1QSeU+LJDBb8cEQI=
 1_initial.up.sql h1:hFhs5XhRml4KTWGF5td6h1s7xNqAFnaEBbC5Y/NF7i4=
-sub/1.a_sub.up.sql h1:GomhYquMSavc1O/WeukxL02orVIHO2EUK6pWsedwELc=
-sub/2.10.x-20_description.sql h1:9HvZYZZMi9S7eLQvNAR9W9BxaMRvFz0A9TUmpdEHt4o=
-sub/3_partly.sql h1:WC7hyKTXrRB3woOqTAdc2m7XSLnbctgtF+W2ozDLUVc=