mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* build(deps): Bump golangci/golangci-lint-action from 6 to 7 Bumps [golangci/golangci-lint-action](https://github.com/golangci/golangci-lint-action) from 6 to 7. - [Release notes](https://github.com/golangci/golangci-lint-action/releases) - [Commits](https://github.com/golangci/golangci-lint-action/compare/v6...v7) --- updated-dependencies: - dependency-name: golangci/golangci-lint-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * golangci-lint: Bump to v2 and fix issues * refactor: remove no longer need ifshort nolint directives, fix test grammar and improve for loop logic * nits: update order pair handling in tests and improve string replacement logic --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
"github.com/thrasher-corp/gocryptotrader/core"
|
|
"github.com/thrasher-corp/gocryptotrader/database"
|
|
dbPSQL "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres"
|
|
dbsqlite3 "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite3"
|
|
"github.com/thrasher-corp/gocryptotrader/database/repository"
|
|
"github.com/thrasher-corp/goose"
|
|
)
|
|
|
|
var (
|
|
dbConn *database.Instance
|
|
configFile string
|
|
defaultDataDir string
|
|
migrationDir string
|
|
command string
|
|
args string
|
|
)
|
|
|
|
func openDBConnection(cfg *database.Config) (err error) {
|
|
switch cfg.Driver {
|
|
case database.DBPostgreSQL:
|
|
dbConn, err = dbPSQL.Connect(cfg)
|
|
case database.DBSQLite, database.DBSQLite3:
|
|
dbConn, err = dbsqlite3.Connect(cfg.Database)
|
|
default:
|
|
return fmt.Errorf("unsupported database driver: %q", cfg.Driver)
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("database failed to connect: %w, some features that utilise a database will be unavailable", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println("GoCryptoTrader database migration tool")
|
|
fmt.Println(core.Copyright)
|
|
fmt.Println()
|
|
|
|
flag.StringVar(&command, "command", "", "command to run status|up|up-by-one|up-to|down|create")
|
|
flag.StringVar(&args, "args", "", "arguments to pass to goose")
|
|
flag.StringVar(&configFile, "config", config.DefaultFilePath(), "config file to load")
|
|
flag.StringVar(&defaultDataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files")
|
|
flag.StringVar(&migrationDir, "migrationdir", database.MigrationDir, "override migration folder")
|
|
|
|
flag.Parse()
|
|
|
|
var conf config.Config
|
|
err := conf.LoadConfig(configFile, true)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if !conf.Database.Enabled {
|
|
fmt.Println("Database support is disabled")
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = openDBConnection(&conf.Database)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
drv := repository.GetSQLDialect()
|
|
if drv == database.DBSQLite || drv == database.DBSQLite3 {
|
|
fmt.Printf("Database file: %s\n", conf.Database.Database)
|
|
} else {
|
|
fmt.Printf("Connected to: %s\n", conf.Database.Host)
|
|
}
|
|
|
|
if command == "" {
|
|
_ = goose.Run("status", dbConn.SQL, drv, migrationDir, "")
|
|
fmt.Println()
|
|
flag.Usage()
|
|
return
|
|
}
|
|
|
|
if err = goose.Run(command, dbConn.SQL, drv, migrationDir, args); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|