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 protected function setUp() {
35 if ( !Sqlite
::isPresent() ) {
36 $this->markTestSkipped( 'No SQLite support detected' );
38 $this->db
= new MockDatabaseSqlite();
39 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
40 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
44 private function replaceVars( $sql ) {
45 // normalize spacing to hide implementation details
46 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
49 private function assertResultIs( $expected, $res ) {
50 $this->assertNotNull( $res );
52 foreach ( $res as $row ) {
53 foreach ( $expected[$i] as $key => $value ) {
54 $this->assertTrue( isset( $row->$key ) );
55 $this->assertEquals( $value, $row->$key );
59 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
62 public static function provideAddQuotes() {
68 'foo bar', "'foo bar'"
70 array( // #2: including quote
71 'foo\'bar', "'foo''bar'"
73 array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
77 array( // #4: blob object (must be represented as hex)
85 * @dataProvider provideAddQuotes()
87 public function testAddQuotes( $value, $expected ) {
89 $db = new DatabaseSqliteStandalone( ':memory:' );
90 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
92 // ok, quoting works as expected, now try a round trip.
93 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
95 $this->assertTrue( $re !== false, 'query failed' );
97 if ( $row = $re->fetchRow() ) {
98 if ( $value instanceof Blob
) {
99 $value = $value->fetch();
102 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
104 $this->fail( 'query returned no result' );
108 public function testReplaceVars() {
109 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
111 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
112 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
113 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
114 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
117 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
118 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
121 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
122 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
125 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
126 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
130 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
131 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
133 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
134 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
137 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
138 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
141 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
142 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
145 $this->assertEquals( "DROP INDEX foo",
146 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
149 $this->assertEquals( "DROP INDEX foo -- dropping index",
150 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
154 public function testTableName() {
156 $db = new DatabaseSqliteStandalone( ':memory:' );
157 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
158 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
159 $db->tablePrefix( 'foo' );
160 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
161 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
164 public function testDuplicateTableStructure() {
165 $db = new DatabaseSqliteStandalone( ':memory:' );
166 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
168 $db->duplicateTableStructure( 'foo', 'bar' );
169 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
170 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
171 'Normal table duplication'
174 $db->duplicateTableStructure( 'foo', 'baz', true );
175 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
176 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
177 'Creation of temporary duplicate'
179 $this->assertEquals( 0,
180 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
181 'Create a temporary duplicate only'
185 public function testDuplicateTableStructureVirtual() {
186 $db = new DatabaseSqliteStandalone( ':memory:' );
187 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
188 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
190 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
192 $db->duplicateTableStructure( 'foo', 'bar' );
193 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
194 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
195 'Duplication of virtual tables'
198 $db->duplicateTableStructure( 'foo', 'baz', true );
199 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
200 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
201 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
205 public function testDeleteJoin() {
206 $db = new DatabaseSqliteStandalone( ':memory:' );
207 $db->query( 'CREATE TABLE a (a_1)', __METHOD__
);
208 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__
);
209 $db->insert( 'a', array(
216 $db->insert( 'b', array(
217 array( 'b_1' => 2, 'b_2' => 'a' ),
218 array( 'b_1' => 3, 'b_2' => 'b' ),
222 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__
);
223 $res = $db->query( "SELECT * FROM a", __METHOD__
);
224 $this->assertResultIs( array(
232 public function testEntireSchema() {
235 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
236 if ( $result !== true ) {
237 $this->fail( $result );
239 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
243 * Runs upgrades of older databases and compares results with current schema
244 * @todo Currently only checks list of tables
246 public function testUpgrades() {
247 global $IP, $wgVersion, $wgProfileToDatabase;
251 //'1.13', disabled for now, was totally screwed up
252 // SQLite wasn't included in 1.14
259 // Mismatches for these columns we can safely ignore
260 $ignoredColumns = array(
261 'user_newtalk.user_last_timestamp', // r84185
264 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
265 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
266 if ( $wgProfileToDatabase ) {
267 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
269 $currentTables = $this->getTables( $currentDB );
270 sort( $currentTables );
272 foreach ( $versions as $version ) {
273 $versions = "upgrading from $version to $wgVersion";
274 $db = $this->prepareDB( $version );
275 $tables = $this->getTables( $db );
276 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
277 foreach ( $tables as $table ) {
278 $currentCols = $this->getColumns( $currentDB, $table );
279 $cols = $this->getColumns( $db, $table );
281 array_keys( $currentCols ),
283 "Mismatching columns for table \"$table\" $versions"
285 foreach ( $currentCols as $name => $column ) {
286 $fullName = "$table.$name";
289 (bool)$cols[$name]->pk
,
290 "PRIMARY KEY status does not match for column $fullName $versions"
292 if ( !in_array( $fullName, $ignoredColumns ) ) {
294 (bool)$column->notnull
,
295 (bool)$cols[$name]->notnull
,
296 "NOT NULL status does not match for column $fullName $versions"
300 $cols[$name]->dflt_value
,
301 "Default values does not match for column $fullName $versions"
305 $currentIndexes = $this->getIndexes( $currentDB, $table );
306 $indexes = $this->getIndexes( $db, $table );
308 array_keys( $currentIndexes ),
309 array_keys( $indexes ),
310 "mismatching indexes for table \"$table\" $versions"
317 public function testInsertIdType() {
318 $db = new DatabaseSqliteStandalone( ':memory:' );
319 $this->assertInstanceOf( 'ResultWrapper',
320 $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
), "Database creationg" );
321 $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__
),
322 "Insertion worked" );
323 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
324 $this->assertTrue( $db->close(), "closing database" );
327 private function prepareDB( $version ) {
328 static $maint = null;
329 if ( $maint === null ) {
330 $maint = new FakeMaintenance();
331 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
335 $db = new DatabaseSqliteStandalone( ':memory:' );
336 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
337 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
338 $updater->doUpdates( array( 'core' ) );
343 private function getTables( $db ) {
344 $list = array_flip( $db->listTables() );
346 'external_user', // removed from core in 1.22
347 'math', // moved out of core in 1.18
348 'trackbacks', // removed from core in 1.19
350 'searchindex_content',
351 'searchindex_segments',
352 'searchindex_segdir',
354 'searchindex_docsize',
357 foreach ( $excluded as $t ) {
360 $list = array_flip( $list );
366 private function getColumns( $db, $table ) {
368 $res = $db->query( "PRAGMA table_info($table)" );
369 $this->assertNotNull( $res );
370 foreach ( $res as $col ) {
371 $cols[$col->name
] = $col;
378 private function getIndexes( $db, $table ) {
380 $res = $db->query( "PRAGMA index_list($table)" );
381 $this->assertNotNull( $res );
382 foreach ( $res as $index ) {
383 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
384 $this->assertNotNull( $res2 );
385 $index->columns
= array();
386 foreach ( $res2 as $col ) {
387 $index->columns
[] = $col;
389 $indexes[$index->name
] = $index;
396 function testCaseInsensitiveLike() {
397 // TODO: Test this for all databases
398 $db = new DatabaseSqliteStandalone( ':memory:' );
399 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
400 $row = $res->fetchRow();
401 $this->assertFalse( (bool)$row['a'] );