Repository: ariga/atlas
Pull request #942 (2022-07-13 16:10:14)

sql/postgres: skip comment change in alter column


Patch:
diff --git a/sql/postgres/migrate.go b/sql/postgres/migrate.go
index b389f4ef..24ac70cf 100644
--- a/sql/postgres/migrate.go
+++ b/sql/postgres/migrate.go
@@ -289,7 +289,7 @@ func (s *state) modifyTable(ctx context.Context, modify *schema.ModifyTable) err
 					return err
 				}
 			}
-			alter = append(alter, change)
+			alter = append(alter, &schema.ModifyColumn{To: change.To, From: change.From, Change: k})
 		case *schema.RenameColumn:
 			// "RENAME COLUMN" cannot be combined with other alterations.
 			b := Build("ALTER TABLE").Table(modify.T).P("RENAME COLUMN")
@@ -690,10 +690,7 @@ func (s *state) alterColumn(b *sqlx.Builder, c *schema.ModifyColumn) error {
 			}
 			b.P("DROP EXPRESSION")
 			k &= ^schema.ChangeGenerated
-		case k.Is(schema.ChangeComment):
-			// Handled separately on modifyTable.
-			k &= ^schema.ChangeComment
-		default:
+		default: // e.g. schema.ChangeComment.
 			return fmt.Errorf("unexpected column change: %d", k)
 		}
 		if !k.Is(schema.NoChange) {


Test patch:
diff --git a/internal/integration/testdata/postgres/column-comment.txt b/internal/integration/testdata/postgres/column-comment.txt
new file mode 100644
index 00000000..fa5c6299
--- /dev/null
+++ b/internal/integration/testdata/postgres/column-comment.txt
@@ -0,0 +1,42 @@
+apply 1.hcl
+cmpshow users 1.sql
+
+apply 2.hcl
+cmpshow users 2.sql
+
+-- 1.hcl --
+schema "$db" {}
+
+table "users" {
+  schema = schema.$db
+  column "created_at" {
+    null = false
+    type = timestamp
+    comment = "without time zone"
+  }
+}
+
+-- 1.sql --
+                   Table "script_column_comment.users"
+   Column   |            Type             | Collation | Nullable | Default
+------------+-----------------------------+-----------+----------+---------
+ created_at | timestamp without time zone |           | not null |
+
+-- 2.hcl --
+schema "$db" {}
+
+table "users" {
+  schema = schema.$db
+  column "created_at" {
+    null = false
+    type = timestamptz
+    comment = "with time zone"
+  }
+}
+
+-- 2.sql --
+                 Table "script_column_comment.users"
+   Column   |           Type           | Collation | Nullable | Default
+------------+--------------------------+-----------+----------+---------
+ created_at | timestamp with time zone |           | not null |
+
diff --git a/sql/postgres/migrate_test.go b/sql/postgres/migrate_test.go
index 9503b586..b0175a32 100644
--- a/sql/postgres/migrate_test.go
+++ b/sql/postgres/migrate_test.go
@@ -420,9 +420,9 @@ func TestPlanChanges(t *testing.T) {
 								C: &schema.Column{Name: "name", Type: &schema.ColumnType{Type: &schema.StringType{T: "varchar"}}},
 							},
 							&schema.ModifyColumn{
-								From:   &schema.Column{Name: "id", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}}, Attrs: []schema.Attr{&Identity{}}},
+								From:   &schema.Column{Name: "id", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}}, Attrs: []schema.Attr{&Identity{}, &schema.Comment{Text: "comment"}}},
 								To:     &schema.Column{Name: "id", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}}, Attrs: []schema.Attr{&Identity{Sequence: &Sequence{Start: 1024}}}},
-								Change: schema.ChangeAttr,
+								Change: schema.ChangeAttr | schema.ChangeComment,
 							},
 						},
 					}
@@ -436,6 +436,10 @@ func TestPlanChanges(t *testing.T) {
 						Cmd:     `ALTER TABLE "users" DROP COLUMN "name", ALTER COLUMN "id" SET GENERATED BY DEFAULT SET START WITH 1024 SET INCREMENT BY 1 RESTART`,
 						Reverse: `ALTER TABLE "users" ALTER COLUMN "id" SET GENERATED BY DEFAULT SET START WITH 1 SET INCREMENT BY 1 RESTART, ADD COLUMN "name" character varying NOT NULL`,
 					},
+					{
+						Cmd:     `COMMENT ON COLUMN "users" ."id" IS ''`,
+						Reverse: `COMMENT ON COLUMN "users" ."id" IS 'comment'`,
+					},
 				},
 			},
 		},