3 class MockDatabaseSqlite
extends DatabaseSqlite
{
6 public static function newInstance( array $p = array() ) {
7 $p['dbFilePath'] = ':memory:';
12 function query( $sql, $fname = '', $tempIgnore = false ) {
13 $this->lastQuery
= $sql;
19 * Override parent visibility to public
21 public function replaceVars( $s ) {
22 return parent
::replaceVars( $s );
31 class DatabaseSqliteTest
extends MediaWikiTestCase
{
32 /** @var MockDatabaseSqlite */
35 protected function setUp() {
38 if ( !Sqlite
::isPresent() ) {
39 $this->markTestSkipped( 'No SQLite support detected' );
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" );
47 private function replaceVars( $sql ) {
48 // normalize spacing to hide implementation details
49 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
52 private function assertResultIs( $expected, $res ) {
53 $this->assertNotNull( $res );
55 foreach ( $res as $row ) {
56 foreach ( $expected[$i] as $key => $value ) {
57 $this->assertTrue( isset( $row->$key ) );
58 $this->assertEquals( $value, $row->$key );
62 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
65 public static function provideAddQuotes() {
71 'foo bar', "'foo bar'"
73 array( // #2: including quote
74 'foo\'bar', "'foo''bar'"
76 // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
81 array( // #4: blob object (must be represented as hex)
89 * @dataProvider provideAddQuotes()
90 * @covers DatabaseSqlite::addQuotes
92 public function testAddQuotes( $value, $expected ) {
94 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
95 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
97 // ok, quoting works as expected, now try a round trip.
98 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
100 $this->assertTrue( $re !== false, 'query failed' );
102 if ( $row = $re->fetchRow() ) {
103 if ( $value instanceof Blob
) {
104 $value = $value->fetch();
107 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
109 $this->fail( 'query returned no result' );
114 * @covers DatabaseSqlite::replaceVars
116 public function testReplaceVars() {
117 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
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 );",
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;"
130 "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
132 "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
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) );" )
140 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
141 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
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 );" )
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 );" )
152 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
153 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
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" )
160 $this->assertEquals( "DROP INDEX foo",
161 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
164 $this->assertEquals( "DROP INDEX foo -- dropping index",
165 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
167 $this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
168 $this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
173 * @covers DatabaseSqlite::tableName
175 public function testTableName() {
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' ) );
186 * @covers DatabaseSqlite::duplicateTableStructure
188 public function testDuplicateTableStructure() {
189 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
190 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
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'
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'
203 $this->assertEquals( 0,
204 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
205 'Create a temporary duplicate only'
210 * @covers DatabaseSqlite::duplicateTableStructure
212 public function testDuplicateTableStructureVirtual() {
213 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
214 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
215 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
217 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
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'
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"
233 * @covers DatabaseSqlite::deleteJoin
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(
246 $db->insert( 'b', array(
247 array( 'b_1' => 2, 'b_2' => 'a' ),
248 array( 'b_1' => 3, 'b_2' => 'b' ),
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(
262 public function testEntireSchema() {
265 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
266 if ( $result !== true ) {
267 $this->fail( $result );
269 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
273 * Runs upgrades of older databases and compares results with current schema
274 * @todo Currently only checks list of tables
276 public function testUpgrades() {
277 global $IP, $wgVersion, $wgProfiler;
281 //'1.13', disabled for now, was totally screwed up
282 // SQLite wasn't included in 1.14
289 // Mismatches for these columns we can safely ignore
290 $ignoredColumns = array(
291 'user_newtalk.user_last_timestamp', // r84185
294 $currentDB = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
295 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
297 $profileToDb = false;
298 if ( isset( $wgProfiler['output'] ) ) {
299 $out = $wgProfiler['output'];
300 if ( $out === 'db' ) {
302 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
307 if ( $profileToDb ) {
308 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
310 $currentTables = $this->getTables( $currentDB );
311 sort( $currentTables );
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 );
322 array_keys( $currentCols ),
324 "Mismatching columns for table \"$table\" $versions"
326 foreach ( $currentCols as $name => $column ) {
327 $fullName = "$table.$name";
330 (bool)$cols[$name]->pk
,
331 "PRIMARY KEY status does not match for column $fullName $versions"
333 if ( !in_array( $fullName, $ignoredColumns ) ) {
335 (bool)$column->notnull
,
336 (bool)$cols[$name]->notnull
,
337 "NOT NULL status does not match for column $fullName $versions"
341 $cols[$name]->dflt_value
,
342 "Default values does not match for column $fullName $versions"
346 $currentIndexes = $this->getIndexes( $currentDB, $table );
347 $indexes = $this->getIndexes( $db, $table );
349 array_keys( $currentIndexes ),
350 array_keys( $indexes ),
351 "mismatching indexes for table \"$table\" $versions"
359 * @covers DatabaseSqlite::insertId
361 public function testInsertIdType() {
362 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
364 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
365 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
367 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__
);
368 $this->assertTrue( $insertion, "Insertion worked" );
370 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
371 $this->assertTrue( $db->close(), "closing database" );
374 private function prepareDB( $version ) {
375 static $maint = null;
376 if ( $maint === null ) {
377 $maint = new FakeMaintenance();
378 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
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' ) );
390 private function getTables( $db ) {
391 $list = array_flip( $db->listTables() );
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
397 'searchindex_content',
398 'searchindex_segments',
399 'searchindex_segdir',
401 'searchindex_docsize',
404 foreach ( $excluded as $t ) {
407 $list = array_flip( $list );
413 private function getColumns( $db, $table ) {
415 $res = $db->query( "PRAGMA table_info($table)" );
416 $this->assertNotNull( $res );
417 foreach ( $res as $col ) {
418 $cols[$col->name
] = $col;
425 private function getIndexes( $db, $table ) {
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;
436 $indexes[$index->name
] = $index;
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'] );
452 * @covers DatabaseSqlite::numFields
454 public function testNumFields() {
455 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
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" );
466 $this->assertTrue( $db->close(), "closing database" );