Skip to content

Commit

Permalink
Replace DateTime with DateTime64
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz committed Oct 26, 2024
1 parent 76a3ca5 commit 8c64d7a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions storage/data/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
positiveFeedbackType = "positiveFeedbackType"
negativeFeedbackType = "negativeFeedbackType"
duplicateFeedbackType = "duplicateFeedbackType"
dateTime64Zero = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
)

type baseTestSuite struct {
Expand Down Expand Up @@ -279,8 +280,8 @@ func (suite *baseTestSuite) TestFeedback() {
suite.Equal(strconv.Itoa(i*2), item.ItemId)
if item.ItemId != "0" {
if suite.isClickHouse() {
// ClickHouse returns 1970-01-01 as zero date.
suite.Zero(item.Timestamp.Unix())
// ClickHouse returns 1900-01-01 00:00:00 +0000 UTC as zero date.
suite.Equal(dateTime64Zero, item.Timestamp)
} else {
suite.Zero(item.Timestamp)
}
Expand Down
6 changes: 4 additions & 2 deletions storage/data/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (d *SQLDatabase) Init() error {
ItemId string `gorm:"column:item_id;type:String"`
IsHidden int `gorm:"column:is_hidden;type:Boolean;default:0"`
Categories string `gorm:"column:categories;type:String;default:'[]'"`
Timestamp time.Time `gorm:"column:time_stamp;type:Datetime"`
Timestamp time.Time `gorm:"column:time_stamp;type:Datetime64(9,'UTC')"`
Labels string `gorm:"column:labels;type:String;default:'[]'"`
Comment string `gorm:"column:comment;type:String"`
Version struct{} `gorm:"column:version;type:DateTime"`
Expand All @@ -249,7 +249,7 @@ func (d *SQLDatabase) Init() error {
FeedbackType string `gorm:"column:feedback_type;type:String"`
UserId string `gorm:"column:user_id;type:String;index:user_index,type:bloom_filter(0.01),granularity:1"`
ItemId string `gorm:"column:item_id;type:String;index:item_index,type:bloom_filter(0.01),granularity:1"`
Timestamp time.Time `gorm:"column:time_stamp;type:DateTime"`
Timestamp time.Time `gorm:"column:time_stamp;type:DateTime64(9,'UTC')"`
Comment string `gorm:"column:comment;type:String"`
Version struct{} `gorm:"column:version;type:DateTime"`
}
Expand Down Expand Up @@ -496,6 +496,8 @@ func (d *SQLDatabase) GetItemFeedback(ctx context.Context, itemId string, feedba
switch d.driver {
case SQLite:
tx.Where("time_stamp <= DATETIME() AND item_id = ?", itemId)
case ClickHouse:
tx.Where("time_stamp <= NOW('UTC') AND item_id = ?", itemId)
default:
tx.Where("time_stamp <= NOW() AND item_id = ?", itemId)
}
Expand Down

0 comments on commit 8c64d7a

Please sign in to comment.