From d0e238e64aca82be96f8eee1d045510977a7f7d8 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Thu, 21 May 2020 20:07:32 +0200 Subject: [PATCH] fix: use repository instead db package --- pkg/daemon/daemon.go | 16 ++++++++-------- pkg/repository/db/db.go | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 862a6f8..14d7173 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -8,7 +8,7 @@ import ( "syscall" "github.com/volker-raschek/flucky/pkg/config" - "github.com/volker-raschek/flucky/pkg/repository/db" + "github.com/volker-raschek/flucky/pkg/repository" "github.com/volker-raschek/flucky/pkg/sensor" "github.com/volker-raschek/flucky/pkg/types" "github.com/volker-raschek/go-logger/pkg/logger" @@ -37,7 +37,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error { return err } - backend, err := db.New(storageEndpointURL, flogger) + repo, err := repository.New(storageEndpointURL, flogger) if err != nil { return err } @@ -49,18 +49,18 @@ func Start(cnf *config.Config, flogger logger.Logger) error { parentCtx := context.Background() // Insert device if not exist - device, _ := backend.SelectDevice(parentCtx, cnf.Device.ID) + device, _ := repo.GetDevice(cnf.Device.ID) if device == nil { - if err := backend.InsertDevices(parentCtx, cnf.Device); err != nil { + if err := repo.AddDevices(cnf.Device); err != nil { return err } } // Insert sensors if not exist for _, cnfSensor := range cnf.Sensors { - sensor, _ := backend.SelectSensor(parentCtx, cnfSensor.ID) + sensor, _ := repo.GetSensor(cnfSensor.ID) if sensor == nil { - if err := backend.InsertSensors(parentCtx, cnfSensor); err != nil { + if err := repo.AddSensors(cnfSensor); err != nil { return err } } @@ -97,7 +97,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error { if cap(measuredValues) == len(measuredValues) { flogger.Debug("Flush cache") - err := backend.InsertMeasuredValues(ctx, measuredValues...) + err := repo.AddMeasuredValues(measuredValues...) if err != nil { flogger.Error("%v", err) } @@ -108,7 +108,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error { cancel() close(measuredValueChannel) - err := backend.InsertMeasuredValues(ctx, measuredValues...) + err := repo.AddMeasuredValues(measuredValues...) if err != nil { flogger.Error("%v", err) } diff --git a/pkg/repository/db/db.go b/pkg/repository/db/db.go index 65d7002..ff26dde 100644 --- a/pkg/repository/db/db.go +++ b/pkg/repository/db/db.go @@ -85,6 +85,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { case "sqlite3": + // Create directory where the db file will be created if not exists. if _, err := os.Stat(filepath.Dir(storageEndpoint.Path)); os.IsNotExist(err) { err := os.MkdirAll(filepath.Dir(storageEndpoint.Path), 0755) if err != nil { @@ -108,6 +109,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { return nil, fmt.Errorf("Unsupported database scheme: %v", storageEndpoint.Scheme) } + // Initialize database scheme if not exists err = database.Scheme(context.Background()) if err != nil { return nil, err