@@ -14,9 +14,12 @@ import (
1414 "sync/atomic"
1515 "time"
1616
17+ gosql "database/sql"
1718 "github.com/github/gh-ost/go/mysql"
1819 "github.com/github/gh-ost/go/sql"
1920
21+ "github.com/outbrain/golib/sqlutils"
22+
2023 "gopkg.in/gcfg.v1"
2124 gcfgscanner "gopkg.in/gcfg.v1/scanner"
2225)
@@ -197,6 +200,9 @@ type MigrationContext struct {
197200 recentBinlogCoordinates mysql.BinlogCoordinates
198201
199202 CanStopStreaming func () bool
203+
204+ knownDBs map [string ]* gosql.DB
205+ knownDBsMutex * sync.Mutex
200206}
201207
202208type ContextConfig struct {
@@ -230,6 +236,8 @@ func NewMigrationContext() *MigrationContext {
230236 pointOfInterestTimeMutex : & sync.Mutex {},
231237 ColumnRenameMap : make (map [string ]string ),
232238 PanicAbort : make (chan error ),
239+ knownDBsMutex : & sync.Mutex {},
240+ knownDBs : make (map [string ]* gosql.DB ),
233241 }
234242}
235243
@@ -242,6 +250,23 @@ func getSafeTableName(baseName string, suffix string) string {
242250 return fmt .Sprintf ("_%s_%s" , baseName [0 :len (baseName )- extraCharacters ], suffix )
243251}
244252
253+ // GetDB returns a DB instance based on uri.
254+ // bool result indicates whether the DB was returned from cache; err
255+ func (this * MigrationContext ) GetDB (mysql_uri string ) (* gosql.DB , bool , error ) {
256+ this .knownDBsMutex .Lock ()
257+ defer this .knownDBsMutex .Unlock ()
258+
259+ var exists bool
260+ if _ , exists = this .knownDBs [mysql_uri ]; ! exists {
261+ if db , err := sqlutils .GetDB (mysql_uri ); err == nil {
262+ this .knownDBs [mysql_uri ] = db
263+ } else {
264+ return db , exists , err
265+ }
266+ }
267+ return this .knownDBs [mysql_uri ], exists , nil
268+ }
269+
245270// GetGhostTableName generates the name of ghost table, based on original table name
246271// or a given table name
247272func (this * MigrationContext ) GetGhostTableName () string {
0 commit comments