-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql_main_test.txt
188 lines (154 loc) · 4.32 KB
/
mssql_main_test.txt
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
184
185
186
187
188
package models
// Singleton version of main test, copied from repo for patching and logging
import (
"bytes"
"database/sql"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
_ "github.com/denisenkom/go-mssqldb"
"github.com/kat-co/vala"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/drivers/sqlboiler-mssql/driver"
"github.com/volatiletech/sqlboiler/randomize"
)
var rgxMSSQLkey = regexp.MustCompile(`(?m)^ALTER TABLE .*ADD\s+CONSTRAINT .* FOREIGN KEY.*?.*\n?REFERENCES.*`)
type mssqlTester struct {
dbConn *sql.DB
dbName string
host string
user string
pass string
sslmode string
port int
testDBName string
skipSQLCmd bool
}
func init() {
dbMain = &mssqlTester{}
}
func (m *mssqlTester) setup() error {
var err error
viper.SetDefault("mssql.schema", "dbo")
viper.SetDefault("mssql.sslmode", "true")
viper.SetDefault("mssql.port", 1433)
m.dbName = viper.GetString("mssql.dbname")
m.host = viper.GetString("mssql.host")
m.user = viper.GetString("mssql.user")
m.pass = viper.GetString("mssql.pass")
m.port = viper.GetInt("mssql.port")
m.sslmode = viper.GetString("mssql.sslmode")
m.testDBName = viper.GetString("mssql.testdbname")
m.skipSQLCmd = viper.GetBool("mssql.skipsqlcmd")
err = vala.BeginValidation().Validate(
vala.StringNotEmpty(viper.GetString("mssql.user"), "mssql.user"),
vala.StringNotEmpty(viper.GetString("mssql.host"), "mssql.host"),
vala.Not(vala.Equals(viper.GetInt("mssql.port"), 0, "mssql.port")),
vala.StringNotEmpty(viper.GetString("mssql.dbname"), "mssql.dbname"),
vala.StringNotEmpty(viper.GetString("mssql.sslmode"), "mssql.sslmode"),
).Check()
if err != nil {
return err
}
// Create a randomized db name.
if len(m.testDBName) == 0 {
m.testDBName = randomize.StableDBName(m.dbName)
}
if !m.skipSQLCmd {
if err = m.dropTestDB(); err != nil {
return err
}
if err = m.createTestDB(); err != nil {
return err
}
createCmd := exec.Command("sqlcmd", "-S", m.host, "-U", m.user, "-P", m.pass, "-d", m.testDBName)
f, err := os.Open("tables_schema.sql")
if err != nil {
return errors.Wrap(err, "failed to open tables_schema.sql file")
}
defer func() { _ = f.Close() }()
stderr := &bytes.Buffer{}
createCmd.Stdin = newFKeyDestroyer(rgxMSSQLkey, f)
createCmd.Stderr = stderr
if err = createCmd.Start(); err != nil {
return errors.Wrap(err, "failed to start sqlcmd command")
}
if err = createCmd.Wait(); err != nil {
fmt.Println(err)
fmt.Println(stderr.String())
return errors.Wrap(err, "failed to wait for sqlcmd command")
}
}
return nil
}
func (m *mssqlTester) sslMode(mode string) string {
switch mode {
case "true":
return "true"
case "false":
return "false"
default:
return "disable"
}
}
func (m *mssqlTester) createTestDB() error {
sql := fmt.Sprintf(`
CREATE DATABASE %s;
GO
ALTER DATABASE %[1]s
SET READ_COMMITTED_SNAPSHOT ON;
GO`, m.testDBName)
return m.runCmd(sql, "sqlcmd", "-S", m.host, "-U", m.user, "-P", m.pass)
}
func (m *mssqlTester) dropTestDB() error {
// Since MS SQL 2016 it can be done with
// DROP DATABASE [ IF EXISTS ] { database_name | database_snapshot_name } [ ,...n ] [;]
sql := fmt.Sprintf(`
IF EXISTS(SELECT name FROM sys.databases
WHERE name = '%s')
DROP DATABASE %s
GO`, m.testDBName, m.testDBName)
return m.runCmd(sql, "sqlcmd", "-S", m.host, "-U", m.user, "-P", m.pass)
}
func (m *mssqlTester) teardown() error {
if m.dbConn != nil {
m.dbConn.Close()
}
if !m.skipSQLCmd {
if err := m.dropTestDB(); err != nil {
return err
}
}
return nil
}
func (m *mssqlTester) runCmd(stdin, command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdin = strings.NewReader(stdin)
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
fmt.Println("failed running:", command, args)
fmt.Println(stdout.String())
fmt.Println(stderr.String())
return err
}
return nil
}
func (m *mssqlTester) conn() (*sql.DB, error) {
if m.dbConn != nil {
return m.dbConn, nil
}
var err error
m.dbConn, err = sql.Open("mssql", driver.MSSQLBuildQueryString(m.user, m.pass, m.testDBName, m.host, m.port, m.sslmode))
boil.DebugMode = true
if err != nil {
return nil, err
}
return m.dbConn, nil
}