Skip to content

Commit e8829db

Browse files
fix: Address Copilot review feedback
- Update all DROP PROCEDURE statements to use IF EXISTS for SQL 2016+ compatibility - Add unit tests for IPv6 address handling in URL() Updates queries_go19_test.go to use consistent DROP PROCEDURE IF EXISTS syntax throughout, matching the pattern already used in tvp_go19_db_test.go. Adds TestURLWithIPv6Address to validate that net.JoinHostPort correctly formats IPv6 addresses with brackets (e.g., [::1]:1433).
1 parent d829433 commit e8829db

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

msdsn/conn_str_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,59 @@ func TestConnParseRoundTripFixed(t *testing.T) {
287287
}
288288
}
289289

290+
func TestURLWithIPv6Address(t *testing.T) {
291+
tests := []struct {
292+
name string
293+
host string
294+
port uint64
295+
expected string
296+
}{
297+
{
298+
name: "IPv6 loopback with port",
299+
host: "::1",
300+
port: 1433,
301+
expected: "[::1]:1433",
302+
},
303+
{
304+
name: "IPv6 full address with port",
305+
host: "2001:db8::1",
306+
port: 1433,
307+
expected: "[2001:db8::1]:1433",
308+
},
309+
{
310+
name: "IPv4 address with port",
311+
host: "192.168.1.1",
312+
port: 1433,
313+
expected: "192.168.1.1:1433",
314+
},
315+
{
316+
name: "hostname with port",
317+
host: "localhost",
318+
port: 1433,
319+
expected: "localhost:1433",
320+
},
321+
{
322+
name: "IPv6 without port",
323+
host: "::1",
324+
port: 0,
325+
expected: "::1",
326+
},
327+
}
328+
329+
for _, tt := range tests {
330+
t.Run(tt.name, func(t *testing.T) {
331+
cfg := Config{
332+
Host: tt.host,
333+
Port: tt.port,
334+
}
335+
u := cfg.URL()
336+
if u.Host != tt.expected {
337+
t.Errorf("URL().Host = %q, expected %q", u.Host, tt.expected)
338+
}
339+
})
340+
}
341+
}
342+
290343
func TestServerNameInTLSConfig(t *testing.T) {
291344
var tests = []struct {
292345
dsn string

queries_go19_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestOutputParam(t *testing.T) {
3939
AS
4040
SELECT @strparam = REPLICATE('a', 8000)
4141
RETURN 0`
42-
sqltextdrop := `drop procedure GetTask`
42+
sqltextdrop := `DROP PROCEDURE IF EXISTS GetTask`
4343
sqltextrun := `GetTask`
4444
_, _ = db.ExecContext(ctx, sqltextdrop)
4545
_, err = db.ExecContext(ctx, sqltextcreate)
@@ -74,7 +74,7 @@ BEGIN
7474
SELECT @intparam = 10
7575
END;
7676
`
77-
sqltextdrop := `DROP PROCEDURE spwithrows;`
77+
sqltextdrop := `DROP PROCEDURE IF EXISTS spwithrows;`
7878
sqltextrun := `spwithrows`
7979

8080
db.ExecContext(ctx, sqltextdrop)
@@ -146,7 +146,7 @@ BEGIN
146146
SELECT @bid = @aid, @cstr = 'OK', @datetime = '2010-01-01T00:00:00';
147147
END;
148148
`
149-
sqltextdrop := `DROP PROCEDURE abassign;`
149+
sqltextdrop := `DROP PROCEDURE IF EXISTS abassign;`
150150
sqltextrun := `abassign`
151151

152152
db.ExecContext(ctx, sqltextdrop)
@@ -386,7 +386,7 @@ BEGIN
386386
SET @sinout = 'long_long_value'
387387
END;
388388
`
389-
sqltextdrop := `DROP PROCEDURE vinout;`
389+
sqltextdrop := `DROP PROCEDURE IF EXISTS vinout;`
390390
sqltextrun := `vinout`
391391

392392
checkConnStr(t)
@@ -484,7 +484,7 @@ BEGIN
484484
SET @binout = CONVERT(VARBINARY(4000), 'long_long_value')
485485
END;
486486
`
487-
sqltextdrop := `DROP PROCEDURE vinout;`
487+
sqltextdrop := `DROP PROCEDURE IF EXISTS vinout;`
488488
sqltextrun := `vinout`
489489

490490
checkConnStr(t)
@@ -537,7 +537,7 @@ BEGIN
537537
SET @dinout = 29342.1234
538538
END;
539539
`
540-
sqltextdrop := `DROP PROCEDURE vinout;`
540+
sqltextdrop := `DROP PROCEDURE IF EXISTS vinout;`
541541
sqltextrun := `vinout`
542542

543543
checkConnStr(t)
@@ -645,7 +645,7 @@ BEGIN
645645
SET @minout = 29342.1234
646646
END;
647647
`
648-
sqltextdrop := `DROP PROCEDURE vinout;`
648+
sqltextdrop := `DROP PROCEDURE IF EXISTS vinout;`
649649
sqltextrun := `vinout`
650650

651651
checkConnStr(t)
@@ -758,7 +758,7 @@ BEGIN
758758
SELECT @dinout
759759
END;
760760
`
761-
sqltextdrop := `DROP PROCEDURE vinout;`
761+
sqltextdrop := `DROP PROCEDURE IF EXISTS vinout;`
762762
sqltextrun := `vinout`
763763

764764
checkConnStr(t)
@@ -962,7 +962,7 @@ BEGIN
962962
SELECT @minout
963963
END;
964964
`
965-
sqltextdrop := `DROP PROCEDURE vinout;`
965+
sqltextdrop := `DROP PROCEDURE IF EXISTS vinout;`
966966
sqltextrun := `vinout`
967967

968968
checkConnStr(t)
@@ -1249,7 +1249,7 @@ BEGIN
12491249
;
12501250
END;
12511251
`
1252-
sqltextdrop := `DROP PROCEDURE abinout;`
1252+
sqltextdrop := `DROP PROCEDURE IF EXISTS abinout;`
12531253
sqltextrun := `abinout`
12541254

12551255
checkConnStr(t)
@@ -1451,7 +1451,7 @@ func TestOutputParamWithRows(t *testing.T) {
14511451
SELECT 'Row 1'
14521452
END
14531453
`
1454-
sqltextdrop := `DROP PROCEDURE spwithoutputandrows;`
1454+
sqltextdrop := `DROP PROCEDURE IF EXISTS spwithoutputandrows;`
14551455
sqltextrun := `spwithoutputandrows`
14561456

14571457
checkConnStr(t)
@@ -1556,7 +1556,7 @@ func TestParamNoName(t *testing.T) {
15561556
AS BEGIN
15571557
SELECT @intCol, @nvarcharCol, @varcharCol, @decimalCol, @nullDecimalCol
15581558
END`
1559-
sqltextdrop := `DROP PROCEDURE spnoparamname`
1559+
sqltextdrop := `DROP PROCEDURE IF EXISTS spnoparamname`
15601560
sqltextrun := `spnoparamname`
15611561

15621562
db.ExecContext(ctx, sqltextdrop)
@@ -2233,9 +2233,7 @@ func TestCancelWithNoResults(t *testing.T) {
22332233
}
22342234
}
22352235

2236-
const DropSprocWithCursor = `IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestSqlCmd]') AND type in (N'P', N'PC'))
2237-
DROP PROCEDURE [dbo].[TestSqlCmd]
2238-
`
2236+
const DropSprocWithCursor = `DROP PROCEDURE IF EXISTS [dbo].[TestSqlCmd]`
22392237

22402238
// This query generates half a dozen tokenDoneInProc tokens which fill the channel if the app isn't scanning Rowsq
22412239
const CreateSprocWithCursor = `

0 commit comments

Comments
 (0)