2023-01-04 17:58:14 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/golang-migrate/migrate/v4/database"
|
2023-06-30 01:57:57 +00:00
|
|
|
"github.com/golang-migrate/migrate/v4/database/pgx"
|
2023-07-07 01:38:23 +00:00
|
|
|
_ "github.com/jackc/pgx/v5/stdlib" // Blank import to register the postgres driver
|
2023-01-04 17:58:14 +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/postgres/migrations"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WithDB is a DBOption that lets you use a postgresql DBStore and run migrations
|
|
|
|
func WithDB(dburl string, migrate bool) persistence.DBOption {
|
|
|
|
return func(d *persistence.DBStore) error {
|
2023-06-30 01:57:57 +00:00
|
|
|
driverOption := persistence.WithDriver("pgx", dburl)
|
2023-01-04 17:58:14 +00:00
|
|
|
err := driverOption(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !migrate {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
migrationOpt := persistence.WithMigrations(Migrate)
|
|
|
|
err = migrationOpt(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDB connects to postgres DB in the specified path
|
|
|
|
func NewDB(dburl string) (*sql.DB, func(*sql.DB) error, error) {
|
2023-06-30 01:57:57 +00:00
|
|
|
db, err := sql.Open("pgx", dburl)
|
2023-01-04 17:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, Migrate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrationDriver(db *sql.DB) (database.Driver, error) {
|
2023-06-30 01:57:57 +00:00
|
|
|
return pgx.WithInstance(db, &pgx.Config{
|
|
|
|
MigrationsTable: "gowaku_" + pgx.DefaultMigrationsTable,
|
2023-01-04 17:58:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-07 01:38:23 +00:00
|
|
|
// Migrate is the function used for DB migration with postgres driver
|
|
|
|
func Migrate(db *sql.DB) error {
|
|
|
|
migrationDriver, err := migrationDriver(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return migrate.Migrate(db, migrationDriver, migrations.AssetNames(), migrations.Asset)
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
// 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 BYTEA);", tableName)
|
|
|
|
_, err := db.Exec(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-07 01:38:23 +00:00
|
|
|
// NewQueries creates a new SQL set of queries for the passed table
|
|
|
|
func NewQueries(tbl string, db *sql.DB) (*persistence.Queries, error) {
|
|
|
|
err := CreateTable(db, tbl)
|
2023-01-04 17:58:14 +00:00
|
|
|
if err != nil {
|
2023-07-07 01:38:23 +00:00
|
|
|
return nil, err
|
2023-01-04 17:58:14 +00:00
|
|
|
}
|
2023-07-07 01:38:23 +00:00
|
|
|
return persistence.CreateQueries(tbl, db), nil
|
2023-01-04 17:58:14 +00:00
|
|
|
}
|