Repository: ariga/atlas
Pull request #1322 (2022-11-30 20:09:18)

sql/postgres: use shared uuid type


Patch:
diff --git a/sql/postgres/convert.go b/sql/postgres/convert.go
index 3162cc70..7b6ec0fe 100644
--- a/sql/postgres/convert.go
+++ b/sql/postgres/convert.go
@@ -136,7 +136,7 @@ func FormatType(t schema.Type) (string, error) {
 		}
 	case *schema.JSONType:
 		f = strings.ToLower(t.T)
-	case *UUIDType:
+	case *schema.UUIDType:
 		f = strings.ToLower(t.T)
 	case *schema.SpatialType:
 		f = strings.ToLower(t.T)
@@ -244,7 +244,7 @@ func columnType(c *columnDesc) (schema.Type, error) {
 	case TypeSmallSerial, TypeSerial, TypeBigSerial, TypeSerial2, TypeSerial4, TypeSerial8:
 		typ = &SerialType{T: t, Precision: int(c.precision)}
 	case TypeUUID:
-		typ = &UUIDType{T: t}
+		typ = &schema.UUIDType{T: t}
 	case TypeXML:
 		typ = &XMLType{T: t}
 	case TypeArray:
diff --git a/sql/postgres/diff.go b/sql/postgres/diff.go
index 328f5c1b..f4209fa9 100644
--- a/sql/postgres/diff.go
+++ b/sql/postgres/diff.go
@@ -233,7 +233,7 @@ func (d *diff) typeChanged(from, to *schema.Column) (bool, error) {
 	switch fromT := fromT.(type) {
 	case *schema.BinaryType, *BitType, *schema.BoolType, *schema.DecimalType, *schema.FloatType,
 		*IntervalType, *schema.IntegerType, *schema.JSONType, *RangeType, *SerialType, *schema.SpatialType,
-		*schema.StringType, *schema.TimeType, *TextSearchType, *NetworkType, *UserDefinedType:
+		*schema.StringType, *schema.TimeType, *TextSearchType, *NetworkType, *UserDefinedType, *schema.UUIDType:
 		t1, err := FormatType(toT)
 		if err != nil {
 			return false, err
@@ -251,9 +251,6 @@ func (d *diff) typeChanged(from, to *schema.Column) (bool, error) {
 	case *CurrencyType:
 		toT := toT.(*CurrencyType)
 		changed = fromT.T != toT.T
-	case *UUIDType:
-		toT := toT.(*UUIDType)
-		changed = fromT.T != toT.T
 	case *XMLType:
 		toT := toT.(*XMLType)
 		changed = fromT.T != toT.T
diff --git a/sql/postgres/inspect.go b/sql/postgres/inspect.go
index f4fb7e19..8f74a996 100644
--- a/sql/postgres/inspect.go
+++ b/sql/postgres/inspect.go
@@ -644,7 +644,7 @@ func canConvert(t *schema.ColumnType, x string) (string, bool) {
 		if sqlx.IsLiteralNumber(x) {
 			return x, true
 		}
-	case *ArrayType, *schema.BinaryType, *schema.JSONType, *NetworkType, *schema.SpatialType, *schema.StringType, *schema.TimeType, *UUIDType, *XMLType:
+	case *ArrayType, *schema.BinaryType, *schema.JSONType, *NetworkType, *schema.SpatialType, *schema.StringType, *schema.TimeType, *schema.UUIDType, *XMLType:
 		return q, true
 	}
 	return "", false
@@ -737,11 +737,9 @@ type (
 		T string
 	}
 
-	// A UUIDType defines a UUID type.
-	UUIDType struct {
-		schema.Type
-		T string
-	}
+	// UUIDType is alias to schema.UUIDType.
+	// Defined here for backward compatibility reasons.
+	UUIDType = schema.UUIDType
 
 	// A XMLType defines an XML type.
 	XMLType struct {


Test patch:
diff --git a/internal/integration/postgres_test.go b/internal/integration/postgres_test.go
index 2a32f23b..db80a865 100644
--- a/internal/integration/postgres_test.go
+++ b/internal/integration/postgres_test.go
@@ -1298,7 +1298,7 @@ create table atlas_types_sanity
 				},
 				{
 					Name: "tUUID",
-					Type: &schema.ColumnType{Type: &postgres.UUIDType{T: "uuid"}, Raw: "uuid", Null: true},
+					Type: &schema.ColumnType{Type: &schema.UUIDType{T: "uuid"}, Raw: "uuid", Null: true},
 					Default: &schema.Literal{
 						V: "'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'",
 					},
diff --git a/sql/postgres/inspect_test.go b/sql/postgres/inspect_test.go
index 57593367..51419fd6 100644
--- a/sql/postgres/inspect_test.go
+++ b/sql/postgres/inspect_test.go
@@ -124,7 +124,7 @@ func TestDriver_InspectTable(t *testing.T) {
 					{Name: "c17", Type: &schema.ColumnType{Raw: "numeric", Type: &schema.DecimalType{T: "numeric"}}},
 					{Name: "c18", Type: &schema.ColumnType{Raw: "numeric", Type: &schema.DecimalType{T: "numeric", Precision: 4, Scale: 4}}},
 					{Name: "c19", Type: &schema.ColumnType{Raw: "serial", Type: &SerialType{T: "serial", SequenceName: "t1_c19_seq"}}},
-					{Name: "c20", Type: &schema.ColumnType{Raw: "uuid", Type: &UUIDType{T: "uuid"}}},
+					{Name: "c20", Type: &schema.ColumnType{Raw: "uuid", Type: &schema.UUIDType{T: "uuid"}}},
 					{Name: "c21", Type: &schema.ColumnType{Raw: "xml", Type: &XMLType{T: "xml"}}},
 					{Name: "c22", Type: &schema.ColumnType{Raw: "ARRAY", Null: true, Type: &ArrayType{Type: &schema.IntegerType{T: "integer"}, T: "integer[]"}}},
 					{Name: "c23", Type: &schema.ColumnType{Raw: "USER-DEFINED", Null: true, Type: &UserDefinedType{T: "ltree"}}},
diff --git a/sql/postgres/sqlspec_test.go b/sql/postgres/sqlspec_test.go
index 220984c0..2ca88c16 100644
--- a/sql/postgres/sqlspec_test.go
+++ b/sql/postgres/sqlspec_test.go
@@ -1357,7 +1357,7 @@ func TestTypes(t *testing.T) {
 		},
 		{
 			typeExpr: "uuid",
-			expected: &UUIDType{T: TypeUUID},
+			expected: &schema.UUIDType{T: TypeUUID},
 		},
 		{
 			typeExpr: "money",