2021-04-13 18:52:57 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2022-12-10 11:25:26 +00:00
|
|
|
"strings"
|
2021-04-13 18:52:57 +00:00
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
"github.com/golang-migrate/migrate/v4/database"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
2021-10-09 18:18:53 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence"
|
2023-07-07 01:38:23 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence/migrate"
|
2023-01-04 17:58:14 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence/sqlite/migrations"
|
2023-08-08 15:46:32 +00:00
|
|
|
"go.uber.org/zap"
|
2021-04-13 18:52:57 +00:00
|
|
|
)
|
|
|
|
|
2022-12-10 11:25:26 +00:00
|
|
|
func addSqliteURLDefaults(dburl string) string {
|
|
|
|
if !strings.Contains(dburl, "?") {
|
|
|
|
dburl += "?"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(dburl, "_journal=") {
|
|
|
|
dburl += "&_journal=WAL"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(dburl, "_timeout=") {
|
|
|
|
dburl += "&_timeout=5000"
|
|
|
|
}
|
|
|
|
|
|
|
|
return dburl
|
|
|
|
}
|
|
|
|
|
2023-08-09 17:14:02 +00:00
|
|
|
func executeVacuum(db *sql.DB, logger *zap.Logger) error {
|
|
|
|
logger.Info("starting sqlite database vacuuming")
|
|
|
|
_, err := db.Exec("VACUUM")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Info("finished sqlite database vacuuming")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:49:52 +00:00
|
|
|
// NewDB creates a sqlite3 DB in the specified path
|
2023-08-09 17:23:44 +00:00
|
|
|
func NewDB(dburl string, shouldVacuum bool, logger *zap.Logger) (*sql.DB, error) {
|
2022-12-10 11:25:26 +00:00
|
|
|
db, err := sql.Open("sqlite3", addSqliteURLDefaults(dburl))
|
2021-04-13 18:52:57 +00:00
|
|
|
if err != nil {
|
2023-08-09 17:23:44 +00:00
|
|
|
return nil, err
|
2021-04-13 18:52:57 +00:00
|
|
|
}
|
2022-12-10 03:08:18 +00:00
|
|
|
|
|
|
|
// Disable concurrent access as not supported by the driver
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
2023-08-08 15:46:32 +00:00
|
|
|
if shouldVacuum {
|
2023-08-09 17:14:02 +00:00
|
|
|
err := executeVacuum(db, logger)
|
2023-08-08 15:46:32 +00:00
|
|
|
if err != nil {
|
2023-08-09 17:23:44 +00:00
|
|
|
return nil, err
|
2023-08-08 15:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 17:23:44 +00:00
|
|
|
return db, nil
|
2021-04-13 18:52:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
func migrationDriver(db *sql.DB) (database.Driver, error) {
|
|
|
|
return sqlite3.WithInstance(db, &sqlite3.Config{
|
|
|
|
MigrationsTable: "gowaku_" + sqlite3.DefaultMigrationsTable,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-09 17:23:44 +00:00
|
|
|
// Migrations is the function used for DB migration with sqlite driver
|
|
|
|
func Migrations(db *sql.DB) error {
|
2023-01-04 17:58:14 +00:00
|
|
|
migrationDriver, err := migrationDriver(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-07 01:38:23 +00:00
|
|
|
return migrate.Migrate(db, migrationDriver, migrations.AssetNames(), migrations.Asset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTable creates the table that will persist the peers
|
|
|
|
func CreateTable(db *sql.DB, tableName string) error {
|
|
|
|
sqlStmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (key TEXT NOT NULL UNIQUE, data BLOB);", tableName)
|
|
|
|
_, err := db.Exec(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueries creates a table if it doesn't exist and a new SQL set of queries for the passed table
|
|
|
|
func NewQueries(tbl string, db *sql.DB) (*persistence.Queries, error) {
|
|
|
|
err := CreateTable(db, tbl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return persistence.CreateQueries(tbl, db), nil
|
2023-01-04 17:58:14 +00:00
|
|
|
}
|