forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Database interface & auditing feature (thrasher-corp#332)
* added audit manager * Basic database DOA setup * Added base config file * added sqlite support and creation of schema * added basic tests and config entry * corrected issues of database is disabled * fixed path for test * WIP * Added tests fixed config checking * reverted files back to upstream * reverted go.mod files * no more test test test * removed local testing details for psql * hello * added comments * increased ping to 30 seconds * renamed database table and added additional condition around test * removed database test details * goimport ran on all files * WIP * first attempt at migration * fixes for migration system * Migration system logger interface implemented * fixes to print functions * added write pooling pass * gofmt :D * formatted imports correctly * removed old code * added creation of migration * gofmt * :D Hello * ❌ 🏎️ * maybe one day i will remember to revert go mod files * checked err return condition correctly * first changes for PR feedback * code clean up * protect Connected with RWmutex & event with mutex * : D * we can just pretend like it never happened * MOved migrations back to source directory and added README * readme formatting update * Addd command line override for datadir * use correct var when creating a migration and confirm folder is created * Check if database version is newer than latest migration and also you know make migrations work..... * uses filepath instead of manual path to use correct path seperator * Add connection message and lower timeout * Added support for sslmode for psql * no longer force Close of database instead allow driver to maage * Added closer func to test output * sslmode added to example config
- Loading branch information
Showing
30 changed files
with
1,295 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/thrasher-corp/gocryptotrader/common" | ||
"github.com/thrasher-corp/gocryptotrader/config" | ||
"github.com/thrasher-corp/gocryptotrader/core" | ||
"github.com/thrasher-corp/gocryptotrader/database" | ||
db "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres" | ||
dbsqlite3 "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite" | ||
mg "github.com/thrasher-corp/gocryptotrader/database/migration" | ||
) | ||
|
||
var ( | ||
dbConn *database.Database | ||
configFile string | ||
defaultDataDir string | ||
createMigration string | ||
migrationDir string | ||
) | ||
|
||
var defaultMigration = []byte(`-- up | ||
-- down | ||
`) | ||
|
||
func openDbConnection(driver string) (err error) { | ||
if driver == "postgres" { | ||
dbConn, err = db.Connect() | ||
if err != nil { | ||
return fmt.Errorf("database failed to connect: %v Some features that utilise a database will be unavailable", err) | ||
} | ||
|
||
dbConn.SQL.SetMaxOpenConns(2) | ||
dbConn.SQL.SetMaxIdleConns(1) | ||
dbConn.SQL.SetConnMaxLifetime(time.Hour) | ||
|
||
} else if driver == "sqlite" { | ||
dbConn, err = dbsqlite3.Connect() | ||
|
||
if err != nil { | ||
return fmt.Errorf("database failed to connect: %v Some features that utilise a database will be unavailable", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type tmpLogger struct{} | ||
|
||
// Printf implantation of migration Logger interface | ||
// Passes directly to Printf from fmt package | ||
func (t tmpLogger) Printf(format string, v ...interface{}) { | ||
fmt.Printf(format, v...) | ||
} | ||
|
||
// Println implantation of migration Logger interface | ||
// Passes directly to Println from fmt package | ||
func (t tmpLogger) Println(v ...interface{}) { | ||
fmt.Println(v...) | ||
} | ||
|
||
// Errorf implantation of migration Logger interface | ||
// Passes directly to Printf from fmt package | ||
func (t tmpLogger) Errorf(format string, v ...interface{}) { | ||
fmt.Printf(format, v...) | ||
} | ||
|
||
func main() { | ||
fmt.Println("GoCryptoTrader database migration tool") | ||
fmt.Println(core.Copyright) | ||
fmt.Println() | ||
|
||
defaultPath, err := config.GetFilePath("") | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
flag.StringVar(&configFile, "config", defaultPath, "config file to load") | ||
flag.StringVar(&defaultDataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files") | ||
flag.StringVar(&createMigration, "create", "", "create a new empty migration file") | ||
flag.StringVar(&migrationDir, "migrationdir", mg.MigrationDir, "override migration folder") | ||
|
||
flag.Parse() | ||
|
||
if createMigration != "" { | ||
err = newMigrationFile(createMigration) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Migration created successfully") | ||
os.Exit(0) | ||
} | ||
|
||
tempLogger := tmpLogger{} | ||
|
||
temp := mg.Migrator{ | ||
Log: tempLogger, | ||
} | ||
|
||
err = temp.LoadMigrations() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
|
||
conf := config.GetConfig() | ||
|
||
err = conf.LoadConfig(configFile) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
|
||
err = openDbConnection(conf.Database.Driver) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Printf("Connected to: %s\n", conf.Database.Host) | ||
|
||
temp.Conn = dbConn | ||
|
||
err = temp.RunMigration() | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
if dbConn.SQL != nil { | ||
err = dbConn.SQL.Close() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
} | ||
|
||
func newMigrationFile(filename string) error { | ||
curTime := strconv.FormatInt(time.Now().Unix(), 10) | ||
path := filepath.Join(migrationDir, curTime+"_"+filename+".sql") | ||
err := common.CreateDir(migrationDir) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Creating new empty migration: %v\n", path) | ||
f, err := os.Create(path) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = f.Write(defaultMigration) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return f.Close() | ||
} |
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
Oops, something went wrong.