jet/tests/init/init.go

152 lines
3 KiB
Go
Raw Normal View History

2019-06-11 12:47:35 +02:00
package main
import (
"database/sql"
2019-07-29 18:08:53 +02:00
"flag"
2019-06-21 13:56:57 +02:00
"fmt"
"github.com/go-jet/jet/v2/generator/sqlite"
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
2021-08-30 15:09:09 +03:00
"io/ioutil"
"os"
"os/exec"
"strings"
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/generator/mysql"
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/internal/utils/throw"
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/tests/dbconfig"
2019-07-30 11:18:12 +02:00
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
2019-06-11 12:47:35 +02:00
)
2019-07-29 18:08:53 +02:00
var testSuite string
func init() {
flag.StringVar(&testSuite, "testsuite", "all", "Test suite name (postgres or mysql)")
flag.Parse()
}
2019-06-11 12:47:35 +02:00
func main() {
2019-08-15 12:28:59 +02:00
testSuite = strings.ToLower(testSuite)
2019-07-29 18:08:53 +02:00
if testSuite == "postgres" {
initPostgresDB()
return
}
2019-08-15 12:28:59 +02:00
if testSuite == "mysql" || testSuite == "mariadb" {
2019-07-29 18:08:53 +02:00
initMySQLDB()
return
}
if testSuite == "sqlite" {
initSQLiteDB()
return
}
2019-07-29 18:08:53 +02:00
initMySQLDB()
initPostgresDB()
initSQLiteDB()
}
func initSQLiteDB() {
err := sqlite.GenerateDSN(dbconfig.SakilaDBPath, repo.GetTestsFilePath("./.gentestdata/sqlite/sakila"))
throw.OnError(err)
err = sqlite.GenerateDSN(dbconfig.ChinookDBPath, repo.GetTestsFilePath("./.gentestdata/sqlite/chinook"))
throw.OnError(err)
err = sqlite.GenerateDSN(dbconfig.TestSampleDBPath, repo.GetTestsFilePath("./.gentestdata/sqlite/test_sample"))
throw.OnError(err)
2019-07-29 18:08:53 +02:00
}
func initMySQLDB() {
mySQLDBs := []string{
2019-08-01 16:56:54 +02:00
"dvds",
"dvds2",
2019-07-29 18:08:53 +02:00
"test_sample",
}
for _, dbName := range mySQLDBs {
cmdLine := fmt.Sprintf("mysql -h %s -P%d -u %s -p%s %s < %s",
dbconfig.MySqLHost,
dbconfig.MySQLPort,
dbconfig.MySQLUser,
dbconfig.MySQLPassword,
dbName,
"./testdata/init/mysql/"+dbName+".sql")
2019-08-08 12:02:32 +02:00
fmt.Println(cmdLine)
2019-07-29 18:08:53 +02:00
cmd := exec.Command("sh", "-c", cmdLine)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
throw.OnError(err)
2019-07-29 18:08:53 +02:00
err = mysql.Generate("./.gentestdata/mysql", mysql.DBConnection{
Host: dbconfig.MySqLHost,
Port: dbconfig.MySQLPort,
User: dbconfig.MySQLUser,
Password: dbconfig.MySQLPassword,
2019-08-19 10:40:34 +02:00
DBName: dbName,
2019-07-29 18:08:53 +02:00
})
throw.OnError(err)
2019-07-29 18:08:53 +02:00
}
}
func initPostgresDB() {
db, err := sql.Open("postgres", dbconfig.PostgresConnectString)
2019-06-11 12:47:35 +02:00
if err != nil {
panic("Failed to connect to test db")
}
2019-06-21 13:56:57 +02:00
defer func() {
err := db.Close()
printOnError(err)
}()
schemaNames := []string{
"dvds",
"test_sample",
"chinook",
"chinook2",
2019-07-17 11:03:16 +02:00
"northwind",
2019-06-21 13:56:57 +02:00
}
2019-06-11 12:47:35 +02:00
2019-06-21 13:56:57 +02:00
for _, schemaName := range schemaNames {
2019-06-11 12:47:35 +02:00
execFile(db, "./testdata/init/postgres/"+schemaName+".sql")
2019-07-17 11:03:16 +02:00
2019-07-08 10:48:03 +02:00
err = postgres.Generate("./.gentestdata", postgres.DBConnection{
Host: dbconfig.PgHost,
2021-08-30 12:19:21 +02:00
Port: dbconfig.PgPort,
User: dbconfig.PgUser,
Password: dbconfig.PgPassword,
DBName: dbconfig.PgDBName,
2019-06-21 13:56:57 +02:00
SchemaName: schemaName,
SslMode: "disable",
2019-06-21 13:56:57 +02:00
})
throw.OnError(err)
2019-06-21 13:56:57 +02:00
}
2019-06-11 12:47:35 +02:00
}
2019-07-29 18:08:53 +02:00
func execFile(db *sql.DB, sqlFilePath string) {
testSampleSql, err := ioutil.ReadFile(sqlFilePath)
throw.OnError(err)
2019-07-29 18:08:53 +02:00
_, err = db.Exec(string(testSampleSql))
throw.OnError(err)
2019-06-11 12:47:35 +02:00
}
2019-06-21 13:56:57 +02:00
func printOnError(err error) {
if err != nil {
fmt.Println(err.Error())
}
}