Skip to content

Commit

Permalink
Remove separate lax-length constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed May 27, 2019
1 parent ab16617 commit ad16c77
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 89 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/table/postgres.go
Expand Up @@ -83,7 +83,7 @@ func (r *ReconcileTable) deployPostgres(connection *databasesv1alpha1.PostgresCo
existingColumn.ColumnDefault = &columnDefault.String
}
if charMaxLength.Valid {
existingColumn.Constraints.MaxLength = &charMaxLength.Int64
existingColumn.DataType = fmt.Sprintf("%s (%d)", existingColumn.DataType, charMaxLength.Int64)
}

columnStatement, err := postgres.AlterColumnStatement(tableName, postgresTableSchema.Columns, &existingColumn)
Expand Down
19 changes: 2 additions & 17 deletions pkg/database/postgres/column.go
Expand Up @@ -9,8 +9,7 @@ import (
)

type ColumnConstraints struct {
NotNull *bool
MaxLength *int64
NotNull *bool
}

type Column struct {
Expand Down Expand Up @@ -63,21 +62,11 @@ func schemaColumnToPostgresColumn(schemaColumn *schemasv1alpha1.SQLTableColumn)
return column, nil
}

columnType, maxLength, err := maybeParseParameterizedColumnType(requestedType)
columnType, err := maybeParseParameterizedColumnType(requestedType)
if err != nil {
return nil, err
}

if maxLength != nil {
if column.Constraints == nil {
column.Constraints = &ColumnConstraints{
MaxLength: maxLength,
}
} else {
column.Constraints.MaxLength = maxLength
}
}

if columnType != "" {
column.DataType = columnType
return column, nil
Expand All @@ -100,10 +89,6 @@ func postgresColumnAsInsert(column *schemasv1alpha1.SQLTableColumn) (string, err

formatted := fmt.Sprintf("%s %s", pq.QuoteIdentifier(column.Name), postgresColumn.DataType)

if postgresColumn.Constraints != nil && postgresColumn.Constraints.MaxLength != nil {
formatted = fmt.Sprintf("%s (%d)", formatted, *postgresColumn.Constraints.MaxLength)
}

if postgresColumn.Constraints != nil && postgresColumn.Constraints.NotNull != nil {
if *postgresColumn.Constraints.NotNull == true {
formatted = fmt.Sprintf("%s not null", formatted)
Expand Down
14 changes: 2 additions & 12 deletions pkg/database/postgres/column_test.go
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/stretchr/testify/require"
)

var (
ten = int64(10)
)

func Test_postgresColumnAsInsert(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -142,11 +138,8 @@ func Test_schemaColumnToPostgresColumn(t *testing.T) {
Type: "character varying (10)",
},
expectedColumn: &Column{
DataType: "character varying",
DataType: "character varying (10)",
ColumnDefault: nil,
Constraints: &ColumnConstraints{
MaxLength: &ten,
},
},
},
{
Expand All @@ -156,11 +149,8 @@ func Test_schemaColumnToPostgresColumn(t *testing.T) {
Type: "varchar (10)",
},
expectedColumn: &Column{
DataType: "character varying",
DataType: "character varying (10)",
ColumnDefault: nil,
Constraints: &ColumnConstraints{
MaxLength: &ten,
},
},
},
{
Expand Down
37 changes: 14 additions & 23 deletions pkg/database/postgres/parameterized.go
Expand Up @@ -41,9 +41,8 @@ var unparameterizedColumnTypes = []string{
"xml",
}

func maybeParseParameterizedColumnType(requestedType string) (string, *int64, error) {
func maybeParseParameterizedColumnType(requestedType string) (string, error) {
columnType := ""
var maxLength *int64

if strings.HasPrefix(requestedType, "bit varying") {
columnType = "bit varying"
Expand All @@ -52,16 +51,14 @@ func maybeParseParameterizedColumnType(requestedType string) (string, *int64, er

matchGroups := r.FindStringSubmatch(requestedType)
if len(matchGroups) == 0 {
max64 := int64(1)
maxLength = &max64
columnType = "bit varying (1)"
} else {
maxStr := matchGroups[1]
max, err := strconv.Atoi(maxStr)
if err != nil {
return "", maxLength, err
return "", err
}
max64 := int64(max)
maxLength = &max64
columnType = fmt.Sprintf("bit varying (%d)", max)
}
} else if strings.HasPrefix(requestedType, "bit") {
columnType = "bit"
Expand All @@ -70,16 +67,14 @@ func maybeParseParameterizedColumnType(requestedType string) (string, *int64, er

matchGroups := r.FindStringSubmatch(requestedType)
if len(matchGroups) == 0 {
max64 := int64(1)
maxLength = &max64
columnType = "bit (1)"
} else {
maxStr := matchGroups[1]
max, err := strconv.Atoi(maxStr)
if err != nil {
return "", maxLength, err
return "", err
}
max64 := int64(max)
maxLength = &max64
columnType = fmt.Sprintf("bit (%d)", max)
}
} else if strings.HasPrefix(requestedType, "character varying") {
columnType = "character varying"
Expand All @@ -88,16 +83,14 @@ func maybeParseParameterizedColumnType(requestedType string) (string, *int64, er

matchGroups := r.FindStringSubmatch(requestedType)
if len(matchGroups) == 0 {
max64 := int64(1)
maxLength = &max64
columnType = "character varying (1)"
} else {
maxStr := matchGroups[1]
max, err := strconv.Atoi(maxStr)
if err != nil {
return "", maxLength, err
return "", err
}
max64 := int64(max)
maxLength = &max64
columnType = fmt.Sprintf("character varying (%d)", max)
}
} else if strings.HasPrefix(requestedType, "character") {
columnType = "character"
Expand All @@ -106,16 +99,14 @@ func maybeParseParameterizedColumnType(requestedType string) (string, *int64, er

matchGroups := r.FindStringSubmatch(requestedType)
if len(matchGroups) == 0 {
max64 := int64(1)
maxLength = &max64
columnType = "character (1)"
} else {
maxStr := matchGroups[1]
max, err := strconv.Atoi(maxStr)
if err != nil {
return "", maxLength, err
return "", err
}
max64 := int64(max)
maxLength = &max64
columnType = fmt.Sprintf("character (%d)", max)
}
} else if strings.HasPrefix(requestedType, "timestamp") {
columnType = "timestamp"
Expand Down Expand Up @@ -155,7 +146,7 @@ func maybeParseParameterizedColumnType(requestedType string) (string, *int64, er
}
}

return columnType, maxLength, nil
return columnType, nil
}

func isParameterizedColumnType(requestedType string) bool {
Expand Down
40 changes: 8 additions & 32 deletions pkg/database/postgres/parameterized_test.go
Expand Up @@ -8,118 +8,94 @@ import (
)

func Test_maybeParseParameterizedColumnType(t *testing.T) {
none := int64(-1)

tests := []struct {
name string
requestedType string
expectedColumnType string
expectedMaxLength int64
}{
{
name: "fake",
requestedType: "fake",
expectedColumnType: "",
expectedMaxLength: none,
},
{
name: "timestamp",
requestedType: "timestamp",
expectedColumnType: "timestamp",
expectedMaxLength: none,
},
{
name: "timestamp without time zone",
requestedType: "timestamp without time zone",
expectedColumnType: "timestamp without time zone",
expectedMaxLength: none,
},
{
name: "timestamp (01:02)",
requestedType: "timestamp (01:02)",
expectedColumnType: "timestamp (01:02)",
expectedMaxLength: none,
},
{
name: "timestamp (01:02) without time zone",
requestedType: "timestamp (01:02) without time zone",
expectedColumnType: "timestamp (01:02) without time zone",
expectedMaxLength: none,
},
{
name: "character varying (10)",
requestedType: "character varying (10)",
expectedColumnType: "character varying",
expectedMaxLength: int64(10),
expectedColumnType: "character varying (10)",
},
{
name: "bit varying (10)",
requestedType: "bit varying (10)",
expectedColumnType: "bit varying",
expectedMaxLength: int64(10),
expectedColumnType: "bit varying (10)",
},
{
name: "bit varying",
requestedType: "bit varying",
expectedColumnType: "bit varying",
expectedMaxLength: int64(1),
expectedColumnType: "bit varying (1)",
},
{
name: "bit(10)",
requestedType: "bit(10)",
expectedColumnType: "bit",
expectedMaxLength: int64(10),
expectedColumnType: "bit (10)",
},
{
name: "bit",
requestedType: "bit",
expectedColumnType: "bit",
expectedMaxLength: int64(1),
expectedColumnType: "bit (1)",
},
{
name: "character (10)",
requestedType: "character (10)",
expectedColumnType: "character",
expectedMaxLength: int64(10),
expectedColumnType: "character (10)",
},
{
name: "character",
requestedType: "character",
expectedColumnType: "character",
expectedMaxLength: int64(1),
expectedColumnType: "character (1)",
},
{
name: "numeric",
requestedType: "numeric",
expectedColumnType: "numeric",
expectedMaxLength: none,
},
{
name: "numeric(10)",
requestedType: "numeric(10)",
expectedColumnType: "numeric (10)",
expectedMaxLength: none,
},
{
name: "numeric (5,3)",
requestedType: "numeric (5,3)",
expectedColumnType: "numeric (5, 3)",
expectedMaxLength: none,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
columnType, maxLength, err := maybeParseParameterizedColumnType(test.requestedType)
columnType, err := maybeParseParameterizedColumnType(test.requestedType)
req := require.New(t)
req.NoError(err)
assert.Equal(t, test.expectedColumnType, columnType)

if test.expectedMaxLength == none {
assert.Nil(t, maxLength)
} else {
assert.Equal(t, test.expectedMaxLength, *maxLength)
}
})
}
}
4 changes: 0 additions & 4 deletions pkg/database/postgres/tables.go
Expand Up @@ -89,10 +89,6 @@ func (p *PostgresConnection) GetTableSchema(tableName string) ([]*Column, error)
}
}

if maxLength.Valid {
column.Constraints.MaxLength = &maxLength.Int64
}

if columnDefault.Valid {
column.ColumnDefault = &columnDefault.String
}
Expand Down

0 comments on commit ad16c77

Please sign in to comment.