MediaWiki
REL1_23
|
00001 <?php 00002 00003 class MockDatabaseSqlite extends DatabaseSqliteStandalone { 00004 var $lastQuery; 00005 00006 function __construct() { 00007 parent::__construct( ':memory:' ); 00008 } 00009 00010 function query( $sql, $fname = '', $tempIgnore = false ) { 00011 $this->lastQuery = $sql; 00012 00013 return true; 00014 } 00015 00019 public function replaceVars( $s ) { 00020 return parent::replaceVars( $s ); 00021 } 00022 } 00023 00029 class DatabaseSqliteTest extends MediaWikiTestCase { 00030 00034 var $db; 00035 00036 protected function setUp() { 00037 parent::setUp(); 00038 00039 if ( !Sqlite::isPresent() ) { 00040 $this->markTestSkipped( 'No SQLite support detected' ); 00041 } 00042 $this->db = new MockDatabaseSqlite(); 00043 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) { 00044 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" ); 00045 } 00046 } 00047 00048 private function replaceVars( $sql ) { 00049 // normalize spacing to hide implementation details 00050 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) ); 00051 } 00052 00053 private function assertResultIs( $expected, $res ) { 00054 $this->assertNotNull( $res ); 00055 $i = 0; 00056 foreach ( $res as $row ) { 00057 foreach ( $expected[$i] as $key => $value ) { 00058 $this->assertTrue( isset( $row->$key ) ); 00059 $this->assertEquals( $value, $row->$key ); 00060 } 00061 $i++; 00062 } 00063 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' ); 00064 } 00065 00066 public static function provideAddQuotes() { 00067 return array( 00068 array( // #0: empty 00069 '', "''" 00070 ), 00071 array( // #1: simple 00072 'foo bar', "'foo bar'" 00073 ), 00074 array( // #2: including quote 00075 'foo\'bar', "'foo''bar'" 00076 ), 00077 array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419) 00078 "x\0y", 00079 "x'780079'", 00080 ), 00081 array( // #4: blob object (must be represented as hex) 00082 new Blob( "hello" ), 00083 "x'68656c6c6f'", 00084 ), 00085 ); 00086 } 00087 00092 public function testAddQuotes( $value, $expected ) { 00093 // check quoting 00094 $db = new DatabaseSqliteStandalone( ':memory:' ); 00095 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' ); 00096 00097 // ok, quoting works as expected, now try a round trip. 00098 $re = $db->query( 'select ' . $db->addQuotes( $value ) ); 00099 00100 $this->assertTrue( $re !== false, 'query failed' ); 00101 00102 if ( $row = $re->fetchRow() ) { 00103 if ( $value instanceof Blob ) { 00104 $value = $value->fetch(); 00105 } 00106 00107 $this->assertEquals( $value, $row[0], 'string mangled by the database' ); 00108 } else { 00109 $this->fail( 'query returned no result' ); 00110 } 00111 } 00112 00116 public function testReplaceVars() { 00117 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" ); 00118 00119 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " 00120 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );", 00121 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, 00122 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" ) 00123 ); 00124 00125 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );", 00126 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" ) 00127 ); 00128 00129 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );", 00130 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" ) 00131 ); 00132 00133 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );", 00134 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ), 00135 'Table name changed' 00136 ); 00137 00138 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );", 00139 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" ) 00140 ); 00141 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );", 00142 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" ) 00143 ); 00144 00145 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)", 00146 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" ) 00147 ); 00148 00149 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42", 00150 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" ) 00151 ); 00152 00153 $this->assertEquals( "DROP INDEX foo", 00154 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" ) 00155 ); 00156 00157 $this->assertEquals( "DROP INDEX foo -- dropping index", 00158 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" ) 00159 ); 00160 } 00161 00165 public function testTableName() { 00166 // @todo Moar! 00167 $db = new DatabaseSqliteStandalone( ':memory:' ); 00168 $this->assertEquals( 'foo', $db->tableName( 'foo' ) ); 00169 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) ); 00170 $db->tablePrefix( 'foo' ); 00171 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) ); 00172 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) ); 00173 } 00174 00178 public function testDuplicateTableStructure() { 00179 $db = new DatabaseSqliteStandalone( ':memory:' ); 00180 $db->query( 'CREATE TABLE foo(foo, barfoo)' ); 00181 00182 $db->duplicateTableStructure( 'foo', 'bar' ); 00183 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)', 00184 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ), 00185 'Normal table duplication' 00186 ); 00187 00188 $db->duplicateTableStructure( 'foo', 'baz', true ); 00189 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)', 00190 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ), 00191 'Creation of temporary duplicate' 00192 ); 00193 $this->assertEquals( 0, 00194 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ), 00195 'Create a temporary duplicate only' 00196 ); 00197 } 00198 00202 public function testDuplicateTableStructureVirtual() { 00203 $db = new DatabaseSqliteStandalone( ':memory:' ); 00204 if ( $db->getFulltextSearchModule() != 'FTS3' ) { 00205 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' ); 00206 } 00207 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' ); 00208 00209 $db->duplicateTableStructure( 'foo', 'bar' ); 00210 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)', 00211 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ), 00212 'Duplication of virtual tables' 00213 ); 00214 00215 $db->duplicateTableStructure( 'foo', 'baz', true ); 00216 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)', 00217 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ), 00218 "Can't create temporary virtual tables, should fall back to non-temporary duplication" 00219 ); 00220 } 00221 00225 public function testDeleteJoin() { 00226 $db = new DatabaseSqliteStandalone( ':memory:' ); 00227 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ ); 00228 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ ); 00229 $db->insert( 'a', array( 00230 array( 'a_1' => 1 ), 00231 array( 'a_1' => 2 ), 00232 array( 'a_1' => 3 ), 00233 ), 00234 __METHOD__ 00235 ); 00236 $db->insert( 'b', array( 00237 array( 'b_1' => 2, 'b_2' => 'a' ), 00238 array( 'b_1' => 3, 'b_2' => 'b' ), 00239 ), 00240 __METHOD__ 00241 ); 00242 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ ); 00243 $res = $db->query( "SELECT * FROM a", __METHOD__ ); 00244 $this->assertResultIs( array( 00245 array( 'a_1' => 1 ), 00246 array( 'a_1' => 3 ), 00247 ), 00248 $res 00249 ); 00250 } 00251 00252 public function testEntireSchema() { 00253 global $IP; 00254 00255 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" ); 00256 if ( $result !== true ) { 00257 $this->fail( $result ); 00258 } 00259 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions 00260 } 00261 00266 public function testUpgrades() { 00267 global $IP, $wgVersion, $wgProfileToDatabase; 00268 00269 // Versions tested 00270 $versions = array( 00271 //'1.13', disabled for now, was totally screwed up 00272 // SQLite wasn't included in 1.14 00273 '1.15', 00274 '1.16', 00275 '1.17', 00276 '1.18', 00277 ); 00278 00279 // Mismatches for these columns we can safely ignore 00280 $ignoredColumns = array( 00281 'user_newtalk.user_last_timestamp', // r84185 00282 ); 00283 00284 $currentDB = new DatabaseSqliteStandalone( ':memory:' ); 00285 $currentDB->sourceFile( "$IP/maintenance/tables.sql" ); 00286 if ( $wgProfileToDatabase ) { 00287 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" ); 00288 } 00289 $currentTables = $this->getTables( $currentDB ); 00290 sort( $currentTables ); 00291 00292 foreach ( $versions as $version ) { 00293 $versions = "upgrading from $version to $wgVersion"; 00294 $db = $this->prepareDB( $version ); 00295 $tables = $this->getTables( $db ); 00296 $this->assertEquals( $currentTables, $tables, "Different tables $versions" ); 00297 foreach ( $tables as $table ) { 00298 $currentCols = $this->getColumns( $currentDB, $table ); 00299 $cols = $this->getColumns( $db, $table ); 00300 $this->assertEquals( 00301 array_keys( $currentCols ), 00302 array_keys( $cols ), 00303 "Mismatching columns for table \"$table\" $versions" 00304 ); 00305 foreach ( $currentCols as $name => $column ) { 00306 $fullName = "$table.$name"; 00307 $this->assertEquals( 00308 (bool)$column->pk, 00309 (bool)$cols[$name]->pk, 00310 "PRIMARY KEY status does not match for column $fullName $versions" 00311 ); 00312 if ( !in_array( $fullName, $ignoredColumns ) ) { 00313 $this->assertEquals( 00314 (bool)$column->notnull, 00315 (bool)$cols[$name]->notnull, 00316 "NOT NULL status does not match for column $fullName $versions" 00317 ); 00318 $this->assertEquals( 00319 $column->dflt_value, 00320 $cols[$name]->dflt_value, 00321 "Default values does not match for column $fullName $versions" 00322 ); 00323 } 00324 } 00325 $currentIndexes = $this->getIndexes( $currentDB, $table ); 00326 $indexes = $this->getIndexes( $db, $table ); 00327 $this->assertEquals( 00328 array_keys( $currentIndexes ), 00329 array_keys( $indexes ), 00330 "mismatching indexes for table \"$table\" $versions" 00331 ); 00332 } 00333 $db->close(); 00334 } 00335 } 00336 00340 public function testInsertIdType() { 00341 $db = new DatabaseSqliteStandalone( ':memory:' ); 00342 00343 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ); 00344 $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" ); 00345 00346 $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ ); 00347 $this->assertTrue( $insertion, "Insertion worked" ); 00348 00349 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" ); 00350 $this->assertTrue( $db->close(), "closing database" ); 00351 } 00352 00353 private function prepareDB( $version ) { 00354 static $maint = null; 00355 if ( $maint === null ) { 00356 $maint = new FakeMaintenance(); 00357 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) ); 00358 } 00359 00360 global $IP; 00361 $db = new DatabaseSqliteStandalone( ':memory:' ); 00362 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" ); 00363 $updater = DatabaseUpdater::newForDB( $db, false, $maint ); 00364 $updater->doUpdates( array( 'core' ) ); 00365 00366 return $db; 00367 } 00368 00369 private function getTables( $db ) { 00370 $list = array_flip( $db->listTables() ); 00371 $excluded = array( 00372 'external_user', // removed from core in 1.22 00373 'math', // moved out of core in 1.18 00374 'trackbacks', // removed from core in 1.19 00375 'searchindex', 00376 'searchindex_content', 00377 'searchindex_segments', 00378 'searchindex_segdir', 00379 // FTS4 ready!!1 00380 'searchindex_docsize', 00381 'searchindex_stat', 00382 ); 00383 foreach ( $excluded as $t ) { 00384 unset( $list[$t] ); 00385 } 00386 $list = array_flip( $list ); 00387 sort( $list ); 00388 00389 return $list; 00390 } 00391 00392 private function getColumns( $db, $table ) { 00393 $cols = array(); 00394 $res = $db->query( "PRAGMA table_info($table)" ); 00395 $this->assertNotNull( $res ); 00396 foreach ( $res as $col ) { 00397 $cols[$col->name] = $col; 00398 } 00399 ksort( $cols ); 00400 00401 return $cols; 00402 } 00403 00404 private function getIndexes( $db, $table ) { 00405 $indexes = array(); 00406 $res = $db->query( "PRAGMA index_list($table)" ); 00407 $this->assertNotNull( $res ); 00408 foreach ( $res as $index ) { 00409 $res2 = $db->query( "PRAGMA index_info({$index->name})" ); 00410 $this->assertNotNull( $res2 ); 00411 $index->columns = array(); 00412 foreach ( $res2 as $col ) { 00413 $index->columns[] = $col; 00414 } 00415 $indexes[$index->name] = $index; 00416 } 00417 ksort( $indexes ); 00418 00419 return $indexes; 00420 } 00421 00422 public function testCaseInsensitiveLike() { 00423 // TODO: Test this for all databases 00424 $db = new DatabaseSqliteStandalone( ':memory:' ); 00425 $res = $db->query( 'SELECT "a" LIKE "A" AS a' ); 00426 $row = $res->fetchRow(); 00427 $this->assertFalse( (bool)$row['a'] ); 00428 } 00429 }