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"
|
2021-10-21 13:39:24 +02:00
|
|
|
"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"
|
2021-07-27 17:39:21 +02:00
|
|
|
"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"
|
2019-07-18 18:42:03 +02:00
|
|
|
_ "github.com/lib/pq"
|
2021-10-21 13:39:24 +02:00
|
|
|
|
|
|
|
|
_ "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-06-21 16:16:57 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 13:39:24 +02:00
|
|
|
if testSuite == "sqlite" {
|
|
|
|
|
initSQLiteDB()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 18:08:53 +02:00
|
|
|
initMySQLDB()
|
|
|
|
|
initPostgresDB()
|
2021-10-21 13:39:24 +02:00
|
|
|
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",
|
2021-03-21 17:17:44 +01:00
|
|
|
"dvds2",
|
2019-07-29 18:08:53 +02:00
|
|
|
"test_sample",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, dbName := range mySQLDBs {
|
2019-08-08 12:02:32 +02:00
|
|
|
cmdLine := fmt.Sprintf("mysql -h 127.0.0.1 -u %s -p%s %s < %s",
|
2019-08-16 12:43:41 +02:00
|
|
|
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()
|
2021-07-27 17:39:21 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
|
|
2021-07-27 17:39:21 +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",
|
2021-03-21 17:17:44 +01:00
|
|
|
"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
|
|
|
|
2019-08-16 12:43:41 +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{
|
2021-07-27 17:39:21 +02:00
|
|
|
Host: dbconfig.PgHost,
|
2021-08-30 12:19:21 +02:00
|
|
|
Port: dbconfig.PgPort,
|
2021-07-27 17:39:21 +02:00
|
|
|
User: dbconfig.PgUser,
|
|
|
|
|
Password: dbconfig.PgPassword,
|
|
|
|
|
DBName: dbconfig.PgDBName,
|
2019-06-21 13:56:57 +02:00
|
|
|
SchemaName: schemaName,
|
2019-06-21 16:16:57 +02:00
|
|
|
SslMode: "disable",
|
2019-06-21 13:56:57 +02:00
|
|
|
})
|
2021-07-27 17:39:21 +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)
|
2021-07-27 17:39:21 +02:00
|
|
|
throw.OnError(err)
|
2019-07-29 18:08:53 +02:00
|
|
|
|
|
|
|
|
_, err = db.Exec(string(testSampleSql))
|
2021-07-27 17:39:21 +02:00
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|