Repository: ariga/atlas
Pull request #2094 (2023-09-18 06:29:11)

sql/postgres: remove unused collate/ctype attributes form inspection
Fixed https://github.com/ariga/atlas/issues/2093
Fixed https://github.com/ent/ent/issues/3750
Fixed https://github.com/mikestefanello/pagoda/issues/39


Patch:
diff --git a/sql/postgres/driver.go b/sql/postgres/driver.go
index f19e5bbb..ef78e730 100644
--- a/sql/postgres/driver.go
+++ b/sql/postgres/driver.go
@@ -36,8 +36,6 @@ type (
 		// The schema in the `search_path` parameter (if given).
 		schema string
 		// System variables that are set on `Open`.
-		collate string
-		ctype   string
 		version int
 		crdb    bool
 	}
@@ -95,10 +93,9 @@ func Open(db schema.ExecQuerier) (migrate.Driver, error) {
 	if err != nil {
 		return nil, fmt.Errorf("postgres: failed scanning rows: %w", err)
 	}
-	if len(params) != 3 && len(params) != 4 {
+	if len(params) != 1 && len(params) != 2 {
 		return nil, fmt.Errorf("postgres: unexpected number of rows: %d", len(params))
 	}
-	c.ctype, c.collate = params[1], params[2]
 	if c.version, err = strconv.Atoi(params[0]); err != nil {
 		return nil, fmt.Errorf("postgres: malformed version: %s: %w", params[0], err)
 	}
@@ -106,7 +103,7 @@ func Open(db schema.ExecQuerier) (migrate.Driver, error) {
 		return nil, fmt.Errorf("postgres: unsupported postgres version: %d", c.version)
 	}
 	// Means we are connected to CockroachDB because we have a result for name='crdb_version'. see `paramsQuery`.
-	if c.crdb = len(params) == 4; c.crdb {
+	if c.crdb = len(params) == 2; c.crdb {
 		return noLockDriver{
 			&Driver{
 				conn:        c,
diff --git a/sql/postgres/inspect.go b/sql/postgres/inspect.go
index fbcbe411..2c789f09 100644
--- a/sql/postgres/inspect.go
+++ b/sql/postgres/inspect.go
@@ -34,8 +34,7 @@ func (i *inspect) InspectRealm(ctx context.Context, opts *schema.InspectRealmOpt
 	if opts == nil {
 		opts = &schema.InspectRealmOption{}
 	}
-	r := schema.NewRealm(schemas...).SetCollation(i.collate)
-	r.Attrs = append(r.Attrs, &CType{V: i.ctype})
+	r := schema.NewRealm(schemas...)
 	if len(schemas) > 0 {
 		mode := sqlx.ModeInspectRealm(opts)
 		if mode.Is(schema.InspectTables) {
@@ -72,8 +71,7 @@ func (i *inspect) InspectSchema(ctx context.Context, name string, opts *schema.I
 	if opts == nil {
 		opts = &schema.InspectOptions{}
 	}
-	r := schema.NewRealm(schemas...).SetCollation(i.collate)
-	r.Attrs = append(r.Attrs, &CType{V: i.ctype})
+	r := schema.NewRealm(schemas...)
 	if sqlx.ModeInspectSchema(opts).Is(schema.InspectTables) {
 		if err := i.inspectTables(ctx, r, opts); err != nil {
 			return nil, err
@@ -702,12 +700,6 @@ func canConvert(t *schema.ColumnType, x string) (string, bool) {
 }
 
 type (
-	// CType describes the character classification setting (LC_CTYPE).
-	CType struct {
-		schema.Attr
-		V string
-	}
-
 	// UserDefinedType defines a user-defined type attribute.
 	UserDefinedType struct {
 		schema.Type
@@ -1161,7 +1153,7 @@ func newEnumType(t string, id int64) *enumType {
 
 const (
 	// Query to list runtime parameters.
-	paramsQuery = `SELECT setting FROM pg_settings WHERE name IN ('lc_collate', 'lc_ctype', 'server_version_num', 'crdb_version') ORDER BY name DESC`
+	paramsQuery = `SELECT setting FROM pg_settings WHERE name IN ('server_version_num', 'crdb_version') ORDER BY name DESC`
 
 	// Query to list database schemas.
 	schemasQuery = `


Test patch:
diff --git a/internal/integration/cockroach_test.go b/internal/integration/cockroach_test.go
index 293fca56..2851b360 100644
--- a/internal/integration/cockroach_test.go
+++ b/internal/integration/cockroach_test.go
@@ -897,10 +897,6 @@ func (t *crdbTest) realm() *schema.Realm {
 				Name: "public",
 			},
 		},
-		Attrs: []schema.Attr{
-			&schema.Collation{V: "C.UTF-8"},
-			&postgres.CType{V: "C.UTF-8"},
-		},
 	}
 	r.Schemas[0].Realm = r
 	return r
diff --git a/internal/integration/postgres_test.go b/internal/integration/postgres_test.go
index cf1c67e8..c118e12d 100644
--- a/internal/integration/postgres_test.go
+++ b/internal/integration/postgres_test.go
@@ -1611,10 +1611,6 @@ func (t *pgTest) realm() *schema.Realm {
 				},
 			},
 		},
-		Attrs: []schema.Attr{
-			&schema.Collation{V: "en_US.utf8"},
-			&postgres.CType{V: "en_US.utf8"},
-		},
 	}
 	r.Schemas[0].Realm = r
 	return r
diff --git a/sql/postgres/inspect_test.go b/sql/postgres/inspect_test.go
index f95331d1..edf0c170 100644
--- a/sql/postgres/inspect_test.go
+++ b/sql/postgres/inspect_test.go
@@ -431,12 +431,10 @@ func TestDriver_InspectCRDBSchema(t *testing.T) {
 	mk := mock{m}
 	mk.ExpectQuery(sqltest.Escape(paramsQuery)).
 		WillReturnRows(sqltest.Rows(`
-					setting
-				------------
-				130000
-				en_US.utf8
-				en_US.utf8
-				cockroach
+	setting
+------------
+130000
+cockroach
 				`))
 	drv, err := Open(db)
 	require.NoError(t, err)
@@ -527,15 +525,6 @@ func TestDriver_InspectSchema(t *testing.T) {
 			Schemas: []*schema.Schema{
 				schema.New("test").SetComment("boring"),
 			},
-			// Server default configuration.
-			Attrs: []schema.Attr{
-				&schema.Collation{
-					V: "en_US.utf8",
-				},
-				&CType{
-					V: "en_US.utf8",
-				},
-			},
 		}
 		r.Schemas[0].Realm = r
 		return r.Schemas[0]
@@ -575,15 +564,6 @@ func TestDriver_Realm(t *testing.T) {
 					Name: "public",
 				},
 			},
-			// Server default configuration.
-			Attrs: []schema.Attr{
-				&schema.Collation{
-					V: "en_US.utf8",
-				},
-				&CType{
-					V: "en_US.utf8",
-				},
-			},
 		}
 		r.Schemas[0].Realm = r
 		r.Schemas[1].Realm = r
@@ -618,15 +598,6 @@ func TestDriver_Realm(t *testing.T) {
 					Name: "public",
 				},
 			},
-			// Server default configuration.
-			Attrs: []schema.Attr{
-				&schema.Collation{
-					V: "en_US.utf8",
-				},
-				&CType{
-					V: "en_US.utf8",
-				},
-			},
 		}
 		r.Schemas[0].Realm = r
 		r.Schemas[1].Realm = r
@@ -656,15 +627,6 @@ func TestDriver_Realm(t *testing.T) {
 					Name: "test",
 				},
 			},
-			// Server default configuration.
-			Attrs: []schema.Attr{
-				&schema.Collation{
-					V: "en_US.utf8",
-				},
-				&CType{
-					V: "en_US.utf8",
-				},
-			},
 		}
 		r.Schemas[0].Realm = r
 		return r
@@ -698,15 +660,6 @@ func TestInspectMode_InspectRealm(t *testing.T) {
 					Name: "public",
 				},
 			},
-			// Server default configuration.
-			Attrs: []schema.Attr{
-				&schema.Collation{
-					V: "en_US.utf8",
-				},
-				&CType{
-					V: "en_US.utf8",
-				},
-			},
 		}
 		r.Schemas[0].Realm = r
 		r.Schemas[1].Realm = r
@@ -737,8 +690,6 @@ func (m mock) version(version string) {
   setting
 ------------
  ` + version + `
- en_US.utf8
- en_US.utf8
 `))
 }