Skip to content

Commit

Permalink
Merge pull request #13 from planetscale/double-timestamp-iso8601
Browse files Browse the repository at this point in the history
Add support for generating ISO 8601 timestamp values
  • Loading branch information
Phani Raj authored Aug 1, 2022
2 parents 531a49a + 29eeaa3 commit 7513a00
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
14 changes: 13 additions & 1 deletion cmd/internal/planetscale_edge_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ func (p PlanetScaleEdgeDatabase) printQueryResult(qr *sqltypes.Result, s Stream)
for selectedProperty := range s.Schema.Properties {
subset[selectedProperty] = datum[selectedProperty]
if len(s.Schema.Properties[selectedProperty].CustomFormat) > 0 {
subset[selectedProperty] = datum[selectedProperty].(sqltypes.Value).ToString()
if s.Schema.Properties[selectedProperty].CustomFormat == "date-time" {
subset[selectedProperty] = getISOTimeStamp(datum[selectedProperty].(sqltypes.Value).ToString())
} else {
subset[selectedProperty] = datum[selectedProperty].(sqltypes.Value).ToString()
}
}
}
record := NewRecord()
Expand All @@ -274,3 +278,11 @@ func (p PlanetScaleEdgeDatabase) printQueryResult(qr *sqltypes.Result, s Stream)
p.Logger.Record(record, s)
}
}

func getISOTimeStamp(value string) string {
p, err := time.Parse("2006-01-02 15:04:05", value)
if err != nil {
return ""
}
return p.Format(time.RFC3339)
}
26 changes: 21 additions & 5 deletions cmd/internal/planetscale_edge_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"context"
"fmt"
"testing"
"time"

psdbconnect "github.com/planetscale/airbyte-source/proto/psdbconnect/v1alpha1"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"testing"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/proto/query"
)
Expand Down Expand Up @@ -446,10 +448,10 @@ func TestRead_CanLogResults(t *testing.T) {

result := []*query.QueryResult{
sqltypes.ResultToProto3(sqltypes.MakeTestResult(sqltypes.MakeTestFields(
"pid|description",
"int64|varbinary"),
"1|keyboard",
"2|monitor",
"pid|description|timestamp",
"int64|varbinary|timestamp"),
"1|keyboard|2006-01-02 15:04:05",
"2|monitor|2006-01-02 15:04:05",
)),
}

Expand Down Expand Up @@ -482,6 +484,10 @@ func TestRead_CanLogResults(t *testing.T) {
"description": {
Types: []string{"null", "string"},
},
"timestamp": {
Types: []string{"null", "string"},
CustomFormat: "date-time",
},
},
},
Metadata: MetadataCollection{
Expand All @@ -503,6 +509,12 @@ func TestRead_CanLogResults(t *testing.T) {
BreadCrumb: []string{"properties", "description"},
},
},
Metadata{
Metadata: NodeMetadata{
Selected: true,
BreadCrumb: []string{"properties", "timestamp"},
},
},
},
}
sc, err := ped.Read(context.Background(), ps, cs, tc)
Expand All @@ -515,6 +527,10 @@ func TestRead_CanLogResults(t *testing.T) {
for _, r := range records {
id, err := r.Data["pid"].(sqltypes.Value).ToInt64()
assert.NoError(t, err)

_, err = time.Parse(time.RFC3339, r.Data["timestamp"].(string))
assert.NoError(t, err, "should print timestamp as ISO 8601/RFC3339 values")

if id == 1 {
assert.False(t, keyboardFound, "should not find keyboard twice")
keyboardFound = true
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/planetscale_edge_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func getJsonSchemaType(mysqlType string) StreamProperty {
switch mysqlType {
case "tinyint(1)":
return StreamProperty{Types: []string{"null", "boolean"}}
case "double":
return StreamProperty{Types: []string{"null", "number"}}
case "date":
return StreamProperty{Types: []string{"null", "string"}}
case "datetime", "timestamp":
Expand Down

0 comments on commit 7513a00

Please sign in to comment.