|
| 1 | +############################################################################### |
| 2 | +# Test for --sql-safe-updates command line option # |
| 3 | +# # |
| 4 | +# This test verifies that the --sql-safe-updates command line option works # |
| 5 | +# correctly to set the global default value for sql_safe_updates. # |
| 6 | +# # |
| 7 | +# The server is started with --sql-safe-updates=1 (see .opt file) # |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +# Force restart after test because we modify global sql_safe_updates |
| 11 | +--source include/force_restart.inc |
| 12 | + |
| 13 | +--echo # |
| 14 | +--echo # Test 1: Verify global and session default values are ON |
| 15 | +--echo # when server is started with --sql-safe-updates=1 |
| 16 | +--echo # |
| 17 | + |
| 18 | +SELECT @@global.sql_safe_updates AS global_value; |
| 19 | +SELECT @@session.sql_safe_updates AS session_value; |
| 20 | + |
| 21 | +--echo # |
| 22 | +--echo # Test 2: Verify that sql_safe_updates is enforced |
| 23 | +--echo # (UPDATE without WHERE using KEY should fail) |
| 24 | +--echo # |
| 25 | + |
| 26 | +CREATE TABLE t1 (id INT PRIMARY KEY, name VARCHAR(50)); |
| 27 | +INSERT INTO t1 VALUES (1, 'test1'), (2, 'test2'), (3, 'test3'); |
| 28 | + |
| 29 | +--echo # This should fail with sql_safe_updates enabled |
| 30 | +--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE |
| 31 | +UPDATE t1 SET name = 'updated'; |
| 32 | + |
| 33 | +--echo # This should fail with sql_safe_updates enabled |
| 34 | +--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE |
| 35 | +DELETE FROM t1; |
| 36 | + |
| 37 | +--echo # UPDATE with WHERE using KEY column should succeed |
| 38 | +UPDATE t1 SET name = 'updated1' WHERE id = 1; |
| 39 | +SELECT * FROM t1 WHERE id = 1; |
| 40 | + |
| 41 | +--echo # |
| 42 | +--echo # Test 3: Verify session can override the global value |
| 43 | +--echo # |
| 44 | + |
| 45 | +SET SESSION sql_safe_updates = OFF; |
| 46 | +SELECT @@session.sql_safe_updates AS session_after_off; |
| 47 | +SELECT @@global.sql_safe_updates AS global_unchanged; |
| 48 | + |
| 49 | +--echo # Now UPDATE without WHERE should succeed |
| 50 | +UPDATE t1 SET name = 'all_updated'; |
| 51 | +SELECT * FROM t1 ORDER BY id; |
| 52 | + |
| 53 | +--echo # |
| 54 | +--echo # Test 4: Verify new session inherits global value |
| 55 | +--echo # |
| 56 | + |
| 57 | +--echo # Reconnect to get a new session |
| 58 | +--connect (con1, localhost, root,,) |
| 59 | + |
| 60 | +SELECT @@session.sql_safe_updates AS new_session_value; |
| 61 | +SELECT @@global.sql_safe_updates AS global_value; |
| 62 | + |
| 63 | +--echo # UPDATE without WHERE should fail in new session |
| 64 | +--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE |
| 65 | +UPDATE t1 SET name = 'fail'; |
| 66 | + |
| 67 | +--connection default |
| 68 | +--disconnect con1 |
| 69 | + |
| 70 | +--echo # |
| 71 | +--echo # Test 5: Verify SET GLOBAL works |
| 72 | +--echo # |
| 73 | + |
| 74 | +SET GLOBAL sql_safe_updates = OFF; |
| 75 | +SELECT @@global.sql_safe_updates AS global_after_set_off; |
| 76 | + |
| 77 | +--echo # Reconnect to verify new session gets the new global value |
| 78 | +--connect (con2, localhost, root,,) |
| 79 | + |
| 80 | +SELECT @@session.sql_safe_updates AS new_session_inherits_global; |
| 81 | + |
| 82 | +--echo # UPDATE without WHERE should now succeed in new session |
| 83 | +UPDATE t1 SET name = 'success'; |
| 84 | +SELECT * FROM t1 ORDER BY id; |
| 85 | + |
| 86 | +--connection default |
| 87 | +--disconnect con2 |
| 88 | + |
| 89 | +--echo # |
| 90 | +--echo # Cleanup |
| 91 | +--echo # |
| 92 | + |
| 93 | +# Reset to default OFF to avoid affecting MTR check_warnings |
| 94 | +SET GLOBAL sql_safe_updates = OFF; |
| 95 | +DROP TABLE t1; |
| 96 | + |
| 97 | +--echo # |
| 98 | +--echo # Test completed successfully |
| 99 | +--echo # |
0 commit comments