Repository: ariga/atlas
Pull request #1863 (2023-07-16 11:25:54)

sql/postgres: skip comparing default public schema comments


Patch:
diff --git a/sql/postgres/diff.go b/sql/postgres/diff.go
index 083d6fc3..cb35d9ab 100644
--- a/sql/postgres/diff.go
+++ b/sql/postgres/diff.go
@@ -29,12 +29,20 @@ type diff struct{ conn }
 // SchemaAttrDiff returns a changeset for migrating schema attributes from one state to the other.
 func (*diff) SchemaAttrDiff(from, to *schema.Schema) []schema.Change {
 	var changes []schema.Change
-	if change := sqlx.CommentDiff(from.Attrs, to.Attrs); change != nil {
+	if change := sqlx.CommentDiff(skipDefaultComment(from), skipDefaultComment(to)); change != nil {
 		changes = append(changes, change)
 	}
 	return changes
 }
 
+func skipDefaultComment(s *schema.Schema) []schema.Attr {
+	attrs := s.Attrs
+	if c := (schema.Comment{}); sqlx.Has(attrs, &c) && c.Text == "standard public schema" && (s.Name == "" || s.Name == "public") {
+		attrs = schema.RemoveAttr[*schema.Comment](attrs)
+	}
+	return attrs
+}
+
 // SchemaObjectDiff returns a changeset for migrating schema objects from
 // one state to the other.
 func (*diff) SchemaObjectDiff(from, to *schema.Schema) ([]schema.Change, error) {


Test patch:
diff --git a/internal/integration/postgres_test.go b/internal/integration/postgres_test.go
index 259d6301..bea11297 100644
--- a/internal/integration/postgres_test.go
+++ b/internal/integration/postgres_test.go
@@ -991,9 +991,7 @@ CREATE TABLE "public"."users" ("id" integer NOT NULL);
 CREATE TABLE "other"."posts" ("id" integer NOT NULL);`, diff("test1?sslmode=disable", "test2?sslmode=disable"))
 
 		// Diffing schema should both tables and comments (from 'public' to 'other').
-		require.Equal(t, `-- Set comment to schema: ""
-COMMENT ON SCHEMA IS 'standard public schema';
--- Create "users" table
+		require.Equal(t, `-- Create "users" table
 CREATE TABLE "users" ("id" integer NOT NULL);
 -- Drop "posts" table
 DROP TABLE "posts";`, diff("test2?sslmode=disable&search_path=other", "test2?sslmode=disable&search_path=public"))
diff --git a/sql/postgres/diff_test.go b/sql/postgres/diff_test.go
index cb741052..cceda18f 100644
--- a/sql/postgres/diff_test.go
+++ b/sql/postgres/diff_test.go
@@ -522,6 +522,48 @@ func TestDiff_SchemaDiff(t *testing.T) {
 			},
 		}},
 	}, changes)
+
+	t.Run("DefaultComment", func(t *testing.T) {
+		from, to := schema.New("public").SetComment("standard public schema"), schema.New("public")
+		changes, err = drv.SchemaDiff(from, to)
+		require.NoError(t, err)
+		require.Empty(t, changes)
+
+		changes, err = drv.SchemaDiff(to, from)
+		require.NoError(t, err)
+		require.Empty(t, changes)
+
+		from.Name, to.Name = "", ""
+		changes, err = drv.SchemaDiff(from, to)
+		require.NoError(t, err)
+		require.Empty(t, changes)
+
+		from.Name, to.Name = "other", "other"
+		changes, err = drv.SchemaDiff(from, to)
+		require.NoError(t, err)
+		require.Len(t, changes, 1)
+		require.EqualValues(t, []schema.Change{
+			&schema.ModifySchema{S: to, Changes: schema.Changes{
+				&schema.ModifyAttr{
+					From: &schema.Comment{Text: "standard public schema"},
+					To:   &schema.Comment{Text: ""},
+				},
+			}},
+		}, changes)
+
+		from.Name, to.Name = "public", "public"
+		from.SetComment("custom public schema")
+		changes, err = drv.SchemaDiff(from, to)
+		require.NoError(t, err)
+		require.EqualValues(t, []schema.Change{
+			&schema.ModifySchema{S: to, Changes: schema.Changes{
+				&schema.ModifyAttr{
+					From: &schema.Comment{Text: "custom public schema"},
+					To:   &schema.Comment{Text: ""},
+				},
+			}},
+		}, changes)
+	})
 }
 
 func TestDefaultDiff(t *testing.T) {