Skip to content

Commit

Permalink
sql/sqlite: allow setting user-defined types
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Sep 29, 2024
1 parent b98b84b commit 90522ec
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
54 changes: 54 additions & 0 deletions internal/integration/testdata/sqlite/column-user-defined.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Initial changes.
atlas schema apply --url URL --dev-url DEV_URL --to file://schema.v1.hcl --auto-approve
atlas schema apply --url URL --dev-url DEV_URL --to file://schema.v1.hcl --auto-approve
stdout 'Schema is synced, no changes to be made'
atlas schema inspect --url URL > got
cmp schema.v1.hcl.inspected got

# Changing user defined type.
atlas schema apply --url URL --dev-url DEV_URL --to file://schema.v2.hcl --auto-approve
atlas schema apply --url URL --dev-url DEV_URL --to file://schema.v2.hcl --auto-approve
stdout 'Schema is synced, no changes to be made'
atlas schema inspect --url URL > got
cmp schema.v2.hcl.inspected got

-- schema.v1.hcl --
table "t" {
schema = schema.main
column "c" {
null = true
type = sql("USER_DEFINED")
}
}
schema "main" {
}
-- schema.v1.hcl.inspected --
table "t" {
schema = schema.main
column "c" {
null = true
type = sql("USER_DEFINED")
}
}
schema "main" {
}
-- schema.v2.hcl --
table "t" {
schema = schema.main
column "c" {
null = true
type = sql("USER_TYPE")
}
}
schema "main" {
}
-- schema.v2.hcl.inspected --
table "t" {
schema = schema.main
column "c" {
null = true
type = sql("USER_TYPE")
}
}
schema "main" {
}
4 changes: 3 additions & 1 deletion sql/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func FormatType(t schema.Type) (string, error) {
f = strings.ToLower(t.T)
case *schema.UUIDType:
f = strings.ToLower(t.T)
case *UserDefinedType:
f = t.T
case *schema.UnsupportedType:
return "", fmt.Errorf("sqlite: unsupported type: %q", t.T)
default:
Expand Down Expand Up @@ -102,6 +104,6 @@ func ParseType(c string) (schema.Type, error) {
case "uuid":
return &schema.UUIDType{T: t}, nil
default:
return &schema.UnsupportedType{T: t}, nil
return &UserDefinedType{T: c}, nil
}
}
4 changes: 4 additions & 0 deletions sql/sqlite/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (d *diff) typeChanged(from, to *schema.Column) (bool, error) {
if fromT == nil || toT == nil {
return false, fmt.Errorf("sqlite: missing type information for column %q", from.Name)
}
if u1, ok := fromT.(*UserDefinedType); ok {
u2, ok := toT.(*UserDefinedType)
return !ok || u1.T != u2.T, nil
}
// Types are mismatched if they do not have the same "type affinity".
return reflect.TypeOf(fromT) != reflect.TypeOf(toT), nil
}
Expand Down
6 changes: 6 additions & 0 deletions sql/sqlite/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ type (
//
// Deprecated: Use schema.UUIDType instead.
UUIDType = schema.UUIDType

// UserDefinedType defines a user-defined type attribute.
UserDefinedType struct {
schema.Type
T string
}
)

func columnParts(t string) []string {
Expand Down
2 changes: 1 addition & 1 deletion sql/sqlite/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestTypes(t *testing.T) {
},
{
typeExpr: `sql("custom")`,
expected: &schema.UnsupportedType{T: "custom"},
expected: &UserDefinedType{T: "custom"},
},
{
typeExpr: "tinyint(10)",
Expand Down

0 comments on commit 90522ec

Please sign in to comment.