2016-09-11 18:17:08 +02:00
|
|
|
type pgTester struct {
|
|
|
|
dbConn *sql.DB
|
2016-07-15 07:44:52 +02:00
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
dbName string
|
|
|
|
host string
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
sslmode string
|
|
|
|
port int
|
2016-06-27 02:44:01 +02:00
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
testDBName string
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
func init() {
|
|
|
|
dbMain = &pgTester{}
|
|
|
|
}
|
2016-09-11 18:17:08 +02:00
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
// disableTriggers is used to disable foreign key constraints for every table.
|
|
|
|
// If this is not used we cannot test inserts due to foreign key constraint errors.
|
2016-09-11 21:07:39 +02:00
|
|
|
func (p *pgTester) disableTriggers() error {
|
2016-06-02 23:07:51 +02:00
|
|
|
var stmts []string
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
{{range .Tables -}}
|
2016-06-14 14:58:46 +02:00
|
|
|
stmts = append(stmts, `ALTER TABLE {{.Name}} DISABLE TRIGGER ALL;`)
|
2016-09-11 21:07:39 +02:00
|
|
|
{{end -}}
|
2016-06-02 23:07:51 +02:00
|
|
|
|
|
|
|
if len(stmts) == 0 {
|
|
|
|
return nil
|
2016-04-09 01:45:44 +02:00
|
|
|
}
|
2016-03-26 07:54:55 +01:00
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
var err error
|
|
|
|
for _, s := range stmts {
|
2016-09-11 18:17:08 +02:00
|
|
|
_, err = p.dbConn.Exec(s)
|
2016-06-02 23:07:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// teardown executes cleanup tasks when the tests finish running
|
2016-09-11 21:07:39 +02:00
|
|
|
func (p *pgTester) teardown() error {
|
|
|
|
return p.dropTestDB()
|
2016-06-02 23:07:51 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 08:49:47 +02:00
|
|
|
func (p *pgTester) conn() (*sql.DB, error) {
|
|
|
|
return p.dbConn, nil
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
// dropTestDB switches its connection to the template1 database temporarily
|
|
|
|
// so that it can drop the test database without causing "in use" conflicts.
|
|
|
|
// The template1 database should be present on all default postgres installations.
|
2016-09-11 21:07:39 +02:00
|
|
|
func (p *pgTester) dropTestDB() error {
|
2016-06-02 23:07:51 +02:00
|
|
|
var err error
|
2016-09-11 18:17:08 +02:00
|
|
|
if p.dbConn != nil {
|
|
|
|
if err = p.dbConn.Close(); err != nil {
|
2016-06-02 23:07:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
p.dbConn, err = DBConnect(p.user, p.pass, "template1", p.host, p.port, p.sslmode)
|
2016-03-26 07:54:55 +01:00
|
|
|
if err != nil {
|
2016-04-09 01:45:44 +02:00
|
|
|
return err
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
_, err = p.dbConn.Exec(fmt.Sprintf(`DROP DATABASE IF EXISTS %s;`, p.testDBName))
|
2016-04-09 01:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
return p.dbConn.Close()
|
2016-04-09 01:45:44 +02:00
|
|
|
}
|
2016-03-26 07:54:55 +01:00
|
|
|
|
2016-04-09 01:45:44 +02:00
|
|
|
// DBConnect connects to a database and returns the handle.
|
2016-07-12 00:17:49 +02:00
|
|
|
func DBConnect(user, pass, dbname, host string, port int, sslmode string) (*sql.DB, error) {
|
2016-09-09 14:31:51 +02:00
|
|
|
connStr := drivers.PostgresBuildQueryString(user, pass, dbname, host, port, sslmode)
|
2016-04-09 01:45:44 +02:00
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
return sql.Open("postgres", connStr)
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 01:45:44 +02:00
|
|
|
// 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.
|
2016-09-11 21:07:39 +02:00
|
|
|
func (p *pgTester) setup() error {
|
2016-06-27 06:53:23 +02:00
|
|
|
var err error
|
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
p.dbName = viper.GetString("postgres.dbname")
|
|
|
|
p.host = viper.GetString("postgres.host")
|
|
|
|
p.user = viper.GetString("postgres.user")
|
|
|
|
p.pass = viper.GetString("postgres.pass")
|
|
|
|
p.port = viper.GetInt("postgres.port")
|
2016-09-11 21:07:39 +02:00
|
|
|
p.sslmode = viper.GetString("postgres.sslmode")
|
2016-09-11 18:17:08 +02:00
|
|
|
// Create a randomized db name.
|
2016-09-13 07:43:29 +02:00
|
|
|
p.testDBName = randomize.StableDBName(p.dbName)
|
2016-06-02 23:07:51 +02:00
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
err = p.dropTestDB()
|
2016-06-02 23:07:51 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%#v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
2016-04-09 01:45:44 +02:00
|
|
|
|
2016-03-26 07:54:55 +01:00
|
|
|
fhSchema, err := ioutil.TempFile(os.TempDir(), "sqlboilerschema")
|
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "Unable to create sqlboiler schema tmp file")
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
defer os.Remove(fhSchema.Name())
|
|
|
|
|
|
|
|
passDir, err := ioutil.TempDir(os.TempDir(), "sqlboiler")
|
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "Unable to create sqlboiler tmp dir for postgres pw file")
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(passDir)
|
|
|
|
|
|
|
|
// Write the postgres user password to a tmp file for pg_dump
|
2016-09-11 21:07:39 +02:00
|
|
|
pwBytes := []byte(fmt.Sprintf("%s:%d:%s:%s", p.host, p.port, p.dbName, p.user))
|
2016-09-11 18:17:08 +02:00
|
|
|
|
|
|
|
if len(p.pass) > 0 {
|
|
|
|
pwBytes = []byte(fmt.Sprintf("%s:%s", pwBytes, p.pass))
|
2016-08-01 06:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
passFilePath := filepath.Join(passDir, "pwfile")
|
2016-03-26 07:54:55 +01:00
|
|
|
|
|
|
|
err = ioutil.WriteFile(passFilePath, pwBytes, 0600)
|
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "Unable to create pwfile in passDir")
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 01:45:44 +02:00
|
|
|
// The params for the pg_dump command to dump the database schema
|
2016-03-26 07:54:55 +01:00
|
|
|
params := []string{
|
2016-09-11 18:17:08 +02:00
|
|
|
fmt.Sprintf(`--host=%s`, p.host),
|
|
|
|
fmt.Sprintf(`--port=%d`, p.port),
|
|
|
|
fmt.Sprintf(`--username=%s`, p.user),
|
2016-03-26 07:54:55 +01:00
|
|
|
"--schema-only",
|
2016-09-11 18:17:08 +02:00
|
|
|
p.dbName,
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 01:45:44 +02:00
|
|
|
// Dump the database schema into the sqlboilerschema tmp file
|
2016-03-26 07:54:55 +01:00
|
|
|
errBuf := bytes.Buffer{}
|
|
|
|
cmd := exec.Command("pg_dump", params...)
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
cmd.Stdout = fhSchema
|
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf(`PGPASSFILE=%s`, passFilePath))
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Printf("pg_dump exec failed: %s\n\n%s\n", err, errBuf.String())
|
2016-06-27 02:44:01 +02:00
|
|
|
return err
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
p.dbConn, err = DBConnect(p.user, p.pass, p.dbName, p.host, p.port, p.sslmode)
|
2016-04-09 01:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the randomly generated database
|
2016-09-11 18:17:08 +02:00
|
|
|
_, err = p.dbConn.Exec(fmt.Sprintf(`CREATE DATABASE %s WITH ENCODING 'UTF8'`, p.testDBName))
|
2016-04-09 01:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
// Close the old connection so we can reconnect to the test database
|
2016-09-11 18:17:08 +02:00
|
|
|
if err = p.dbConn.Close(); err != nil {
|
2016-04-09 01:45:44 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
// Connect to the generated test db
|
2016-09-11 18:17:08 +02:00
|
|
|
p.dbConn, err = DBConnect(p.user, p.pass, p.testDBName, p.host, p.port, p.sslmode)
|
2016-04-09 01:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
// Write the test config credentials to a tmp file for pg_dump
|
2016-09-11 18:17:08 +02:00
|
|
|
testPwBytes := []byte(fmt.Sprintf("%s:%d:%s:%s", p.host, p.port, p.testDBName, p.user))
|
|
|
|
|
|
|
|
if len(p.pass) > 0 {
|
|
|
|
testPwBytes = []byte(fmt.Sprintf("%s:%s", testPwBytes, p.pass))
|
2016-08-01 06:24:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-09 01:45:44 +02:00
|
|
|
testPassFilePath := passDir + "/testpwfile"
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(testPassFilePath, testPwBytes, 0600)
|
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrapf(err, "Unable to create testpwfile in passDir")
|
2016-04-09 01:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The params for the psql schema import command
|
|
|
|
params = []string{
|
2016-09-11 18:17:08 +02:00
|
|
|
fmt.Sprintf(`--dbname=%s`, p.testDBName),
|
|
|
|
fmt.Sprintf(`--host=%s`, p.host),
|
|
|
|
fmt.Sprintf(`--port=%d`, p.port),
|
|
|
|
fmt.Sprintf(`--username=%s`, p.user),
|
2016-04-09 01:45:44 +02:00
|
|
|
fmt.Sprintf(`--file=%s`, fhSchema.Name()),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import the database schema into the generated database.
|
|
|
|
// It is now ready to be used by the generated ORM package for testing.
|
|
|
|
outBuf := bytes.Buffer{}
|
|
|
|
cmd = exec.Command("psql", params...)
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
cmd.Stdout = &outBuf
|
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf(`PGPASSFILE=%s`, testPassFilePath))
|
|
|
|
|
|
|
|
if err = cmd.Run(); err != nil {
|
|
|
|
fmt.Printf("psql schema import exec failed: %s\n\n%s\n", err, errBuf.String())
|
|
|
|
}
|
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
return p.disableTriggers()
|
2016-03-26 07:54:55 +01:00
|
|
|
}
|