Skip to content

Commit

Permalink
fix(pluginapi/store): init master db if no replica (mattermost#29674)
Browse files Browse the repository at this point in the history
* fix(pluginapi/store): init master db if no replica

GetReplicaDB return nil because masterDB is not initialized.

* chore: add test
  • Loading branch information
ebuildy authored Jan 2, 2025
1 parent 64c566a commit b61580f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/public/pluginapi/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (s *StoreService) GetReplicaDB() (*sql.DB, error) {
return s.replicaDB, nil
}

if err := s.initializeMaster(); err != nil {
return nil, err
}

return s.masterDB, nil
}

Expand Down
33 changes: 33 additions & 0 deletions server/public/pluginapi/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ func TestStore(t *testing.T) {
require.NoError(t, store.Close())
})

t.Run("master db fallback without get master first", func(t *testing.T) {
config := &model.Config{
SqlSettings: model.SqlSettings{
DriverName: model.NewPointer("ramsql"),
DataSource: model.NewPointer("TestStore-master-db"),
ConnMaxLifetimeMilliseconds: model.NewPointer(2),
},
}

driver := &plugintest.Driver{}
defer driver.AssertExpectations(t)
driver.On("Conn", true).Return("test", nil)
driver.On("ConnPing", "test").Return(nil)
driver.On("ConnClose", "test").Return(nil)

api := &plugintest.API{}
defer api.AssertExpectations(t)
store := pluginapi.NewClient(api, driver).Store

api.On("GetUnsanitizedConfig").Return(config)
// No replica is set up, should fallback to master
replicaDB, err := store.GetReplicaDB()
require.NoError(t, err)
require.NotNil(t, replicaDB)

masterDB, err := store.GetMasterDB()
require.NoError(t, err)
require.NotNil(t, masterDB)
require.Same(t, replicaDB, masterDB)

require.NoError(t, store.Close())
})

t.Run("replica db singleton", func(t *testing.T) {
config := &model.Config{
SqlSettings: model.SqlSettings{
Expand Down

0 comments on commit b61580f

Please sign in to comment.