3 class MockDatabaseSqlite
extends DatabaseSqliteStandalone
{
6 function __construct( ) {
7 parent
::__construct( ':memory:' );
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery
= $sql;
15 function replaceVars( $s ) {
16 return parent
::replaceVars( $s );
25 class DatabaseSqliteTest
extends MediaWikiTestCase
{
28 protected function setUp() {
31 if ( !Sqlite
::isPresent() ) {
32 $this->markTestSkipped( 'No SQLite support detected' );
34 $this->db
= new MockDatabaseSqlite();
35 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
36 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
40 private function replaceVars( $sql ) {
41 // normalize spacing to hide implementation details
42 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
45 private function assertResultIs( $expected, $res ) {
46 $this->assertNotNull( $res );
48 foreach( $res as $row ) {
49 foreach( $expected[$i] as $key => $value ) {
50 $this->assertTrue( isset( $row->$key ) );
51 $this->assertEquals( $value, $row->$key );
55 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
58 public static function provideAddQuotes() {
64 'foo bar', "'foo bar'"
66 array( // #2: including quote
67 'foo\'bar', "'foo''bar'"
69 array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
73 array( // #4: blob object (must be represented as hex)
81 * @dataProvider provideAddQuotes()
83 public function testAddQuotes( $value, $expected ) {
85 $db = new DatabaseSqliteStandalone( ':memory:' );
86 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
88 // ok, quoting works as expected, now try a round trip.
89 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
91 $this->assertTrue( $re !== false, 'query failed' );
93 if ( $row = $re->fetchRow() ) {
94 if ( $value instanceof Blob
) {
95 $value = $value->fetch();
98 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
100 $this->fail( 'query returned no result' );
104 public function testReplaceVars() {
105 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
107 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
108 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
109 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
110 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
113 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
114 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
117 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
118 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
121 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
122 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
126 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
127 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
129 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
130 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
133 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
134 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
137 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
138 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
142 public function testTableName() {
144 $db = new DatabaseSqliteStandalone( ':memory:' );
145 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
146 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
147 $db->tablePrefix( 'foo' );
148 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
149 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
152 public function testDuplicateTableStructure() {
153 $db = new DatabaseSqliteStandalone( ':memory:' );
154 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
156 $db->duplicateTableStructure( 'foo', 'bar' );
157 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
158 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
159 'Normal table duplication'
162 $db->duplicateTableStructure( 'foo', 'baz', true );
163 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
164 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
165 'Creation of temporary duplicate'
167 $this->assertEquals( 0,
168 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
169 'Create a temporary duplicate only'
173 public function testDuplicateTableStructureVirtual() {
174 $db = new DatabaseSqliteStandalone( ':memory:' );
175 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
176 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
178 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
180 $db->duplicateTableStructure( 'foo', 'bar' );
181 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
182 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
183 'Duplication of virtual tables'
186 $db->duplicateTableStructure( 'foo', 'baz', true );
187 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
188 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
189 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
193 public function testDeleteJoin() {
194 $db = new DatabaseSqliteStandalone( ':memory:' );
195 $db->query( 'CREATE TABLE a (a_1)', __METHOD__
);
196 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__
);
197 $db->insert( 'a', array(
204 $db->insert( 'b', array(
205 array( 'b_1' => 2, 'b_2' => 'a' ),
206 array( 'b_1' => 3, 'b_2' => 'b' ),
210 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__
);
211 $res = $db->query( "SELECT * FROM a", __METHOD__
);
212 $this->assertResultIs( array(
220 public function testEntireSchema() {
223 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
224 if ( $result !== true ) {
225 $this->fail( $result );
227 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
231 * Runs upgrades of older databases and compares results with current schema
232 * @todo: currently only checks list of tables
234 public function testUpgrades() {
235 global $IP, $wgVersion, $wgProfileToDatabase;
239 //'1.13', disabled for now, was totally screwed up
240 // SQLite wasn't included in 1.14
247 // Mismatches for these columns we can safely ignore
248 $ignoredColumns = array(
249 'user_newtalk.user_last_timestamp', // r84185
252 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
253 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
254 if ( $wgProfileToDatabase ) {
255 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
257 $currentTables = $this->getTables( $currentDB );
258 sort( $currentTables );
260 foreach ( $versions as $version ) {
261 $versions = "upgrading from $version to $wgVersion";
262 $db = $this->prepareDB( $version );
263 $tables = $this->getTables( $db );
264 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
265 foreach ( $tables as $table ) {
266 $currentCols = $this->getColumns( $currentDB, $table );
267 $cols = $this->getColumns( $db, $table );
269 array_keys( $currentCols ),
271 "Mismatching columns for table \"$table\" $versions"
273 foreach ( $currentCols as $name => $column ) {
274 $fullName = "$table.$name";
277 (bool)$cols[$name]->pk
,
278 "PRIMARY KEY status does not match for column $fullName $versions"
280 if ( !in_array( $fullName, $ignoredColumns ) ) {
282 (bool)$column->notnull
,
283 (bool)$cols[$name]->notnull
,
284 "NOT NULL status does not match for column $fullName $versions"
288 $cols[$name]->dflt_value
,
289 "Default values does not match for column $fullName $versions"
293 $currentIndexes = $this->getIndexes( $currentDB, $table );
294 $indexes = $this->getIndexes( $db, $table );
296 array_keys( $currentIndexes ),
297 array_keys( $indexes ),
298 "mismatching indexes for table \"$table\" $versions"
305 public function testInsertIdType() {
306 $db = new DatabaseSqliteStandalone( ':memory:' );
307 $this->assertInstanceOf( 'ResultWrapper',
308 $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
), "Database creationg" );
309 $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__
),
310 "Insertion worked" );
311 $this->assertEquals( "integer", gettype( $db->insertId() ), "Actual typecheck" );
312 $this->assertTrue( $db->close(), "closing database" );
315 private function prepareDB( $version ) {
316 static $maint = null;
317 if ( $maint === null ) {
318 $maint = new FakeMaintenance();
319 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
323 $db = new DatabaseSqliteStandalone( ':memory:' );
324 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
325 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
326 $updater->doUpdates( array( 'core' ) );
330 private function getTables( $db ) {
331 $list = array_flip( $db->listTables() );
333 'math', // moved out of core in 1.18
334 'trackbacks', // removed from core in 1.19
336 'searchindex_content',
337 'searchindex_segments',
338 'searchindex_segdir',
340 'searchindex_docsize',
343 foreach ( $excluded as $t ) {
346 $list = array_flip( $list );
351 private function getColumns( $db, $table ) {
353 $res = $db->query( "PRAGMA table_info($table)" );
354 $this->assertNotNull( $res );
355 foreach ( $res as $col ) {
356 $cols[$col->name
] = $col;
362 private function getIndexes( $db, $table ) {
364 $res = $db->query( "PRAGMA index_list($table)" );
365 $this->assertNotNull( $res );
366 foreach ( $res as $index ) {
367 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
368 $this->assertNotNull( $res2 );
369 $index->columns
= array();
370 foreach ( $res2 as $col ) {
371 $index->columns
[] = $col;
373 $indexes[$index->name
] = $index;