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
{
32 * @var MockDatabaseSqlite
36 protected function setUp() {
39 if ( !Sqlite
::isPresent() ) {
40 $this->markTestSkipped( 'No SQLite support detected' );
42 $this->db
= new MockDatabaseSqlite();
43 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
44 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
48 private function replaceVars( $sql ) {
49 // normalize spacing to hide implementation details
50 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
53 private function assertResultIs( $expected, $res ) {
54 $this->assertNotNull( $res );
56 foreach ( $res as $row ) {
57 foreach ( $expected[$i] as $key => $value ) {
58 $this->assertTrue( isset( $row->$key ) );
59 $this->assertEquals( $value, $row->$key );
63 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
66 public static function provideAddQuotes() {
72 'foo bar', "'foo bar'"
74 array( // #2: including quote
75 'foo\'bar', "'foo''bar'"
77 array( // #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 = new DatabaseSqliteStandalone( ':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" );
119 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
120 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
121 $this->replaceVars( "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 '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
125 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
126 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
129 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
130 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
133 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
134 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
138 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
139 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
141 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
142 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
145 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
146 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
149 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
150 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
153 $this->assertEquals( "DROP INDEX foo",
154 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
157 $this->assertEquals( "DROP INDEX foo -- dropping index",
158 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
163 * @covers DatabaseSqlite::tableName
165 public function testTableName() {
167 $db = new DatabaseSqliteStandalone( ':memory:' );
168 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
169 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
170 $db->tablePrefix( 'foo' );
171 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
172 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
176 * @covers DatabaseSqlite::duplicateTableStructure
178 public function testDuplicateTableStructure() {
179 $db = new DatabaseSqliteStandalone( ':memory:' );
180 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
182 $db->duplicateTableStructure( 'foo', 'bar' );
183 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
184 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
185 'Normal table duplication'
188 $db->duplicateTableStructure( 'foo', 'baz', true );
189 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
190 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
191 'Creation of temporary duplicate'
193 $this->assertEquals( 0,
194 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
195 'Create a temporary duplicate only'
200 * @covers DatabaseSqlite::duplicateTableStructure
202 public function testDuplicateTableStructureVirtual() {
203 $db = new DatabaseSqliteStandalone( ':memory:' );
204 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
205 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
207 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
209 $db->duplicateTableStructure( 'foo', 'bar' );
210 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
211 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
212 'Duplication of virtual tables'
215 $db->duplicateTableStructure( 'foo', 'baz', true );
216 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
217 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
218 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
223 * @covers DatabaseSqlite::deleteJoin
225 public function testDeleteJoin() {
226 $db = new DatabaseSqliteStandalone( ':memory:' );
227 $db->query( 'CREATE TABLE a (a_1)', __METHOD__
);
228 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__
);
229 $db->insert( 'a', array(
236 $db->insert( 'b', array(
237 array( 'b_1' => 2, 'b_2' => 'a' ),
238 array( 'b_1' => 3, 'b_2' => 'b' ),
242 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__
);
243 $res = $db->query( "SELECT * FROM a", __METHOD__
);
244 $this->assertResultIs( array(
252 public function testEntireSchema() {
255 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
256 if ( $result !== true ) {
257 $this->fail( $result );
259 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
263 * Runs upgrades of older databases and compares results with current schema
264 * @todo Currently only checks list of tables
266 public function testUpgrades() {
267 global $IP, $wgVersion, $wgProfileToDatabase;
271 //'1.13', disabled for now, was totally screwed up
272 // SQLite wasn't included in 1.14
279 // Mismatches for these columns we can safely ignore
280 $ignoredColumns = array(
281 'user_newtalk.user_last_timestamp', // r84185
284 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
285 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
286 if ( $wgProfileToDatabase ) {
287 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
289 $currentTables = $this->getTables( $currentDB );
290 sort( $currentTables );
292 foreach ( $versions as $version ) {
293 $versions = "upgrading from $version to $wgVersion";
294 $db = $this->prepareDB( $version );
295 $tables = $this->getTables( $db );
296 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
297 foreach ( $tables as $table ) {
298 $currentCols = $this->getColumns( $currentDB, $table );
299 $cols = $this->getColumns( $db, $table );
301 array_keys( $currentCols ),
303 "Mismatching columns for table \"$table\" $versions"
305 foreach ( $currentCols as $name => $column ) {
306 $fullName = "$table.$name";
309 (bool)$cols[$name]->pk
,
310 "PRIMARY KEY status does not match for column $fullName $versions"
312 if ( !in_array( $fullName, $ignoredColumns ) ) {
314 (bool)$column->notnull
,
315 (bool)$cols[$name]->notnull
,
316 "NOT NULL status does not match for column $fullName $versions"
320 $cols[$name]->dflt_value
,
321 "Default values does not match for column $fullName $versions"
325 $currentIndexes = $this->getIndexes( $currentDB, $table );
326 $indexes = $this->getIndexes( $db, $table );
328 array_keys( $currentIndexes ),
329 array_keys( $indexes ),
330 "mismatching indexes for table \"$table\" $versions"
338 * @covers DatabaseSqlite::insertId
340 public function testInsertIdType() {
341 $db = new DatabaseSqliteStandalone( ':memory:' );
343 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
344 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
346 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__
);
347 $this->assertTrue( $insertion, "Insertion worked" );
349 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
350 $this->assertTrue( $db->close(), "closing database" );
353 private function prepareDB( $version ) {
354 static $maint = null;
355 if ( $maint === null ) {
356 $maint = new FakeMaintenance();
357 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
361 $db = new DatabaseSqliteStandalone( ':memory:' );
362 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
363 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
364 $updater->doUpdates( array( 'core' ) );
369 private function getTables( $db ) {
370 $list = array_flip( $db->listTables() );
372 'external_user', // removed from core in 1.22
373 'math', // moved out of core in 1.18
374 'trackbacks', // removed from core in 1.19
376 'searchindex_content',
377 'searchindex_segments',
378 'searchindex_segdir',
380 'searchindex_docsize',
383 foreach ( $excluded as $t ) {
386 $list = array_flip( $list );
392 private function getColumns( $db, $table ) {
394 $res = $db->query( "PRAGMA table_info($table)" );
395 $this->assertNotNull( $res );
396 foreach ( $res as $col ) {
397 $cols[$col->name
] = $col;
404 private function getIndexes( $db, $table ) {
406 $res = $db->query( "PRAGMA index_list($table)" );
407 $this->assertNotNull( $res );
408 foreach ( $res as $index ) {
409 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
410 $this->assertNotNull( $res2 );
411 $index->columns
= array();
412 foreach ( $res2 as $col ) {
413 $index->columns
[] = $col;
415 $indexes[$index->name
] = $index;
422 public function testCaseInsensitiveLike() {
423 // TODO: Test this for all databases
424 $db = new DatabaseSqliteStandalone( ':memory:' );
425 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
426 $row = $res->fetchRow();
427 $this->assertFalse( (bool)$row['a'] );