fix: use NewQueries from db utils

This commit is contained in:
harsh-98 2023-10-05 07:20:02 +07:00 committed by harsh jain
parent d96e1aedde
commit b5802adf5b
4 changed files with 38 additions and 6 deletions

View File

@ -18,7 +18,6 @@ import (
"github.com/pbnjay/memory" "github.com/pbnjay/memory"
"github.com/prometheus/client_golang/prometheus" "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" dbutils "github.com/waku-org/go-waku/waku/persistence/utils"
wakupeerstore "github.com/waku-org/go-waku/waku/v2/peerstore" wakupeerstore "github.com/waku-org/go-waku/waku/v2/peerstore"
"github.com/waku-org/go-waku/waku/v2/rendezvous" "github.com/waku-org/go-waku/waku/v2/rendezvous"
@ -203,7 +202,7 @@ func Execute(options NodeOptions) {
if options.Store.Enable && options.PersistPeers { if options.Store.Enable && options.PersistPeers {
// Create persistent peerstore // Create persistent peerstore
queries, err := sqlite.NewQueries("peerstore", db) queries, err := dbutils.NewQueries("peerstore", db)
failOnErr(err, "Peerstore") failOnErr(err, "Peerstore")
datastore := dssql.NewDatastore(db, queries) datastore := dssql.NewDatastore(db, queries)

View File

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

View File

@ -5,7 +5,6 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"reflect"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -239,12 +238,13 @@ func (d *DBStore) cleanOlderRecords(ctx context.Context) error {
return nil return nil
} }
func (d *DBStore) getDeleteOldRowsQuery() string { func (d *DBStore) getDeleteOldRowsQuery() string {
sqlStmt := `DELETE FROM message WHERE id IN (SELECT id FROM message ORDER BY receiverTimestamp DESC %s OFFSET $1)` 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() { switch GetDriverType(d.db) {
case "*sqlite3.SQLiteDriver": case SQLiteDriver:
sqlStmt = fmt.Sprintf(sqlStmt, "LIMIT -1") sqlStmt = fmt.Sprintf(sqlStmt, "LIMIT -1")
case "*stdlib.Driver": case PostgresDriver:
sqlStmt = fmt.Sprintf(sqlStmt, "") sqlStmt = fmt.Sprintf(sqlStmt, "")
} }
return sqlStmt return sqlStmt

View File

@ -6,6 +6,7 @@ import (
"regexp" "regexp"
"strings" "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/postgres"
"github.com/waku-org/go-waku/waku/persistence/sqlite" "github.com/waku-org/go-waku/waku/persistence/sqlite"
"go.uber.org/zap" "go.uber.org/zap"
@ -65,3 +66,13 @@ func ExtractDBAndMigration(databaseURL string, dbSettings DBSettings, logger *za
return db, migrationFn, nil 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")
}