Skip to content

Commit

Permalink
Fix history database URI on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Jul 28, 2023
1 parent b882b91 commit 2452a92
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 7 additions & 5 deletions netquery/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"path"
"path/filepath"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}

Expand Down Expand Up @@ -180,7 +183,7 @@ func New(dbPath string) (*Database, error) {
readConnPool: pool,
Schema: schema,
writeConn: writeConn,
historyPath: historyPath,
historyPath: historyURI,
}, nil
}

Expand All @@ -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()

Expand Down
1 change: 1 addition & 0 deletions netquery/module_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2452a92

Please sign in to comment.