| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Rdbms; |
| 4 |
|
| 5 |
use DatabaseTestHelper; |
| 6 |
use MediaWikiCoversValidator; |
| 7 |
use MediaWikiTestCaseTrait; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
use RuntimeException; |
| 10 |
use Wikimedia\Rdbms\Database; |
| 11 |
use Wikimedia\Rdbms\DBError; |
| 12 |
use Wikimedia\Rdbms\DBTransactionError; |
| 13 |
use Wikimedia\Rdbms\DBTransactionStateError; |
| 14 |
use Wikimedia\Rdbms\DBUnexpectedError; |
| 15 |
use Wikimedia\Rdbms\IDatabase; |
| 16 |
use Wikimedia\Rdbms\Platform\SQLPlatform; |
| 17 |
use Wikimedia\Rdbms\TransactionManager; |
| 18 |
use Wikimedia\TestingAccessWrapper; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class DatabaseSQLTest extends TestCase { |
| 29 |
|
| 30 |
use MediaWikiCoversValidator; |
| 31 |
use MediaWikiTestCaseTrait; |
| 32 |
|
| 33 |
|
| 34 |
private $database; |
| 35 |
|
| 36 |
|
| 37 |
private $platform; |
| 38 |
|
| 39 |
protected function setUp(): void { |
| 40 |
parent::setUp(); |
| 41 |
$this->database = new DatabaseTestHelper( __CLASS__, [ 'cliMode' => true ] ); |
| 42 |
} |
| 43 |
|
| 44 |
protected function assertLastSql( $sqlText ) { |
| 45 |
$this->assertEquals( |
| 46 |
$sqlText, |
| 47 |
$this->database->getLastSqls() |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
protected function assertLastSqlDb( $sqlText, DatabaseTestHelper $db ) { |
| 52 |
$this->assertEquals( $sqlText, $db->getLastSqls() ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function testLockForUpdate( $sql, $sqlText ) { |
| 59 |
$this->database->startAtomic( __METHOD__ ); |
| 60 |
$this->database->lockForUpdate( |
| 61 |
$sql['tables'], |
| 62 |
$sql['conds'] ?? [], |
| 63 |
__METHOD__, |
| 64 |
$sql['options'] ?? [], |
| 65 |
$sql['join_conds'] ?? [] |
| 66 |
); |
| 67 |
$this->database->endAtomic( __METHOD__ ); |
| 68 |
|
| 69 |
$this->assertLastSql( "BEGIN; $sqlText; COMMIT" ); |
| 70 |
} |
| 71 |
|
| 72 |
public static function provideLockForUpdate() { |
| 73 |
return [ |
| 74 |
[ |
| 75 |
[ |
| 76 |
'tables' => [ 'table' ], |
| 77 |
'conds' => [ 'field' => [ 1, 2, 3, 4 ] ], |
| 78 |
], |
| 79 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 80 |
"(SELECT 1 FROM table WHERE field IN (1,2,3,4) " . |
| 81 |
"FOR UPDATE) tmp_count" |
| 82 |
], |
| 83 |
[ |
| 84 |
[ |
| 85 |
'tables' => [ 'table', 't2' => 'table2' ], |
| 86 |
'conds' => [ 'field' => 'text' ], |
| 87 |
'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ], |
| 88 |
'join_conds' => [ 't2' => [ |
| 89 |
'LEFT JOIN', 'tid = t2.id' |
| 90 |
] ], |
| 91 |
], |
| 92 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 93 |
"(SELECT 1 FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " . |
| 94 |
"WHERE field = 'text' ORDER BY field LIMIT 1 FOR UPDATE) tmp_count" |
| 95 |
], |
| 96 |
[ |
| 97 |
[ |
| 98 |
'tables' => 'table', |
| 99 |
], |
| 100 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 101 |
"(SELECT 1 FROM table FOR UPDATE) tmp_count" |
| 102 |
], |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testSelectRowCount( $sql, $sqlText ) { |
| 112 |
$this->database->selectRowCount( |
| 113 |
$sql['tables'], |
| 114 |
$sql['field'], |
| 115 |
$sql['conds'] ?? [], |
| 116 |
__METHOD__, |
| 117 |
$sql['options'] ?? [], |
| 118 |
$sql['join_conds'] ?? [] |
| 119 |
); |
| 120 |
$this->assertLastSql( $sqlText ); |
| 121 |
} |
| 122 |
|
| 123 |
public static function provideSelectRowCount() { |
| 124 |
return [ |
| 125 |
[ |
| 126 |
[ |
| 127 |
'tables' => 'table', |
| 128 |
'field' => [ '*' ], |
| 129 |
'conds' => [ 'field' => 'text' ], |
| 130 |
], |
| 131 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 132 |
"(SELECT 1 FROM table WHERE field = 'text' ) tmp_count" |
| 133 |
], |
| 134 |
[ |
| 135 |
[ |
| 136 |
'tables' => 'table', |
| 137 |
'field' => [ 'column' ], |
| 138 |
'conds' => [ 'field' => 'text' ], |
| 139 |
], |
| 140 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 141 |
"(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count" |
| 142 |
], |
| 143 |
[ |
| 144 |
[ |
| 145 |
'tables' => 'table', |
| 146 |
'field' => [ 'alias' => 'column' ], |
| 147 |
'conds' => [ 'field' => 'text' ], |
| 148 |
], |
| 149 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 150 |
"(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count" |
| 151 |
], |
| 152 |
[ |
| 153 |
[ |
| 154 |
'tables' => 'table', |
| 155 |
'field' => [ 'alias' => 'column' ], |
| 156 |
'conds' => '', |
| 157 |
], |
| 158 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 159 |
"(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count" |
| 160 |
], |
| 161 |
[ |
| 162 |
[ |
| 163 |
'tables' => 'table', |
| 164 |
'field' => [ 'alias' => 'column' ], |
| 165 |
'conds' => false, |
| 166 |
], |
| 167 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 168 |
"(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count" |
| 169 |
], |
| 170 |
[ |
| 171 |
[ |
| 172 |
'tables' => 'table', |
| 173 |
'field' => [ 'alias' => 'column' ], |
| 174 |
'conds' => null, |
| 175 |
], |
| 176 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 177 |
"(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count" |
| 178 |
], |
| 179 |
[ |
| 180 |
[ |
| 181 |
'tables' => 'table', |
| 182 |
'field' => [ 'alias' => 'column' ], |
| 183 |
'conds' => 1, |
| 184 |
], |
| 185 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 186 |
"(SELECT 1 FROM table WHERE (1) AND (column IS NOT NULL) ) tmp_count" |
| 187 |
], |
| 188 |
[ |
| 189 |
[ |
| 190 |
'tables' => 'table', |
| 191 |
'field' => [ 'alias' => 'column' ], |
| 192 |
'conds' => 0, |
| 193 |
], |
| 194 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 195 |
"(SELECT 1 FROM table WHERE (0) AND (column IS NOT NULL) ) tmp_count" |
| 196 |
], |
| 197 |
[ |
| 198 |
[ |
| 199 |
'tables' => 'table', |
| 200 |
'field' => [ 'column' ], |
| 201 |
'conds' => 1, |
| 202 |
'options' => 'DISTINCT', |
| 203 |
], |
| 204 |
"SELECT COUNT(*) AS rowcount FROM " . |
| 205 |
"(SELECT DISTINCT column FROM table WHERE (1) AND (column IS NOT NULL) ) tmp_count" |
| 206 |
], |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function testUpsert( $sql, $sqlText ) { |
| 214 |
$this->database->setNextQueryAffectedRowCounts( [ 0 ] ); |
| 215 |
|
| 216 |
foreach ( $sql['set'] as $k => $v ) { |
| 217 |
$sql['set'][$k] = preg_replace_callback( |
| 218 |
'/\$EXCLUDED\((\w+)\)/', |
| 219 |
function ( $m ) { |
| 220 |
return $this->database->buildExcludedValue( $m[1] ); |
| 221 |
}, |
| 222 |
$v |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
$this->database->upsert( |
| 227 |
$sql['table'], |
| 228 |
$sql['rows'], |
| 229 |
$sql['uniqueIndexes'], |
| 230 |
$sql['set'], |
| 231 |
__METHOD__ |
| 232 |
); |
| 233 |
$this->assertLastSql( $sqlText ); |
| 234 |
} |
| 235 |
|
| 236 |
public static function provideUpsert() { |
| 237 |
return [ |
| 238 |
[ |
| 239 |
[ |
| 240 |
'table' => 'upsert_table', |
| 241 |
'rows' => [ 'id' => 'text', 'field1' => 'text2' ], |
| 242 |
'uniqueIndexes' => 'id', |
| 243 |
'set' => [ 'field2' => 'set' ], |
| 244 |
], |
| 245 |
"BEGIN; " . |
| 246 |
"UPDATE upsert_table SET field2 = 'set' " . |
| 247 |
"WHERE (id = 'text'); " . |
| 248 |
"INSERT INTO upsert_table (id,field1) VALUES ('text','text2'); " . |
| 249 |
"COMMIT" |
| 250 |
], |
| 251 |
[ |
| 252 |
[ |
| 253 |
'table' => 'upsert_table', |
| 254 |
'rows' => [ 'id' => 'text', 'field1' => 'text2' ], |
| 255 |
'uniqueIndexes' => [ [ 'id' ] ], |
| 256 |
'set' => [ 'field2' => 'set' ], |
| 257 |
], |
| 258 |
"BEGIN; " . |
| 259 |
"UPDATE upsert_table SET field2 = 'set' " . |
| 260 |
"WHERE (id = 'text'); " . |
| 261 |
"INSERT INTO upsert_table (id,field1) VALUES ('text','text2'); " . |
| 262 |
"COMMIT" |
| 263 |
], |
| 264 |
[ |
| 265 |
[ |
| 266 |
'table' => 'upsert_table2', |
| 267 |
'rows' => [ 'code' => 'text', 'name' => 'more', 'field3' => 'text2' ], |
| 268 |
'uniqueIndexes' => [ [ 'code', 'name' ] ], |
| 269 |
'set' => [ 'field2' => 'set' ], |
| 270 |
], |
| 271 |
"BEGIN; " . |
| 272 |
"UPDATE upsert_table2 SET field2 = 'set' " . |
| 273 |
"WHERE (code = 'text' AND name = 'more'); " . |
| 274 |
"INSERT INTO upsert_table2 (code,name,field3) VALUES ('text','more','text2'); " . |
| 275 |
"COMMIT" |
| 276 |
], |
| 277 |
[ |
| 278 |
[ |
| 279 |
'table' => 'upsert_table', |
| 280 |
'rows' => [ |
| 281 |
[ 'id' => 1, 'field1' => 10, 'field2' => 1583464659, 'field3' => 1 ], |
| 282 |
[ 'id' => 6, 'field1' => 60, 'field2' => 1583464659, 'field3' => 1 ] |
| 283 |
], |
| 284 |
'uniqueIndexes' => 'id', |
| 285 |
'set' => [ |
| 286 |
'field1 = field1 + $EXCLUDED(field1)', |
| 287 |
'field2 = COALESCE(field2,$EXCLUDED(field2))' |
| 288 |
], |
| 289 |
], |
| 290 |
"BEGIN; " . |
| 291 |
"WITH __VALS (__id,__field1,__field2,__field3) " . |
| 292 |
"AS (VALUES (1,10,1583464659,1)) " . |
| 293 |
"UPDATE upsert_table SET " . |
| 294 |
"field1 = field1 + (SELECT __field1 FROM __VALS)," . |
| 295 |
"field2 = COALESCE(field2,(SELECT __field2 FROM __VALS)) " . |
| 296 |
"WHERE (id = 1); " . |
| 297 |
"INSERT INTO upsert_table (id,field1,field2,field3) " . |
| 298 |
"VALUES (1,10,1583464659,1); " . |
| 299 |
"WITH __VALS (__id,__field1,__field2,__field3) " . |
| 300 |
"AS (VALUES (6,60,1583464659,1)) " . |
| 301 |
"UPDATE upsert_table SET " . |
| 302 |
"field1 = field1 + (SELECT __field1 FROM __VALS)," . |
| 303 |
"field2 = COALESCE(field2,(SELECT __field2 FROM __VALS)) " . |
| 304 |
"WHERE (id = 6); " . |
| 305 |
"INSERT INTO upsert_table (id,field1,field2,field3) " . |
| 306 |
"VALUES (6,60,1583464659,1); " . |
| 307 |
"COMMIT" |
| 308 |
], |
| 309 |
]; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function testInsert( $sql, $sqlText ) { |
| 316 |
$this->database->insert( |
| 317 |
$sql['table'], |
| 318 |
$sql['rows'], |
| 319 |
__METHOD__, |
| 320 |
$sql['options'] ?? [] |
| 321 |
); |
| 322 |
$this->assertLastSql( $sqlText ); |
| 323 |
} |
| 324 |
|
| 325 |
public static function provideInsert() { |
| 326 |
return [ |
| 327 |
[ |
| 328 |
[ |
| 329 |
'table' => 'table', |
| 330 |
'rows' => [ 'field' => 'text', 'field2' => 2 ], |
| 331 |
], |
| 332 |
"INSERT INTO table " . |
| 333 |
"(field,field2) " . |
| 334 |
"VALUES ('text',2)" |
| 335 |
], |
| 336 |
[ |
| 337 |
[ |
| 338 |
'table' => 'table', |
| 339 |
'rows' => [ 'field' => 'text', 'field2' => 2 ], |
| 340 |
'options' => 'IGNORE', |
| 341 |
], |
| 342 |
"INSERT IGNORE INTO table " . |
| 343 |
"(field,field2) " . |
| 344 |
"VALUES ('text',2)" |
| 345 |
], |
| 346 |
[ |
| 347 |
[ |
| 348 |
'table' => 'table', |
| 349 |
'rows' => [ |
| 350 |
[ 'field' => 'text', 'field2' => 2 ], |
| 351 |
[ 'field' => 'multi', 'field2' => 3 ], |
| 352 |
], |
| 353 |
'options' => 'IGNORE', |
| 354 |
], |
| 355 |
"INSERT IGNORE INTO table " . |
| 356 |
"(field,field2) " . |
| 357 |
"VALUES " . |
| 358 |
"('text',2)," . |
| 359 |
"('multi',3)" |
| 360 |
], |
| 361 |
]; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public function testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert ) { |
| 368 |
$this->database->insertSelect( |
| 369 |
$sql['destTable'], |
| 370 |
$sql['srcTable'], |
| 371 |
$sql['varMap'], |
| 372 |
$sql['conds'], |
| 373 |
__METHOD__, |
| 374 |
$sql['insertOptions'] ?? [], |
| 375 |
$sql['selectOptions'] ?? [], |
| 376 |
$sql['selectJoinConds'] ?? [] |
| 377 |
); |
| 378 |
$this->assertLastSql( $sqlTextNative ); |
| 379 |
|
| 380 |
$dbWeb = new DatabaseTestHelper( __CLASS__, [ 'cliMode' => false ] ); |
| 381 |
$dbWeb->forceNextResult( [ |
| 382 |
array_flip( array_keys( $sql['varMap'] ) ) |
| 383 |
] ); |
| 384 |
$dbWeb->insertSelect( |
| 385 |
$sql['destTable'], |
| 386 |
$sql['srcTable'], |
| 387 |
$sql['varMap'], |
| 388 |
$sql['conds'], |
| 389 |
__METHOD__, |
| 390 |
$sql['insertOptions'] ?? [], |
| 391 |
$sql['selectOptions'] ?? [], |
| 392 |
$sql['selectJoinConds'] ?? [] |
| 393 |
); |
| 394 |
$this->assertLastSqlDb( implode( '; ', [ $sqlSelect, 'BEGIN', $sqlInsert, 'COMMIT' ] ), $dbWeb ); |
| 395 |
} |
| 396 |
|
| 397 |
public static function provideInsertSelect() { |
| 398 |
return [ |
| 399 |
[ |
| 400 |
[ |
| 401 |
'destTable' => 'insert_table', |
| 402 |
'srcTable' => 'select_table', |
| 403 |
'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ], |
| 404 |
'conds' => '*', |
| 405 |
], |
| 406 |
"INSERT INTO insert_table " . |
| 407 |
"(field_insert,field) " . |
| 408 |
"SELECT field_select,field2 " . |
| 409 |
"FROM select_table", |
| 410 |
"SELECT field_select AS field_insert,field2 AS field " . |
| 411 |
"FROM select_table FOR UPDATE", |
| 412 |
"INSERT INTO insert_table (field_insert,field) VALUES (0,1)" |
| 413 |
], |
| 414 |
[ |
| 415 |
[ |
| 416 |
'destTable' => 'insert_table', |
| 417 |
'srcTable' => 'select_table', |
| 418 |
'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ], |
| 419 |
'conds' => [ 'field' => 2 ], |
| 420 |
], |
| 421 |
"INSERT INTO insert_table " . |
| 422 |
"(field_insert,field) " . |
| 423 |
"SELECT field_select,field2 " . |
| 424 |
"FROM select_table " . |
| 425 |
"WHERE field = 2", |
| 426 |
"SELECT field_select AS field_insert,field2 AS field FROM " . |
| 427 |
"select_table WHERE field = 2 FOR UPDATE", |
| 428 |
"INSERT INTO insert_table (field_insert,field) VALUES (0,1)" |
| 429 |
], |
| 430 |
[ |
| 431 |
[ |
| 432 |
'destTable' => 'insert_table', |
| 433 |
'srcTable' => 'select_table', |
| 434 |
'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ], |
| 435 |
'conds' => [ 'field' => 2 ], |
| 436 |
'insertOptions' => 'IGNORE', |
| 437 |
'selectOptions' => [ 'ORDER BY' => 'field' ], |
| 438 |
], |
| 439 |
"INSERT IGNORE INTO insert_table " . |
| 440 |
"(field_insert,field) " . |
| 441 |
"SELECT field_select,field2 " . |
| 442 |
"FROM select_table " . |
| 443 |
"WHERE field = 2 " . |
| 444 |
"ORDER BY field", |
| 445 |
"SELECT field_select AS field_insert,field2 AS field " . |
| 446 |
"FROM select_table WHERE field = 2 ORDER BY field FOR UPDATE", |
| 447 |
"INSERT IGNORE INTO insert_table (field_insert,field) VALUES (0,1)" |
| 448 |
], |
| 449 |
[ |
| 450 |
[ |
| 451 |
'destTable' => 'insert_table', |
| 452 |
'srcTable' => [ 'select_table1', 'select_table2' ], |
| 453 |
'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ], |
| 454 |
'conds' => [ 'field' => 2 ], |
| 455 |
'insertOptions' => [ 'NO_AUTO_COLUMNS' ], |
| 456 |
'selectOptions' => [ 'ORDER BY' => 'field', 'FORCE INDEX' => [ 'select_table1' => 'index1' ] ], |
| 457 |
'selectJoinConds' => [ |
| 458 |
'select_table2' => [ 'LEFT JOIN', [ 'select_table1.foo = select_table2.bar' ] ], |
| 459 |
], |
| 460 |
], |
| 461 |
"INSERT INTO insert_table " . |
| 462 |
"(field_insert,field) " . |
| 463 |
"SELECT field_select,field2 " . |
| 464 |
"FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " . |
| 465 |
"WHERE field = 2 " . |
| 466 |
"ORDER BY field", |
| 467 |
"SELECT field_select AS field_insert,field2 AS field " . |
| 468 |
"FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " . |
| 469 |
"WHERE field = 2 ORDER BY field FOR UPDATE", |
| 470 |
"INSERT INTO insert_table (field_insert,field) VALUES (0,1)" |
| 471 |
], |
| 472 |
]; |
| 473 |
} |
| 474 |
|
| 475 |
public function testInsertSelectBatching() { |
| 476 |
$dbWeb = new DatabaseTestHelper( __CLASS__, [ 'cliMode' => false ] ); |
| 477 |
$rows = []; |
| 478 |
for ( $i = 0; $i <= 25000; $i++ ) { |
| 479 |
$rows[] = [ 'field' => $i ]; |
| 480 |
} |
| 481 |
$dbWeb->forceNextResult( $rows ); |
| 482 |
$dbWeb->insertSelect( |
| 483 |
'insert_table', |
| 484 |
'select_table', |
| 485 |
[ 'field' => 'field2' ], |
| 486 |
'*', |
| 487 |
__METHOD__ |
| 488 |
); |
| 489 |
$this->assertLastSqlDb( implode( '; ', [ |
| 490 |
'SELECT field2 AS field FROM select_table FOR UPDATE', |
| 491 |
'BEGIN', |
| 492 |
"INSERT INTO insert_table (field) VALUES (" . implode( "),(", range( 0, 9999 ) ) . ")", |
| 493 |
"INSERT INTO insert_table (field) VALUES (" . implode( "),(", range( 10000, 19999 ) ) . ")", |
| 494 |
"INSERT INTO insert_table (field) VALUES (" . implode( "),(", range( 20000, 25000 ) ) . ")", |
| 495 |
'COMMIT' |
| 496 |
] ), $dbWeb ); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public function testReplace( $sql, $sqlText ) { |
| 503 |
$this->database->replace( |
| 504 |
$sql['table'], |
| 505 |
$sql['uniqueIndexes'], |
| 506 |
$sql['rows'], |
| 507 |
__METHOD__ |
| 508 |
); |
| 509 |
$this->assertLastSql( $sqlText ); |
| 510 |
} |
| 511 |
|
| 512 |
public static function provideReplace() { |
| 513 |
return [ |
| 514 |
[ |
| 515 |
[ |
| 516 |
'table' => 'replace_table', |
| 517 |
'uniqueIndexes' => [ 'field' ], |
| 518 |
'rows' => [ 'field' => 'text', 'field2' => 'text2' ], |
| 519 |
], |
| 520 |
"BEGIN; DELETE FROM replace_table " . |
| 521 |
"WHERE (field = 'text'); " . |
| 522 |
"INSERT INTO replace_table " . |
| 523 |
"(field,field2) " . |
| 524 |
"VALUES ('text','text2'); COMMIT" |
| 525 |
], |
| 526 |
[ |
| 527 |
[ |
| 528 |
'table' => 'module_deps', |
| 529 |
'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ], |
| 530 |
'rows' => [ |
| 531 |
'md_module' => 'module', |
| 532 |
'md_skin' => 'skin', |
| 533 |
'md_deps' => 'deps', |
| 534 |
], |
| 535 |
], |
| 536 |
"BEGIN; DELETE FROM module_deps " . |
| 537 |
"WHERE (md_module = 'module' AND md_skin = 'skin'); " . |
| 538 |
"INSERT INTO module_deps " . |
| 539 |
"(md_module,md_skin,md_deps) " . |
| 540 |
"VALUES ('module','skin','deps'); COMMIT" |
| 541 |
], |
| 542 |
[ |
| 543 |
[ |
| 544 |
'table' => 'module_deps', |
| 545 |
'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ], |
| 546 |
'rows' => [ |
| 547 |
[ |
| 548 |
'md_module' => 'module', |
| 549 |
'md_skin' => 'skin', |
| 550 |
'md_deps' => 'deps', |
| 551 |
], [ |
| 552 |
'md_module' => 'module2', |
| 553 |
'md_skin' => 'skin2', |
| 554 |
'md_deps' => 'deps2', |
| 555 |
], |
| 556 |
], |
| 557 |
], |
| 558 |
"BEGIN; DELETE FROM module_deps " . |
| 559 |
"WHERE (md_module = 'module' AND md_skin = 'skin'); " . |
| 560 |
"INSERT INTO module_deps " . |
| 561 |
"(md_module,md_skin,md_deps) " . |
| 562 |
"VALUES ('module','skin','deps'); " . |
| 563 |
"DELETE FROM module_deps " . |
| 564 |
"WHERE (md_module = 'module2' AND md_skin = 'skin2'); " . |
| 565 |
"INSERT INTO module_deps " . |
| 566 |
"(md_module,md_skin,md_deps) " . |
| 567 |
"VALUES ('module2','skin2','deps2'); COMMIT" |
| 568 |
], |
| 569 |
]; |
| 570 |
} |
| 571 |
|
| 572 |
public function testTransactionCommit() { |
| 573 |
$this->database->begin( __METHOD__ ); |
| 574 |
$this->database->commit( __METHOD__ ); |
| 575 |
$this->assertLastSql( 'BEGIN; COMMIT' ); |
| 576 |
} |
| 577 |
|
| 578 |
public function testTransactionRollback() { |
| 579 |
$this->database->begin( __METHOD__ ); |
| 580 |
$this->database->rollback( __METHOD__ ); |
| 581 |
$this->assertLastSql( 'BEGIN; ROLLBACK' ); |
| 582 |
} |
| 583 |
|
| 584 |
public function testDropTable() { |
| 585 |
$this->database->setExistingTables( [ 'table' ] ); |
| 586 |
$this->database->dropTable( 'table', __METHOD__ ); |
| 587 |
$this->assertLastSql( 'DROP TABLE table CASCADE' ); |
| 588 |
} |
| 589 |
|
| 590 |
public function testDropNonExistingTable() { |
| 591 |
$this->assertFalse( |
| 592 |
$this->database->dropTable( 'non_existing', __METHOD__ ) |
| 593 |
); |
| 594 |
} |
| 595 |
|
| 596 |
public function testSessionTempTables() { |
| 597 |
$temp1 = $this->database->tableName( 'tmp_table_1' ); |
| 598 |
$temp2 = $this->database->tableName( 'tmp_table_2' ); |
| 599 |
$temp3 = $this->database->tableName( 'tmp_table_3' ); |
| 600 |
|
| 601 |
$this->database->query( "CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__ ); |
| 602 |
$this->database->query( "CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__ ); |
| 603 |
$this->database->query( "CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__ ); |
| 604 |
|
| 605 |
$this->assertTrue( $this->database->tableExists( "tmp_table_1", __METHOD__ ) ); |
| 606 |
$this->assertTrue( $this->database->tableExists( "tmp_table_2", __METHOD__ ) ); |
| 607 |
$this->assertTrue( $this->database->tableExists( "tmp_table_3", __METHOD__ ) ); |
| 608 |
|
| 609 |
$this->database->dropTable( 'tmp_table_1', __METHOD__ ); |
| 610 |
$this->database->dropTable( 'tmp_table_2', __METHOD__ ); |
| 611 |
$this->database->dropTable( 'tmp_table_3', __METHOD__ ); |
| 612 |
|
| 613 |
$this->assertFalse( $this->database->tableExists( "tmp_table_1", __METHOD__ ) ); |
| 614 |
$this->assertFalse( $this->database->tableExists( "tmp_table_2", __METHOD__ ) ); |
| 615 |
$this->assertFalse( $this->database->tableExists( "tmp_table_3", __METHOD__ ) ); |
| 616 |
|
| 617 |
$this->database->query( "CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ ); |
| 618 |
$this->database->query( "CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ ); |
| 619 |
$this->database->query( "CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ ); |
| 620 |
|
| 621 |
$this->assertTrue( $this->database->tableExists( "tmp_table_1", __METHOD__ ) ); |
| 622 |
$this->assertTrue( $this->database->tableExists( "tmp_table_2", __METHOD__ ) ); |
| 623 |
$this->assertTrue( $this->database->tableExists( "tmp_table_3", __METHOD__ ) ); |
| 624 |
|
| 625 |
$this->database->query( "DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ ); |
| 626 |
$this->database->query( "DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ ); |
| 627 |
$this->database->query( "DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ ); |
| 628 |
|
| 629 |
$this->assertFalse( $this->database->tableExists( "tmp_table_1", __METHOD__ ) ); |
| 630 |
$this->assertFalse( $this->database->tableExists( "tmp_table_2", __METHOD__ ) ); |
| 631 |
$this->assertFalse( $this->database->tableExists( "tmp_table_3", __METHOD__ ) ); |
| 632 |
|
| 633 |
$this->database->query( "DROP TEMPORARY TABLE IF EXISTS `tmp_table_4`, `tmp_table_5`", __METHOD__ ); |
| 634 |
} |
| 635 |
|
| 636 |
public function testAtomicSections() { |
| 637 |
$this->database->startAtomic( __METHOD__ ); |
| 638 |
$this->database->endAtomic( __METHOD__ ); |
| 639 |
$this->assertLastSql( 'BEGIN; COMMIT' ); |
| 640 |
|
| 641 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 642 |
$this->database->cancelAtomic( __METHOD__ ); |
| 643 |
$this->assertLastSql( 'BEGIN; ROLLBACK' ); |
| 644 |
|
| 645 |
$this->database->begin( __METHOD__ ); |
| 646 |
$this->database->startAtomic( __METHOD__ ); |
| 647 |
$this->database->endAtomic( __METHOD__ ); |
| 648 |
$this->database->commit( __METHOD__ ); |
| 649 |
$this->assertLastSql( 'BEGIN; COMMIT' ); |
| 650 |
|
| 651 |
$this->database->begin( __METHOD__ ); |
| 652 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 653 |
$this->database->endAtomic( __METHOD__ ); |
| 654 |
$this->database->commit( __METHOD__ ); |
| 655 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' ); |
| 656 |
|
| 657 |
$this->database->begin( __METHOD__ ); |
| 658 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 659 |
$this->database->cancelAtomic( __METHOD__ ); |
| 660 |
$this->database->commit( __METHOD__ ); |
| 661 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' ); |
| 662 |
|
| 663 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 664 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 665 |
$this->database->cancelAtomic( __METHOD__ ); |
| 666 |
$this->database->endAtomic( __METHOD__ ); |
| 667 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' ); |
| 668 |
|
| 669 |
$noOpCallack = static function () { |
| 670 |
}; |
| 671 |
|
| 672 |
$this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE ); |
| 673 |
$this->assertLastSql( 'BEGIN; COMMIT' ); |
| 674 |
|
| 675 |
$this->database->doAtomicSection( __METHOD__, $noOpCallack ); |
| 676 |
$this->assertLastSql( 'BEGIN; COMMIT' ); |
| 677 |
|
| 678 |
$this->database->begin( __METHOD__ ); |
| 679 |
$this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE ); |
| 680 |
$this->database->rollback( __METHOD__ ); |
| 681 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK' ); |
| 682 |
|
| 683 |
$fname = __METHOD__; |
| 684 |
$triggerMap = [ |
| 685 |
'-' => '-', |
| 686 |
IDatabase::TRIGGER_COMMIT => 'tCommit', |
| 687 |
IDatabase::TRIGGER_ROLLBACK => 'tRollback', |
| 688 |
IDatabase::TRIGGER_CANCEL => 'tCancel', |
| 689 |
]; |
| 690 |
$pcCallback = function ( IDatabase $db ) use ( $fname ) { |
| 691 |
$this->database->query( "SELECT 0", $fname ); |
| 692 |
}; |
| 693 |
$callback1 = function ( $trigger = '-' ) use ( $fname, $triggerMap ) { |
| 694 |
$this->database->query( "SELECT 1, {$triggerMap[$trigger]} AS t", $fname ); |
| 695 |
}; |
| 696 |
$callback2 = function ( $trigger = '-' ) use ( $fname, $triggerMap ) { |
| 697 |
$this->database->query( "SELECT 2, {$triggerMap[$trigger]} AS t", $fname ); |
| 698 |
}; |
| 699 |
$callback3 = function ( $trigger = '-' ) use ( $fname, $triggerMap ) { |
| 700 |
$this->database->query( "SELECT 3, {$triggerMap[$trigger]} AS t", $fname ); |
| 701 |
}; |
| 702 |
|
| 703 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 704 |
$this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ ); |
| 705 |
$this->database->cancelAtomic( __METHOD__ ); |
| 706 |
$this->assertLastSql( 'BEGIN; ROLLBACK' ); |
| 707 |
|
| 708 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 709 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 710 |
$this->database->cancelAtomic( __METHOD__ ); |
| 711 |
$this->assertLastSql( 'BEGIN; ROLLBACK' ); |
| 712 |
|
| 713 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 714 |
$this->database->onTransactionResolution( $callback1, __METHOD__ ); |
| 715 |
$this->database->cancelAtomic( __METHOD__ ); |
| 716 |
$this->assertLastSql( 'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' ); |
| 717 |
|
| 718 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 719 |
$this->database->onAtomicSectionCancel( $callback1, __METHOD__ ); |
| 720 |
$this->database->cancelAtomic( __METHOD__ ); |
| 721 |
$this->assertLastSql( 'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' ); |
| 722 |
|
| 723 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 724 |
$this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ ); |
| 725 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 726 |
$this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ ); |
| 727 |
$this->database->cancelAtomic( __METHOD__ ); |
| 728 |
$this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ ); |
| 729 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 730 |
$this->assertLastSql( implode( "; ", [ |
| 731 |
'BEGIN', |
| 732 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 733 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 734 |
'SELECT 0', |
| 735 |
'SELECT 0', |
| 736 |
'COMMIT' |
| 737 |
] ) ); |
| 738 |
|
| 739 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 740 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 741 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 742 |
$this->database->onTransactionCommitOrIdle( $callback2, __METHOD__ ); |
| 743 |
$this->database->cancelAtomic( __METHOD__ ); |
| 744 |
$this->database->onTransactionCommitOrIdle( $callback3, __METHOD__ ); |
| 745 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 746 |
$this->assertLastSql( implode( "; ", [ |
| 747 |
'BEGIN', |
| 748 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 749 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 750 |
'COMMIT', |
| 751 |
'SELECT 1, tCommit AS t', |
| 752 |
'SELECT 3, tCommit AS t' |
| 753 |
] ) ); |
| 754 |
|
| 755 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 756 |
$this->database->onTransactionResolution( $callback1, __METHOD__ ); |
| 757 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 758 |
$this->database->onTransactionResolution( $callback2, __METHOD__ ); |
| 759 |
$this->database->cancelAtomic( __METHOD__ ); |
| 760 |
$this->database->onTransactionResolution( $callback3, __METHOD__ ); |
| 761 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 762 |
$this->assertLastSql( implode( "; ", [ |
| 763 |
'BEGIN', |
| 764 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 765 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 766 |
'COMMIT', |
| 767 |
'SELECT 1, tCommit AS t', |
| 768 |
'SELECT 2, tRollback AS t', |
| 769 |
'SELECT 3, tCommit AS t' |
| 770 |
] ) ); |
| 771 |
|
| 772 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 773 |
$this->database->onAtomicSectionCancel( $callback1, __METHOD__ ); |
| 774 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 775 |
$this->database->onAtomicSectionCancel( $callback2, __METHOD__ ); |
| 776 |
$this->database->cancelAtomic( __METHOD__ ); |
| 777 |
$this->database->onAtomicSectionCancel( $callback3, __METHOD__ ); |
| 778 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 779 |
$this->assertLastSql( implode( "; ", [ |
| 780 |
'BEGIN', |
| 781 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 782 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 783 |
'SELECT 2, tCancel AS t', |
| 784 |
'COMMIT', |
| 785 |
] ) ); |
| 786 |
|
| 787 |
$makeCallback = function ( $id ) use ( $fname, $triggerMap ) { |
| 788 |
return function ( $trigger = '-' ) use ( $id, $fname, $triggerMap ) { |
| 789 |
$this->database->query( "SELECT $id, {$triggerMap[$trigger]} AS t", $fname ); |
| 790 |
}; |
| 791 |
}; |
| 792 |
|
| 793 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 794 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 795 |
$this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ ); |
| 796 |
$this->database->cancelAtomic( __METHOD__ ); |
| 797 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 798 |
$this->assertLastSql( implode( "; ", [ |
| 799 |
'BEGIN', |
| 800 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 801 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 802 |
'COMMIT', |
| 803 |
'SELECT 1, tRollback AS t' |
| 804 |
] ) ); |
| 805 |
|
| 806 |
$this->database->startAtomic( __METHOD__ . '_level1', IDatabase::ATOMIC_CANCELABLE ); |
| 807 |
$this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ ); |
| 808 |
$this->database->startAtomic( __METHOD__ . '_level2' ); |
| 809 |
$this->database->startAtomic( __METHOD__ . '_level3', IDatabase::ATOMIC_CANCELABLE ); |
| 810 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 811 |
$this->database->onTransactionResolution( $makeCallback( 2 ), __METHOD__ ); |
| 812 |
$this->database->endAtomic( __METHOD__ ); |
| 813 |
$this->database->onTransactionResolution( $makeCallback( 3 ), __METHOD__ ); |
| 814 |
$this->database->cancelAtomic( __METHOD__ . '_level3' ); |
| 815 |
$this->database->endAtomic( __METHOD__ . '_level2' ); |
| 816 |
$this->database->onTransactionResolution( $makeCallback( 4 ), __METHOD__ ); |
| 817 |
$this->database->endAtomic( __METHOD__ . '_level1' ); |
| 818 |
$this->assertLastSql( implode( "; ", [ |
| 819 |
'BEGIN', |
| 820 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 821 |
'SAVEPOINT wikimedia_rdbms_atomic2', |
| 822 |
'RELEASE SAVEPOINT wikimedia_rdbms_atomic2', |
| 823 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 824 |
'COMMIT; SELECT 1, tCommit AS t', |
| 825 |
'SELECT 2, tRollback AS t', |
| 826 |
'SELECT 3, tRollback AS t', |
| 827 |
'SELECT 4, tCommit AS t' |
| 828 |
] ) ); |
| 829 |
|
| 830 |
$this->database->startAtomic( __METHOD__ . '_level1', IDatabase::ATOMIC_CANCELABLE ); |
| 831 |
$this->database->onAtomicSectionCancel( $makeCallback( 1 ), __METHOD__ ); |
| 832 |
$this->database->startAtomic( __METHOD__ . '_level2' ); |
| 833 |
$this->database->startAtomic( __METHOD__ . '_level3', IDatabase::ATOMIC_CANCELABLE ); |
| 834 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 835 |
$this->database->onAtomicSectionCancel( $makeCallback( 2 ), __METHOD__ ); |
| 836 |
$this->database->endAtomic( __METHOD__ ); |
| 837 |
$this->database->onAtomicSectionCancel( $makeCallback( 3 ), __METHOD__ ); |
| 838 |
$this->database->cancelAtomic( __METHOD__ . '_level3' ); |
| 839 |
$this->database->endAtomic( __METHOD__ . '_level2' ); |
| 840 |
$this->database->onAtomicSectionCancel( $makeCallback( 4 ), __METHOD__ ); |
| 841 |
$this->database->endAtomic( __METHOD__ . '_level1' ); |
| 842 |
$this->assertLastSql( implode( "; ", [ |
| 843 |
'BEGIN', |
| 844 |
'SAVEPOINT wikimedia_rdbms_atomic1', |
| 845 |
'SAVEPOINT wikimedia_rdbms_atomic2', |
| 846 |
'RELEASE SAVEPOINT wikimedia_rdbms_atomic2', |
| 847 |
'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1', |
| 848 |
'SELECT 2, tCancel AS t', |
| 849 |
'SELECT 3, tCancel AS t', |
| 850 |
'COMMIT', |
| 851 |
] ) ); |
| 852 |
} |
| 853 |
|
| 854 |
public function testAtomicSectionsRecovery() { |
| 855 |
$this->database->begin( __METHOD__ ); |
| 856 |
try { |
| 857 |
$this->database->doAtomicSection( |
| 858 |
__METHOD__, |
| 859 |
function () { |
| 860 |
$this->database->startAtomic( 'inner_func1' ); |
| 861 |
$this->database->startAtomic( 'inner_func2' ); |
| 862 |
|
| 863 |
throw new RuntimeException( 'Test exception' ); |
| 864 |
}, |
| 865 |
IDatabase::ATOMIC_CANCELABLE |
| 866 |
); |
| 867 |
$this->fail( 'Expected exception not thrown' ); |
| 868 |
} catch ( RuntimeException $ex ) { |
| 869 |
$this->assertSame( 'Test exception', $ex->getMessage() ); |
| 870 |
} |
| 871 |
$this->database->commit( __METHOD__ ); |
| 872 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' ); |
| 873 |
|
| 874 |
$this->database->begin( __METHOD__ ); |
| 875 |
try { |
| 876 |
$this->database->doAtomicSection( |
| 877 |
__METHOD__, |
| 878 |
static function () { |
| 879 |
throw new RuntimeException( 'Test exception' ); |
| 880 |
} |
| 881 |
); |
| 882 |
$this->fail( 'Test exception not thrown' ); |
| 883 |
} catch ( RuntimeException $ex ) { |
| 884 |
$this->assertSame( 'Test exception', $ex->getMessage() ); |
| 885 |
} |
| 886 |
try { |
| 887 |
$this->database->commit( __METHOD__ ); |
| 888 |
$this->fail( 'Test exception not thrown' ); |
| 889 |
} catch ( DBTransactionError $ex ) { |
| 890 |
$this->assertSame( |
| 891 |
'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR', |
| 892 |
$ex->getMessage() |
| 893 |
); |
| 894 |
} |
| 895 |
$this->database->rollback( __METHOD__ ); |
| 896 |
$this->assertLastSql( 'BEGIN; ROLLBACK' ); |
| 897 |
} |
| 898 |
|
| 899 |
public function testAtomicSectionsCallbackCancellation() { |
| 900 |
$fname = __METHOD__; |
| 901 |
$callback1Called = null; |
| 902 |
$callback1 = function ( $trigger = '-' ) use ( $fname, &$callback1Called ) { |
| 903 |
$callback1Called = $trigger; |
| 904 |
$this->database->query( "SELECT 1", $fname ); |
| 905 |
}; |
| 906 |
$callback2Called = null; |
| 907 |
$callback2 = function ( $trigger = '-' ) use ( $fname, &$callback2Called ) { |
| 908 |
$callback2Called = $trigger; |
| 909 |
$this->database->query( "SELECT 2", $fname ); |
| 910 |
}; |
| 911 |
$callback3Called = null; |
| 912 |
$callback3 = function ( $trigger = '-' ) use ( $fname, &$callback3Called ) { |
| 913 |
$callback3Called = $trigger; |
| 914 |
$this->database->query( "SELECT 3", $fname ); |
| 915 |
}; |
| 916 |
$callback4Called = 0; |
| 917 |
$callback4 = function () use ( $fname, &$callback4Called ) { |
| 918 |
$callback4Called++; |
| 919 |
$this->database->query( "SELECT 4", $fname ); |
| 920 |
}; |
| 921 |
$callback5Called = 0; |
| 922 |
$callback5 = function () use ( $fname, &$callback5Called ) { |
| 923 |
$callback5Called++; |
| 924 |
$this->database->query( "SELECT 5", $fname ); |
| 925 |
}; |
| 926 |
|
| 927 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 928 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 929 |
$this->database->startAtomic( __METHOD__ . '_inner' ); |
| 930 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 931 |
$this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ ); |
| 932 |
$this->database->onTransactionResolution( $callback3, __METHOD__ ); |
| 933 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 934 |
$this->database->endAtomic( __METHOD__ . '_inner' ); |
| 935 |
$this->database->cancelAtomic( __METHOD__ ); |
| 936 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 937 |
$this->assertNull( $callback1Called ); |
| 938 |
$this->assertNull( $callback2Called ); |
| 939 |
$this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called ); |
| 940 |
$this->assertSame( 1, $callback4Called ); |
| 941 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 4; COMMIT; SELECT 3' ); |
| 942 |
|
| 943 |
$callback1Called = null; |
| 944 |
$callback2Called = null; |
| 945 |
$callback3Called = null; |
| 946 |
$callback4Called = 0; |
| 947 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 948 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 949 |
$this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE ); |
| 950 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 951 |
$this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ ); |
| 952 |
$this->database->onTransactionResolution( $callback3, __METHOD__ ); |
| 953 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 954 |
$this->database->endAtomic( __METHOD__ . '_inner' ); |
| 955 |
$this->database->cancelAtomic( __METHOD__ ); |
| 956 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 957 |
$this->assertNull( $callback1Called ); |
| 958 |
$this->assertNull( $callback2Called ); |
| 959 |
$this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called ); |
| 960 |
$this->assertSame( 1, $callback4Called ); |
| 961 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; RELEASE SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 4; COMMIT; SELECT 3' ); |
| 962 |
|
| 963 |
$callback1Called = null; |
| 964 |
$callback2Called = null; |
| 965 |
$callback3Called = null; |
| 966 |
$callback4Called = 0; |
| 967 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 968 |
$atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 969 |
$this->database->startAtomic( __METHOD__ . '_inner' ); |
| 970 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 971 |
$this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ ); |
| 972 |
$this->database->onTransactionResolution( $callback3, __METHOD__ ); |
| 973 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 974 |
$this->database->cancelAtomic( __METHOD__, $atomicId ); |
| 975 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 976 |
$this->assertNull( $callback1Called ); |
| 977 |
$this->assertNull( $callback2Called ); |
| 978 |
$this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called ); |
| 979 |
$this->assertSame( 1, $callback4Called ); |
| 980 |
|
| 981 |
$callback1Called = null; |
| 982 |
$callback2Called = null; |
| 983 |
$callback3Called = null; |
| 984 |
$callback4Called = 0; |
| 985 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 986 |
$atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 987 |
$this->database->startAtomic( __METHOD__ . '_inner' ); |
| 988 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 989 |
$this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ ); |
| 990 |
$this->database->onTransactionResolution( $callback3, __METHOD__ ); |
| 991 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 992 |
try { |
| 993 |
$this->database->cancelAtomic( __METHOD__ . '_X', $atomicId ); |
| 994 |
} catch ( DBUnexpectedError $e ) { |
| 995 |
$m = __METHOD__; |
| 996 |
$this->assertSame( |
| 997 |
"Invalid atomic section ended (got {$m}_X but expected {$m})", |
| 998 |
$e->getMessage() |
| 999 |
); |
| 1000 |
} |
| 1001 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1002 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 1003 |
$this->assertNull( $callback1Called ); |
| 1004 |
$this->assertNull( $callback2Called ); |
| 1005 |
$this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called ); |
| 1006 |
$this->assertSame( 1, $callback4Called ); |
| 1007 |
|
| 1008 |
$callback4Called = 0; |
| 1009 |
$callback5Called = 0; |
| 1010 |
$this->database->getLastSqls(); |
| 1011 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 1012 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 1013 |
$this->database->onAtomicSectionCancel( $callback5, __METHOD__ ); |
| 1014 |
$this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE ); |
| 1015 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 1016 |
$this->database->cancelAtomic( __METHOD__ . '_inner' ); |
| 1017 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1018 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 1019 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic2; SELECT 4; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 5; COMMIT' ); |
| 1020 |
$this->assertSame( 1, $callback4Called ); |
| 1021 |
$this->assertSame( 1, $callback5Called ); |
| 1022 |
|
| 1023 |
$callback4Called = 0; |
| 1024 |
$callback5Called = 0; |
| 1025 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 1026 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 1027 |
$this->database->onAtomicSectionCancel( $callback5, __METHOD__ ); |
| 1028 |
$this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE ); |
| 1029 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 1030 |
$this->database->endAtomic( __METHOD__ . '_inner' ); |
| 1031 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1032 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 1033 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; RELEASE SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 5; SELECT 4; COMMIT' ); |
| 1034 |
$this->assertSame( 1, $callback4Called ); |
| 1035 |
$this->assertSame( 1, $callback5Called ); |
| 1036 |
|
| 1037 |
$callback4Called = 0; |
| 1038 |
$callback5Called = 0; |
| 1039 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 1040 |
$sectionId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 1041 |
$this->database->onAtomicSectionCancel( $callback5, __METHOD__ ); |
| 1042 |
$this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE ); |
| 1043 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 1044 |
$this->database->cancelAtomic( __METHOD__, $sectionId ); |
| 1045 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 1046 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 5; SELECT 4; COMMIT' ); |
| 1047 |
$this->assertSame( 1, $callback4Called ); |
| 1048 |
$this->assertSame( 1, $callback5Called ); |
| 1049 |
|
| 1050 |
$wrapper = TestingAccessWrapper::newFromObject( $this->database ); |
| 1051 |
$callback1Called = null; |
| 1052 |
$callback2Called = null; |
| 1053 |
$callback3Called = null; |
| 1054 |
$callback4Called = 0; |
| 1055 |
$this->database->startAtomic( __METHOD__ . '_outer' ); |
| 1056 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 1057 |
$this->database->startAtomic( __METHOD__ . '_inner' ); |
| 1058 |
$this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ ); |
| 1059 |
$this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ ); |
| 1060 |
$this->database->onTransactionResolution( $callback3, __METHOD__ ); |
| 1061 |
$this->database->onAtomicSectionCancel( $callback4, __METHOD__ ); |
| 1062 |
$wrapper->transactionManager->setTransactionError( new DBUnexpectedError( null, 'error' ) ); |
| 1063 |
$this->database->cancelAtomic( __METHOD__ . '_inner' ); |
| 1064 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1065 |
$this->database->endAtomic( __METHOD__ . '_outer' ); |
| 1066 |
$this->assertNull( $callback1Called ); |
| 1067 |
$this->assertNull( $callback2Called ); |
| 1068 |
$this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called ); |
| 1069 |
$this->assertSame( 1, $callback4Called ); |
| 1070 |
} |
| 1071 |
|
| 1072 |
public function testAtomicSectionsTrxRound() { |
| 1073 |
$this->database->setFlag( IDatabase::DBO_TRX ); |
| 1074 |
$this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 1075 |
$this->database->query( 'SELECT 1', __METHOD__ ); |
| 1076 |
$this->database->endAtomic( __METHOD__ ); |
| 1077 |
$this->database->commit( __METHOD__, IDatabase::FLUSHING_ALL_PEERS ); |
| 1078 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SELECT 1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' ); |
| 1079 |
} |
| 1080 |
|
| 1081 |
public static function provideAtomicSectionMethodsForErrors() { |
| 1082 |
return [ |
| 1083 |
[ 'endAtomic' ], |
| 1084 |
[ 'cancelAtomic' ], |
| 1085 |
]; |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
public function testNoAtomicSection( $method ) { |
| 1092 |
try { |
| 1093 |
$this->database->$method( __METHOD__ ); |
| 1094 |
$this->fail( 'Expected exception not thrown' ); |
| 1095 |
} catch ( DBUnexpectedError $ex ) { |
| 1096 |
$this->assertSame( |
| 1097 |
'No atomic section is open (got ' . __METHOD__ . ')', |
| 1098 |
$ex->getMessage() |
| 1099 |
); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
public function testInvalidAtomicSectionEnded( $method ) { |
| 1107 |
$this->database->startAtomic( __METHOD__ . 'X' ); |
| 1108 |
try { |
| 1109 |
$this->database->$method( __METHOD__ ); |
| 1110 |
$this->fail( 'Expected exception not thrown' ); |
| 1111 |
} catch ( DBUnexpectedError $ex ) { |
| 1112 |
$this->assertSame( |
| 1113 |
'Invalid atomic section ended (got ' . __METHOD__ . ' but expected ' . |
| 1114 |
__METHOD__ . 'X)', |
| 1115 |
$ex->getMessage() |
| 1116 |
); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
public function testUncancellableAtomicSection() { |
| 1121 |
$this->database->startAtomic( __METHOD__ ); |
| 1122 |
try { |
| 1123 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1124 |
$this->database->select( 'test', '1', [], __METHOD__ ); |
| 1125 |
$this->fail( 'Expected exception not thrown' ); |
| 1126 |
} catch ( DBTransactionError $ex ) { |
| 1127 |
$this->assertSame( |
| 1128 |
'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR', |
| 1129 |
$ex->getMessage() |
| 1130 |
); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
public function testNoAtomicSectionForCallback() { |
| 1135 |
try { |
| 1136 |
$this->database->onAtomicSectionCancel( static function () { |
| 1137 |
}, __METHOD__ ); |
| 1138 |
$this->fail( 'Expected exception not thrown' ); |
| 1139 |
} catch ( DBUnexpectedError $ex ) { |
| 1140 |
$this->assertSame( |
| 1141 |
'No atomic section is open (got ' . __METHOD__ . ')', |
| 1142 |
$ex->getMessage() |
| 1143 |
); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
public function testTransactionErrorState1() { |
| 1148 |
$wrapper = TestingAccessWrapper::newFromObject( $this->database ); |
| 1149 |
|
| 1150 |
$this->database->begin( __METHOD__ ); |
| 1151 |
$wrapper->transactionManager->setTransactionError( new DBUnexpectedError( null, 'error' ) ); |
| 1152 |
$this->expectException( DBTransactionStateError::class ); |
| 1153 |
$this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ ); |
| 1154 |
} |
| 1155 |
|
| 1156 |
public function testTransactionErrorState2() { |
| 1157 |
$wrapper = TestingAccessWrapper::newFromObject( $this->database ); |
| 1158 |
|
| 1159 |
|
| 1160 |
$this->database->startAtomic( __METHOD__ ); |
| 1161 |
$wrapper->transactionManager->setTransactionError( new DBUnexpectedError( null, 'error' ) ); |
| 1162 |
$this->database->rollback( __METHOD__ ); |
| 1163 |
$this->assertSame( 0, $this->database->trxLevel() ); |
| 1164 |
$this->assertEquals( TransactionManager::STATUS_TRX_NONE, $wrapper->trxStatus() ); |
| 1165 |
$this->assertLastSql( 'BEGIN; ROLLBACK' ); |
| 1166 |
|
| 1167 |
$this->database->startAtomic( __METHOD__ ); |
| 1168 |
$this->assertEquals( TransactionManager::STATUS_TRX_OK, $wrapper->trxStatus() ); |
| 1169 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1170 |
$this->database->endAtomic( __METHOD__ ); |
| 1171 |
$this->assertEquals( TransactionManager::STATUS_TRX_NONE, $wrapper->trxStatus() ); |
| 1172 |
$this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = 1; COMMIT' ); |
| 1173 |
$this->assertSame( 0, $this->database->trxLevel(), 'Use after rollback()' ); |
| 1174 |
|
| 1175 |
$this->database->begin( __METHOD__ ); |
| 1176 |
$this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE ); |
| 1177 |
$this->database->update( 'y', [ 'a' => 1 ], [ 'field' => 1 ], __METHOD__ ); |
| 1178 |
$wrapper->transactionManager->setTransactionError( new DBUnexpectedError( null, 'error' ) ); |
| 1179 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1180 |
$this->assertEquals( TransactionManager::STATUS_TRX_OK, $wrapper->trxStatus() ); |
| 1181 |
$this->database->startAtomic( __METHOD__ ); |
| 1182 |
$this->database->delete( 'y', [ 'field' => 1 ], __METHOD__ ); |
| 1183 |
$this->database->endAtomic( __METHOD__ ); |
| 1184 |
$this->database->commit( __METHOD__ ); |
| 1185 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; UPDATE y SET a = 1 WHERE field = 1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; DELETE FROM y WHERE field = 1; COMMIT' ); |
| 1186 |
$this->assertSame( 0, $this->database->trxLevel(), 'Use after rollback()' ); |
| 1187 |
|
| 1188 |
|
| 1189 |
$this->database->startAtomic( __METHOD__ ); |
| 1190 |
$this->assertEquals( TransactionManager::STATUS_TRX_OK, $wrapper->trxStatus() ); |
| 1191 |
$this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ ); |
| 1192 |
$this->database->endAtomic( __METHOD__ ); |
| 1193 |
$this->assertEquals( TransactionManager::STATUS_TRX_NONE, $wrapper->trxStatus() ); |
| 1194 |
$this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = 3; COMMIT' ); |
| 1195 |
$this->assertSame( 0, $this->database->trxLevel() ); |
| 1196 |
} |
| 1197 |
|
| 1198 |
public function testImplicitTransactionRollback() { |
| 1199 |
$doError = function () { |
| 1200 |
$this->database->forceNextResult( false, 666, 'Evilness' ); |
| 1201 |
try { |
| 1202 |
$this->database->delete( 'error', '1', __CLASS__ . '::SomeCaller' ); |
| 1203 |
$this->fail( 'Expected exception not thrown' ); |
| 1204 |
} catch ( DBError $e ) { |
| 1205 |
$this->assertSame( 666, $e->errno ); |
| 1206 |
} |
| 1207 |
}; |
| 1208 |
|
| 1209 |
$this->database->setFlag( Database::DBO_TRX ); |
| 1210 |
|
| 1211 |
|
| 1212 |
$this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL ); |
| 1213 |
$doError(); |
| 1214 |
try { |
| 1215 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1216 |
$this->fail( 'Expected exception not thrown' ); |
| 1217 |
} catch ( DBTransactionError $e ) { |
| 1218 |
$this->assertEquals( |
| 1219 |
'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR', |
| 1220 |
$e->getMessage() |
| 1221 |
); |
| 1222 |
} |
| 1223 |
try { |
| 1224 |
$this->database->commit( __METHOD__, Database::FLUSHING_INTERNAL ); |
| 1225 |
$this->fail( 'Expected exception not thrown' ); |
| 1226 |
} catch ( DBTransactionError $e ) { |
| 1227 |
$this->assertEquals( |
| 1228 |
'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR', |
| 1229 |
$e->getMessage() |
| 1230 |
); |
| 1231 |
} |
| 1232 |
$this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL ); |
| 1233 |
$this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; ROLLBACK' ); |
| 1234 |
|
| 1235 |
|
| 1236 |
$this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL ); |
| 1237 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1238 |
$doError(); |
| 1239 |
try { |
| 1240 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1241 |
$this->fail( 'Expected exception not thrown' ); |
| 1242 |
} catch ( DBTransactionStateError $e ) { |
| 1243 |
} |
| 1244 |
$this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL ); |
| 1245 |
|
| 1246 |
$this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = 1; DELETE FROM error WHERE 1; ROLLBACK' ); |
| 1247 |
} |
| 1248 |
|
| 1249 |
public function testTransactionStatementRollbackIgnoring() { |
| 1250 |
$wrapper = TestingAccessWrapper::newFromObject( $this->database ); |
| 1251 |
$warning = []; |
| 1252 |
$wrapper->deprecationLogger = static function ( $msg ) use ( &$warning ) { |
| 1253 |
$warning[] = $msg; |
| 1254 |
}; |
| 1255 |
|
| 1256 |
$doError = function () { |
| 1257 |
$this->database->forceNextResult( |
| 1258 |
false, |
| 1259 |
666, |
| 1260 |
'Evilness', |
| 1261 |
[ 'isKnownStatementRollbackError' => true ] |
| 1262 |
); |
| 1263 |
try { |
| 1264 |
$this->database->delete( 'error', '1', __CLASS__ . '::SomeCaller' ); |
| 1265 |
$this->fail( 'Expected exception not thrown' ); |
| 1266 |
} catch ( DBError $e ) { |
| 1267 |
$this->assertSame( 666, $e->errno ); |
| 1268 |
} |
| 1269 |
}; |
| 1270 |
$expectWarning = 'Caller from ' . __METHOD__ . |
| 1271 |
' ignored an error originally raised from ' . __CLASS__ . '::SomeCaller: [666] Evilness'; |
| 1272 |
|
| 1273 |
|
| 1274 |
$warning = []; |
| 1275 |
$this->database->startAtomic( __METHOD__ ); |
| 1276 |
$doError(); |
| 1277 |
$this->database->rollback( __METHOD__ ); |
| 1278 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1279 |
$this->assertSame( [], $warning ); |
| 1280 |
|
| 1281 |
$this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; ROLLBACK; DELETE FROM x WHERE field = 1' ); |
| 1282 |
|
| 1283 |
|
| 1284 |
$warning = []; |
| 1285 |
$this->database->begin( __METHOD__ ); |
| 1286 |
$this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE ); |
| 1287 |
$doError(); |
| 1288 |
$this->database->cancelAtomic( __METHOD__ ); |
| 1289 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1290 |
$this->database->commit( __METHOD__ ); |
| 1291 |
$this->assertSame( [], $warning ); |
| 1292 |
|
| 1293 |
$this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; DELETE FROM error WHERE 1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; DELETE FROM x WHERE field = 1; COMMIT' ); |
| 1294 |
|
| 1295 |
|
| 1296 |
$warning = []; |
| 1297 |
$this->database->begin( __METHOD__ ); |
| 1298 |
$doError(); |
| 1299 |
$this->database->commit( __METHOD__ ); |
| 1300 |
$this->assertSame( [ $expectWarning ], $warning ); |
| 1301 |
$this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; COMMIT' ); |
| 1302 |
|
| 1303 |
|
| 1304 |
$warning = []; |
| 1305 |
$this->database->begin( __METHOD__ ); |
| 1306 |
$doError(); |
| 1307 |
$this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ ); |
| 1308 |
$this->database->commit( __METHOD__ ); |
| 1309 |
$this->assertSame( [ $expectWarning ], $warning ); |
| 1310 |
|
| 1311 |
$this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; DELETE FROM x WHERE field = 1; COMMIT' ); |
| 1312 |
} |
| 1313 |
|
| 1314 |
public function testPrematureClose1() { |
| 1315 |
$fname = __METHOD__; |
| 1316 |
$this->database->begin( __METHOD__ ); |
| 1317 |
$this->database->onTransactionCommitOrIdle( function () use ( $fname ) { |
| 1318 |
$this->database->query( 'SELECT 1', $fname ); |
| 1319 |
} ); |
| 1320 |
$this->database->onTransactionResolution( function () use ( $fname ) { |
| 1321 |
$this->database->query( 'SELECT 2', $fname ); |
| 1322 |
} ); |
| 1323 |
$this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ ); |
| 1324 |
$this->database->close(); |
| 1325 |
|
| 1326 |
$this->assertFalse( $this->database->isOpen() ); |
| 1327 |
$this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = 3; ROLLBACK; SELECT 2' ); |
| 1328 |
$this->assertSame( 0, $this->database->trxLevel() ); |
| 1329 |
} |
| 1330 |
|
| 1331 |
public function testPrematureClose2() { |
| 1332 |
$fname = __METHOD__; |
| 1333 |
$this->database->startAtomic( __METHOD__ ); |
| 1334 |
$this->database->onTransactionCommitOrIdle( function () use ( $fname ) { |
| 1335 |
$this->database->query( 'SELECT 1', $fname ); |
| 1336 |
} ); |
| 1337 |
$this->database->onAtomicSectionCancel( function () use ( $fname ) { |
| 1338 |
$this->database->query( 'SELECT 2', $fname ); |
| 1339 |
} ); |
| 1340 |
$this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ ); |
| 1341 |
$this->database->close(); |
| 1342 |
|
| 1343 |
$this->assertFalse( $this->database->isOpen() ); |
| 1344 |
$this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = 3; ROLLBACK; SELECT 2' ); |
| 1345 |
$this->assertSame( 0, $this->database->trxLevel() ); |
| 1346 |
} |
| 1347 |
|
| 1348 |
public function testPrematureClose3() { |
| 1349 |
$this->database->setFlag( IDatabase::DBO_TRX ); |
| 1350 |
$this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ ); |
| 1351 |
$this->assertSame( 1, $this->database->trxLevel() ); |
| 1352 |
$this->database->close(); |
| 1353 |
|
| 1354 |
$this->assertFalse( $this->database->isOpen() ); |
| 1355 |
$this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = 3; ROLLBACK' ); |
| 1356 |
$this->assertSame( 0, $this->database->trxLevel() ); |
| 1357 |
} |
| 1358 |
|
| 1359 |
public function testPrematureClose4() { |
| 1360 |
$this->database->setFlag( IDatabase::DBO_TRX ); |
| 1361 |
$this->database->query( 'SELECT 1', __METHOD__ ); |
| 1362 |
$this->assertSame( 1, $this->database->trxLevel() ); |
| 1363 |
$this->database->close(); |
| 1364 |
$this->database->clearFlag( IDatabase::DBO_TRX ); |
| 1365 |
|
| 1366 |
$this->assertFalse( $this->database->isOpen() ); |
| 1367 |
$this->assertLastSql( 'BEGIN; SELECT 1; ROLLBACK' ); |
| 1368 |
$this->assertSame( 0, $this->database->trxLevel() ); |
| 1369 |
} |
| 1370 |
|
| 1371 |
public function testSelectFieldValues() { |
| 1372 |
$this->database->forceNextResult( [ |
| 1373 |
(object)[ 'value' => 'row1' ], |
| 1374 |
(object)[ 'value' => 'row2' ], |
| 1375 |
(object)[ 'value' => 'row3' ], |
| 1376 |
] ); |
| 1377 |
|
| 1378 |
$this->assertSame( |
| 1379 |
[ 'row1', 'row2', 'row3' ], |
| 1380 |
$this->database->selectFieldValues( 'table', 'table.field', 'conds', __METHOD__ ) |
| 1381 |
); |
| 1382 |
$this->assertLastSql( 'SELECT table.field AS value FROM table WHERE conds' ); |
| 1383 |
} |
| 1384 |
} |