From 02fcb7b7e824a5b8e0eeee2fcaee10c58f983b5d Mon Sep 17 00:00:00 2001 From: Sacha Froment Date: Tue, 8 Jan 2019 17:26:32 +0100 Subject: [PATCH 1/2] fix: state db always get last and create instead of update Signed-off-by: Sacha Froment --- client/react-native/gomobile/core/network.go | 13 ++++++++--- core/manager/account/db.go | 24 ++++++++++---------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/client/react-native/gomobile/core/network.go b/client/react-native/gomobile/core/network.go index 7bd6836476..cf3938a4f0 100644 --- a/client/react-native/gomobile/core/network.go +++ b/client/react-native/gomobile/core/network.go @@ -78,13 +78,14 @@ func UpdateNetworkConfig(jsonConf string) error { defer panicHandler() waitDaemon(accountName) currentAccount, _ := account.Get(rootContext, accountName) + newState := &account.StateDB{} var newNetworkConfig networkConfig if err := json.Unmarshal([]byte(jsonConf), &newNetworkConfig); err != nil { return err } - appConfig.JSONNetConf = jsonConf + newState.JSONNetConf = jsonConf netConf, err := createNetworkConfig() if err != nil { return err @@ -93,10 +94,16 @@ func UpdateNetworkConfig(jsonConf string) error { return err } - appConfig.StartCounter++ - if err := appConfig.Save(); err != nil { + newState.StartCounter = appConfig.StartCounter + 1 + newState.BotMode = appConfig.BotMode + newState.LocalGRPC = appConfig.LocalGRPC + newState.Gorm = appConfig.Gorm + if err := newState.Create(); err != nil { return errors.Wrap(err, "state DB save failed") } + // if Create is successfull assign the newState to our global + appConfig = newState + return nil } diff --git a/core/manager/account/db.go b/core/manager/account/db.go index bb4f1fcf21..37413037e7 100644 --- a/core/manager/account/db.go +++ b/core/manager/account/db.go @@ -15,7 +15,7 @@ import ( // type StateDB struct { - gorm *gorm.DB `gorm:"-"` + Gorm *gorm.DB `gorm:"-"` gorm.Model StartCounter int @@ -39,21 +39,17 @@ func OpenStateDB(path string, initialState StateDB) (*StateDB, error) { // preload last state var state StateDB - if err := db.FirstOrInit(&state).Error; err != nil { + if err := db.Last(&state).Error; err != nil && err != gorm.ErrRecordNotFound { return nil, err - } - - // if no previous state found, set initial state - if state.StartCounter == 0 { + } else if err == gorm.ErrRecordNotFound { state = initialState - state.gorm = db - if err := state.Save(); err != nil { + if err := db.Save(&state).Error; err != nil { return nil, err } - } else { - state.gorm = db } + state.Gorm = db + return &state, nil } @@ -63,11 +59,15 @@ func (state StateDB) String() string { } func (state *StateDB) Save() error { - return state.gorm.Save(state).Error + return state.Gorm.Save(state).Error +} + +func (state *StateDB) Create() error { + return state.Gorm.Create(state).Error } func (state *StateDB) Close() { - state.gorm.Close() + state.Gorm.Close() } // From 30aaf0284b60497803c2d61ff66b47ac338c4664 Mon Sep 17 00:00:00 2001 From: Sacha Froment Date: Tue, 8 Jan 2019 17:43:09 +0100 Subject: [PATCH 2/2] fix: db state not stored in the right path Like for account.dbPath we should add a trailing / Signed-off-by: Sacha Froment --- client/react-native/gomobile/core/core.go | 2 +- client/react-native/gomobile/core/network.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/react-native/gomobile/core/core.go b/client/react-native/gomobile/core/core.go index 2fa43ff045..777ae29bbb 100644 --- a/client/react-native/gomobile/core/core.go +++ b/client/react-native/gomobile/core/core.go @@ -111,7 +111,7 @@ func initOrRestoreAppState(datastorePath string) error { LocalGRPC: initiallocalGRPC, } - appState, err := account.OpenStateDB(datastorePath+"berty.state.db", initialState) + appState, err := account.OpenStateDB(datastorePath+"/berty.state.db", initialState) if err != nil { return errors.Wrap(err, "state DB init failed") } diff --git a/client/react-native/gomobile/core/network.go b/client/react-native/gomobile/core/network.go index cf3938a4f0..a3f46c5db6 100644 --- a/client/react-native/gomobile/core/network.go +++ b/client/react-native/gomobile/core/network.go @@ -102,7 +102,7 @@ func UpdateNetworkConfig(jsonConf string) error { return errors.Wrap(err, "state DB save failed") } - // if Create is successfull assign the newState to our global + // if Create is successful assign the newState to our global appConfig = newState return nil