2024-05-21 20:09:22 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-06-03 22:03:21 +02:00
|
|
|
package filestore
|
2024-05-12 18:52:12 +02:00
|
|
|
|
2024-06-03 22:03:21 +02:00
|
|
|
// setup for filestore db
|
2024-05-12 18:52:12 +02:00
|
|
|
// includes migration support and txwrap setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-06-15 23:59:14 +02:00
|
|
|
"path/filepath"
|
2024-05-12 18:52:12 +02:00
|
|
|
"time"
|
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/util/migrateutil"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wavebase"
|
2024-05-12 18:52:12 +02:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/sawka/txwrap"
|
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
dbfs "github.com/wavetermdev/waveterm/db"
|
2024-05-12 18:52:12 +02:00
|
|
|
)
|
|
|
|
|
2024-06-03 22:03:21 +02:00
|
|
|
const FilestoreDBName = "filestore.db"
|
2024-05-12 18:52:12 +02:00
|
|
|
|
|
|
|
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)
|
2024-05-12 18:52:12 +02:00
|
|
|
|
2024-06-03 22:03:21 +02:00
|
|
|
func InitFilestore() 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)
|
2024-05-12 18:52:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-03 22:03:21 +02:00
|
|
|
err = migrateutil.Migrate("filestore", globalDB.DB, dbfs.FilestoreMigrationFS, "migrations-filestore")
|
2024-05-12 18:52:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-29 03:27:38 +02:00
|
|
|
if !stopFlush.Load() {
|
2024-06-03 22:03:21 +02:00
|
|
|
go WFS.runFlusher()
|
2024-05-29 03:27:38 +02:00
|
|
|
}
|
2024-06-03 22:03:21 +02:00
|
|
|
log.Printf("filestore initialized\n")
|
2024-05-12 18:52:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDBName() string {
|
Update data and config paths to match platform defaults (#1047)
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`.
2024-10-22 18:26:58 +02:00
|
|
|
waveHome := wavebase.GetWaveDataDir()
|
2024-09-05 23:05:42 +02:00
|
|
|
return filepath.Join(waveHome, wavebase.WaveDBDir, FilestoreDBName)
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-05-12 18:52:12 +02:00
|
|
|
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))
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-13 20:45:47 +02:00
|
|
|
return nil, fmt.Errorf("opening db: %w", err)
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|
2024-05-13 20:45:47 +02:00
|
|
|
rtn.DB.SetMaxOpenConns(1)
|
|
|
|
return rtn, nil
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|