mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
33f05c6e0c
Going forward for new installations, config and data files will be stored at the platform default paths, as defined by [env-paths](https://www.npmjs.com/package/env-paths). For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME` directory exists and contains valid data, it will be used. If this check fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be used. If these are not defined, then `XDG_DATA_HOME` and `XDG_CONFIG_HOME` will be used. Finally, if none of these are defined, the [env-paths](https://www.npmjs.com/package/env-paths) defaults will be used. As with the existing app, dev instances will write to `waveterm-dev` directories, while all others will write to `waveterm`.
82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
// Copyright 2024, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package wstore
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/sawka/txwrap"
|
|
"github.com/wavetermdev/waveterm/pkg/util/migrateutil"
|
|
"github.com/wavetermdev/waveterm/pkg/wavebase"
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
|
|
|
dbfs "github.com/wavetermdev/waveterm/db"
|
|
)
|
|
|
|
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 = migrateutil.Migrate("wstore", globalDB.DB, dbfs.WStoreMigrationFS, "migrations-wstore")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Printf("wstore initialized\n")
|
|
return nil
|
|
}
|
|
|
|
func GetDBName() string {
|
|
waveHome := wavebase.GetWaveDataDir()
|
|
return filepath.Join(waveHome, wavebase.WaveDBDir, 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 WithTx(ctx context.Context, fn func(tx *TxWrap) error) (rtnErr error) {
|
|
waveobj.ContextUpdatesBeginTx(ctx)
|
|
defer func() {
|
|
if rtnErr != nil {
|
|
waveobj.ContextUpdatesRollbackTx(ctx)
|
|
} else {
|
|
waveobj.ContextUpdatesCommitTx(ctx)
|
|
}
|
|
}()
|
|
return txwrap.WithTx(ctx, globalDB, fn)
|
|
}
|
|
|
|
func WithTxRtn[RT any](ctx context.Context, fn func(tx *TxWrap) (RT, error)) (rtnVal RT, rtnErr error) {
|
|
waveobj.ContextUpdatesBeginTx(ctx)
|
|
defer func() {
|
|
if rtnErr != nil {
|
|
waveobj.ContextUpdatesRollbackTx(ctx)
|
|
} else {
|
|
waveobj.ContextUpdatesCommitTx(ctx)
|
|
}
|
|
}()
|
|
return txwrap.WithTxRtn(ctx, globalDB, fn)
|
|
}
|