Skip to content

Commit

Permalink
Merge pull request #124 from GreenmaskIO/fix/overridden_column_types
Browse files Browse the repository at this point in the history
doc: Fixed overridden_column_type logic
  • Loading branch information
wwoytenko committed May 16, 2024
2 parents de1ae53 + 6e525d9 commit 3ce2d37
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
40 changes: 33 additions & 7 deletions internal/db/postgres/context/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ import (
"github.com/greenmaskio/greenmask/pkg/toolkit"
)

var typeSizes = map[string]int{
"int2": 2,
"int4": 4,
"int8": 8,
"float4": 4,
"float8": 8,
}

func getTypeSizeByeName(name string) int {
res, ok := typeSizes[name]
if !ok {
return -1
}
return res
}

func getTypeOidByName(name string, typeMap *pgtype.Map) toolkit.Oid {
t, ok := typeMap.TypeForName(name)
if !ok {
return toolkit.Oid(t.OID)
}
return 0
}

// ValidateAndBuildTableConfig - validates tables, toolkit and their parameters. Builds config for tables and returns
// ValidationWarnings that can be used for checking helpers in configuring and debugging transformation. Those
// may contain the schema affection warnings that would be useful for considering consistency
Expand Down Expand Up @@ -58,23 +82,25 @@ func validateAndBuildTablesConfig(
}
table.Constraints = constraints

// Assign columns and transformersMap if were found
columns, err := getColumnsConfig(ctx, tx, table.Oid, version)
if err != nil {
return nil, nil, err
}
table.Columns = columns

// Assigning overridden column types for driver initialization
if tableCfg.ColumnsTypeOverride != nil {
for _, c := range table.Columns {
overridingType, ok := tableCfg.ColumnsTypeOverride[c.Name]
if ok {
c.OverriddenTypeName = overridingType
c.OverriddenTypeSize = getTypeSizeByeName(overridingType)
c.OverriddenTypeOid = getTypeOidByName(overridingType, typeMap)
}
}
}

// Assign columns and transformersMap if were found
columns, err := getColumnsConfig(ctx, tx, table.Oid, version)
if err != nil {
return nil, nil, err
}
table.Columns = columns

driver, driverWarnings, err := toolkit.NewDriver(table.Table, types)
if err != nil {
return nil, nil, fmt.Errorf("unnable to initialise driver: %w", err)
Expand Down
12 changes: 11 additions & 1 deletion pkg/toolkit/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ type Column struct {
// OverriddenTypeName - replacement of original type. For instance override TEXT to INT2
OverriddenTypeName string `json:"overridden_type_name"`
OverriddenTypeOid Oid `json:"overridden_type_oid"`
OverriddenTypeSize int `json:"overridden_type_size"`
}

func (c *Column) GetColumnSize() int {
if c.Length != -1 {
if c.OverriddenTypeSize != 0 {
return c.OverriddenTypeSize
} else if c.Length != -1 {
return c.Length
}
return c.TypeLength
}

func (c *Column) GetType() (string, Oid) {
if c.OverriddenTypeName != "" {
return c.OverriddenTypeName, c.OverriddenTypeOid
}
return c.TypeName, c.TypeOid
}
7 changes: 1 addition & 6 deletions pkg/toolkit/static_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,7 @@ func (sp *StaticParameter) Init(columnParams map[string]*StaticParameter, rawVal
}
sp.Column = column

columnTypeName := sp.Column.TypeName
columnTypeOid := sp.Column.TypeOid
if sp.Column.OverriddenTypeName != "" {
columnTypeName = sp.Column.OverriddenTypeName
columnTypeOid = 0
}
columnTypeName, columnTypeOid := sp.Column.GetType()

if sp.definition.ColumnProperties != nil &&
len(sp.definition.ColumnProperties.AllowedTypes) > 0 &&
Expand Down

0 comments on commit 3ce2d37

Please sign in to comment.