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