Debian Codegraph @

tests/phpunit/integration/includes/db/DatabaseMysqlTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\Rdbms\ChangedTablesTracker;
5 use Wikimedia\Rdbms\DatabaseMySQL;
6 use Wikimedia\Rdbms\DBError;
7 use Wikimedia\Rdbms\DBQueryDisconnectedError;
8 use Wikimedia\Rdbms\DBQueryError;
9 use Wikimedia\Rdbms\DBQueryTimeoutError;
10 use Wikimedia\Rdbms\DBSessionStateError;
11 use Wikimedia\Rdbms\DBTransactionStateError;
12 use Wikimedia\Rdbms\DBUnexpectedError;
13 use Wikimedia\Rdbms\IDatabase;
14 use Wikimedia\Rdbms\Platform\ISQLPlatform;
15 use Wikimedia\Rdbms\Platform\SQLPlatform;
16 use Wikimedia\Rdbms\Query;
17 use Wikimedia\Rdbms\TransactionManager;
18
19 /**
20 * @covers \Wikimedia\Rdbms\Database
21 * @covers \Wikimedia\Rdbms\DatabaseMySQL
22 * @covers \Wikimedia\Rdbms\Platform\MySQLPlatform
23 * @group mysql
24 * @group Database
25 * @group medium
26 * @requires extension mysqli
27 */
28 class DatabaseMysqlTest extends \MediaWikiIntegrationTestCase {
29 /** @var DatabaseMySQL */
30 protected $conn;
31
32 protected function setUp(): void {
33 parent::setUp();
34
35 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
36 if ( $lb->getServerType( 0 ) !== 'mysql' ) {
37 $this->markTestSkipped( 'No MySQL $wgLBFactoryConf config detected' );
38 }
39
40 $this->conn = $this->newConnection();
41 // FIXME: Tables used by this test aren't parsed correctly, see T344510.
42 ChangedTablesTracker::stopTracking();
43 }
44
45 protected function tearDown(): void {
46 if ( $this->conn ) {
47 $this->conn->close( __METHOD__ );
48 ChangedTablesTracker::startTracking();
49 }
50
51 parent::tearDown();
52 }
53
54 public function testQueryTimeout() {
55 try {
56 $res = $this->conn->query(
57 'SET STATEMENT max_statement_time=0.001 FOR SELECT sleep(1) FROM dual',
58 __METHOD__
59 );
60 // if the query did not time out, there should be a single row where sleep() returned 1
61 $this->assertSame( 1, $res->numRows() );
62 $row = $res->fetchRow();
63 $this->assertSame( '1', (string)reset( $row ) );
64 } catch ( DBQueryTimeoutError $e ) {
65 $this->assertInstanceOf( DBQueryTimeoutError::class, $e );
66 }
67
68 $row = $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
69 $this->assertSame( 'x', $row->v, "Still connected/usable" );
70 }
71
72 public function testConnectionKill() {
73 try {
74 $this->conn->query( 'KILL (SELECT connection_id())', __METHOD__ );
75 $this->fail( "No DBQueryDisconnectedError caught" );
76 } catch ( DBQueryDisconnectedError $e ) {
77 $this->assertInstanceOf( DBQueryDisconnectedError::class, $e );
78 }
79
80 $row = $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
81 $this->assertSame( 'x', $row->v, "Recovered" );
82 }
83
84 public function testConnectionLossQuery() {
85 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
86 $encId = intval( $row->id );
87
88 $adminConn = $this->newConnection();
89 $adminConn->query( "KILL $encId", __METHOD__ );
90
91 $row = $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
92 $this->assertSame( 'x', $row->v, "Recovered" );
93
94 $this->conn->startAtomic( __METHOD__ );
95 $this->assertSame( 1, $this->conn->trxLevel(), "Transaction exists" );
96 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
97 $encId = intval( $row->id );
98
99 $adminConn->query( "KILL $encId", __METHOD__ );
100 try {
101 $this->conn->query( 'SELECT 1', __METHOD__ );
102 $this->fail( "No DBQueryDisconnectedError caught" );
103 } catch ( DBQueryDisconnectedError $e ) {
104 $this->assertTrue( $this->conn->isOpen(), "Reconnected" );
105 try {
106 $this->conn->endAtomic( __METHOD__ );
107 $this->fail( "No DBUnexpectedError caught" );
108 } catch ( DBUnexpectedError $e ) {
109 $this->assertInstanceOf( DBUnexpectedError::class, $e );
110 }
111
112 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
113 try {
114 $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
115 $this->fail( "No DBTransactionStateError caught" );
116 } catch ( DBTransactionStateError $e ) {
117 $this->assertInstanceOf( DBTransactionStateError::class, $e );
118 }
119
120 $this->assertSame( 0, $this->conn->trxLevel(), "Transaction lost" );
121 $this->conn->rollback( __METHOD__ );
122 $this->assertSame( 0, $this->conn->trxLevel(), "No transaction" );
123
124 $row = $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
125 $this->assertSame( 'x', $row->v, "Recovered" );
126 }
127
128 $this->conn->lock( 'session_lock_' . mt_rand(), __METHOD__, 0 );
129 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
130 $encId = intval( $row->id );
131 $adminConn->query( "KILL $encId", __METHOD__ );
132 try {
133 $this->conn->query( 'SELECT 1', __METHOD__ );
134 $this->fail( "No DBQueryDisconnectedError caught" );
135 } catch ( DBQueryDisconnectedError $e ) {
136 $this->assertInstanceOf( DBQueryDisconnectedError::class, $e );
137 }
138
139 $this->assertTrue( $this->conn->isOpen(), "Reconnected" );
140 try {
141 $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
142 $this->fail( "No DBSessionStateError caught" );
143 } catch ( DBSessionStateError $e ) {
144 $this->assertInstanceOf( DBSessionStateError::class, $e );
145 }
146
147 $this->assertTrue( $this->conn->isOpen(), "Connection remains" );
148 $this->conn->rollback( __METHOD__ );
149 $this->conn->flushSession( __METHOD__ );
150
151 $row = $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
152 $this->assertSame( 'x', $row->v, "Recovered" );
153
154 $adminConn->close( __METHOD__ );
155 }
156
157 public function testConnectionLossSnapshotFlush() {
158 $fakeWriteQuery = new Query( 'SELECT 1', SQLPlatform::QUERY_CHANGE_ROWS, 'SELECT' );
159
160 $this->conn->begin( __METHOD__, IDatabase::TRANSACTION_INTERNAL );
161 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
162 $encId = intval( $row->id );
163
164 $adminConn = $this->newConnection();
165 $adminConn->query( "KILL $encId", __METHOD__ );
166
167 $this->conn->flushSnapshot( __METHOD__ );
168 $this->assertSame( 0, $this->conn->trxLevel(), "Lost connection during snapshot flush" );
169 $this->assertSame( TransactionManager::STATUS_TRX_NONE, $this->conn->trxStatus() );
170
171 $this->conn->begin( __METHOD__, IDatabase::TRANSACTION_INTERNAL );
172 $this->conn->query( $fakeWriteQuery, __METHOD__ );
173
174 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
175 $encId = intval( $row->id );
176 $adminConn->query( "KILL $encId", __METHOD__ );
177
178 try {
179 $this->conn->query( $fakeWriteQuery, __METHOD__ );
180 $this->fail( "Error thrown due to connection loss with pending writes" );
181 } catch ( DBError $e ) {
182 $this->assertInstanceOf( DBQueryDisconnectedError::class, $e );
183 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
184 }
185
186 try {
187 $this->conn->query( 'SELECT 1', __METHOD__ );
188 $this->fail( "Error thrown due to unacknowledged transaction error" );
189 } catch ( DBError $e ) {
190 $this->assertInstanceOf( DBTransactionStateError::class, $e );
191 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
192 }
193
194 $this->conn->flushSnapshot( __METHOD__ );
195 $this->assertSame( 0, $this->conn->trxLevel(), "Snapshot flush after lost writes" );
196 $this->assertSame( TransactionManager::STATUS_TRX_NONE, $this->conn->trxStatus() );
197
198 // Get a lock outside of any transaction
199 $unlocker = $this->conn->getScopedLockAndFlush( 'testing-key', __METHOD__, 0 );
200 // Start transaction after getting the lock
201 $this->conn->begin( __METHOD__, IDatabase::TRANSACTION_INTERNAL );
202
203 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
204 $encId = intval( $row->id );
205 $adminConn->query( "KILL $encId", __METHOD__ );
206
207 try {
208 $this->conn->query( 'SELECT 1', __METHOD__ );
209 $this->fail( "Error thrown due to connection loss with locks" );
210 } catch ( DBError $e ) {
211 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
212 }
213
214 $this->conn->flushSnapshot( __METHOD__ );
215 $this->assertSame( 0, $this->conn->trxLevel(), "Snapshot flush with lost lock" );
216
217 try {
218 $this->conn->query( 'SELECT 1', __METHOD__ );
219 $this->fail( "Error thrown due to unacknowledged session error" );
220 } catch ( DBError $e ) {
221 $this->assertInstanceOf( DBSessionStateError::class, $e );
222 }
223
224 $adminConn->close( __METHOD__ );
225 }
226
227 public function testConnectionLossScopedLock() {
228 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
229 $encId = intval( $row->id );
230
231 try {
232 ( function () use ( $encId ) {
233 $unlocker = $this->conn->getScopedLockAndFlush( 'x', 'fn', 1 );
234
235 $adminConn = $this->newConnection();
236 $adminConn->query( "KILL $encId" );
237
238 $this->conn->query( "SELECT 1" );
239 } )();
240 $this->fail( "Expected DBQueryDisconnectedError" );
241 } catch ( DBQueryDisconnectedError $e ) {
242 // This should report the explicit query that failed,
243 // instead of the (later) implicit query from the $unlocker deref.
244 $this->assertStringContainsString( "SELECT 1", $e->getMessage() );
245 }
246
247 $this->conn->rollback( __METHOD__ );
248 $this->conn->unlock( 'x', __METHOD__ );
249 }
250
251 public function testTransactionError() {
252 $this->assertSame( TransactionManager::STATUS_TRX_NONE, $this->conn->trxStatus() );
253
254 $this->conn->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
255 $this->assertSame( TransactionManager::STATUS_TRX_OK, $this->conn->trxStatus() );
256 $row = $this->conn->query( 'SELECT connection_id() AS id', __METHOD__ )->fetchObject();
257 $encId = intval( $row->id );
258
259 try {
260 $this->conn->lock( 'trx_lock_' . mt_rand(), __METHOD__, 0 );
261 $this->assertSame( TransactionManager::STATUS_TRX_OK, $this->conn->trxStatus() );
262 $this->conn->query( "SELECT invalid query()", __METHOD__ );
263 $this->fail( "No DBQueryError caught" );
264 } catch ( DBQueryError $e ) {
265 $this->assertInstanceOf( DBQueryError::class, $e );
266 }
267
268 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
269 try {
270 $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
271 $this->fail( "No DBTransactionStateError caught" );
272 } catch ( DBTransactionStateError $e ) {
273 $this->assertInstanceOf( DBTransactionStateError::class, $e );
274 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
275 $this->assertSame( 1, $this->conn->trxLevel(), "Transaction remains" );
276 $this->assertTrue( $this->conn->isOpen(), "Connection remains" );
277 }
278
279 $adminConn = $this->newConnection();
280 $adminConn->query( "KILL $encId", __METHOD__ );
281
282 $this->assertSame( TransactionManager::STATUS_TRX_ERROR, $this->conn->trxStatus() );
283 try {
284 $this->conn->query( 'SELECT 1', __METHOD__ );
285 $this->fail( "No DBTransactionStateError caught" );
286 } catch ( DBTransactionStateError $e ) {
287 $this->assertInstanceOf( DBTransactionStateError::class, $e );
288 }
289
290 $this->assertSame(
291 1,
292 $this->conn->trxLevel(),
293 "Transaction loss not yet detected (due to STATUS_TRX_ERROR)"
294 );
295 $this->assertTrue(
296 $this->conn->isOpen(),
297 "Connection loss not detected (due to STATUS_TRX_ERROR)"
298 );
299
300 $this->conn->cancelAtomic( __METHOD__ );
301 $this->assertSame( 0, $this->conn->trxLevel(), "No transaction remains" );
302 $this->assertTrue( $this->conn->isOpen(), "Reconnected" );
303
304 $row = $this->conn->query( 'SELECT "x" AS v', __METHOD__ )->fetchObject();
305 $this->assertSame( 'x', $row->v, "Recovered" );
306
307 $this->conn->rollback( __METHOD__ );
308
309 $adminConn->close( __METHOD__ );
310 }
311
312 private function newConnection(): DatabaseMySQL {
313 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
314 $dbFactory = MediaWikiServices::getInstance()->getDatabaseFactory();
315 /** @var DatabaseMySQL $conn */
316 $conn = $dbFactory->create(
317 'mysql',
318 array_merge(
319 $lb->getServerInfo( 0 ),
320 [
321 'dbname' => null,
322 'schema' => null,
323 'tablePrefix' => '',
324 ]
325 )
326 );
327
328 return $conn;
329 }
330
331 public function testInsertIdAfterInsert() {
332 $dTable = $this->createDestTable();
333
334 $rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
335
336 $this->conn->insert( $dTable, $rows, __METHOD__ );
337 $this->assertSame( 1, $this->conn->affectedRows() );
338 $this->assertSame( 1, $this->conn->insertId() );
339
340 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
341 $this->assertSame( 1, $this->conn->affectedRows() );
342 $this->assertSame( 0, $this->conn->insertId() );
343
344 $this->dropDestTable();
345 }
346
347 public function testInsertIdAfterInsertIgnore() {
348 $dTable = $this->createDestTable();
349
350 $rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
351
352 $this->conn->insert( $dTable, $rows, __METHOD__, 'IGNORE' );
353 $this->assertSame( 1, $this->conn->affectedRows() );
354 $this->assertSame( 1, $this->conn->insertId() );
355 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
356
357 $this->conn->insert( $dTable, $rows, __METHOD__, 'IGNORE' );
358 $this->assertSame( 0, $this->conn->affectedRows() );
359 $this->assertSame( 0, $this->conn->insertId() );
360
361 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
362 $this->assertSame( 1, $this->conn->affectedRows() );
363 $this->assertSame( 0, $this->conn->insertId() );
364
365 $this->dropDestTable();
366 }
367
368 public function testInsertIdAfterReplace() {
369 $dTable = $this->createDestTable();
370
371 $rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
372
373 $this->conn->replace( $dTable, 'k', $rows, __METHOD__ );
374 $this->assertSame( 1, $this->conn->affectedRows() );
375 $this->assertSame( 1, $this->conn->insertId() );
376 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
377
378 $this->conn->replace( $dTable, 'k', $rows, __METHOD__ );
379 $this->assertSame( 1, $this->conn->affectedRows() );
380 $this->assertSame( 2, $this->conn->insertId() );
381
382 $this->assertSame( 2, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
383 $this->assertSame( 1, $this->conn->affectedRows() );
384 $this->assertSame( 0, $this->conn->insertId() );
385
386 $this->dropDestTable();
387 }
388
389 public function testInsertIdAfterUpsert() {
390 $dTable = $this->createDestTable();
391
392 $rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
393 $set = [
394 'v = ' . $this->conn->buildExcludedValue( 'v' ),
395 't = ' . $this->conn->buildExcludedValue( 't' ) . ' + 1'
396 ];
397
398 $this->conn->upsert( $dTable, $rows, 'k', $set, __METHOD__ );
399 $this->assertSame( 1, $this->conn->affectedRows() );
400 $this->assertSame( 1, $this->conn->insertId() );
401 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
402
403 $this->conn->upsert( $dTable, $rows, 'k', $set, __METHOD__ );
404 $this->assertSame( 1, $this->conn->affectedRows() );
405 $this->assertSame( 1, $this->conn->insertId() );
406
407 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
408 $this->assertSame( 1, $this->conn->affectedRows() );
409 $this->assertSame( 0, $this->conn->insertId() );
410
411 $this->dropDestTable();
412 }
413
414 public function testInsertIdAfterInsertSelect() {
415 $sTable = $this->createSourceTable();
416 $dTable = $this->createDestTable();
417
418 $rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ];
419 $this->conn->insert( $sTable, $rows, __METHOD__, 'IGNORE' );
420 $this->assertSame( 1, $this->conn->affectedRows() );
421 $this->assertSame( 1, $this->conn->insertId() );
422 $this->assertSame( 1, (int)$this->conn->selectField( $sTable, 'sn', [ 'sk' => 'Luca' ] ) );
423
424 $this->conn->insertSelect(
425 $dTable,
426 $sTable,
427 [ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ],
428 [ 'sk' => 'Luca' ],
429 __METHOD__,
430 'IGNORE'
431 );
432 $this->assertSame( 1, $this->conn->affectedRows() );
433 $this->assertSame( 1, $this->conn->insertId() );
434
435 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
436 $this->assertSame( 1, $this->conn->affectedRows() );
437 $this->assertSame( 0, $this->conn->insertId() );
438
439 $this->dropSourceTable();
440 $this->dropDestTable();
441 }
442
443 public function testInsertIdAfterInsertSelectIgnore() {
444 $sTable = $this->createSourceTable();
445 $dTable = $this->createDestTable();
446
447 $rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ];
448 $this->conn->insert( $sTable, $rows, __METHOD__, 'IGNORE' );
449 $this->assertSame( 1, $this->conn->affectedRows() );
450 $this->assertSame( 1, $this->conn->insertId() );
451 $this->assertSame( 1, (int)$this->conn->selectField( $sTable, 'sn', [ 'sk' => 'Luca' ] ) );
452
453 $this->conn->insertSelect(
454 $dTable,
455 $sTable,
456 [ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ],
457 [ 'sk' => 'Luca' ],
458 __METHOD__,
459 'IGNORE'
460 );
461 $this->assertSame( 1, $this->conn->affectedRows() );
462 $this->assertSame( 1, $this->conn->insertId() );
463 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
464
465 $this->conn->insertSelect(
466 $dTable,
467 $sTable,
468 [ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ],
469 [ 'sk' => 'Luca' ],
470 __METHOD__,
471 'IGNORE'
472 );
473 $this->assertSame( 0, $this->conn->affectedRows() );
474 $this->assertSame( 0, $this->conn->insertId() );
475
476 $this->assertSame( 1, (int)$this->conn->selectField( $dTable, 'n', [ 'k' => 'Luca' ] ) );
477 $this->assertSame( 1, $this->conn->affectedRows() );
478 $this->assertSame( 0, $this->conn->insertId() );
479
480 $this->dropSourceTable();
481 $this->dropDestTable();
482 }
483
484 /**
485 * @coversNothing
486 */
487 public function testAffectedRowsAfterUpdateIgnore() {
488 $sTable = $this->createSourceTable();
489
490 $rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ],
491 [ 'sk' => 'Test', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ];
492 $this->conn->insert( $sTable, $rows, __METHOD__, 'IGNORE' );
493 $this->assertSame( 2, $this->conn->affectedRows(), 'Test inserted' );
494
495 // Test changing something
496 $this->conn->update(
497 $sTable,
498 [ 'sk' => 'TestRow' ],
499 [ 'sk' => 'Test' ],
500 __METHOD__,
501 [ 'IGNORE' ]
502 );
503 $this->assertSame( 1, $this->conn->affectedRows(), 'Updated' );
504
505 // Test changing nothing
506 $this->conn->update(
507 $sTable,
508 [ 'sk' => 'TestRow' ],
509 [ 'sk' => 'TestRow' ],
510 __METHOD__,
511 [ 'IGNORE' ]
512 );
513 $this->assertSame( 1, $this->conn->affectedRows(), 'Nothing changed' );
514
515 // Test nothing found
516 $this->conn->update(
517 $sTable,
518 [ 'sk' => 'TestExistingRow' ],
519 [ 'sk' => 'TestNonexistingRow' ],
520 __METHOD__,
521 [ 'IGNORE' ]
522 );
523 $this->assertSame( 0, $this->conn->affectedRows(), 'Not found' );
524
525 // Test key conflict on the unique sk field
526 $this->conn->update(
527 $sTable,
528 [ 'sk' => 'TestRow' ],
529 [ 'sk' => 'Luca' ],
530 __METHOD__,
531 [ 'IGNORE' ]
532 );
533 $this->assertSame( 1, $this->conn->affectedRows(), 'Key conflict, nothing changed on database' );
534 }
535
536 public function testFieldAndIndexInfo() {
537 global $wgDBname;
538
539 $this->conn->selectDomain( $wgDBname );
540 $this->conn->query(
541 "CREATE TEMPORARY TABLE tmp_schema_tbl (" .
542 "n integer not null auto_increment, " .
543 "k varchar(255), " .
544 "v integer, " .
545 "t integer," .
546 "PRIMARY KEY (n)," .
547 "UNIQUE INDEX k (k)," .
548 "INDEX t (t)" .
549 ")"
550 );
551
552 $this->assertTrue( $this->conn->fieldExists( 'tmp_schema_tbl', 'n' ) );
553 $this->assertTrue( $this->conn->fieldExists( 'tmp_schema_tbl', 'k' ) );
554 $this->assertTrue( $this->conn->fieldExists( 'tmp_schema_tbl', 'v' ) );
555 $this->assertTrue( $this->conn->fieldExists( 'tmp_schema_tbl', 't' ) );
556 $this->assertFalse( $this->conn->fieldExists( 'tmp_schema_tbl', 'x' ) );
557
558 $this->assertTrue( $this->conn->indexExists( 'tmp_schema_tbl', 'k' ) );
559 $this->assertTrue( $this->conn->indexExists( 'tmp_schema_tbl', 't' ) );
560 $this->assertFalse( $this->conn->indexExists( 'tmp_schema_tbl', 'x' ) );
561 $this->assertTrue( $this->conn->indexExists( 'tmp_schema_tbl', 'PRIMARY' ) );
562
563 $this->assertTrue( $this->conn->indexUnique( 'tmp_schema_tbl', 'k' ) );
564 $this->assertFalse( $this->conn->indexUnique( 'tmp_schema_tbl', 't' ) );
565 $this->assertNull( $this->conn->indexUnique( 'tmp_schema_tbl', 'x' ) );
566 $this->assertTrue( $this->conn->indexUnique( 'tmp_schema_tbl', 'PRIMARY' ) );
567 }
568
569 private function createSourceTable() {
570 global $wgDBname;
571
572 $this->conn->query( "DROP TABLE IF EXISTS `$wgDBname`.`tmp_src_tbl`" );
573 $this->conn->query(
574 "CREATE TEMPORARY TABLE `$wgDBname`.`tmp_src_tbl` (" .
575 "sn integer not null unique key auto_increment, " .
576 "sk varchar(255) unique, " .
577 "sv integer, " .
578 "st integer" .
579 ")"
580 );
581
582 return "$wgDBname.tmp_src_tbl";
583 }
584
585 private function createDestTable() {
586 global $wgDBname;
587
588 $this->conn->query( "DROP TABLE IF EXISTS `$wgDBname`.`tmp_dst_tbl`" );
589 $this->conn->query(
590 "CREATE TEMPORARY TABLE `$wgDBname`.`tmp_dst_tbl` (" .
591 "n integer not null unique key auto_increment, " .
592 "k varchar(255) unique, " .
593 "v integer, " .
594 "t integer" .
595 ")"
596 );
597
598 return "$wgDBname.tmp_dst_tbl";
599 }
600
601 private function dropSourceTable() {
602 global $wgDBname;
603
604 $this->conn->query( "DROP TEMPORARY TABLE IF EXISTS `$wgDBname`.`tmp_src_tbl`" );
605 }
606
607 private function dropDestTable() {
608 global $wgDBname;
609
610 $this->conn->query( "DROP TEMPORARY TABLE IF EXISTS `$wgDBname`.`tmp_dst_tbl`" );
611 }
612
613 /**
614 * Insert a null value into a field that is not nullable using INSERT IGNORE
615 */
616 public function testInsertIgnoreNull() {
617 $this->expectException( DBQueryError::class );
618 $this->conn->newInsertQueryBuilder()
619 ->insertInto( 'log_search' )
620 ->ignore()
621 ->row( [ 'ls_field' => 'test', 'ls_value' => null, 'ls_log_id' => 1 ] )
622 ->execute();
623 }
624
625 /**
626 * @covers \Wikimedia\Rdbms\DatabaseMySQL::query
627 * @covers \Wikimedia\Rdbms\DatabaseMySQL::dropTable
628 */
629 public function testTempTableDomains() {
630 global $wgDBname;
631
632 $table = 'temp_test_tbl';
633
634 $this->conn->selectDomain( $wgDBname . '-xxx1_' );
635 $this->assertFalse( $this->conn->tableExists( $table ) );
636
637 $query = new Query(
638 "CREATE TEMPORARY TABLE " . $this->conn->tableName( $table ) .
639 " (n integer not null unique key)",
640 ISQLPlatform::QUERY_CHANGE_SCHEMA,
641 "CREATE TEMPORARY",
642 $table
643 );
644 $this->conn->query( $query, __METHOD__ );
645 $this->assertTrue( $this->conn->tableExists( $table ) );
646
647 $this->conn->selectDomain( $wgDBname . '-xxx2_' );
648 $this->assertFalse( $this->conn->tableExists( $table ) );
649
650 $query = new Query(
651 "CREATE TEMPORARY TABLE " . $this->conn->tableName( $table ) .
652 " (n integer not null unique key)",
653 ISQLPlatform::QUERY_CHANGE_SCHEMA,
654 "CREATE TEMPORARY",
655 $table
656 );
657 $this->conn->query( $query, __METHOD__ );
658 $this->assertTrue( $this->conn->tableExists( $table ) );
659
660 $this->conn->selectDomain( $wgDBname . '-xxx1_' );
661 $this->conn->dropTable( $table );
662 $this->assertFalse( $this->conn->tableExists( $table ) );
663
664 $this->conn->selectDomain( $wgDBname . '-xxx2_' );
665 $this->conn->dropTable( $table );
666 $this->assertFalse( $this->conn->tableExists( $table ) );
667 }
668
669 /**
670 * @covers \Wikimedia\Rdbms\DatabaseMySQL::query
671 * @covers \Wikimedia\Rdbms\DatabaseMySQL::dropTable
672 */
673 public function testTempTableDomainsTableWithDot() {
674 global $wgDBname;
675
676 $table = '.temp_test_tbl';
677
678 $this->conn->selectDomain( $wgDBname . '-xxx1_' );
679 $this->assertFalse( $this->conn->tableExists( $table ) );
680
681 $query = new Query(
682 "CREATE TEMPORARY TABLE " . $this->conn->tableName( $table ) .
683 " (n integer not null unique key)",
684 ISQLPlatform::QUERY_CHANGE_SCHEMA,
685 "CREATE TEMPORARY",
686 $table
687 );
688 $this->conn->query( $query, __METHOD__ );
689 $this->assertTrue( $this->conn->tableExists( $table ) );
690
691 $this->conn->selectDomain( $wgDBname . '-xxx2_' );
692 $this->assertTrue( $this->conn->tableExists( $table ) );
693
694 $this->conn->dropTable( $table );
695 $this->assertFalse( $this->conn->tableExists( $table ) );
696
697 $this->conn->selectDomain( $wgDBname . '-xxx1_' );
698 $this->assertFalse( $this->conn->tableExists( $table ) );
699 }
700 }