fix: Fix database query

Fix ordering in database query if more than one tweet with same timestamp
This commit is contained in:
Manuel 2023-10-17 00:07:00 +02:00
parent 711f2d2e7b
commit 712d83c0f0
Signed by: SunRed
GPG Key ID: 4085037435E1F07A
1 changed files with 4 additions and 4 deletions

View File

@ -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