Skip to content

Commit

Permalink
Add timestamp types
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed May 19, 2019
1 parent 2816f7d commit dd231d4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
35 changes: 28 additions & 7 deletions pkg/database/postgres/column.go
Expand Up @@ -57,9 +57,9 @@ type Column struct {
Constraints *ColumnConstraints
}

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

// if strings.HasPrefix(requestedType, "bit varying" {
// columnType = "bit varying"
Expand All @@ -78,9 +78,32 @@ func maybeParseParameterizedColumnType(requestedType string) (string, int64, err
maxStr := matchGroups[1]
max, err := strconv.Atoi(maxStr)
if err != nil {
return "", int64(0), err
return "", maxLength, err
}
max64 := int64(max)
maxLength = &max64
} else if strings.HasPrefix(requestedType, "timestamp") {
columnType = "timestamp"

withPrecisionWithoutTimeZone := regexp.MustCompile(`timestamp\s*\(\s*(?P<precision>.*)\s*\)without time zone`)
withPrecision := regexp.MustCompile(`timestamp\s*\(\s*(?P<precision>.*)\s*\)`)
withoutPrecisionWithoutTimeZone := regexp.MustCompile(`timestamp\s*without time zone`)
withoutPrecision := regexp.MustCompile(`timestamp\s*`)

withPrecisionMatchGroups := withPrecision.FindStringSubmatch(requestedType)
withPrecisionWithoutTimeZoneMatchGroups := withPrecisionWithoutTimeZone.FindStringSubmatch(requestedType)
withoutPrecisionMatchGroups := withoutPrecision.FindStringSubmatch(requestedType)
withoutPrecisionWithoutTimeZoneMatchGroups := withoutPrecisionWithoutTimeZone.FindStringSubmatch(requestedType)

if len(withPrecisionWithoutTimeZoneMatchGroups) == 2 {
columnType = fmt.Sprintf("timestamp (%s) without time zone", withPrecisionWithoutTimeZoneMatchGroups[1])
} else if len(withoutPrecisionWithoutTimeZoneMatchGroups) == 1 {
columnType = "timestamp without time zone"
} else if len(withPrecisionMatchGroups) == 2 {
columnType = fmt.Sprintf("timestamp (%s)", withPrecisionMatchGroups[1])
} else if len(withoutPrecisionMatchGroups) == 1 {
columnType = "timestamp"
}
maxLength = int64(max)
}

return columnType, maxLength, nil
Expand Down Expand Up @@ -232,7 +255,7 @@ func schemaColumnToPostgresColumn(schemaColumn *schemasv1alpha1.PostgresTableCol

if columnType != "" {
column.DataType = columnType
column.CharMaxLength = &maxLength
column.CharMaxLength = maxLength

return column, nil
}
Expand All @@ -258,8 +281,6 @@ func postgresColumnAsInsert(column *schemasv1alpha1.PostgresTableColumn) (string
formatted = fmt.Sprintf("%s(%d)", formatted, *postgresColumn.CharMaxLength)
}

fmt.Printf("%#v\n", postgresColumn)

if postgresColumn.Constraints != nil {
if postgresColumn.Constraints.NotNull {
formatted = fmt.Sprintf("%s not null", formatted)
Expand Down
25 changes: 25 additions & 0 deletions pkg/database/postgres/column_test.go
Expand Up @@ -42,6 +42,23 @@ func Test_unaliasParameterizedColumnType(t *testing.T) {
}
}

func Test_maybeParseParameterizedColumnType(t *testing.T) {
parameterizedTests := map[string]string{
"fake": "",
"timestamp": "timestamp",
"timestamp without time zone": "timestamp without time zone",
// "timestamp (01:02)": "timestamp (01:02)",
// "timestamp (01:02) without time zone": "timestamp (01:02) without time zone",
}

for input, expectedOutput := range parameterizedTests {
t.Run(input, func(t *testing.T) {
output, _, _ := maybeParseParameterizedColumnType(input)
assert.Equal(t, expectedOutput, output)
})
}
}

func Test_unaliasUnparameterizedColumnType(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -125,6 +142,14 @@ func Test_postgresColumnAsInsert(t *testing.T) {
},
expectedStatement: `"t" text`,
},
{
name: "timestamp without time zone",
column: &schemasv1alpha1.PostgresTableColumn{
Name: "t",
Type: "timestamp without time zone",
},
expectedStatement: `"t" timestamp without time zone`,
},
}

for _, test := range tests {
Expand Down

0 comments on commit dd231d4

Please sign in to comment.