diff --git a/.gitignore b/.gitignore index 66656da29..2a4d26489 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ ffldb_stake*/ *.pprof testnet/ .DS_Store + +testdata/ diff --git a/db/dcrsqlite/.gitignore b/db/dcrsqlite/.gitignore new file mode 100644 index 000000000..13b54c9fb --- /dev/null +++ b/db/dcrsqlite/.gitignore @@ -0,0 +1 @@ +test.data diff --git a/db/dcrsqlite/dcrsqlite_test.go b/db/dcrsqlite/dcrsqlite_test.go index 0d3298f78..6462ed08a 100644 --- a/db/dcrsqlite/dcrsqlite_test.go +++ b/db/dcrsqlite/dcrsqlite_test.go @@ -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 @@ -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) @@ -41,12 +42,9 @@ 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) @@ -54,11 +52,11 @@ func resetTempFolder(t *testing.T) { } // 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) } @@ -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) diff --git a/db/dcrsqlite/sqlite.go b/db/dcrsqlite/sqlite.go index e264ee27e..71583a26b 100644 --- a/db/dcrsqlite/sqlite.go +++ b/db/dcrsqlite/sqlite.go @@ -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 ) @@ -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 }