From 712d83c0f0ebe9ba53b596227f2f092b6fb2bc1c Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 17 Oct 2023 00:07:00 +0200 Subject: [PATCH] fix: Fix database query Fix ordering in database query if more than one tweet with same timestamp --- cmd/database.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/database.go b/cmd/database.go index d90188d..44fff72 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -57,7 +57,7 @@ func NewDatabase(driver string, connectString string) (*Database, error) { func (db *Database) GetNewestTweet(channel string) (*Tweet, error) { tweet := Tweet{} - err := db.Get(&tweet, "SELECT * FROM tweet WHERE channel=$1 ORDER BY timestamp DESC LIMIT 1", channel) + err := db.Get(&tweet, "SELECT * FROM tweet WHERE channel=$1 ORDER BY timestamp DESC, snowflake DESC LIMIT 1", channel) if err != nil { return nil, err } @@ -66,7 +66,7 @@ func (db *Database) GetNewestTweet(channel string) (*Tweet, error) { func (db *Database) GetTweets(channel string) ([]*Tweet, error) { tweet := []*Tweet{} - err := db.Select(&tweet, "SELECT * FROM tweet WHERE channel=$1 ORDER BY timestamp DESC", channel) + err := db.Select(&tweet, "SELECT * FROM tweet WHERE channel=$1 ORDER BY timestamp DESC, snowflake DESC", channel) if err != nil { return nil, err } @@ -80,7 +80,7 @@ func (db *Database) ContainsTweet(channel string, tweet *ts.Tweet) (bool, error) } t := Tweet{} - rows, err := db.Queryx("SELECT * FROM tweet WHERE channel=$1 ORDER BY timestamp DESC", channel) + rows, err := db.Queryx("SELECT * FROM tweet WHERE channel=$1 ORDER BY timestamp DESC, snowflake DESC", channel) if err != nil { return false, err } @@ -124,7 +124,7 @@ func (db *Database) PruneOldestTweets(channel string) error { tx.Rollback() return err } - rows, err := tx.Queryx("SELECT tweet_id from tweet WHERE channel=$1 ORDER by timestamp ASC LIMIT $2", channel, count-KeepTweets) + rows, err := tx.Queryx("SELECT tweet_id from tweet WHERE channel=$1 ORDER by timestamp ASC, snowflake ASC LIMIT $2", channel, count-KeepTweets) if err != nil { tx.Rollback() return err