From 1c20be9d91f366d1be223cef6facc89e3645dcff Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 8 Apr 2020 09:51:10 +0100 Subject: [PATCH] add Store Close method (#1500) * add Store Close method * Update sync store build failure --- file.go | 13 ++++++++++--- file_test.go | 15 ++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/file.go b/file.go index 2f92438..2a62207 100644 --- a/file.go +++ b/file.go @@ -72,13 +72,13 @@ func (m *fileStore) init(opts ...store.Option) error { } // create a directory /tmp/micro - dir := filepath.Join(DefaultDir, "micro") + dir := filepath.Join(DefaultDir, m.options.Database) // create the database handle - fname := m.options.Database + ".db" + fname := m.options.Table + ".db" // Ignoring this as the folder might exist. // Reads/Writes updates will return with sensible error messages // about the dir not existing in case this cannot create the path anyway - _ = os.Mkdir(dir, 0700) + os.MkdirAll(dir, 0700) m.dir = dir m.fileName = fname @@ -223,6 +223,13 @@ func (m *fileStore) set(r *store.Record) error { }) } +func (m *fileStore) Close() error { + if m.db != nil { + return m.db.Close() + } + return nil +} + func (m *fileStore) Init(opts ...store.Option) error { return m.init(opts...) } diff --git a/file_test.go b/file_test.go index 86c533f..b3466b9 100644 --- a/file_test.go +++ b/file_test.go @@ -13,14 +13,15 @@ import ( "github.com/micro/go-micro/v2/store" ) -func cleanup() { - dir := filepath.Join(DefaultDir, "micro/") +func cleanup(db string, s store.Store) { + s.Close() + dir := filepath.Join(DefaultDir, db + "/") os.RemoveAll(dir) } func TestFileStoreReInit(t *testing.T) { - defer cleanup() s := NewStore(store.Table("aaa")) + defer cleanup(DefaultDatabase, s) s.Init(store.Table("bbb")) if s.Options().Table != "bbb" { t.Error("Init didn't reinitialise the store") @@ -28,26 +29,26 @@ func TestFileStoreReInit(t *testing.T) { } func TestFileStoreBasic(t *testing.T) { - defer cleanup() s := NewStore() + defer cleanup(DefaultDatabase, s) fileTest(s, t) } func TestFileStoreTable(t *testing.T) { - defer cleanup() s := NewStore(store.Table("testTable")) + defer cleanup(DefaultDatabase, s) fileTest(s, t) } func TestFileStoreDatabase(t *testing.T) { - defer cleanup() s := NewStore(store.Database("testdb")) + defer cleanup("testdb", s) fileTest(s, t) } func TestFileStoreDatabaseTable(t *testing.T) { - defer cleanup() s := NewStore(store.Table("testTable"), store.Database("testdb")) + defer cleanup("testdb", s) fileTest(s, t) }