Skip to content

Commit

Permalink
fix: use NewQueries from db utils
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Oct 13, 2023
1 parent 1194ba9 commit a5c3887
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cmd/waku/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/pbnjay/memory"
"github.com/prometheus/client_golang/prometheus"

"github.com/waku-org/go-waku/waku/persistence/sqlite"
dbutils "github.com/waku-org/go-waku/waku/persistence/utils"
wakupeerstore "github.com/waku-org/go-waku/waku/v2/peerstore"
"github.com/waku-org/go-waku/waku/v2/rendezvous"
Expand Down Expand Up @@ -203,7 +202,7 @@ func Execute(options NodeOptions) {

if options.Store.Enable && options.PersistPeers {
// Create persistent peerstore
queries, err := sqlite.NewQueries("peerstore", db)
queries, err := dbutils.NewQueries("peerstore", db)
failOnErr(err, "Peerstore")

datastore := dssql.NewDatastore(db, queries)
Expand Down
22 changes: 22 additions & 0 deletions waku/persistence/driver_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package persistence

import (
"database/sql"
"reflect"
)

const (
UndefinedDriver = iota
PostgresDriver
SQLiteDriver
)

func GetDriverType(db *sql.DB) int {
switch reflect.TypeOf(db.Driver()).String() {
case "*sqlite3.SQLiteDriver":
return SQLiteDriver
case "*stdlib.Driver":
return PostgresDriver
}
return UndefinedDriver
}
8 changes: 4 additions & 4 deletions waku/persistence/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -239,12 +238,13 @@ func (d *DBStore) cleanOlderRecords(ctx context.Context) error {

return nil
}

func (d *DBStore) getDeleteOldRowsQuery() string {
sqlStmt := `DELETE FROM message WHERE id IN (SELECT id FROM message ORDER BY receiverTimestamp DESC %s OFFSET $1)`
switch reflect.TypeOf(d.db.Driver()).String() {
case "*sqlite3.SQLiteDriver":
switch GetDriverType(d.db) {
case SQLiteDriver:
sqlStmt = fmt.Sprintf(sqlStmt, "LIMIT -1")
case "*stdlib.Driver":
case PostgresDriver:
sqlStmt = fmt.Sprintf(sqlStmt, "")
}
return sqlStmt
Expand Down
11 changes: 11 additions & 0 deletions waku/persistence/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"

"github.com/waku-org/go-waku/waku/persistence"
"github.com/waku-org/go-waku/waku/persistence/postgres"
"github.com/waku-org/go-waku/waku/persistence/sqlite"
"go.uber.org/zap"
Expand Down Expand Up @@ -65,3 +66,13 @@ func ExtractDBAndMigration(databaseURL string, dbSettings DBSettings, logger *za
return db, migrationFn, nil

}

func NewQueries(tbl string, db *sql.DB) (*persistence.Queries, error) {
switch persistence.GetDriverType(db) {
case persistence.SQLiteDriver:
return sqlite.NewQueries(tbl, db)
case persistence.PostgresDriver:
return postgres.NewQueries(tbl, db)
}
return nil, errors.New("unsupported database engine")
}

0 comments on commit a5c3887

Please sign in to comment.