mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Migrated to goose & sqlboiler * create tests with sqlboiler * code clean up * Added gct -> sqlboiler config gen * dropped pgx support * dropped pgx support because who needs connection pools * reenable sqlite audit tests * first pass of migration changes * stuff is broken :D * sqlboiler :D * end of date commit * Added comments code clean up * revert go module files back to upstream * bug fix * pushed go.mod update to use correc goose version * renamed sqlite to sqlite3 for consistency across codebase and PR feedback changes * makefile updates * things are broken end of day commit * added postgresql test * use correct database name * travis fixes for env vars * travis fixes for env vars * test fixes * run migration on test setup * test adding postgres support to appveyor * Skip tests on appveyor due to issues with missing binaries * oh yeah i have to support windows don't i * bumped goose version up * add postgres to osx * fix travis config as osx does not support services move spin up to before_script * added PGDATA path fix * pass PG_DATA to pg_ctl * added initdb to before install * fixes to wording and bumps up goose version * who needs ssl anyway * moved ssl to correct section :D * bumped goose version up * unbreak travis * unbreak travis * fix if database is disabled in config * move strings to consts * converted more strings to const * improvements to sqlboiler mmodel gen * Added contrib\sqlboiler file * sqlboiler windows contrib fixes * bumped goose version up * :D whoops * further fixes to sql models * further fixes to sql models * database type fix for config gen * README update * go.mod clean up * added config details for appveyor * appveyor ordering fix * force psql9.6 * appveyor config changes * all the environmen vars * model changes for psql * model changes for psql * sqlite model fixes * attempt at osx fix * added error check for migration * typos and check against goose error instead of string :D * updated sqlboiler commit id * bump sqlboiler version again * set decimal package to @0bb1631 * readme and makefile updates * bump goose version update readme and add override flag to config gen * README typo fix and lowered inserts in test down to 20 as we are only testing that inserts work running 200 was unnecessary * added gctcli command for audit event * Added debug output toggle to config added both postgres & sqlite support to gctcli command * Wording changes on errors * set sqlite to 1 connection to stop locke database issues * Usage update for order * README updates with config examples * go.mod/sum tidy * removed lines in import second * removed lines in imports * convert local time to utc for database and display output * go mod clean up and error checking to time * renamed all packages to sqlite3 * added windows command output for sql model gen * time conversion fix * time conversion on gctcli
244 lines
5.6 KiB
Go
244 lines
5.6 KiB
Go
// Code generated by SQLBoiler 3.5.0-gct (https://github.com/thrasher-corp/sqlboiler). DO NOT EDIT.
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/kat-co/vala"
|
|
_ "github.com/lib/pq"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/viper"
|
|
"github.com/thrasher-corp/goose"
|
|
"github.com/thrasher-corp/sqlboiler/drivers/sqlboiler-psql/driver"
|
|
"github.com/thrasher-corp/sqlboiler/randomize"
|
|
)
|
|
|
|
var rgxPGFkey = regexp.MustCompile(`(?m)^ALTER TABLE ONLY .*\n\s+ADD CONSTRAINT .*? FOREIGN KEY .*?;\n`)
|
|
|
|
type pgTester struct {
|
|
dbConn *sql.DB
|
|
|
|
dbName string
|
|
host string
|
|
user string
|
|
pass string
|
|
sslmode string
|
|
port int
|
|
|
|
pgPassFile string
|
|
|
|
testDBName string
|
|
skipSQLCmd bool
|
|
}
|
|
|
|
func init() {
|
|
dbMain = &pgTester{}
|
|
}
|
|
|
|
// setup dumps the database schema and imports it into a temporary randomly
|
|
// generated test database so that tests can be run against it using the
|
|
// generated sqlboiler ORM package.
|
|
func (p *pgTester) setup() error {
|
|
var err error
|
|
|
|
viper.SetDefault("psql.schema", "public")
|
|
viper.SetDefault("psql.port", 5432)
|
|
viper.SetDefault("psql.sslmode", "require")
|
|
|
|
p.dbName = viper.GetString("psql.dbname")
|
|
p.host = viper.GetString("psql.host")
|
|
p.user = viper.GetString("psql.user")
|
|
p.pass = viper.GetString("psql.pass")
|
|
p.port = viper.GetInt("psql.port")
|
|
p.sslmode = viper.GetString("psql.sslmode")
|
|
p.testDBName = viper.GetString("psql.testdbname")
|
|
p.skipSQLCmd = viper.GetBool("psql.skipsqlcmd")
|
|
|
|
err = vala.BeginValidation().Validate(
|
|
vala.StringNotEmpty(p.user, "psql.user"),
|
|
vala.StringNotEmpty(p.host, "psql.host"),
|
|
vala.Not(vala.Equals(p.port, 0, "psql.port")),
|
|
vala.StringNotEmpty(p.dbName, "psql.dbname"),
|
|
vala.StringNotEmpty(p.sslmode, "psql.sslmode"),
|
|
).Check()
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// if no testing DB passed
|
|
if len(p.testDBName) == 0 {
|
|
// Create a randomized db name.
|
|
p.testDBName = randomize.StableDBName(p.dbName)
|
|
}
|
|
|
|
if err = p.makePGPassFile(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !p.skipSQLCmd {
|
|
if err = p.dropTestDB(); err != nil {
|
|
return err
|
|
}
|
|
if err = p.createTestDB(); err != nil {
|
|
return err
|
|
}
|
|
|
|
dumpCmd := exec.Command("pg_dump", "--schema-only", p.dbName)
|
|
dumpCmd.Env = append(os.Environ(), p.pgEnv()...)
|
|
createCmd := exec.Command("psql", p.testDBName)
|
|
createCmd.Env = append(os.Environ(), p.pgEnv()...)
|
|
|
|
r, w := io.Pipe()
|
|
dumpCmdStderr := &bytes.Buffer{}
|
|
createCmdStderr := &bytes.Buffer{}
|
|
|
|
dumpCmd.Stdout = w
|
|
dumpCmd.Stderr = dumpCmdStderr
|
|
|
|
createCmd.Stdin = newFKeyDestroyer(rgxPGFkey, r)
|
|
createCmd.Stderr = createCmdStderr
|
|
|
|
if err = dumpCmd.Start(); err != nil {
|
|
return errors.Wrap(err, "failed to start pg_dump command")
|
|
}
|
|
if err = createCmd.Start(); err != nil {
|
|
return errors.Wrap(err, "failed to start psql command")
|
|
}
|
|
|
|
if err = dumpCmd.Wait(); err != nil {
|
|
fmt.Println(err)
|
|
fmt.Println(dumpCmdStderr.String())
|
|
return errors.Wrap(err, "failed to wait for pg_dump command")
|
|
}
|
|
|
|
_ = w.Close() // After dumpCmd is done, close the write end of the pipe
|
|
|
|
if err = createCmd.Wait(); err != nil {
|
|
fmt.Println(err)
|
|
fmt.Println(createCmdStderr.String())
|
|
return errors.Wrap(err, "failed to wait for psql command")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *pgTester) runCmd(stdin, command string, args ...string) error {
|
|
cmd := exec.Command(command, args...)
|
|
cmd.Env = append(os.Environ(), p.pgEnv()...)
|
|
|
|
if len(stdin) != 0 {
|
|
cmd.Stdin = strings.NewReader(stdin)
|
|
}
|
|
|
|
stdout := &bytes.Buffer{}
|
|
stderr := &bytes.Buffer{}
|
|
cmd.Stdout = stdout
|
|
cmd.Stderr = stderr
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Println("failed running:", command, args)
|
|
fmt.Println(stdout.String())
|
|
fmt.Println(stderr.String())
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *pgTester) pgEnv() []string {
|
|
return []string{
|
|
fmt.Sprintf("PGHOST=%s", p.host),
|
|
fmt.Sprintf("PGPORT=%d", p.port),
|
|
fmt.Sprintf("PGUSER=%s", p.user),
|
|
fmt.Sprintf("PGPASSFILE=%s", p.pgPassFile),
|
|
}
|
|
}
|
|
|
|
func (p *pgTester) makePGPassFile() error {
|
|
tmp, err := ioutil.TempFile("", "pgpass")
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to create option file")
|
|
}
|
|
|
|
fmt.Fprintf(tmp, "%s:%d:postgres:%s", p.host, p.port, p.user)
|
|
if len(p.pass) != 0 {
|
|
fmt.Fprintf(tmp, ":%s", p.pass)
|
|
}
|
|
fmt.Fprintln(tmp)
|
|
|
|
fmt.Fprintf(tmp, "%s:%d:%s:%s", p.host, p.port, p.dbName, p.user)
|
|
if len(p.pass) != 0 {
|
|
fmt.Fprintf(tmp, ":%s", p.pass)
|
|
}
|
|
fmt.Fprintln(tmp)
|
|
|
|
fmt.Fprintf(tmp, "%s:%d:%s:%s", p.host, p.port, p.testDBName, p.user)
|
|
if len(p.pass) != 0 {
|
|
fmt.Fprintf(tmp, ":%s", p.pass)
|
|
}
|
|
fmt.Fprintln(tmp)
|
|
|
|
p.pgPassFile = tmp.Name()
|
|
return tmp.Close()
|
|
}
|
|
|
|
func (p *pgTester) createTestDB() error {
|
|
return p.runCmd("", "createdb", p.testDBName)
|
|
}
|
|
|
|
func (p *pgTester) dropTestDB() error {
|
|
return p.runCmd("", "dropdb", "--if-exists", p.testDBName)
|
|
}
|
|
|
|
// teardown executes cleanup tasks when the tests finish running
|
|
func (p *pgTester) teardown() error {
|
|
var err error
|
|
if err = p.dbConn.Close(); err != nil {
|
|
return err
|
|
}
|
|
p.dbConn = nil
|
|
|
|
if !p.skipSQLCmd {
|
|
if err = p.dropTestDB(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return os.Remove(p.pgPassFile)
|
|
}
|
|
|
|
func (p *pgTester) conn() (*sql.DB, error) {
|
|
if p.dbConn != nil {
|
|
return p.dbConn, nil
|
|
}
|
|
|
|
var err error
|
|
p.dbConn, err = sql.Open("postgres", driver.PSQLBuildQueryString(p.user, p.pass, p.testDBName, p.host, p.port, p.sslmode))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
path := filepath.Join("..", "..", "migrations")
|
|
err = goose.Run("up", p.dbConn, "postgres", path, "")
|
|
if err != nil {
|
|
if err == goose.ErrNoNextVersion {
|
|
return p.dbConn, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return p.dbConn, nil
|
|
}
|