3 class MockDatabaseSqlite
extends DatabaseSqliteStandalone
{
6 function __construct() {
7 parent
::__construct( ':memory:' );
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery
= $sql;
17 * Override parent visibility to public
19 public function replaceVars( $s ) {
20 return parent
::replaceVars( $s );
29 class DatabaseSqliteTest
extends MediaWikiTestCase
{
30 /** @var MockDatabaseSqlite */
33 protected function setUp() {
36 if ( !Sqlite
::isPresent() ) {
37 $this->markTestSkipped( 'No SQLite support detected' );
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" );
45 private function replaceVars( $sql ) {
46 // normalize spacing to hide implementation details
47 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
50 private function assertResultIs( $expected, $res ) {
51 $this->assertNotNull( $res );
53 foreach ( $res as $row ) {
54 foreach ( $expected[$i] as $key => $value ) {
55 $this->assertTrue( isset( $row->$key ) );
56 $this->assertEquals( $value, $row->$key );
60 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
63 public static function provideAddQuotes() {
69 'foo bar', "'foo bar'"
71 array( // #2: including quote
72 'foo\'bar', "'foo''bar'"
74 // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
79 array( // #4: blob object (must be represented as hex)
87 * @dataProvider provideAddQuotes()
88 * @covers DatabaseSqlite::addQuotes
90 public function testAddQuotes( $value, $expected ) {
92 $db = new DatabaseSqliteStandalone( ':memory:' );
93 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
95 // ok, quoting works as expected, now try a round trip.
96 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
98 $this->assertTrue( $re !== false, 'query failed' );
100 if ( $row = $re->fetchRow() ) {
101 if ( $value instanceof Blob
) {
102 $value = $value->fetch();
105 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
107 $this->fail( 'query returned no result' );
112 * @covers DatabaseSqlite::replaceVars
114 public function testReplaceVars() {
115 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
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 );",
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;"
128 "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
130 "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
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) );" )
138 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
139 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
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 );" )
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 );" )
150 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
151 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
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" )
158 $this->assertEquals( "DROP INDEX foo",
159 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
162 $this->assertEquals( "DROP INDEX foo -- dropping index",
163 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
165 $this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
166 $this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
171 * @covers DatabaseSqlite::tableName
173 public function testTableName() {
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' ) );
184 * @covers DatabaseSqlite::duplicateTableStructure
186 public function testDuplicateTableStructure() {
187 $db = new DatabaseSqliteStandalone( ':memory:' );
188 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
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'
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'
201 $this->assertEquals( 0,
202 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
203 'Create a temporary duplicate only'
208 * @covers DatabaseSqlite::duplicateTableStructure
210 public function testDuplicateTableStructureVirtual() {
211 $db = new DatabaseSqliteStandalone( ':memory:' );
212 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
213 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
215 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
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'
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"
231 * @covers DatabaseSqlite::deleteJoin
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(
244 $db->insert( 'b', array(
245 array( 'b_1' => 2, 'b_2' => 'a' ),
246 array( 'b_1' => 3, 'b_2' => 'b' ),
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(
260 public function testEntireSchema() {
263 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
264 if ( $result !== true ) {
265 $this->fail( $result );
267 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
271 * Runs upgrades of older databases and compares results with current schema
272 * @todo Currently only checks list of tables
274 public function testUpgrades() {
275 global $IP, $wgVersion, $wgProfileToDatabase;
279 //'1.13', disabled for now, was totally screwed up
280 // SQLite wasn't included in 1.14
287 // Mismatches for these columns we can safely ignore
288 $ignoredColumns = array(
289 'user_newtalk.user_last_timestamp', // r84185
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" );
297 $currentTables = $this->getTables( $currentDB );
298 sort( $currentTables );
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 );
309 array_keys( $currentCols ),
311 "Mismatching columns for table \"$table\" $versions"
313 foreach ( $currentCols as $name => $column ) {
314 $fullName = "$table.$name";
317 (bool)$cols[$name]->pk
,
318 "PRIMARY KEY status does not match for column $fullName $versions"
320 if ( !in_array( $fullName, $ignoredColumns ) ) {
322 (bool)$column->notnull
,
323 (bool)$cols[$name]->notnull
,
324 "NOT NULL status does not match for column $fullName $versions"
328 $cols[$name]->dflt_value
,
329 "Default values does not match for column $fullName $versions"
333 $currentIndexes = $this->getIndexes( $currentDB, $table );
334 $indexes = $this->getIndexes( $db, $table );
336 array_keys( $currentIndexes ),
337 array_keys( $indexes ),
338 "mismatching indexes for table \"$table\" $versions"
346 * @covers DatabaseSqlite::insertId
348 public function testInsertIdType() {
349 $db = new DatabaseSqliteStandalone( ':memory:' );
351 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
352 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
354 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__
);
355 $this->assertTrue( $insertion, "Insertion worked" );
357 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
358 $this->assertTrue( $db->close(), "closing database" );
361 private function prepareDB( $version ) {
362 static $maint = null;
363 if ( $maint === null ) {
364 $maint = new FakeMaintenance();
365 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
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' ) );
377 private function getTables( $db ) {
378 $list = array_flip( $db->listTables() );
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
384 'searchindex_content',
385 'searchindex_segments',
386 'searchindex_segdir',
388 'searchindex_docsize',
391 foreach ( $excluded as $t ) {
394 $list = array_flip( $list );
400 private function getColumns( $db, $table ) {
402 $res = $db->query( "PRAGMA table_info($table)" );
403 $this->assertNotNull( $res );
404 foreach ( $res as $col ) {
405 $cols[$col->name
] = $col;
412 private function getIndexes( $db, $table ) {
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;
423 $indexes[$index->name
] = $index;
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'] );
439 * @covers DatabaseSqlite::numFields
441 public function testNumFields() {
442 $db = new DatabaseSqliteStandalone( ':memory:' );
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" );
453 $this->assertTrue( $db->close(), "closing database" );