Skip to content

Commit d68f97a

Browse files
authored
Merge pull request #158 from cloudspannerecosystem/add-allow-commitment-timestamp-test
support AllowCommitTimestamp
2 parents e048210 + fa2b723 commit d68f97a

File tree

12 files changed

+819
-24
lines changed

12 files changed

+819
-24
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ testdata-from-ddl/default:
6969
$(YOBIN) generate ./test/testdata/schema.sql --from-ddl --package models --out test/testmodels/default/
7070

7171
testdata-from-ddl/underscore:
72-
rm -rf test/testmodels/underscores && mkdir -p test/testmodels/underscores
73-
$(YOBIN) generate ./test/testdata/schema.sql --from-ddl --package models --underscore --out test/testmodels/underscores/
72+
rm -rf test/testmodels/underscore && mkdir -p test/testmodels/underscore
73+
$(YOBIN) generate ./test/testdata/schema.sql --from-ddl --package models --underscore --out test/testmodels/underscore/
7474

7575
testdata-from-ddl/single:
7676
rm -rf test/testmodels/single && mkdir -p test/testmodels/single

loaders/parser.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ func (s *SpannerLoaderFromDDL) ColumnList(name string) ([]*models.Column, error)
148148
}
149149

150150
cols = append(cols, &models.Column{
151-
FieldOrdinal: i + 1,
152-
ColumnName: c.Name.Name,
153-
DataType: c.Type.SQL(),
154-
NotNull: c.NotNull,
155-
IsPrimaryKey: pk,
156-
IsGenerated: isGenerated,
157-
AllowCommitTimestamp: allowCommitTimestamp,
151+
FieldOrdinal: i + 1,
152+
ColumnName: c.Name.Name,
153+
DataType: c.Type.SQL(),
154+
NotNull: c.NotNull,
155+
IsPrimaryKey: pk,
156+
IsGenerated: isGenerated,
157+
IsAllowCommitTimestamp: allowCommitTimestamp,
158158
})
159159
}
160160

loaders/spanner.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,13 @@ func spanTableColumns(client *spanner.Client, table string) ([]*models.Column, e
250250
` AND ic.COLUMN_NAME = c.COLUMN_NAME` +
251251
` AND ic.INDEX_NAME = "PRIMARY_KEY" ` +
252252
`) IS_PRIMARY_KEY, ` +
253-
`IS_GENERATED = "ALWAYS" AS IS_GENERATED ` +
253+
`IS_GENERATED = "ALWAYS" AS IS_GENERATED, ` +
254+
`EXISTS (` +
255+
` SELECT 1 FROM INFORMATION_SCHEMA.COLUMN_OPTIONS ic` +
256+
` WHERE ic.TABLE_SCHEMA = "" AND ic.TABLE_NAME = c.TABLE_NAME` +
257+
` AND ic.COLUMN_NAME = c.COLUMN_NAME` +
258+
` AND ic.OPTION_NAME = "allow_commit_timestamp"` +
259+
`) IS_ALLOW_COMMIT_TIMESTAMP,` +
254260
`FROM INFORMATION_SCHEMA.COLUMNS c ` +
255261
`WHERE c.TABLE_SCHEMA = "" AND c.TABLE_NAME = @table ` +
256262
`ORDER BY c.ORDINAL_POSITION`
@@ -296,6 +302,9 @@ func spanTableColumns(client *spanner.Client, table string) ([]*models.Column, e
296302
if err := row.ColumnByName("IS_GENERATED", &c.IsGenerated); err != nil {
297303
return nil, err
298304
}
305+
if err := row.ColumnByName("IS_ALLOW_COMMIT_TIMESTAMP", &c.IsAllowCommitTimestamp); err != nil {
306+
return nil, err
307+
}
299308

300309
res = append(res, &c)
301310
}

models/model.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type Table struct {
2828

2929
// Column represents column info.
3030
type Column struct {
31-
FieldOrdinal int // field_ordinal
32-
ColumnName string // column_name
33-
DataType string // data_type
34-
NotNull bool // not_null
35-
IsPrimaryKey bool // is_primary_key
36-
IsGenerated bool // is_generated
37-
AllowCommitTimestamp bool // allow_commit_timestamp
31+
FieldOrdinal int // field_ordinal
32+
ColumnName string // column_name
33+
DataType string // data_type
34+
NotNull bool // not_null
35+
IsPrimaryKey bool // is_primary_key
36+
IsGenerated bool // is_generated
37+
IsAllowCommitTimestamp bool // is_allow_commit_timestamp
3838
}
3939

4040
// Index represents an index.

templates/type.go.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ func ({{ $short }} *{{ .Name }}) columnsToValues(cols []string) ([]interface{},
6767
switch col {
6868
{{- range .Fields }}
6969
case "{{ colname .Col }}":
70-
{{- if .CustomType }}
70+
{{- if .Col.IsAllowCommitTimestamp }}
71+
ret = append(ret, spanner.CommitTimestamp)
72+
{{- else if .CustomType }}
7173
ret = append(ret, {{ .Type }}({{ $short }}.{{ .Name }}))
7274
{{- else }}
7375
ret = append(ret, {{ $short }}.{{ .Name }})

test/integration_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"cloud.google.com/go/spanner"
3333
"github.com/google/go-cmp/cmp"
3434
"github.com/googleapis/gax-go/v2/apierror"
35+
"go.mercari.io/yo/loaders"
3536
"go.mercari.io/yo/test/testmodels/customtypes"
3637
models "go.mercari.io/yo/test/testmodels/default"
3738
"go.mercari.io/yo/test/testutil"
@@ -579,6 +580,96 @@ func TestCustomCompositePrimaryKey(t *testing.T) {
579580
})
580581
}
581582

583+
func TestAllowCommitTimestamp(t *testing.T) {
584+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
585+
defer cancel()
586+
587+
v := &models.AllowCommitTimestamp{
588+
ID: 300,
589+
UpdatedAt: spanner.CommitTimestamp,
590+
}
591+
592+
if _, err := client.Apply(ctx, []*spanner.Mutation{v.Insert(ctx)}); err != nil {
593+
t.Fatalf("Apply failed: %v", err)
594+
}
595+
596+
var insertTime time.Time
597+
t.Run("Insert", func(t *testing.T) {
598+
got, err := models.FindAllowCommitTimestamp(ctx, client.Single(), 300)
599+
if err != nil {
600+
t.Fatalf("unexpected error: %v", err)
601+
}
602+
if got.UpdatedAt.IsZero() {
603+
t.Errorf("got UpdatedAt.IsZero")
604+
}
605+
insertTime = got.UpdatedAt
606+
})
607+
608+
t.Run("Update", func(t *testing.T) {
609+
gc := &models.AllowCommitTimestamp{
610+
ID: 300,
611+
UpdatedAt: spanner.CommitTimestamp,
612+
}
613+
614+
if _, err := client.Apply(ctx, []*spanner.Mutation{gc.Update(ctx)}); err != nil {
615+
t.Fatalf("Apply failed: %v", err)
616+
}
617+
618+
got, err := models.FindAllowCommitTimestamp(ctx, client.Single(), 300)
619+
if err != nil {
620+
t.Fatalf("unexpected error: %v", err)
621+
}
622+
623+
if !got.UpdatedAt.After(insertTime) {
624+
t.Errorf("expected UpdatedAt (%v) to be after insertTime (%v)", got.UpdatedAt, insertTime)
625+
}
626+
})
627+
628+
t.Run("InsertOrUpdate", func(t *testing.T) {
629+
gc := &models.AllowCommitTimestamp{
630+
ID: 300,
631+
UpdatedAt: spanner.CommitTimestamp,
632+
}
633+
634+
if _, err := client.Apply(ctx, []*spanner.Mutation{gc.InsertOrUpdate(ctx)}); err != nil {
635+
t.Fatalf("Apply failed: %v", err)
636+
}
637+
638+
got, err := models.FindAllowCommitTimestamp(ctx, client.Single(), 300)
639+
if err != nil {
640+
t.Fatalf("unexpected error: %v", err)
641+
}
642+
643+
if got.UpdatedAt.IsZero() {
644+
t.Errorf("got UpdatedAt.IsZero")
645+
}
646+
})
647+
648+
t.Run("IsAllowCommitTimestamp", func(t *testing.T) {
649+
gc := &models.AllowCommitTimestamp{
650+
ID: 300,
651+
UpdatedAt: spanner.CommitTimestamp,
652+
}
653+
654+
if _, err := client.Apply(ctx, []*spanner.Mutation{gc.Update(ctx)}); err != nil {
655+
t.Fatalf("Apply failed: %v", err)
656+
}
657+
658+
cols, err := loaders.SpanTableColumns(client, "AllowCommitTimestamp")
659+
if err != nil {
660+
t.Fatalf("SpanTableColumns failed: %v", err)
661+
}
662+
663+
for _, col := range cols {
664+
if col.ColumnName == "UpdatedAt" {
665+
if !col.IsAllowCommitTimestamp {
666+
t.Errorf("updated_at is not AllowCommitTimestamp")
667+
}
668+
}
669+
}
670+
})
671+
}
672+
582673
func TestGeneratedColumn(t *testing.T) {
583674
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
584675
defer cancel()

test/testdata/schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@ CREATE TABLE GeneratedColumns (
116116
LastName STRING(50) NOT NULL,
117117
FullName STRING(100) NOT NULL AS (ARRAY_TO_STRING([FirstName, LastName], " ")) STORED,
118118
) PRIMARY KEY (ID);
119+
120+
CREATE TABLE AllowCommitTimestamp (
121+
ID INT64 NOT NULL,
122+
UpdatedAt TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
123+
) PRIMARY KEY(ID);

test/testmodels/customtypes/allowcommittimestamp.yo.go

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)