MediaWiki  REL1_19
DatabaseSqliteTest.php
Go to the documentation of this file.
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                 return true;
00013         }
00014 
00015         function replaceVars( $s ) {
00016                 return parent::replaceVars( $s );
00017         }
00018 }
00019 
00024 class DatabaseSqliteTest extends MediaWikiTestCase {
00025         var $db;
00026 
00027         public function setUp() {
00028                 if ( !Sqlite::isPresent() ) {
00029                         $this->markTestSkipped( 'No SQLite support detected' );
00030                 }
00031                 $this->db = new MockDatabaseSqlite();
00032                 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
00033                         $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
00034                 }
00035         }
00036 
00037         private function replaceVars( $sql ) {
00038                 // normalize spacing to hide implementation details
00039                 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
00040         }
00041 
00042         private function assertResultIs( $expected, $res ) {
00043                 $this->assertNotNull( $res );
00044                 $i = 0;
00045                 foreach( $res as $row ) {
00046                         foreach( $expected[$i] as $key => $value ) {
00047                                 $this->assertTrue( isset( $row->$key ) );
00048                                 $this->assertEquals( $value, $row->$key );
00049                         }
00050                         $i++;
00051                 }
00052                 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
00053         }
00054 
00055         public function testReplaceVars() {
00056                 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
00057 
00058                 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
00059                         . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
00060                         $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
00061                         foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
00062                         );
00063 
00064                 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
00065                         $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
00066                         );
00067 
00068                 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
00069                         $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
00070                         );
00071 
00072                 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
00073                         $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
00074                         'Table name changed'
00075                         );
00076 
00077                 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
00078                         $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
00079                         );
00080                 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
00081                         $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
00082                         );
00083 
00084                 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
00085                         $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
00086                         );
00087 
00088                 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
00089                         $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
00090                         );
00091         }
00092 
00093         public function testTableName() {
00094                 // @todo Moar!
00095                 $db = new DatabaseSqliteStandalone( ':memory:' );
00096                 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
00097                 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
00098                 $db->tablePrefix( 'foo' );
00099                 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
00100                 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
00101         }
00102 
00103         public function testDuplicateTableStructure() {
00104                 $db = new DatabaseSqliteStandalone( ':memory:' );
00105                 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
00106 
00107                 $db->duplicateTableStructure( 'foo', 'bar' );
00108                 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
00109                         $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
00110                         'Normal table duplication'
00111                 );
00112 
00113                 $db->duplicateTableStructure( 'foo', 'baz', true );
00114                 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
00115                         $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
00116                         'Creation of temporary duplicate'
00117                 );
00118                 $this->assertEquals( 0,
00119                         $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
00120                         'Create a temporary duplicate only'
00121                 );
00122         }
00123 
00124         public function testDuplicateTableStructureVirtual() {
00125                 $db = new DatabaseSqliteStandalone( ':memory:' );
00126                 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
00127                         $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
00128                 }
00129                 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
00130 
00131                 $db->duplicateTableStructure( 'foo', 'bar' );
00132                 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
00133                         $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
00134                         'Duplication of virtual tables'
00135                 );
00136 
00137                 $db->duplicateTableStructure( 'foo', 'baz', true );
00138                 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
00139                         $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
00140                         "Can't create temporary virtual tables, should fall back to non-temporary duplication"
00141                 );
00142         }
00143 
00144         public function testDeleteJoin() {
00145                 $db = new DatabaseSqliteStandalone( ':memory:' );
00146                 $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
00147                 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
00148                 $db->insert( 'a', array(
00149                                 array( 'a_1' => 1 ),
00150                                 array( 'a_1' => 2 ),
00151                                 array( 'a_1' => 3 ),
00152                         ),
00153                         __METHOD__
00154                 );
00155                 $db->insert( 'b', array(
00156                                 array( 'b_1' => 2, 'b_2' => 'a' ),
00157                                 array( 'b_1' => 3, 'b_2' => 'b' ),
00158                         ),
00159                         __METHOD__
00160                 );
00161                 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
00162                 $res = $db->query( "SELECT * FROM a", __METHOD__ );
00163                 $this->assertResultIs( array(
00164                                 array( 'a_1' => 1 ),
00165                                 array( 'a_1' => 3 ),
00166                         ),
00167                         $res
00168                 );
00169         }
00170 
00171         public function testEntireSchema() {
00172                 global $IP;
00173 
00174                 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
00175                 if ( $result !== true ) {
00176                         $this->fail( $result );
00177                 }
00178                 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
00179         }
00180 
00185         public function testUpgrades() {
00186                 global $IP, $wgVersion;
00187 
00188                 // Versions tested
00189                 $versions = array(
00190                         //'1.13', disabled for now, was totally screwed up
00191                         // SQLite wasn't included in 1.14
00192                         '1.15',
00193                         '1.16',
00194                         '1.17',
00195                         '1.18',
00196                 );
00197 
00198                 // Mismatches for these columns we can safely ignore
00199                 $ignoredColumns = array(
00200                         'user_newtalk.user_last_timestamp', // r84185
00201                 );
00202 
00203                 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
00204                 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
00205                 $currentTables = $this->getTables( $currentDB );
00206                 sort( $currentTables );
00207 
00208                 foreach ( $versions as $version ) {
00209                         $versions = "upgrading from $version to $wgVersion";
00210                         $db = $this->prepareDB( $version );
00211                         $tables = $this->getTables( $db );
00212                         $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
00213                         foreach ( $tables as $table ) {
00214                                 $currentCols = $this->getColumns( $currentDB, $table );
00215                                 $cols = $this->getColumns( $db, $table );
00216                                 $this->assertEquals(
00217                                         array_keys( $currentCols ),
00218                                         array_keys( $cols ),
00219                                         "Mismatching columns for table \"$table\" $versions"
00220                                 );
00221                                 foreach ( $currentCols as $name => $column ) {
00222                                         $fullName = "$table.$name";
00223                                         $this->assertEquals(
00224                                                 (bool)$column->pk,
00225                                                 (bool)$cols[$name]->pk,
00226                                                 "PRIMARY KEY status does not match for column $fullName $versions"
00227                                         );
00228                                         if ( !in_array( $fullName, $ignoredColumns ) ) {
00229                                                 $this->assertEquals(
00230                                                         (bool)$column->notnull,
00231                                                         (bool)$cols[$name]->notnull,
00232                                                         "NOT NULL status does not match for column $fullName $versions"
00233                                                 );
00234                                                 $this->assertEquals(
00235                                                         $column->dflt_value,
00236                                                         $cols[$name]->dflt_value,
00237                                                         "Default values does not match for column $fullName $versions"
00238                                                 );
00239                                         }
00240                                 }
00241                                 $currentIndexes = $this->getIndexes( $currentDB, $table );
00242                                 $indexes = $this->getIndexes( $db, $table );
00243                                 $this->assertEquals(
00244                                         array_keys( $currentIndexes ),
00245                                         array_keys( $indexes ),
00246                                         "mismatching indexes for table \"$table\" $versions"
00247                                 );
00248                         }
00249                         $db->close();
00250                 }
00251         }
00252 
00253         private function prepareDB( $version ) {
00254                 static $maint = null;
00255                 if ( $maint === null ) {
00256                         $maint = new FakeMaintenance();
00257                         $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
00258                 }
00259 
00260                 global $IP;
00261                 $db = new DatabaseSqliteStandalone( ':memory:' );
00262                 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
00263                 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
00264                 $updater->doUpdates( array( 'core' ) );
00265                 return $db;
00266         }
00267 
00268         private function getTables( $db ) {
00269                 $list = array_flip( $db->listTables() );
00270                 $excluded = array(
00271                         'math', // moved out of core in 1.18
00272                         'trackbacks', // removed from core in 1.19
00273                         'searchindex',
00274                         'searchindex_content',
00275                         'searchindex_segments',
00276                         'searchindex_segdir',
00277                         // FTS4 ready!!1
00278                         'searchindex_docsize',
00279                         'searchindex_stat',
00280                 );
00281                 foreach ( $excluded as $t ) {
00282                         unset( $list[$t] );
00283                 }
00284                 $list = array_flip( $list );
00285                 sort( $list );
00286                 return $list;
00287         }
00288 
00289         private function getColumns( $db, $table ) {
00290                 $cols = array();
00291                 $res = $db->query( "PRAGMA table_info($table)" );
00292                 $this->assertNotNull( $res );
00293                 foreach ( $res as $col ) {
00294                         $cols[$col->name] = $col;
00295                 }
00296                 ksort( $cols );
00297                 return $cols;
00298         }
00299 
00300         private function getIndexes( $db, $table ) {
00301                 $indexes = array();
00302                 $res = $db->query( "PRAGMA index_list($table)" );
00303                 $this->assertNotNull( $res );
00304                 foreach ( $res as $index ) {
00305                         $res2 = $db->query( "PRAGMA index_info({$index->name})" );
00306                         $this->assertNotNull( $res2 );
00307                         $index->columns = array();
00308                         foreach ( $res2 as $col ) {
00309                                 $index->columns[] = $col;
00310                         }
00311                         $indexes[$index->name] = $index;
00312                 }
00313                 ksort( $indexes );
00314                 return $indexes;
00315         }
00316 }