waveterm/pkg/wstore/wstore_dbsetup.go
2024-05-21 11:09:22 -07:00

58 lines
1.0 KiB
Go

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package wstore
import (
"context"
"fmt"
"log"
"path"
"time"
"github.com/jmoiron/sqlx"
"github.com/sawka/txwrap"
"github.com/wavetermdev/thenextwave/pkg/wavebase"
)
const WStoreDBName = "waveterm.db"
type TxWrap = txwrap.TxWrap
var globalDB *sqlx.DB
func InitWStore() error {
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
var err error
globalDB, err = MakeDB(ctx)
if err != nil {
return err
}
err = MigrateWStore()
if err != nil {
return err
}
log.Printf("wstore initialized\n")
return nil
}
func GetDBName() string {
waveHome := wavebase.GetWaveHomeDir()
return path.Join(waveHome, WStoreDBName)
}
func MakeDB(ctx context.Context) (*sqlx.DB, error) {
dbName := GetDBName()
rtn, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&_journal_mode=WAL&_busy_timeout=5000", dbName))
if err != nil {
return nil, err
}
rtn.DB.SetMaxOpenConns(1)
return rtn, nil
}
func MigrateWStore() error {
return nil
}