2016-09-11 18:17:08 +02:00
|
|
|
var flagDebugMode = flag.Bool("test.sqldebug", false, "Turns on debug mode for SQL statements")
|
|
|
|
|
|
|
|
var (
|
|
|
|
dbMain tester
|
|
|
|
)
|
|
|
|
|
|
|
|
type tester interface {
|
|
|
|
setup() error
|
2016-09-12 08:49:47 +02:00
|
|
|
conn() (*sql.DB, error)
|
2016-09-11 18:17:08 +02:00
|
|
|
teardown() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if dbMain == nil {
|
|
|
|
fmt.Println("no dbMain tester interface was ready")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2016-09-11 21:07:39 +02:00
|
|
|
var err error
|
2016-09-11 18:17:08 +02:00
|
|
|
|
|
|
|
// Load configuration
|
|
|
|
err = initViper()
|
|
|
|
if err != nil {
|
2016-09-11 21:07:39 +02:00
|
|
|
fmt.Println("unable to load config file")
|
|
|
|
os.Exit(-2)
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setConfigDefaults()
|
2016-09-11 21:07:39 +02:00
|
|
|
if err := validateConfig("{{.DriverName}}"); err != nil {
|
2016-09-11 18:17:08 +02:00
|
|
|
fmt.Println("failed to validate config", err)
|
2016-09-11 21:07:39 +02:00
|
|
|
os.Exit(-3)
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set DebugMode so we can see generated sql statements
|
|
|
|
flag.Parse()
|
|
|
|
boil.DebugMode = *flagDebugMode
|
|
|
|
|
|
|
|
if err = dbMain.setup(); err != nil {
|
|
|
|
fmt.Println("Unable to execute setup:", err)
|
2016-09-11 21:07:39 +02:00
|
|
|
os.Exit(-4)
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 08:49:47 +02:00
|
|
|
conn, err := dbMain.conn()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("failed to get connection:", err)
|
|
|
|
}
|
|
|
|
|
2016-09-11 18:17:08 +02:00
|
|
|
var code int
|
2016-09-12 08:49:47 +02:00
|
|
|
boil.SetDB(conn)
|
2016-09-11 18:17:08 +02:00
|
|
|
code = m.Run()
|
|
|
|
|
|
|
|
if err = dbMain.teardown(); err != nil {
|
|
|
|
fmt.Println("Unable to execute teardown:", err)
|
2016-09-11 21:07:39 +02:00
|
|
|
os.Exit(-5)
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initViper() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
viper.SetConfigName("sqlboiler")
|
|
|
|
|
|
|
|
configHome := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
homePath := os.Getenv("HOME")
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
wd = "../"
|
|
|
|
} else {
|
|
|
|
wd = wd + "/.."
|
|
|
|
}
|
|
|
|
|
|
|
|
configPaths := []string{wd}
|
|
|
|
if len(configHome) > 0 {
|
|
|
|
configPaths = append(configPaths, filepath.Join(configHome, "sqlboiler"))
|
|
|
|
} else {
|
|
|
|
configPaths = append(configPaths, filepath.Join(homePath, ".config/sqlboiler"))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range configPaths {
|
|
|
|
viper.AddConfigPath(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore errors here, fall back to defaults and validation to provide errs
|
|
|
|
_ = viper.ReadInConfig()
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
// setConfigDefaults is only necessary because of bugs in viper, noted in main
|
|
|
|
func setConfigDefaults() {
|
2016-09-11 18:17:08 +02:00
|
|
|
if viper.GetString("postgres.sslmode") == "" {
|
|
|
|
viper.Set("postgres.sslmode", "require")
|
|
|
|
}
|
|
|
|
if viper.GetInt("postgres.port") == 0 {
|
|
|
|
viper.Set("postgres.port", 5432)
|
|
|
|
}
|
|
|
|
if viper.GetString("mysql.sslmode") == "" {
|
|
|
|
viper.Set("mysql.sslmode", "true")
|
|
|
|
}
|
|
|
|
if viper.GetInt("mysql.port") == 0 {
|
|
|
|
viper.Set("mysql.port", 3306)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateConfig(driverName string) error {
|
2016-09-11 21:07:39 +02:00
|
|
|
if driverName == "postgres" {
|
|
|
|
return vala.BeginValidation().Validate(
|
2016-09-11 18:17:08 +02:00
|
|
|
vala.StringNotEmpty(viper.GetString("postgres.user"), "postgres.user"),
|
|
|
|
vala.StringNotEmpty(viper.GetString("postgres.host"), "postgres.host"),
|
|
|
|
vala.Not(vala.Equals(viper.GetInt("postgres.port"), 0, "postgres.port")),
|
|
|
|
vala.StringNotEmpty(viper.GetString("postgres.dbname"), "postgres.dbname"),
|
|
|
|
vala.StringNotEmpty(viper.GetString("postgres.sslmode"), "postgres.sslmode"),
|
|
|
|
).Check()
|
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
if driverName == "mysql" {
|
|
|
|
return vala.BeginValidation().Validate(
|
2016-09-11 18:17:08 +02:00
|
|
|
vala.StringNotEmpty(viper.GetString("mysql.user"), "mysql.user"),
|
|
|
|
vala.StringNotEmpty(viper.GetString("mysql.host"), "mysql.host"),
|
|
|
|
vala.Not(vala.Equals(viper.GetInt("mysql.port"), 0, "mysql.port")),
|
|
|
|
vala.StringNotEmpty(viper.GetString("mysql.dbname"), "mysql.dbname"),
|
|
|
|
vala.StringNotEmpty(viper.GetString("mysql.sslmode"), "mysql.sslmode"),
|
|
|
|
).Check()
|
|
|
|
}
|
2016-09-11 21:07:39 +02:00
|
|
|
|
|
|
|
return errors.New("not a valid driver name")
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|