@@ -45,21 +45,30 @@ func checkForeignKeys(t *testing.T, q *reform.Querier) {
4545 require .True (t , enabled )
4646}
4747
48- // setIdentityInsert allows or disallows insertions of rows with set primary keys for MS SQL.
49- func setIdentityInsert (t * testing.T , q * reform.Querier , table string , allow bool ) {
48+ // withIdentityInsert executes an action with MS SQL IDENTITY_INSERT enabled for a table
49+ func withIdentityInsert (t * testing.T , q * reform.Querier , table string , action func () ) {
5050 if q .Dialect != mssql .Dialect && q .Dialect != sqlserver .Dialect {
51+ action ()
5152 return
5253 }
5354
54- allowString := "OFF"
55- if allow {
56- allowString = "ON"
57- }
58- sql := fmt .Sprintf ("SET IDENTITY_INSERT %s %s" , q .QuoteIdentifier (table ), allowString )
59- _ , err := q .Exec (sql )
55+ query := fmt .Sprintf ("SET IDENTITY_INSERT %s %%s" , q .QuoteIdentifier (table ))
56+
57+ _ , err := q .Exec (fmt .Sprintf (query , "ON" ))
58+ require .NoError (t , err )
59+
60+ action ()
61+
62+ _ , err = q .Exec (fmt .Sprintf (query , "OFF" ))
6063 require .NoError (t , err )
6164}
6265
66+ func insertPersonWithID (t * testing.T , q * reform.Querier , str reform.Struct ) error {
67+ var err error
68+ withIdentityInsert (t , q , "people" , func () { err = q .Insert (str ) })
69+ return err
70+ }
71+
6372type ReformSuite struct {
6473 suite.Suite
6574 tx * reform.TX
@@ -80,8 +89,6 @@ func (s *ReformSuite) SetupTest() {
8089 s .Require ().NoError (err )
8190
8291 s .q = s .tx .WithTag ("test" )
83-
84- setIdentityInsert (s .T (), s .q , "people" , false )
8592}
8693
8794func (s * ReformSuite ) TearDownTest () {
@@ -161,8 +168,6 @@ func (s *ReformSuite) TestPlaceholders() {
161168}
162169
163170func (s * ReformSuite ) TestTimezones () {
164- setIdentityInsert (s .T (), s .q , "people" , true )
165-
166171 t1 := time .Now ()
167172 t2 := t1 .UTC ()
168173 vlat , err := time .LoadLocation ("Asia/Vladivostok" )
@@ -176,8 +181,11 @@ func (s *ReformSuite) TestTimezones() {
176181 q := fmt .Sprintf (`INSERT INTO people (id, name, created_at) VALUES ` +
177182 `(11, '11', %s), (12, '12', %s), (13, '13', %s), (14, '14', %s)` ,
178183 s .q .Placeholder (1 ), s .q .Placeholder (2 ), s .q .Placeholder (3 ), s .q .Placeholder (4 ))
179- _ , err := s .q .Exec (q , t1 , t2 , tVLAT , tHST )
180- s .NoError (err )
184+
185+ withIdentityInsert (s .T (), s .q , "people" , func () {
186+ _ , err := s .q .Exec (q , t1 , t2 , tVLAT , tHST )
187+ s .NoError (err )
188+ })
181189
182190 q = `SELECT created_at, created_at FROM people WHERE id IN (11, 12, 13, 14) ORDER BY id`
183191 rows , err := s .q .Query (q )
@@ -200,8 +208,11 @@ func (s *ReformSuite) TestTimezones() {
200208 q := fmt .Sprintf (`INSERT INTO projects (id, name, start) VALUES ` +
201209 `('11', '11', %s), ('12', '12', %s), ('13', '13', %s), ('14', '14', %s)` ,
202210 s .q .Placeholder (1 ), s .q .Placeholder (2 ), s .q .Placeholder (3 ), s .q .Placeholder (4 ))
203- _ , err := s .q .Exec (q , t1 , t2 , tVLAT , tHST )
204- s .NoError (err )
211+
212+ withIdentityInsert (s .T (), s .q , "people" , func () {
213+ _ , err := s .q .Exec (q , t1 , t2 , tVLAT , tHST )
214+ s .NoError (err )
215+ })
205216
206217 q = `SELECT start, start FROM projects WHERE id IN ('11', '12', '13', '14') ORDER BY id`
207218 rows , err := s .q .Query (q )
0 commit comments