@@ -20,6 +20,7 @@ import (
2020 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
2121 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
2222 "github.com/cockroachdb/cockroach/pkg/util/leaktest"
23+ "github.com/jackc/pgx/v5"
2324 "github.com/stretchr/testify/require"
2425)
2526
@@ -40,6 +41,8 @@ func TestShowTransferState(t *testing.T) {
4041 require .NoError (t , err )
4142 _ , err = mainDB .Exec ("ALTER TENANT ALL SET CLUSTER SETTING server.user_login.session_revival_token.enabled = true" )
4243 require .NoError (t , err )
44+ _ , err = tenantDB .Exec ("CREATE TYPE typ AS ENUM ('foo', 'bar')" )
45+ require .NoError (t , err )
4346
4447 testUserConn := tenant .SQLConnForUser (t , username .TestUser , "" )
4548
@@ -82,24 +85,30 @@ func TestShowTransferState(t *testing.T) {
8285 q := pgURL .Query ()
8386 q .Add ("application_name" , "carl" )
8487 pgURL .RawQuery = q .Encode ()
85- conn , err := gosql . Open ( "postgres" , pgURL .String ())
88+ conn , err := pgx . Connect ( ctx , pgURL .String ())
8689 require .NoError (t , err )
87- defer conn .Close ()
90+ defer func () { _ = conn .Close ( ctx ) } ()
8891
8992 // Add a prepared statement to make sure SHOW TRANSFER STATE handles it.
90- // Since lib/pq doesn't tell us the name of the prepared statement, we won't
91- // be able to test that we can use it after deserializing the session, but
92- // there are other tests for that.
93- stmt , err := conn .Prepare ("SELECT 1 WHERE 1 = 1" )
93+ _ , err = conn .Prepare (ctx , "prepared_stmt" , "SELECT $1::INT4, 'foo'::typ WHERE 1 = 1" )
94+ require .NoError (t , err )
95+
96+ var intResult int
97+ var enumResult string
98+ err = conn .QueryRow (ctx , "prepared_stmt" , 1 ).Scan (& intResult , & enumResult )
9499 require .NoError (t , err )
95- defer stmt .Close ()
100+ require .Equal (t , 1 , intResult )
101+ require .Equal (t , "foo" , enumResult )
96102
97- rows , err := conn .Query (`SHOW TRANSFER STATE WITH 'foobar'` )
103+ rows , err := conn .Query (ctx , `SHOW TRANSFER STATE WITH 'foobar'` , pgx . QueryExecModeSimpleProtocol )
98104 require .NoError (t , err , "show transfer state failed" )
99105 defer rows .Close ()
100106
101- resultColumns , err := rows .Columns ()
102- require .NoError (t , err )
107+ fieldDescriptions := rows .FieldDescriptions ()
108+ var resultColumns []string
109+ for _ , f := range fieldDescriptions {
110+ resultColumns = append (resultColumns , f .Name )
111+ }
103112
104113 require .Equal (t , []string {
105114 "error" ,
@@ -136,26 +145,40 @@ func TestShowTransferState(t *testing.T) {
136145 q .Add ("application_name" , "someotherapp" )
137146 q .Add ("crdb:session_revival_token_base64" , token )
138147 pgURL .RawQuery = q .Encode ()
139- conn , err := gosql . Open ( "postgres" , pgURL .String ())
148+ conn , err := pgx . Connect ( ctx , pgURL .String ())
140149 require .NoError (t , err )
141- defer conn .Close ()
150+ defer func () { _ = conn .Close ( ctx ) } ()
142151
143152 var appName string
144- err = conn .QueryRow ("SHOW application_name" ).Scan (& appName )
153+ err = conn .QueryRow (ctx , "SHOW application_name" ).Scan (& appName )
145154 require .NoError (t , err )
146155 require .Equal (t , "someotherapp" , appName )
147156
148157 var b bool
149158 err = conn .QueryRow (
159+ ctx ,
150160 "SELECT crdb_internal.deserialize_session(decode($1, 'base64'))" ,
151161 state ,
152162 ).Scan (& b )
153163 require .NoError (t , err )
154164 require .True (t , b )
155165
156- err = conn .QueryRow ("SHOW application_name" ).Scan (& appName )
166+ err = conn .QueryRow (ctx , "SHOW application_name" ).Scan (& appName )
157167 require .NoError (t , err )
158168 require .Equal (t , "carl" , appName )
169+
170+ // Confirm that the prepared statement can be used after deserializing the
171+ // session.
172+ result := conn .PgConn ().ExecPrepared (
173+ ctx ,
174+ "prepared_stmt" ,
175+ [][]byte {{0 , 0 , 0 , 2 }}, // binary representation of 2
176+ []int16 {1 }, // paramFormats - 1 means binary
177+ []int16 {1 }, // resultFormats - 1 means binary
178+ ).Read ()
179+ require .Equal (t , [][][]byte {{
180+ {0 , 0 , 0 , 2 }, {0x66 , 0x6f , 0x6f }, // binary representation of 2, 'foo'
181+ }}, result .Rows )
159182 })
160183
161184 // Errors should be displayed as a SQL value.
0 commit comments