From 2f0dd58f119903c6c0217339cfd6c11e9348a0df Mon Sep 17 00:00:00 2001 From: Jim Date: Sun, 3 May 2020 16:10:14 -0400 Subject: [PATCH] set default value in gorm tag, so database timestamp defaults will get set --- internal/oplog/oplog_test.go | 4 ++++ internal/oplog/store/oplog.pb.go | 20 +++++++++---------- .../storage/oplog/store/v1/oplog.proto | 10 +++++----- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/internal/oplog/oplog_test.go b/internal/oplog/oplog_test.go index b531cfd1f..cf562ed40 100644 --- a/internal/oplog/oplog_test.go +++ b/internal/oplog/oplog_test.go @@ -122,6 +122,8 @@ func Test_BasicOplog(t *testing.T) { resp = db.Create(&l) assert.NilError(t, resp.Error) + assert.Check(t, l.CreateTime != nil) + assert.Check(t, l.UpdateTime != nil) entryId := l.Id var foundEntry Entry @@ -771,6 +773,8 @@ func Test_TicketSerialization(t *testing.T) { err = secondLogEntry.Write(context.Background(), &GormWriter{secondTx}, secondTicket) assert.NilError(t, err) assert.Assert(t, secondLogEntry.Id != 0) + assert.Check(t, secondLogEntry.CreateTime != nil) + assert.Check(t, secondLogEntry.UpdateTime != nil) err = secondTx.Commit().Error assert.NilError(t, err) assert.Assert(t, secondLogEntry.Id != 0) diff --git a/internal/oplog/store/oplog.pb.go b/internal/oplog/store/oplog.pb.go index 764e61b34..3587ba86a 100644 --- a/internal/oplog/store/oplog.pb.go +++ b/internal/oplog/store/oplog.pb.go @@ -88,10 +88,10 @@ type Entry struct { // @inject_tag: gorm:"primary_key" Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" gorm:"primary_key"` - // @inject_tag: gorm:"not_null;type:TIMESTAMP" - CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"not_null;type:TIMESTAMP"` - // @inject_tag: gorm:"not_null;type:TIMESTAMP" - UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"not_null;type:TIMESTAMP"` + // @inject_tag: `gorm:"default:current_timestamp"` + CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"default:current_timestamp"` + // @inject_tag: `gorm:"default:current_timestamp"` + UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"default:current_timestamp"` // @inject_tag: gorm:"not_null" Version string `protobuf:"bytes,4,opt,name=version,proto3" json:"version,omitempty" gorm:"not_null"` // @inject_tat: gorm:"not_null" @@ -204,8 +204,8 @@ type Metadata struct { // @inject_tag: gorm:"primary_key" Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" gorm:"primary_key"` - // @inject_tag: gorm:"not_null;type:TIMESTAMP" - CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"not_null;type:TIMESTAMP"` // @inject_tag: gorm:"not_nul + // @inject_tag: `gorm:"default:current_timestamp"` + CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"default:current_timestamp"` // @inject_tag: gorm:"not_nul EntryId uint32 `protobuf:"varint,3,opt,name=entry_id,json=entryId,proto3" json:"entry_id,omitempty"` // @inject_tag: gorm:"foreignkey:EntryId" Entry *Entry `protobuf:"bytes,4,opt,name=entry,proto3" json:"entry,omitempty" gorm:"foreignkey:EntryId"` @@ -297,10 +297,10 @@ type Ticket struct { // @inject_tag: gorm:"primary_key" Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" gorm:"primary_key"` - // @inject_tag: gorm:"not_null;type:TIMESTAMP" - CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"not_null;type:TIMESTAMP"` - // @inject_tag: gorm:"not_null;type:TIMESTAMP" - UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"not_null;type:TIMESTAMP"` + // @inject_tag: `gorm:"default:current_timestamp"` + CreateTime *Timestamp `protobuf:"bytes,2,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty" gorm:"default:current_timestamp"` + // @inject_tag: `gorm:"default:current_timestamp"` + UpdateTime *Timestamp `protobuf:"bytes,3,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" gorm:"default:current_timestamp"` // @inject_tat: gorm:"not_null" Name string `protobuf:"bytes,6,opt,name=name,proto3" json:"name,omitempty"` // @inject_tat: gorm:"not_null;default:'1'" diff --git a/internal/proto/local/controller/storage/oplog/store/v1/oplog.proto b/internal/proto/local/controller/storage/oplog/store/v1/oplog.proto index 0c239c18f..ab890cc8a 100644 --- a/internal/proto/local/controller/storage/oplog/store/v1/oplog.proto +++ b/internal/proto/local/controller/storage/oplog/store/v1/oplog.proto @@ -19,9 +19,9 @@ message Timestamp { message Entry { // @inject_tag: gorm:"primary_key" uint32 id = 1; - // @inject_tag: gorm:"not_null;type:TIMESTAMP" + // @inject_tag: `gorm:"default:current_timestamp"` Timestamp create_time = 2; - // @inject_tag: gorm:"not_null;type:TIMESTAMP" + // @inject_tag: `gorm:"default:current_timestamp"` Timestamp update_time = 3; // @inject_tag: gorm:"not_null" @@ -48,7 +48,7 @@ message Entry { message Metadata { // @inject_tag: gorm:"primary_key" uint32 id = 1; - // @inject_tag: gorm:"not_null;type:TIMESTAMP" + // @inject_tag: `gorm:"default:current_timestamp"` Timestamp create_time = 2; // @inject_tag: gorm:"not_nul @@ -66,9 +66,9 @@ message Metadata { message Ticket { // @inject_tag: gorm:"primary_key" uint32 id = 1; - // @inject_tag: gorm:"not_null;type:TIMESTAMP" + // @inject_tag: `gorm:"default:current_timestamp"` Timestamp create_time = 2; - // @inject_tag: gorm:"not_null;type:TIMESTAMP" + // @inject_tag: `gorm:"default:current_timestamp"` Timestamp update_time = 3; // @inject_tat: gorm:"not_null"