-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
431 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package base | ||
|
||
import ( | ||
"github.com/synapsecns/sanguine/core/metrics" | ||
submitterDB "github.com/synapsecns/sanguine/ethergo/submitter/db" | ||
"github.com/synapsecns/sanguine/ethergo/submitter/db/txdb" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// Store is a store that implements an underlying gorm db. | ||
type Store struct { | ||
db *gorm.DB | ||
metrics metrics.Handler | ||
submitterStore submitterDB.Service | ||
} | ||
|
||
// NewStore creates a new store. | ||
func NewStore(db *gorm.DB, metrics metrics.Handler) *Store { | ||
txDB := txdb.NewTXStore(db, metrics) | ||
return &Store{db: db, metrics: metrics, submitterStore: txDB} | ||
} | ||
|
||
// DB gets the database object for mutation outside of the lib. | ||
func (s Store) DB() *gorm.DB { | ||
return s.db | ||
} | ||
|
||
// SubmitterDB gets the submitter database object for mutation outside of the lib. | ||
func (s Store) SubmitterDB() submitterDB.Service { | ||
return s.submitterStore | ||
} | ||
|
||
// GetAllModels gets all models to migrate. | ||
// see: https://medium.com/@SaifAbid/slice-interfaces-8c78f8b6345d for an explanation of why we can't do this at initialization time | ||
func GetAllModels() (allModels []interface{}) { | ||
allModels = append(allModels, txdb.GetAllModels()...) | ||
return allModels | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package base contains the base sql implementation | ||
package base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package sql provides a common interface for starting sql-lite databases | ||
package sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package mysql contains a mysql db | ||
package mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/synapsecns/sanguine/contrib/opbot/sql/base" | ||
submitterDB "github.com/synapsecns/sanguine/ethergo/submitter/db" | ||
"time" | ||
|
||
"github.com/ipfs/go-log" | ||
common_base "github.com/synapsecns/sanguine/core/dbcommon" | ||
"github.com/synapsecns/sanguine/core/metrics" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/schema" | ||
) | ||
|
||
// Logger is the mysql logger. | ||
var logger = log.Logger("synapse-mysql") | ||
|
||
// NewMysqlStore creates a new mysql store for a given data store. | ||
func NewMysqlStore(ctx context.Context, dbURL string, handler metrics.Handler) (*Store, error) { | ||
logger.Debug("create mysql store") | ||
|
||
gdb, err := gorm.Open(mysql.Open(dbURL), &gorm.Config{ | ||
Logger: common_base.GetGormLogger(logger), | ||
FullSaveAssociations: true, | ||
NamingStrategy: NamingStrategy, | ||
NowFunc: time.Now, | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not create mysql connection: %w", err) | ||
} | ||
|
||
sqlDB, err := gdb.DB() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get sql db: %w", err) | ||
} | ||
|
||
// fixes a timeout issue https://stackoverflow.com/a/42146536 | ||
sqlDB.SetMaxIdleConns(MaxIdleConns) | ||
sqlDB.SetConnMaxLifetime(time.Hour) | ||
|
||
handler.AddGormCallbacks(gdb) | ||
|
||
err = gdb.WithContext(ctx).AutoMigrate(base.GetAllModels()...) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not migrate on mysql: %w", err) | ||
} | ||
|
||
return &Store{base.NewStore(gdb, handler)}, nil | ||
} | ||
|
||
// Store is the mysql store. It extends the bsae store for mysql queries. | ||
type Store struct { | ||
*base.Store | ||
} | ||
|
||
// MaxIdleConns is exported here for testing. Tests execute too slowly with a reconnect each time. | ||
var MaxIdleConns = 10 | ||
|
||
// NamingStrategy is for table prefixes. | ||
var NamingStrategy = schema.NamingStrategy{} | ||
|
||
var _ submitterDB.SubmitterDBFactory = &Store{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package sqlite implements the sqlite package | ||
package sqlite |
Oops, something went wrong.