diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-07 13:17:18 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-07 13:18:44 -0600 |
commit | a10a604018d0cb07babfe218d9fb2e00e1c8ae3b (patch) | |
tree | 8d701b7350baee5fd619dfa35d1ac31a82fc2ce9 /srv/src/mailinglist | |
parent | c99b37c5b37d8944522eb73288581ca99670b660 (diff) |
Refactor how data dir is initialized
Diffstat (limited to 'srv/src/mailinglist')
-rw-r--r-- | srv/src/mailinglist/store.go | 14 | ||||
-rw-r--r-- | srv/src/mailinglist/store_test.go | 23 |
2 files changed, 17 insertions, 20 deletions
diff --git a/srv/src/mailinglist/store.go b/srv/src/mailinglist/store.go index a7a210f..49e7617 100644 --- a/srv/src/mailinglist/store.go +++ b/srv/src/mailinglist/store.go @@ -7,10 +7,12 @@ import ( "errors" "fmt" "io" + "path" "strings" "time" _ "github.com/mattn/go-sqlite3" + "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" migrate "github.com/rubenv/sql-migrate" ) @@ -83,13 +85,15 @@ type store struct { db *sql.DB } -// NewStore initializes a new Store using a sqlite3 database at the given file -// path. -func NewStore(dbFile string) (Store, error) { +// NewStore initializes a new Store using a sqlite3 database in the given +// DataDir. +func NewStore(dataDir cfg.DataDir) (Store, error) { - db, err := sql.Open("sqlite3", dbFile) + path := path.Join(dataDir.Path, "mailinglist.sqlite3") + + db, err := sql.Open("sqlite3", path) if err != nil { - return nil, fmt.Errorf("opening sqlite file: %w", err) + return nil, fmt.Errorf("opening sqlite file at %q: %w", path, err) } migrations := &migrate.MemoryMigrationSource{Migrations: migrations} diff --git a/srv/src/mailinglist/store_test.go b/srv/src/mailinglist/store_test.go index 25eb150..9093d90 100644 --- a/srv/src/mailinglist/store_test.go +++ b/srv/src/mailinglist/store_test.go @@ -2,31 +2,24 @@ package mailinglist import ( "io" - "io/ioutil" - "os" "testing" "time" + "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" "github.com/stretchr/testify/assert" ) func TestStore(t *testing.T) { - tmpFile, err := ioutil.TempFile(os.TempDir(), "mediocre-blog-mailinglist-store-test-") - if err != nil { - t.Fatal("Cannot create temporary file", err) - } - tmpFilePath := tmpFile.Name() - tmpFile.Close() - t.Logf("using temporary sqlite file at %q", tmpFilePath) + var dataDir cfg.DataDir - t.Cleanup(func() { - if err := os.Remove(tmpFilePath); err != nil { - panic(err) - } - }) + if err := dataDir.Init(); err != nil { + t.Fatal(err) + } + + t.Cleanup(func() { dataDir.Close() }) - store, err := NewStore(tmpFilePath) + store, err := NewStore(dataDir) assert.NoError(t, err) t.Cleanup(func() { |