jet/tests/init/init.go

183 lines
3.9 KiB
Go
Raw Normal View History

2019-06-11 12:47:35 +02:00
package main
import (
2022-05-05 13:01:42 +02:00
"context"
2019-06-11 12:47:35 +02:00
"database/sql"
2019-07-29 18:08:53 +02:00
"flag"
2019-06-21 13:56:57 +02:00
"fmt"
2021-12-18 13:55:40 +01:00
"github.com/go-jet/jet/v2/generator/mysql"
2022-05-05 13:01:42 +02:00
"github.com/go-jet/jet/v2/generator/postgres"
"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"
"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"
2022-05-05 13:01:42 +02:00
_ "github.com/jackc/pgx/v4/stdlib"
2022-05-05 13:01:42 +02:00
_ "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() {
2022-05-05 13:01:42 +02:00
flag.StringVar(&testSuite, "testsuite", "all", "Test suite name (postgres, mysql, mariadb, cockroach, sqlite or all)")
2019-07-29 18:08:53 +02:00
flag.Parse()
}
2022-05-05 13:01:42 +02:00
const (
Postgres = "postgres"
MySql = "mysql"
MariaDB = "mariadb"
Sqlite = "sqlite"
Cockroach = "cockroach"
)
2019-07-29 18:08:53 +02:00
2022-05-05 13:01:42 +02:00
func main() {
2019-07-29 18:08:53 +02:00
2022-05-05 13:01:42 +02:00
switch strings.ToLower(testSuite) {
case Postgres:
initPostgresDB(Postgres, dbconfig.PostgresConnectString)
case Cockroach:
initPostgresDB(Cockroach, dbconfig.CockroachConnectString)
case MySql:
initMySQLDB(false)
case MariaDB:
initMySQLDB(true)
case Sqlite:
initSQLiteDB()
case "all":
initPostgresDB(Cockroach, dbconfig.CockroachConnectString)
initPostgresDB(Postgres, dbconfig.PostgresConnectString)
initMySQLDB(false)
initMySQLDB(true)
initSQLiteDB()
2022-05-05 13:01:42 +02:00
default:
panic("invalid testsuite flag. Test suite name (postgres, mysql, mariadb, cockroach, sqlite or all)")
}
}
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
}
2021-12-18 13:55:40 +01:00
func initMySQLDB(isMariaDB bool) {
2019-07-29 18:08:53 +02:00
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 {
2021-12-18 13:55:40 +01:00
host := dbconfig.MySqLHost
port := dbconfig.MySQLPort
user := dbconfig.MySQLUser
pass := dbconfig.MySQLPassword
if isMariaDB {
host = dbconfig.MariaDBHost
port = dbconfig.MariaDBPort
user = dbconfig.MariaDBUser
pass = dbconfig.MariaDBPassword
}
cmdLine := fmt.Sprintf("mysql -h %s -P %d -u %s -p%s %s < %s", host, port, user, pass, 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{
2021-12-18 13:55:40 +01:00
Host: host,
Port: port,
User: user,
Password: pass,
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
}
}
2022-05-05 13:01:42 +02:00
func initPostgresDB(dbType string, connectionString string) {
db, err := sql.Open("postgres", connectionString)
2019-06-11 12:47:35 +02:00
if err != nil {
2021-12-18 13:55:40 +01:00
panic("Failed to connect to test db: " + err.Error())
2019-06-11 12:47:35 +02:00
}
2019-06-21 13:56:57 +02:00
defer func() {
err := db.Close()
printOnError(err)
}()
schemaNames := []string{
2022-05-05 13:01:42 +02:00
"northwind",
2019-06-21 13:56:57 +02:00
"dvds",
"test_sample",
"chinook",
"chinook2",
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 {
2022-05-05 13:01:42 +02:00
fmt.Println("\nInitializing", schemaName, "schema...")
2019-06-11 12:47:35 +02:00
2022-05-05 13:01:42 +02:00
execFile(db, fmt.Sprintf("./testdata/init/%s/%s.sql", dbType, schemaName))
2019-07-17 11:03:16 +02:00
2022-05-05 13:01:42 +02:00
err = postgres.GenerateDSN(connectionString, schemaName, "./.gentestdata")
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
2022-05-05 13:01:42 +02:00
err = execInTx(db, func(tx *sql.Tx) error {
_, err := tx.Exec(string(testSampleSql))
return err
})
throw.OnError(err)
2019-06-11 12:47:35 +02:00
}
2019-06-21 13:56:57 +02:00
2022-05-05 13:01:42 +02:00
func execInTx(db *sql.DB, f func(tx *sql.Tx) error) error {
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{
Isolation: sql.LevelReadUncommitted, // to speed up initialization of test database
})
if err != nil {
return err
}
err = f(tx)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
2019-06-21 13:56:57 +02:00
func printOnError(err error) {
if err != nil {
fmt.Println(err.Error())
}
}