Skip to content

Commit

Permalink
Fix created date for Moments in SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaydimitrov committed Nov 11, 2024
1 parent cbff1d1 commit 8cbac9b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 5 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

var (
Instance *gorm.DB
TimestampFunc = ""
Instance *gorm.DB
TimestampFunc = ""
CreatedDateFunc = ""
)

func Init() {
Expand All @@ -26,6 +27,7 @@ func Init() {
log.Fatalf("MySQL DB error: %v", err)
}
TimestampFunc = "unix_timestamp()"
CreatedDateFunc = "date(from_unixtime(created_at))"
} else if config.SQLITE_FILE != "" {
// Sqlite setup
db, err = gorm.Open(sqlite.Open(config.SQLITE_FILE), &gorm.Config{})
Expand All @@ -37,6 +39,7 @@ func Init() {
// sqliteDB.SetMaxOpenConns(1)
// }
TimestampFunc = "strftime('%s', 'now')"
CreatedDateFunc = "date(created_at, 'unixepoch')"
} else {
log.Fatal("No database configuration found")
}
Expand Down
12 changes: 6 additions & 6 deletions handlers/moment.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ func MomentList(c *gin.Context, user *models.User) {
rows, err := db.Instance.Raw(`
select date,
if(city = '', area, city),
group_concat(place_id separator ',') places,
group_concat(place_id) places,
max(hero),
min(start),
max(end)
from (select place_id,
from_unixtime(created_at, '%Y-%m-%d') date,
max(id) hero,
count(*) cnt,
min(created_at) start,
max(created_at) end
`+db.CreatedDateFunc+` date,
max(id) hero,
count(*) cnt,
min(created_at) start,
max(created_at) end
from assets
where user_id = ?
and deleted = 0
Expand Down

0 comments on commit 8cbac9b

Please sign in to comment.