DB RPC driver: add master/replica support (#17792)

Automatic Merge
This commit is contained in:
Agniva De Sarker
2021-06-17 21:10:22 +05:30
committed by GitHub
parent 030543fed9
commit 79d4e9e9a9
6 changed files with 37 additions and 19 deletions

View File

@@ -32,15 +32,20 @@ func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model
store := sqlstore.New(p.API.GetUnsanitizedConfig().SqlSettings, nil)
store.GetMaster().Db.Close()
store.GetMaster().Db = sql.OpenDB(driver.NewConnector(p.Driver))
defer store.GetMaster().Db.Close()
for _, isMaster := range []bool{true, false} {
// We replace the master DB with master and replica both just to make
// gorp APIs work.
store.GetMaster().Db = sql.OpenDB(driver.NewConnector(p.Driver, isMaster))
// Testing with a handful of stores
storetest.TestPostStore(p.t, store, store)
storetest.TestUserStore(p.t, store, store)
storetest.TestTeamStore(p.t, store)
storetest.TestChannelStore(p.t, store, store)
storetest.TestBotStore(p.t, store, store)
// Testing with a handful of stores
storetest.TestPostStore(p.t, store, store)
storetest.TestUserStore(p.t, store, store)
storetest.TestTeamStore(p.t, store)
storetest.TestChannelStore(p.t, store, store)
storetest.TestBotStore(p.t, store, store)
store.GetMaster().Db.Close()
}
// Use the API to instantiate the driver
// And then run the full suite of tests.

View File

@@ -39,8 +39,12 @@ func NewDriverImpl(s *Server) *DriverImpl {
}
}
func (d *DriverImpl) Conn() (string, error) {
conn, err := d.s.sqlStore.GetMaster().Db.Conn(context.Background())
func (d *DriverImpl) Conn(isMaster bool) (string, error) {
dbFunc := d.s.sqlStore.GetMaster
if !isMaster {
dbFunc = d.s.sqlStore.GetReplica
}
conn, err := dbFunc().Db.Conn(context.Background())
if err != nil {
return "", err
}