@@ -171,6 +171,17 @@ func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows)
171
171
return rows
172
172
}
173
173
174
+ func maybeSkip (t * testing.T , err error , skipErrno uint16 ) {
175
+ mySQLErr , ok := err .(* MySQLError )
176
+ if ! ok {
177
+ return
178
+ }
179
+
180
+ if mySQLErr .Number == skipErrno {
181
+ t .Skipf ("skipping test as SET SESSION TRANSACTION is not supported" )
182
+ }
183
+ }
184
+
174
185
func TestEmptyQuery (t * testing.T ) {
175
186
runTests (t , dsn , func (dbt * DBTest ) {
176
187
// just a comment, no query
@@ -1168,11 +1179,9 @@ func TestStrict(t *testing.T) {
1168
1179
if conn != nil {
1169
1180
conn .Close ()
1170
1181
}
1171
- if me , ok := err .(* MySQLError ); ok && me .Number == 1231 {
1172
- // Error 1231: Variable 'sql_mode' can't be set to the value of 'ALLOW_INVALID_DATES'
1173
- // => skip test, MySQL server version is too old
1174
- return
1175
- }
1182
+ // Error 1231: Variable 'sql_mode' can't be set to the value of
1183
+ // 'ALLOW_INVALID_DATES' => skip test, MySQL server version is too old
1184
+ maybeSkip (t , err , 1231 )
1176
1185
runTests (t , relaxedDsn , func (dbt * DBTest ) {
1177
1186
dbt .mustExec ("CREATE TABLE test (a TINYINT NOT NULL, b CHAR(4))" )
1178
1187
@@ -1949,3 +1958,35 @@ func TestColumnsReusesSlice(t *testing.T) {
1949
1958
t .Fatalf ("expected columnNames to be set, got nil" )
1950
1959
}
1951
1960
}
1961
+
1962
+ func TestRejectReadOnly (t * testing.T ) {
1963
+ runTests (t , dsn , func (dbt * DBTest ) {
1964
+ // Create Table
1965
+ dbt .mustExec ("CREATE TABLE test (value BOOL)" )
1966
+ // Set the session to read-only. We didn't set the `rejectReadOnly`
1967
+ // option, so any writes after this should fail.
1968
+ _ , err := dbt .db .Exec ("SET SESSION TRANSACTION READ ONLY" )
1969
+ // Error 1193: Unknown system variable 'TRANSACTION'
1970
+ maybeSkip (t , err , 1193 )
1971
+ if _ , err := dbt .db .Exec ("DROP TABLE test" ); err == nil {
1972
+ t .Fatalf ("writing to DB in read-only session without " +
1973
+ "rejectReadOnly did not error" )
1974
+ }
1975
+ // Set the session back to read-write so runTests() can properly clean
1976
+ // up the table `test`.
1977
+ dbt .mustExec ("SET SESSION TRANSACTION READ WRITE" )
1978
+ })
1979
+
1980
+ // Enable the `rejectReadOnly` option.
1981
+ runTests (t , dsn + "&rejectReadOnly=true" , func (dbt * DBTest ) {
1982
+ // Create Table
1983
+ dbt .mustExec ("CREATE TABLE test (value BOOL)" )
1984
+ // Set the session to read only. Any writes after this should error on
1985
+ // a driver.ErrBadConn, and cause `database/sql` to initiate a new
1986
+ // connection.
1987
+ dbt .mustExec ("SET SESSION TRANSACTION READ ONLY" )
1988
+ // This would error, but `database/sql` should automatically retry on a
1989
+ // new connection which is not read-only, and eventually succeed.
1990
+ dbt .mustExec ("DROP TABLE test" )
1991
+ })
1992
+ }
0 commit comments