2021-02-06 01:48:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2021-02-17 11:06:55 +00:00
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
"github.com/c9s/rockhopper/v2"
|
|
|
|
|
2021-02-17 11:06:55 +00:00
|
|
|
mysqlMigrations "github.com/c9s/bbgo/pkg/migrations/mysql"
|
|
|
|
sqlite3Migrations "github.com/c9s/bbgo/pkg/migrations/sqlite3"
|
2021-02-06 01:48:05 +00:00
|
|
|
)
|
|
|
|
|
2022-05-30 10:08:35 +00:00
|
|
|
// reflect cache for database
|
|
|
|
var dbCache = NewReflectCache()
|
|
|
|
|
2021-02-06 01:48:05 +00:00
|
|
|
type DatabaseService struct {
|
|
|
|
Driver string
|
|
|
|
DSN string
|
|
|
|
DB *sqlx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatabaseService(driver, dsn string) *DatabaseService {
|
|
|
|
if driver == "mysql" {
|
|
|
|
var err error
|
|
|
|
dsn, err = ReformatMysqlDSN(dsn)
|
|
|
|
if err != nil {
|
|
|
|
// incorrect mysql dsn is logical exception
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DatabaseService{
|
|
|
|
Driver: driver,
|
|
|
|
DSN: dsn,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DatabaseService) Connect() error {
|
|
|
|
var err error
|
|
|
|
s.DB, err = sqlx.Connect(s.Driver, s.DSN)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-30 10:08:35 +00:00
|
|
|
func (s *DatabaseService) Insert(record interface{}) error {
|
|
|
|
sql := dbCache.InsertSqlOf(record)
|
|
|
|
_, err := s.DB.NamedExec(sql, record)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:48:05 +00:00
|
|
|
func (s *DatabaseService) Close() error {
|
|
|
|
return s.DB.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DatabaseService) Upgrade(ctx context.Context) error {
|
|
|
|
dialect, err := rockhopper.LoadDialect(s.Driver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-17 11:06:55 +00:00
|
|
|
var migrations rockhopper.MigrationSlice
|
|
|
|
|
|
|
|
switch s.Driver {
|
|
|
|
case "sqlite3":
|
|
|
|
migrations = sqlite3Migrations.Migrations()
|
|
|
|
case "mysql":
|
|
|
|
migrations = mysqlMigrations.Migrations()
|
|
|
|
|
2021-02-06 01:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sqlx.DB is different from sql.DB
|
2024-01-18 13:35:19 +00:00
|
|
|
rh := rockhopper.New(s.Driver, dialect, s.DB.DB, rockhopper.TableName)
|
2021-02-06 01:48:05 +00:00
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
if err := rh.Touch(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-18 13:35:19 +00:00
|
|
|
migrations = migrations.FilterPackage([]string{"main"}).SortAndConnect()
|
2024-01-18 16:34:02 +00:00
|
|
|
if len(migrations) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, lastAppliedMigration, err := rh.FindLastAppliedMigration(ctx, migrations)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastAppliedMigration != nil {
|
|
|
|
return rockhopper.Up(ctx, rh, lastAppliedMigration.Next, 0)
|
|
|
|
}
|
2021-02-06 01:48:05 +00:00
|
|
|
|
2024-01-18 16:34:02 +00:00
|
|
|
// TODO: use align in the next major version
|
|
|
|
// return rockhopper.Align(ctx, rh, 20231123125402, migrations)
|
|
|
|
return rockhopper.Up(ctx, rh, migrations.Head(), 0)
|
2021-02-06 01:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReformatMysqlDSN(dsn string) (string, error) {
|
|
|
|
config, err := mysql.ParseDSN(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-02-06 03:33:49 +00:00
|
|
|
// we need timestamp and datetime fields to be parsed into time.Time struct
|
2021-02-06 01:48:05 +00:00
|
|
|
config.ParseTime = true
|
|
|
|
dsn = config.FormatDSN()
|
|
|
|
return dsn, nil
|
|
|
|
}
|