Skip to content

Commit

Permalink
dcrsqlite: create DB-file parent directory when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
JFixby committed Jun 30, 2018
1 parent e0a9b66 commit 9fdada8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ ffldb_stake*/
*.pprof
testnet/
.DS_Store

testdata/
1 change: 1 addition & 0 deletions db/dcrsqlite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.data
22 changes: 10 additions & 12 deletions db/dcrsqlite/dcrsqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package dcrsqlite

import (
"os"

"path/filepath"
"testing"
)

// Clears target folder content
func removeContents(dir string) error {
func RemoveContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
Expand All @@ -32,7 +33,7 @@ const (
)

// Returns path of a temporary directory used by these tests to store some data
func getTempFolderPath(t *testing.T) string {
func TempFolderPath(t *testing.T) string {
testDir, err := filepath.Abs(defaultDataDirname)
if err != nil {
t.Fatalf("Failed to produce DB-test folder path: %v", err)
Expand All @@ -41,24 +42,21 @@ func getTempFolderPath(t *testing.T) string {
}

// Ensures we run our test in a clean room. Removes all files created by any of these tests in the temp directory.
func resetTempFolder(t *testing.T) {
testFolderPath := getTempFolderPath(t)

removeContents(testFolderPath)
func ResetTempFolder(t *testing.T) {
testFolderPath := TempFolderPath(t)
err := os.RemoveAll(testFolderPath)

//Failed to clear test-files
if err != nil {
t.Fatalf("Failed to clear temp folder %v", err)
}
}

// Creates a path to a file inside the temp directory
func getFilePathInsideTempDir(t *testing.T, pathInsideTempFolder string) string {
tempDir := getTempFolderPath(t)

func FilePathInsideTempDir(t *testing.T, pathInsideTempFolder string) string {
tempDir := TempFolderPath(t)
targetPath := filepath.Join(tempDir, pathInsideTempFolder)
targetPath, err := filepath.Abs(targetPath)

if err != nil {
t.Fatalf("Failed to build a path %v", err)
}
Expand All @@ -68,8 +66,8 @@ func getFilePathInsideTempDir(t *testing.T, pathInsideTempFolder string) string
// TestMissingParentFolder ensures InitDB() is able to create a new DB-file parent directory if necessary
// See https://github.com/decred/dcrdata/issues/515
func TestMissingParentFolder(t *testing.T) {
resetTempFolder(t)
targetDBFile := getFilePathInsideTempDir(t, "x/y/z/"+defaultDBFileName)
ResetTempFolder(t)
targetDBFile := FilePathInsideTempDir(t, "x/y/z/"+defaultDBFileName)
dbInfo := &DBInfo{FileName: targetDBFile}
db, err := InitDB(dbInfo)

Expand Down
19 changes: 16 additions & 3 deletions db/dcrsqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ package dcrsqlite
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/decred/slog"

"github.com/decred/dcrd/wire"
apitypes "github.com/decred/dcrdata/api/types"
"github.com/decred/dcrdata/blockdata"
"github.com/decred/slog"
_ "github.com/mattn/go-sqlite3" // register sqlite driver with database/sql
)

Expand Down Expand Up @@ -146,7 +147,19 @@ func NewDB(db *sql.DB) (*DB, error) {
// InitDB creates a new DB instance from a DBInfo containing the name of the
// file used to back the underlying sql database.
func InitDB(dbInfo *DBInfo) (*DB, error) {
db, err := sql.Open("sqlite3", dbInfo.FileName)
dbPath, err := filepath.Abs(dbInfo.FileName)
if err != nil {
return nil, err
}

// Ensures target DB-file has a parent folder
parent := filepath.Dir(dbPath)
err = os.MkdirAll(parent, 0755)
if err != nil {
return nil, err
}

db, err := sql.Open("sqlite3", dbPath)
if err != nil || db == nil {
return nil, err
}
Expand Down

0 comments on commit 9fdada8

Please sign in to comment.