Repository: ariga/atlas
Pull request #1266 (2022-11-06 23:33:37)

sql/schema: Add String() method to ChangeKind
This adds a `String() string` method to `ChangeKind` via [golang.org/x/tools/cmd/stringer](https://pkg.go.dev/golang.org/x/tools/cmd/stringer).


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e0dfff0a..a8c0ad84 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -37,6 +37,8 @@ jobs:
           key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
           restore-keys: |
             ${{ runner.os }}-go-
+      - name: Install stringer
+        run: go install golang.org/x/tools/cmd/stringer@latest
       - name: run "go generate ./..."
         run: go generate ./...
       - name: go generate cmd/atlas
@@ -591,3 +593,4 @@ jobs:
       - name: Run integration tests for cockroach
         working-directory: internal/integration
         run: go test -race -count=2 -v -run="Cockroach" -version="cockroach" ./...
+
diff --git a/internal/ci/ci.tmpl b/internal/ci/ci.tmpl
index 44d9a963..7860b101 100644
--- a/internal/ci/ci.tmpl
+++ b/internal/ci/ci.tmpl
@@ -37,6 +37,8 @@ jobs:
           key: {{ "${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}" }}
           restore-keys: |
             {{ "${{ runner.os }}-go-" }}
+      - name: Install stringer
+        run: go install golang.org/x/tools/cmd/stringer@latest
       - name: run "go generate ./..."
         run: go generate ./...
       - name: go generate cmd/atlas
@@ -166,4 +168,4 @@ jobs:
       - name: Run integration tests for {{ .Version }}
         working-directory: internal/integration
         run: go test -race -count=2 -v -run="{{ .Regex }}" -version="{{ .Version }}" ./...
-{{ end }}
\ No newline at end of file
+{{ end }}
diff --git a/sql/postgres/diff.go b/sql/postgres/diff.go
index 8cf6fd0e..71059fb3 100644
--- a/sql/postgres/diff.go
+++ b/sql/postgres/diff.go
@@ -17,6 +17,11 @@ import (
 	"ariga.io/atlas/sql/schema"
 )
 
+// DefaultDiff provides basic diffing capabilities for PostgreSQL dialects.
+// Note, it is recommended to call Open, create a new Driver and use its Differ
+// when a database connection is available.
+var DefaultDiff schema.Differ = &sqlx.Diff{DiffDriver: &diff{}}
+
 // A diff provides a PostgreSQL implementation for sqlx.DiffDriver.
 type diff struct{ conn }
 
@@ -81,12 +86,16 @@ func (d *diff) defaultChanged(from, to *schema.Column) (bool, error) {
 	if trimCast(d1) == trimCast(d2) || quote(d1) == quote(d2) {
 		return false, nil
 	}
-	// Use database comparison in case of mismatch (e.g. `SELECT ARRAY[1] = '{1}'::int[]`).
-	equals, err := d.valuesEqual(d1, d2)
-	if err != nil {
-		return false, err
+	var (
+		err    error
+		equals bool
+	)
+	// In case a database connection is available (not the DefaultDiff), we use the
+	// database comparison in case of mismatch (e.g. `SELECT ARRAY[1] = '{1}'::int[]`).
+	if d.conn.ExecQuerier != nil {
+		equals, err = d.valuesEqual(d1, d2)
 	}
-	return !equals, nil
+	return !equals, err
 }
 
 // generatedChanged reports if the generated expression of a column was changed.
diff --git a/sql/schema/changekind_string.go b/sql/schema/changekind_string.go
new file mode 100644
index 00000000..eec1ef9b
--- /dev/null
+++ b/sql/schema/changekind_string.go
@@ -0,0 +1,55 @@
+// Code generated by "stringer -type ChangeKind"; DO NOT EDIT.
+
+package schema
+
+import "strconv"
+
+func _() {
+	// An "invalid array index" compiler error signifies that the constant values have changed.
+	// Re-run the stringer command to generate them again.
+	var x [1]struct{}
+	_ = x[NoChange-0]
+	_ = x[ChangeAttr-1]
+	_ = x[ChangeCharset-2]
+	_ = x[ChangeCollate-4]
+	_ = x[ChangeComment-8]
+	_ = x[ChangeNull-16]
+	_ = x[ChangeType-32]
+	_ = x[ChangeDefault-64]
+	_ = x[ChangeGenerated-128]
+	_ = x[ChangeUnique-256]
+	_ = x[ChangeParts-512]
+	_ = x[ChangeColumn-1024]
+	_ = x[ChangeRefColumn-2048]
+	_ = x[ChangeRefTable-4096]
+	_ = x[ChangeUpdateAction-8192]
+	_ = x[ChangeDeleteAction-16384]
+}
+
+const _ChangeKind_name = "NoChangeChangeAttrChangeCharsetChangeCollateChangeCommentChangeNullChangeTypeChangeDefaultChangeGeneratedChangeUniqueChangePartsChangeColumnChangeRefColumnChangeRefTableChangeUpdateActionChangeDeleteAction"
+
+var _ChangeKind_map = map[ChangeKind]string{
+	0:     _ChangeKind_name[0:8],
+	1:     _ChangeKind_name[8:18],
+	2:     _ChangeKind_name[18:31],
+	4:     _ChangeKind_name[31:44],
+	8:     _ChangeKind_name[44:57],
+	16:    _ChangeKind_name[57:67],
+	32:    _ChangeKind_name[67:77],
+	64:    _ChangeKind_name[77:90],
+	128:   _ChangeKind_name[90:105],
+	256:   _ChangeKind_name[105:117],
+	512:   _ChangeKind_name[117:128],
+	1024:  _ChangeKind_name[128:140],
+	2048:  _ChangeKind_name[140:155],
+	4096:  _ChangeKind_name[155:169],
+	8192:  _ChangeKind_name[169:187],
+	16384: _ChangeKind_name[187:205],
+}
+
+func (i ChangeKind) String() string {
+	if str, ok := _ChangeKind_map[i]; ok {
+		return str
+	}
+	return "ChangeKind(" + strconv.FormatInt(int64(i), 10) + ")"
+}
diff --git a/sql/schema/generate.go b/sql/schema/generate.go
new file mode 100644
index 00000000..a9eb306f
--- /dev/null
+++ b/sql/schema/generate.go
@@ -0,0 +1,5 @@
+// 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 schema
diff --git a/sql/schema/migrate.go b/sql/schema/migrate.go
index 5067ae14..d41ffeb9 100644
--- a/sql/schema/migrate.go
+++ b/sql/schema/migrate.go
@@ -186,6 +186,8 @@ type (
 
 // A ChangeKind describes a change kind that can be combined
 // using a set of flags. The zero kind is no change.
+//
+//go:generate stringer -type ChangeKind
 type ChangeKind uint
 
 const (


Test patch:
diff --git a/sql/postgres/diff_test.go b/sql/postgres/diff_test.go
index 73424613..d438b7a7 100644
--- a/sql/postgres/diff_test.go
+++ b/sql/postgres/diff_test.go
@@ -510,3 +510,16 @@ func TestDiff_SchemaDiff(t *testing.T) {
 		&schema.AddTable{T: to.Tables[1]},
 	}, changes)
 }
+
+func TestDefaultDiff(t *testing.T) {
+	changes, err := DefaultDiff.SchemaDiff(
+		schema.New("public").
+			AddTables(
+				schema.NewTable("users").AddColumns(schema.NewIntColumn("id", "int")),
+			),
+		schema.New("public"),
+	)
+	require.NoError(t, err)
+	require.Len(t, changes, 1)
+	require.IsType(t, &schema.DropTable{}, changes[0])
+}