Skip to content

Commit

Permalink
Refactor ir data types a bit to make it more canonical Go and further…
Browse files Browse the repository at this point in the history
… disconnect it from XML/postgres
  • Loading branch information
williammoran committed Apr 29, 2024
1 parent 5174fee commit d49f365
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 316 deletions.
2 changes: 1 addition & 1 deletion lib/encoding/xml/datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ type DataTypeDomainConstraint struct {
Check string `xml:",chardata"`
}

func (self *DataType) ToIR() (*ir.DataType, error) {
func (self *DataType) ToIR() (*ir.TypeDef, error) {
panic("todo")
}
6 changes: 3 additions & 3 deletions lib/format/pgsql8/diff_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func diffTypes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema *
ofs.WriteSql(getFunctionDropSql(oldSchema, oldFunc)...)
}

columns, sql := alterColumnTypePlaceholder(oldSchema, oldType)
columns, sql := alterColumnTypePlaceholder(oldType)
ofs.WriteSql(sql...)

if newType.Kind.Equals(ir.DataTypeKindDomain) {
if newType.Kind == ir.DataTypeKindDomain {
diffDomain(ofs, oldSchema, oldType, newSchema, newType)
} else {
ofs.WriteSql(getDropTypeSql(oldSchema, oldType)...)
Expand Down Expand Up @@ -80,7 +80,7 @@ func createTypes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema
}
}

func diffDomain(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldType *ir.DataType, newSchema *ir.Schema, newType *ir.DataType) {
func diffDomain(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldType *ir.TypeDef, newSchema *ir.Schema, newType *ir.TypeDef) {
oldInfo := oldType.DomainType
newInfo := newType.DomainType

Expand Down
64 changes: 32 additions & 32 deletions lib/format/pgsql8/diff_types_domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
func TestDiffTypes_Domain_BaseType(t *testing.T) {
oldSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -27,8 +27,8 @@ func TestDiffTypes_Domain_BaseType(t *testing.T) {

newSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -52,8 +52,8 @@ func TestDiffTypes_Domain_BaseType(t *testing.T) {
func TestDiffTypes_Domain_ChangeDefault(t *testing.T) {
oldSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -66,8 +66,8 @@ func TestDiffTypes_Domain_ChangeDefault(t *testing.T) {

newSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -90,8 +90,8 @@ func TestDiffTypes_Domain_ChangeDefault(t *testing.T) {
func TestDiffTypes_Domain_DropDefault(t *testing.T) {
oldSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -104,8 +104,8 @@ func TestDiffTypes_Domain_DropDefault(t *testing.T) {

newSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -124,8 +124,8 @@ func TestDiffTypes_Domain_DropDefault(t *testing.T) {
func TestDiffTypes_Domain_MakeNull(t *testing.T) {
oldSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -138,8 +138,8 @@ func TestDiffTypes_Domain_MakeNull(t *testing.T) {

newSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -159,8 +159,8 @@ func TestDiffTypes_Domain_MakeNull(t *testing.T) {
func TestDiffTypes_Domain_MakeNotNull(t *testing.T) {
oldSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -173,8 +173,8 @@ func TestDiffTypes_Domain_MakeNotNull(t *testing.T) {

newSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
Expand All @@ -194,14 +194,14 @@ func TestDiffTypes_Domain_MakeNotNull(t *testing.T) {
func TestDiffTypes_Domain_AddDropChangeConstraints(t *testing.T) {
oldSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
BaseType: "int",
},
DomainConstraints: []*ir.DataTypeDomainConstraint{
DomainConstraints: []ir.DataTypeDomainConstraint{
{"gt5", "VALUE > 5"},
{"lt10", "VALUE < 10"},
{"eq7", "VALUE = 7"},
Expand All @@ -212,14 +212,14 @@ func TestDiffTypes_Domain_AddDropChangeConstraints(t *testing.T) {

newSchema := &ir.Schema{
Name: "domains",
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
BaseType: "int",
},
DomainConstraints: []*ir.DataTypeDomainConstraint{
DomainConstraints: []ir.DataTypeDomainConstraint{
{"gt5", "CHECK(VALUE > 5)"},
{"gt4", "VALUE > 4"},
{"eq7", "VALUE = 2"},
Expand Down Expand Up @@ -251,14 +251,14 @@ func TestDiffTypes_Domain_DependentColumn(t *testing.T) {
},
},
},
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
BaseType: "int",
},
DomainConstraints: []*ir.DataTypeDomainConstraint{
DomainConstraints: []ir.DataTypeDomainConstraint{
{"gt5", "VALUE > 5"},
},
},
Expand All @@ -277,14 +277,14 @@ func TestDiffTypes_Domain_DependentColumn(t *testing.T) {
},
},
},
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "my_domain",
Kind: ir.DataTypeKindDomain,
DomainType: &ir.DataTypeDomainType{
BaseType: "int",
},
DomainConstraints: []*ir.DataTypeDomainConstraint{
DomainConstraints: []ir.DataTypeDomainConstraint{
{"gt5", "VALUE > 3"},
},
},
Expand Down
12 changes: 6 additions & 6 deletions lib/format/pgsql8/diff_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func TestDiffTypes_DiffTypes_RecreateDependentFunctions(t *testing.T) {
},
},
},
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "arch_type",
Kind: ir.DataTypeKindComposite,
CompositeFields: []*ir.DataTypeCompositeField{
CompositeFields: []ir.DataTypeCompositeField{
{Name: "uh_phrasing", Type: "text"},
{Name: "boom_phrasing", Type: "text"},
},
Expand Down Expand Up @@ -81,11 +81,11 @@ func TestDiffTypes_DiffTypes_RecreateDependentFunctions(t *testing.T) {
},
},
},
Types: []*ir.DataType{
&ir.DataType{
Types: []*ir.TypeDef{
&ir.TypeDef{
Name: "arch_type",
Kind: ir.DataTypeKindComposite,
CompositeFields: []*ir.DataTypeCompositeField{
CompositeFields: []ir.DataTypeCompositeField{
{Name: "uh_phrasing", Type: "text"},
{Name: "boom_phrasing", Type: "text"},
{Name: "ummmm_phrasing", Type: "text"},
Expand Down
2 changes: 1 addition & 1 deletion lib/format/pgsql8/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func getFunctionGrantSql(schema *ir.Schema, fn *ir.Function, grant *ir.Grant) []
}

// TODO(go,3) move this to model
func functionDependsOnType(fn *ir.Function, typeSchema *ir.Schema, datatype *ir.DataType) bool {
func functionDependsOnType(fn *ir.Function, typeSchema *ir.Schema, datatype *ir.TypeDef) bool {
// TODO(feat) what about composite/domain types that are also dependent on the type? further refinement needed
qualifiedName := typeSchema.Name + "." + datatype.Name
returns := strings.TrimRight(fn.Returns, "[] ") // allow for arrays
Expand Down
2 changes: 1 addition & 1 deletion lib/format/pgsql8/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (self *Schema) GetGrantSql(doc *ir.Definition, schema *ir.Schema, grant *ir
return ddl
}

func (self *Schema) GetFunctionsDependingOnType(schema *ir.Schema, datatype *ir.DataType) []*ir.Function {
func (self *Schema) GetFunctionsDependingOnType(schema *ir.Schema, datatype *ir.TypeDef) []*ir.Function {
out := []*ir.Function{}
for _, fn := range schema.Functions {
if functionDependsOnType(fn, schema, datatype) {
Expand Down
34 changes: 17 additions & 17 deletions lib/format/pgsql8/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
"github.com/dbsteward/dbsteward/lib/util"
)

func getCreateTypeSql(schema *ir.Schema, datatype *ir.DataType) ([]output.ToSql, error) {
func getCreateTypeSql(schema *ir.Schema, datatype *ir.TypeDef) ([]output.ToSql, error) {
switch datatype.Kind {
case ir.DataTypeKindEnum:
// TODO(go,3) put validation elsewhere
if len(datatype.EnumValues) == 0 {
return nil, fmt.Errorf("Enum type %s.%s contains no enum children", schema.Name, datatype.Name)
return nil, fmt.Errorf("enum type %s.%s contains no enum children", schema.Name, datatype.Name)
}
vals := make([]string, len(datatype.EnumValues))
for i, val := range datatype.EnumValues {
vals[i] = val.Value
vals[i] = string(val)
}
return []output.ToSql{
&sql.TypeEnumCreate{
Expand All @@ -31,7 +31,7 @@ func getCreateTypeSql(schema *ir.Schema, datatype *ir.DataType) ([]output.ToSql,
case ir.DataTypeKindComposite:
// TODO(go,3) put validation elsewhere
if len(datatype.CompositeFields) == 0 {
return nil, fmt.Errorf("Composite type %s.%s contains no typeCompositeElement children", schema.Name, datatype.Name)
return nil, fmt.Errorf("composite type %s.%s contains no typeCompositeElement children", schema.Name, datatype.Name)
}
fields := make([]sql.TypeCompositeCreateField, len(datatype.CompositeFields))
for i, field := range datatype.CompositeFields {
Expand All @@ -49,21 +49,21 @@ func getCreateTypeSql(schema *ir.Schema, datatype *ir.DataType) ([]output.ToSql,
case ir.DataTypeKindDomain:
// TODO(go,3) put validation elsewhere
if datatype.DomainType == nil {
return nil, fmt.Errorf("Domain type %s.%s contains no domainType child", schema.Name, datatype.Name)
return nil, fmt.Errorf("domain type %s.%s contains no domainType child", schema.Name, datatype.Name)
}
if datatype.DomainType.BaseType == "" {
return nil, fmt.Errorf("Domain type %s.%s baseType attribute is not set on domainType", schema.Name, datatype.Name)
return nil, fmt.Errorf("domain type %s.%s baseType attribute is not set on domainType", schema.Name, datatype.Name)
}
constraints := make([]sql.TypeDomainCreateConstraint, len(datatype.DomainConstraints))
for i, constraint := range datatype.DomainConstraints {
// TODO(go,3) put normalization elsewhere
name := strings.TrimSpace(constraint.Name)
check := strings.TrimSpace(constraint.GetNormalizedCheck())
if name == "" {
return nil, fmt.Errorf("Domain type %s.%s constraint %d has empty name", schema.Name, datatype.Name, i)
return nil, fmt.Errorf("domain type %s.%s constraint %d has empty name", schema.Name, datatype.Name, i)
}
if check == "" {
return nil, fmt.Errorf("Domain type %s.%s constraint %s has no definition", schema.Name, datatype.Name, name)
return nil, fmt.Errorf("domain type %s.%s constraint %s has no definition", schema.Name, datatype.Name, name)
}
constraints[i] = sql.TypeDomainCreateConstraint{
Name: name,
Expand All @@ -87,11 +87,11 @@ func getCreateTypeSql(schema *ir.Schema, datatype *ir.DataType) ([]output.ToSql,
},
}, nil
}
return nil, fmt.Errorf("Unknown type %s type %s", datatype.Name, datatype.Kind)
return nil, fmt.Errorf("unknown type %s type %s", datatype.Name, datatype.Kind.String())
}

func getDropTypeSql(schema *ir.Schema, datatype *ir.DataType) []output.ToSql {
if datatype.Kind.Equals(ir.DataTypeKindDomain) {
func getDropTypeSql(schema *ir.Schema, datatype *ir.TypeDef) []output.ToSql {
if datatype.Kind == ir.DataTypeKindDomain {
return []output.ToSql{
&sql.TypeDomainDrop{
Type: sql.TypeRef{Schema: schema.Name, Type: datatype.Name},
Expand Down Expand Up @@ -119,7 +119,7 @@ func isIntType(spec string) bool {
}

// Change all table columns that are the given datatype to a placeholder type
func alterColumnTypePlaceholder(schema *ir.Schema, datatype *ir.DataType) ([]*ir.ColumnRef, []output.ToSql) {
func alterColumnTypePlaceholder(datatype *ir.TypeDef) ([]*ir.ColumnRef, []output.ToSql) {
ddl := []output.ToSql{}
cols := []*ir.ColumnRef{}
for _, newTableRef := range differ.NewTableDependency {
Expand All @@ -138,19 +138,19 @@ func alterColumnTypePlaceholder(schema *ir.Schema, datatype *ir.DataType) ([]*ir
return cols, ddl
}

func alterColumnTypePlaceholderType(datatype *ir.DataType) sql.TypeRef {
if datatype.Kind.Equals(ir.DataTypeKindEnum) {
func alterColumnTypePlaceholderType(datatype *ir.TypeDef) sql.TypeRef {
if datatype.Kind == ir.DataTypeKindEnum {
return sql.BuiltinTypeRef("text")
}
if datatype.Kind.Equals(ir.DataTypeKindDomain) {
if datatype.Kind == ir.DataTypeKindDomain {
return sql.ParseTypeRef(datatype.DomainType.BaseType)
}
util.Assert(false, "Unexpected data type kind %s", string(datatype.Kind))
util.Assert(false, "Unexpected data type kind %s", datatype.Kind.String())
return sql.TypeRef{} // unreachable
}

// restores types changed by AlterColumnTypePlaceholder
func alterColumnTypeRestore(columns []*ir.ColumnRef, schema *ir.Schema, datatype *ir.DataType) []output.ToSql {
func alterColumnTypeRestore(columns []*ir.ColumnRef, schema *ir.Schema, datatype *ir.TypeDef) []output.ToSql {
ddl := []output.ToSql{}
// do the columns backwards to maintain dependency ordering
for i := len(columns) - 1; i >= 0; i-- {
Expand Down
Loading

0 comments on commit d49f365

Please sign in to comment.