Skip to content

Commit

Permalink
fix: state db always get last and create instead of update
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Jan 8, 2019
1 parent ccd9427 commit 02fcb7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
13 changes: 10 additions & 3 deletions client/react-native/gomobile/core/network.go
Expand Up @@ -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
Expand All @@ -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
}
24 changes: 12 additions & 12 deletions core/manager/account/db.go
Expand Up @@ -15,7 +15,7 @@ import (
//

type StateDB struct {
gorm *gorm.DB `gorm:"-"`
Gorm *gorm.DB `gorm:"-"`
gorm.Model

StartCounter int
Expand All @@ -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
}

Expand All @@ -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()
}

//
Expand Down

0 comments on commit 02fcb7b

Please sign in to comment.