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"
|
2023-08-09 17:14:02 +00:00
|
|
|
"go.uber.org/zap"
|
2023-01-04 17:58:14 +00:00
|
|
|
)
|
|
|
|
|
2023-08-09 17:14:02 +00:00
|
|
|
func executeVacuum(db *sql.DB, logger *zap.Logger) error {
|
|
|
|
logger.Info("starting PostgreSQL database vacuuming")
|
2023-08-10 13:40:08 +00:00
|
|
|
_, err := db.Exec("VACUUM FULL")
|
2023-08-09 17:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Info("finished PostgreSQL database vacuuming")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
// NewDB connects to postgres 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) {
|
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 {
|
2023-08-09 17:23:44 +00:00
|
|
|
return nil, err
|
2023-01-04 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 17:14:02 +00:00
|
|
|
if shouldVacuum {
|
|
|
|
err := executeVacuum(db, logger)
|
|
|
|
if err != nil {
|
2023-08-09 17:23:44 +00:00
|
|
|
return nil, err
|
2023-08-09 17:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 17:23:44 +00:00
|
|
|
return db, nil
|
2023-01-04 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-09 17:23:44 +00:00
|
|
|
// Migrations is the function used for DB migration with postgres driver
|
|
|
|
func Migrations(db *sql.DB) error {
|
2023-07-07 01:38:23 +00:00
|
|
|
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
|
|
|
}
|