forked from go-jet/jet
/
init.go
183 lines (150 loc) · 3.97 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"github.com/go-jet/jet/v2/generator/mysql"
"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"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/go-jet/jet/v2/internal/utils/throw"
"github.com/go-jet/jet/v2/tests/dbconfig"
_ "github.com/go-sql-driver/mysql"
_ "github.com/jackc/pgx/v4/stdlib"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var testSuite string
func init() {
flag.StringVar(&testSuite, "testsuite", "all", "Test suite name (postgres, mysql, mariadb, cockroach, sqlite or all)")
flag.Parse()
}
// Database names
const (
Postgres = "postgres"
MySql = "mysql"
MariaDB = "mariadb"
Sqlite = "sqlite"
Cockroach = "cockroach"
)
func main() {
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()
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)
}
func initMySQLDB(isMariaDB bool) {
mySQLDBs := []string{
"dvds",
"dvds2",
"test_sample",
}
for _, dbName := range mySQLDBs {
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")
fmt.Println(cmdLine)
cmd := exec.Command("sh", "-c", cmdLine)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
throw.OnError(err)
err = mysql.Generate("./.gentestdata/mysql", mysql.DBConnection{
Host: host,
Port: port,
User: user,
Password: pass,
DBName: dbName,
})
throw.OnError(err)
}
}
func initPostgresDB(dbType string, connectionString string) {
db, err := sql.Open("postgres", connectionString)
if err != nil {
panic("Failed to connect to test db: " + err.Error())
}
defer func() {
err := db.Close()
printOnError(err)
}()
schemaNames := []string{
"northwind",
"dvds",
"test_sample",
"chinook",
"chinook2",
}
for _, schemaName := range schemaNames {
fmt.Println("\nInitializing", schemaName, "schema...")
execFile(db, fmt.Sprintf("./testdata/init/%s/%s.sql", dbType, schemaName))
err = postgres.GenerateDSN(connectionString, schemaName, "./.gentestdata")
throw.OnError(err)
}
}
func execFile(db *sql.DB, sqlFilePath string) {
testSampleSql, err := ioutil.ReadFile(sqlFilePath)
throw.OnError(err)
err = execInTx(db, func(tx *sql.Tx) error {
_, err := tx.Exec(string(testSampleSql))
return err
})
throw.OnError(err)
}
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()
}
func printOnError(err error) {
if err != nil {
fmt.Println(err.Error())
}
}