Skip to content

Commit

Permalink
sql: multiple bug fixes in hcl inspect/apply (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Nov 28, 2021
1 parent b8bf72e commit d0914c8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions sql/mysql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (d *Driver) FormatType(t schema.Type) (string, error) {
f = strings.ToLower(t.T)
case *schema.TimeType:
f = strings.ToLower(t.T)
case *schema.UnsupportedType:
return "", fmt.Errorf("mysql: unsupported type: %q", t.T)
default:
return "", fmt.Errorf("mysql: invalid schema type: %T", t)
}
Expand Down
2 changes: 1 addition & 1 deletion sql/mysql/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func columnSpec(col *schema.Column) (*sqlspec.Column, error) {
return &sqlspec.Column{
Name: col.Name,
Type: ct.Type,
Null: ct.Null,
Null: col.Type.Null,
DefaultExtension: schemaspec.DefaultExtension{
Extra: schemaspec.Resource{Attrs: ct.DefaultExtension.Extra.Attrs},
},
Expand Down
2 changes: 2 additions & 0 deletions sql/postgres/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func (d *Driver) FormatType(t schema.Type) (string, error) {
f = strings.ToLower(t.T)
case *UserDefinedType:
f = strings.ToLower(t.T)
case *schema.UnsupportedType:
return "", fmt.Errorf("postgres: unsupported type: %q", t.T)
default:
return "", fmt.Errorf("postgres: invalid schema type: %T", t)
}
Expand Down
12 changes: 10 additions & 2 deletions sql/postgres/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ func parseRawType(spec *sqlspec.Column) (schema.Type, error) {
if t, ok := arrayType(spec.Type); ok {
d = &columnDesc{typ: tArray, udt: t}
}
return columnType(d), nil
t := columnType(d)
// If the type is unknown (to us), we fallback to user-defined but expect
// to improve this in future versions by ensuring this against the database.
if ut, ok := t.(*schema.UnsupportedType); ok {
t = &UserDefinedType{T: ut.T}
}
return t, nil
}

// schemaSpec converts from a concrete Postgres schema to Atlas specification.
Expand All @@ -228,7 +234,7 @@ func columnSpec(col *schema.Column) (*sqlspec.Column, error) {
return &sqlspec.Column{
Name: col.Name,
Type: ct.Type,
Null: ct.Null,
Null: col.Type.Null,
DefaultExtension: schemaspec.DefaultExtension{
Extra: schemaspec.Resource{Attrs: ct.DefaultExtension.Extra.Attrs},
},
Expand Down Expand Up @@ -275,6 +281,8 @@ func columnTypeSpec(t schema.Type) (*sqlspec.Column, error) {
return &sqlspec.Column{Type: t.T}, nil
case *UUIDType:
return &sqlspec.Column{Type: t.T}, nil
case *UserDefinedType:
return &sqlspec.Column{Type: t.T}, nil
case *XMLType:
return &sqlspec.Column{Type: t.T}, nil
default:
Expand Down
21 changes: 21 additions & 0 deletions sql/postgres/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ table "table" {
type = "string"
size = 32
}
column "tags" {
type = "hstore"
}
primary_key {
columns = [table.table.column.col]
}
Expand Down Expand Up @@ -96,6 +99,14 @@ table "accounts" {
},
},
},
{
Name: "tags",
Type: &schema.ColumnType{
Type: &UserDefinedType{
T: "hstore",
},
},
},
},
},
{
Expand Down Expand Up @@ -470,6 +481,10 @@ func TestMarshalSpecColumnType(t *testing.T) {
Name: "column",
Type: &schema.ColumnType{Type: tt.schem},
},
{
Name: "nullable_column",
Type: &schema.ColumnType{Type: tt.schem, Null: true},
},
},
},
},
Expand All @@ -482,8 +497,14 @@ func TestMarshalSpecColumnType(t *testing.T) {
}
err = schemahcl.Unmarshal(ddl, &test)
require.NoError(t, err)

require.False(t, test.Table.Columns[0].Null)
require.EqualValues(t, tt.expected.Type, test.Table.Columns[0].Type)
require.ElementsMatch(t, tt.expected.Extra.Attrs, test.Table.Columns[0].Extra.Attrs)

require.True(t, test.Table.Columns[1].Null)
require.EqualValues(t, tt.expected.Type, test.Table.Columns[1].Type)
require.ElementsMatch(t, tt.expected.Extra.Attrs, test.Table.Columns[1].Extra.Attrs)
})
}
}
2 changes: 2 additions & 0 deletions sql/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (d *Driver) FormatType(t schema.Type) (string, error) {
f = strings.ToLower(t.T)
case *UUIDType:
f = strings.ToLower(t.T)
case *schema.UnsupportedType:
return "", fmt.Errorf("sqlite: unsupported type: %q", t.T)
default:
return "", fmt.Errorf("sqlite: invalid schema type: %T", t)
}
Expand Down
2 changes: 1 addition & 1 deletion sql/sqlite/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func columnSpec(col *schema.Column) (*sqlspec.Column, error) {
return &sqlspec.Column{
Name: col.Name,
Type: ct.Type,
Null: ct.Null,
Null: col.Type.Null,
DefaultExtension: schemaspec.DefaultExtension{
Extra: schemaspec.Resource{Attrs: ct.DefaultExtension.Extra.Attrs},
},
Expand Down

0 comments on commit d0914c8

Please sign in to comment.