waveterm/pkg/blockstore/dbsetup.go

140 lines
3.5 KiB
Go
Raw Normal View History

2024-05-21 20:09:22 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package blockstore
// setup for blockstore db
// includes migration support and txwrap setup
import (
"context"
"fmt"
"log"
"path"
"time"
"github.com/wavetermdev/thenextwave/pkg/wavebase"
"github.com/golang-migrate/migrate/v4"
2024-05-13 09:02:32 +02:00
sqlite3migrate "github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/sawka/txwrap"
dbfs "github.com/wavetermdev/thenextwave/db"
)
2024-05-21 20:09:22 +02:00
const BlockstoreDBName = "blockstore.db"
type TxWrap = txwrap.TxWrap
var globalDB *sqlx.DB
2024-05-13 09:02:32 +02:00
var useTestingDb bool // just for testing (forces GetDB() to return an in-memory db)
func InitBlockstore() error {
2024-05-13 20:45:47 +02:00
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
var err error
globalDB, err = MakeDB(ctx)
if err != nil {
return err
}
2024-05-13 20:45:47 +02:00
err = MigrateBlockstore()
if err != nil {
return err
}
log.Printf("blockstore initialized\n")
return nil
}
func GetDBName() string {
2024-05-21 20:09:22 +02:00
waveHome := wavebase.GetWaveHomeDir()
return path.Join(waveHome, BlockstoreDBName)
}
2024-05-13 20:45:47 +02:00
func MakeDB(ctx context.Context) (*sqlx.DB, error) {
var rtn *sqlx.DB
var err error
if useTestingDb {
dbName := ":memory:"
log.Printf("[db] using in-memory db\n")
rtn, err = sqlx.Open("sqlite3", dbName)
} else {
dbName := GetDBName()
2024-05-13 20:45:47 +02:00
log.Printf("[db] opening db %s\n", dbName)
2024-05-21 20:09:22 +02:00
rtn, err = sqlx.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&_journal_mode=WAL&_busy_timeout=5000", dbName))
}
if err != nil {
2024-05-13 20:45:47 +02:00
return nil, fmt.Errorf("opening db: %w", err)
}
2024-05-13 20:45:47 +02:00
rtn.DB.SetMaxOpenConns(1)
return rtn, nil
}
func WithTx(ctx context.Context, fn func(tx *TxWrap) error) error {
2024-05-13 20:45:47 +02:00
return txwrap.WithTx(ctx, globalDB, fn)
}
func WithTxRtn[RT any](ctx context.Context, fn func(tx *TxWrap) (RT, error)) (RT, error) {
2024-05-13 20:45:47 +02:00
return txwrap.WithTxRtn(ctx, globalDB, fn)
}
func MakeBlockstoreMigrate() (*migrate.Migrate, error) {
fsVar, err := iofs.New(dbfs.BlockstoreMigrationFS, "migrations-blockstore")
if err != nil {
return nil, fmt.Errorf("opening iofs: %w", err)
}
2024-05-13 20:45:47 +02:00
mdriver, err := sqlite3migrate.WithInstance(globalDB.DB, &sqlite3migrate.Config{})
2024-05-13 09:02:32 +02:00
if err != nil {
return nil, fmt.Errorf("making blockstore migration driver: %w", err)
}
m, err := migrate.NewWithInstance("iofs", fsVar, "sqlite3", mdriver)
if err != nil {
return nil, fmt.Errorf("making blockstore migration db[%s]: %w", GetDBName(), err)
}
return m, nil
}
2024-05-13 20:45:47 +02:00
func MigrateBlockstore() error {
log.Printf("migrate blockstore\n")
m, err := MakeBlockstoreMigrate()
if err != nil {
return err
}
curVersion, dirty, err := GetMigrateVersion(m)
if dirty {
return fmt.Errorf("cannot migrate up, database is dirty")
}
if err != nil {
return fmt.Errorf("cannot get current migration version: %v", err)
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
return fmt.Errorf("migrating blockstore: %w", err)
}
newVersion, _, err := GetMigrateVersion(m)
if err != nil {
return fmt.Errorf("cannot get new migration version: %v", err)
}
if newVersion != curVersion {
log.Printf("[db] blockstore migration done, version %d -> %d\n", curVersion, newVersion)
}
return nil
}
func GetMigrateVersion(m *migrate.Migrate) (uint, bool, error) {
if m == nil {
var err error
m, err = MakeBlockstoreMigrate()
if err != nil {
return 0, false, err
}
}
curVersion, dirty, err := m.Version()
if err == migrate.ErrNilVersion {
return 0, false, nil
}
return curVersion, dirty, err
}