Merge "Removed odd "partitionsNoPush" setting to simplify the code"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqlite {
4 private $lastQuery;
5
6 public static function newInstance( array $p = array() ) {
7 $p['dbFilePath'] = ':memory:';
8
9 return new self( $p );
10 }
11
12 function query( $sql, $fname = '', $tempIgnore = false ) {
13 $this->lastQuery = $sql;
14
15 return true;
16 }
17
18 /**
19 * Override parent visibility to public
20 */
21 public function replaceVars( $s ) {
22 return parent::replaceVars( $s );
23 }
24 }
25
26 /**
27 * @group sqlite
28 * @group Database
29 * @group medium
30 */
31 class DatabaseSqliteTest extends MediaWikiTestCase {
32 /** @var MockDatabaseSqlite */
33 protected $db;
34
35 protected function setUp() {
36 parent::setUp();
37
38 if ( !Sqlite::isPresent() ) {
39 $this->markTestSkipped( 'No SQLite support detected' );
40 }
41 $this->db = MockDatabaseSqlite::newInstance();
42 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
43 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
44 }
45 }
46
47 private function replaceVars( $sql ) {
48 // normalize spacing to hide implementation details
49 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
50 }
51
52 private function assertResultIs( $expected, $res ) {
53 $this->assertNotNull( $res );
54 $i = 0;
55 foreach ( $res as $row ) {
56 foreach ( $expected[$i] as $key => $value ) {
57 $this->assertTrue( isset( $row->$key ) );
58 $this->assertEquals( $value, $row->$key );
59 }
60 $i++;
61 }
62 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
63 }
64
65 public static function provideAddQuotes() {
66 return array(
67 array( // #0: empty
68 '', "''"
69 ),
70 array( // #1: simple
71 'foo bar', "'foo bar'"
72 ),
73 array( // #2: including quote
74 'foo\'bar', "'foo''bar'"
75 ),
76 // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
77 array(
78 "x\0y",
79 "x'780079'",
80 ),
81 array( // #4: blob object (must be represented as hex)
82 new Blob( "hello" ),
83 "x'68656c6c6f'",
84 ),
85 );
86 }
87
88 /**
89 * @dataProvider provideAddQuotes()
90 * @covers DatabaseSqlite::addQuotes
91 */
92 public function testAddQuotes( $value, $expected ) {
93 // check quoting
94 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
95 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
96
97 // ok, quoting works as expected, now try a round trip.
98 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
99
100 $this->assertTrue( $re !== false, 'query failed' );
101
102 if ( $row = $re->fetchRow() ) {
103 if ( $value instanceof Blob ) {
104 $value = $value->fetch();
105 }
106
107 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
108 } else {
109 $this->fail( 'query returned no result' );
110 }
111 }
112
113 /**
114 * @covers DatabaseSqlite::replaceVars
115 */
116 public function testReplaceVars() {
117 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
118
119 $this->assertEquals(
120 "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
121 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
122 $this->replaceVars(
123 "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, "
124 . "foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', "
125 . "foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;"
126 )
127 );
128
129 $this->assertEquals(
130 "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
131 $this->replaceVars(
132 "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
133 )
134 );
135
136 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
137 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
138 );
139
140 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
141 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
142 'Table name changed'
143 );
144
145 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
146 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
147 );
148 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
149 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
150 );
151
152 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
153 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
154 );
155
156 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
157 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
158 );
159
160 $this->assertEquals( "DROP INDEX foo",
161 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
162 );
163
164 $this->assertEquals( "DROP INDEX foo -- dropping index",
165 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
166 );
167 $this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
168 $this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
169 );
170 }
171
172 /**
173 * @covers DatabaseSqlite::tableName
174 */
175 public function testTableName() {
176 // @todo Moar!
177 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
178 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
179 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
180 $db->tablePrefix( 'foo' );
181 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
182 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
183 }
184
185 /**
186 * @covers DatabaseSqlite::duplicateTableStructure
187 */
188 public function testDuplicateTableStructure() {
189 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
190 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
191
192 $db->duplicateTableStructure( 'foo', 'bar' );
193 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
194 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
195 'Normal table duplication'
196 );
197
198 $db->duplicateTableStructure( 'foo', 'baz', true );
199 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
200 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
201 'Creation of temporary duplicate'
202 );
203 $this->assertEquals( 0,
204 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
205 'Create a temporary duplicate only'
206 );
207 }
208
209 /**
210 * @covers DatabaseSqlite::duplicateTableStructure
211 */
212 public function testDuplicateTableStructureVirtual() {
213 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
214 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
215 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
216 }
217 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
218
219 $db->duplicateTableStructure( 'foo', 'bar' );
220 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
221 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
222 'Duplication of virtual tables'
223 );
224
225 $db->duplicateTableStructure( 'foo', 'baz', true );
226 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
227 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
228 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
229 );
230 }
231
232 /**
233 * @covers DatabaseSqlite::deleteJoin
234 */
235 public function testDeleteJoin() {
236 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
237 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
238 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
239 $db->insert( 'a', array(
240 array( 'a_1' => 1 ),
241 array( 'a_1' => 2 ),
242 array( 'a_1' => 3 ),
243 ),
244 __METHOD__
245 );
246 $db->insert( 'b', array(
247 array( 'b_1' => 2, 'b_2' => 'a' ),
248 array( 'b_1' => 3, 'b_2' => 'b' ),
249 ),
250 __METHOD__
251 );
252 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
253 $res = $db->query( "SELECT * FROM a", __METHOD__ );
254 $this->assertResultIs( array(
255 array( 'a_1' => 1 ),
256 array( 'a_1' => 3 ),
257 ),
258 $res
259 );
260 }
261
262 public function testEntireSchema() {
263 global $IP;
264
265 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
266 if ( $result !== true ) {
267 $this->fail( $result );
268 }
269 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
270 }
271
272 /**
273 * Runs upgrades of older databases and compares results with current schema
274 * @todo Currently only checks list of tables
275 */
276 public function testUpgrades() {
277 global $IP, $wgVersion, $wgProfiler;
278
279 // Versions tested
280 $versions = array(
281 //'1.13', disabled for now, was totally screwed up
282 // SQLite wasn't included in 1.14
283 '1.15',
284 '1.16',
285 '1.17',
286 '1.18',
287 );
288
289 // Mismatches for these columns we can safely ignore
290 $ignoredColumns = array(
291 'user_newtalk.user_last_timestamp', // r84185
292 );
293
294 $currentDB = DatabaseSqlite::newStandaloneInstance( ':memory:' );
295 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
296
297 $profileToDb = false;
298 if ( isset( $wgProfiler['output'] ) ) {
299 $out = $wgProfiler['output'];
300 if ( $out === 'db' ) {
301 $profileToDb = true;
302 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
303 $profileToDb = true;
304 }
305 }
306
307 if ( $profileToDb ) {
308 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
309 }
310 $currentTables = $this->getTables( $currentDB );
311 sort( $currentTables );
312
313 foreach ( $versions as $version ) {
314 $versions = "upgrading from $version to $wgVersion";
315 $db = $this->prepareDB( $version );
316 $tables = $this->getTables( $db );
317 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
318 foreach ( $tables as $table ) {
319 $currentCols = $this->getColumns( $currentDB, $table );
320 $cols = $this->getColumns( $db, $table );
321 $this->assertEquals(
322 array_keys( $currentCols ),
323 array_keys( $cols ),
324 "Mismatching columns for table \"$table\" $versions"
325 );
326 foreach ( $currentCols as $name => $column ) {
327 $fullName = "$table.$name";
328 $this->assertEquals(
329 (bool)$column->pk,
330 (bool)$cols[$name]->pk,
331 "PRIMARY KEY status does not match for column $fullName $versions"
332 );
333 if ( !in_array( $fullName, $ignoredColumns ) ) {
334 $this->assertEquals(
335 (bool)$column->notnull,
336 (bool)$cols[$name]->notnull,
337 "NOT NULL status does not match for column $fullName $versions"
338 );
339 $this->assertEquals(
340 $column->dflt_value,
341 $cols[$name]->dflt_value,
342 "Default values does not match for column $fullName $versions"
343 );
344 }
345 }
346 $currentIndexes = $this->getIndexes( $currentDB, $table );
347 $indexes = $this->getIndexes( $db, $table );
348 $this->assertEquals(
349 array_keys( $currentIndexes ),
350 array_keys( $indexes ),
351 "mismatching indexes for table \"$table\" $versions"
352 );
353 }
354 $db->close();
355 }
356 }
357
358 /**
359 * @covers DatabaseSqlite::insertId
360 */
361 public function testInsertIdType() {
362 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
363
364 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
365 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
366
367 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
368 $this->assertTrue( $insertion, "Insertion worked" );
369
370 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
371 $this->assertTrue( $db->close(), "closing database" );
372 }
373
374 private function prepareDB( $version ) {
375 static $maint = null;
376 if ( $maint === null ) {
377 $maint = new FakeMaintenance();
378 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
379 }
380
381 global $IP;
382 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
383 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
384 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
385 $updater->doUpdates( array( 'core' ) );
386
387 return $db;
388 }
389
390 private function getTables( $db ) {
391 $list = array_flip( $db->listTables() );
392 $excluded = array(
393 'external_user', // removed from core in 1.22
394 'math', // moved out of core in 1.18
395 'trackbacks', // removed from core in 1.19
396 'searchindex',
397 'searchindex_content',
398 'searchindex_segments',
399 'searchindex_segdir',
400 // FTS4 ready!!1
401 'searchindex_docsize',
402 'searchindex_stat',
403 );
404 foreach ( $excluded as $t ) {
405 unset( $list[$t] );
406 }
407 $list = array_flip( $list );
408 sort( $list );
409
410 return $list;
411 }
412
413 private function getColumns( $db, $table ) {
414 $cols = array();
415 $res = $db->query( "PRAGMA table_info($table)" );
416 $this->assertNotNull( $res );
417 foreach ( $res as $col ) {
418 $cols[$col->name] = $col;
419 }
420 ksort( $cols );
421
422 return $cols;
423 }
424
425 private function getIndexes( $db, $table ) {
426 $indexes = array();
427 $res = $db->query( "PRAGMA index_list($table)" );
428 $this->assertNotNull( $res );
429 foreach ( $res as $index ) {
430 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
431 $this->assertNotNull( $res2 );
432 $index->columns = array();
433 foreach ( $res2 as $col ) {
434 $index->columns[] = $col;
435 }
436 $indexes[$index->name] = $index;
437 }
438 ksort( $indexes );
439
440 return $indexes;
441 }
442
443 public function testCaseInsensitiveLike() {
444 // TODO: Test this for all databases
445 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
446 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
447 $row = $res->fetchRow();
448 $this->assertFalse( (bool)$row['a'] );
449 }
450
451 /**
452 * @covers DatabaseSqlite::numFields
453 */
454 public function testNumFields() {
455 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
456
457 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
458 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );
459 $res = $db->select( 'a', '*' );
460 $this->assertEquals( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
461 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
462 $this->assertTrue( $insertion, "Insertion failed" );
463 $res = $db->select( 'a', '*' );
464 $this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
465
466 $this->assertTrue( $db->close(), "closing database" );
467 }
468 }