Skip to content
Permalink
Browse files

sqlite will now create dir if needed (#55)

  • Loading branch information...
vkuznecovas committed Apr 24, 2018
1 parent 6c76d76 commit 45ad89c6239b57576cb7b4df949a1540011197ce
Showing with 80 additions and 2 deletions.
  1. +2 −1 .gitignore
  2. +1 −1 config.json
  3. +19 −0 db/sqlxDriver/sqlite/sqlite.go
  4. +58 −0 db/sqlxDriver/sqlite/sqlite_test.go
@@ -9,4 +9,5 @@ debug.test
goreleaser-build/
.DS_Store
main
coverage.txt
coverage.txt
_mouthful_db
@@ -9,7 +9,7 @@
},
"database": {
"dialect": "sqlite3",
"database": "./test.db"
"database": "./_mouthful_db/test.db"
},
"api": {
"port": 8080,
@@ -5,8 +5,11 @@ import (
"errors"
"log"
"os"
"path/filepath"
"time"

"github.com/spf13/afero"

"github.com/jmoiron/sqlx"
// We absolutely need the sqlite driver here, this whole package depends on it
_ "github.com/mattn/go-sqlite3"
@@ -61,6 +64,10 @@ func CreateDatabase(databaseConfig model.Database) (abstraction.Database, error)
}
db = d
} else {
err := CreateDirectoryIfNotExists(*databaseConfig.Database, afero.NewOsFs())
if err != nil {
return nil, err
}
d, err := sqlx.Connect("sqlite3", *databaseConfig.Database)
if err != nil {
return nil, err
@@ -109,6 +116,18 @@ func CreateTestDatabase() abstraction.Database {
return &DB
}

// CreateDirectoryIfNotExists checks for the given directory and if not found - creates it. Used for sqlite database initialization
func CreateDirectoryIfNotExists(path string, fs afero.Fs) error {
dir := filepath.Dir(path)
if _, err := fs.Stat(dir); os.IsNotExist(err) {
err = fs.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
}
return nil
}

func periodicWipe(db sqlxDriver.Database) {
ticker := time.NewTicker(24 * time.Hour)
quit := make(chan struct{})
@@ -0,0 +1,58 @@
package sqlite_test

import (
"os"
"testing"

"github.com/vkuznecovas/mouthful/db/sqlxDriver/sqlite"

"github.com/stretchr/testify/assert"

"github.com/spf13/afero"
)

type Database struct {
Dialect string `json:"dialect"`
Database *string `json:"database,omitempty"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
Host *string `json:"host,omitempty"`
Port *string `json:"port,omitempty"`
TablePrefix *string `json:"tablePrefix,omitempty"`
DynamoDBThreadReadUnits *int64 `json:"dynamoDBThreadReadUnits,omitempty"`
DynamoDBCommentReadUnits *int64 `json:"dynamoDBCommentReadUnits,omitempty"`
DynamoDBThreadWriteUnits *int64 `json:"dynamoDBThreadWriteUnits,omitempty"`
DynamoDBCommentWriteUnits *int64 `json:"dynamoDBCommentWriteUnits,omitempty"`
DynamoDBIndexWriteUnits *int64 `json:"dynamoDBIndexWriteUnits,omitempty"`
DynamoDBIndexReadUnits *int64 `json:"dynamoDBIndexReadUnits,omitempty"`
AwsAccessKeyID *string `json:"awsAccessKeyID,omitempty"`
AwsSecretAccessKey *string `json:"awsSecretAccessKey,omitempty"`
AwsRegion *string `json:"awsRegion,omitempty"`
SSLEnabled *bool `json:"sslEnabled,omitempty"`
}

func TestCreateDirectoryIfNotExists(t *testing.T) {
path := "./test/test.db"
fs := afero.NewMemMapFs()
fs.Mkdir("./test", os.ModePerm)
d, err := fs.Stat("./test")
assert.Nil(t, err)
assert.NotNil(t, d)

err = sqlite.CreateDirectoryIfNotExists(path, fs)
assert.Nil(t, err)
f, err := fs.Stat("./test")
assert.Nil(t, err)
assert.NotNil(t, f)

path = "./test2/test.db"
_, err = fs.Stat("./test2")
assert.NotNil(t, err)
assert.True(t, os.IsNotExist(err))

err = sqlite.CreateDirectoryIfNotExists(path, fs)
assert.Nil(t, err)
f, err = fs.Stat("./test2")
assert.Nil(t, err)
assert.NotNil(t, f)
}

0 comments on commit 45ad89c

Please sign in to comment.
You can’t perform that action at this time.