From 2452a92b8b8fed1c69ae1d4a6a37263b395ef5ac Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 28 Jul 2023 16:48:26 +0200 Subject: [PATCH] Fix history database URI on windows --- netquery/database.go | 12 +++++++----- netquery/module_api.go | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/netquery/database.go b/netquery/database.go index f1abc633..00e4f006 100644 --- a/netquery/database.go +++ b/netquery/database.go @@ -7,7 +7,7 @@ import ( "encoding/json" "fmt" "io" - "path" + "path/filepath" "sort" "strings" "sync" @@ -124,7 +124,10 @@ func New(dbPath string) (*Database, error) { return nil, fmt.Errorf("failed to ensure database directory exists: %w", err) } - historyPath := "file://" + path.Join(historyParentDir.Path, "history.db") + // Get file location of history database. + historyFile := filepath.Join(historyParentDir.Path, "history.db") + // Convert to SQLite URI path. + historyURI := "file:///" + strings.TrimPrefix(filepath.ToSlash(historyFile), "/") constructor := func(ctx context.Context) (*sqlite.Conn, error) { c, err := sqlite.OpenConn( @@ -137,7 +140,7 @@ func New(dbPath string) (*Database, error) { return nil, fmt.Errorf("failed to open read-only sqlite connection at %s: %w", dbPath, err) } - if err := sqlitex.ExecuteTransient(c, "ATTACH DATABASE '"+historyPath+"?mode=ro' AS history", nil); err != nil { + if err := sqlitex.ExecuteTransient(c, "ATTACH DATABASE '"+historyURI+"?mode=ro' AS history", nil); err != nil { return nil, fmt.Errorf("failed to attach history database: %w", err) } @@ -180,7 +183,7 @@ func New(dbPath string) (*Database, error) { readConnPool: pool, Schema: schema, writeConn: writeConn, - historyPath: historyPath, + historyPath: historyURI, }, nil } @@ -207,7 +210,6 @@ func NewInMemory() (*Database, error) { // any data-migrations. Once the history module is implemented this should // become/use a full migration system -- use zombiezen.com/go/sqlite/sqlitemigration. func (db *Database) ApplyMigrations() error { - log.Errorf("applying migrations ...") db.l.Lock() defer db.l.Unlock() diff --git a/netquery/module_api.go b/netquery/module_api.go index 344f9391..6817e797 100644 --- a/netquery/module_api.go +++ b/netquery/module_api.go @@ -64,6 +64,7 @@ func (m *module) prepare() error { Internal: true, }) + // TODO: Open database in start() phase. m.Store, err = NewInMemory() if err != nil { return fmt.Errorf("failed to create in-memory database: %w", err)