3 use Psr\Log\NullLogger
;
4 use Wikimedia\Rdbms\Blob
;
5 use Wikimedia\Rdbms\Database
;
6 use Wikimedia\Rdbms\DatabaseSqlite
;
7 use Wikimedia\Rdbms\ResultWrapper
;
8 use Wikimedia\Rdbms\TransactionProfiler
;
9 use Wikimedia\TestingAccessWrapper
;
16 class DatabaseSqliteTest
extends \MediaWikiIntegrationTestCase
{
17 /** @var DatabaseSqlite */
20 protected function setUp() {
23 if ( !Sqlite
::isPresent() ) {
24 $this->markTestSkipped( 'No SQLite support detected' );
26 $this->db
= $this->getMockBuilder( DatabaseSqlite
::class )
27 ->setConstructorArgs( [ [
28 'dbFilePath' => ':memory:',
36 'agent' => 'unit-tests',
37 'flags' => DBO_DEFAULT
,
40 'trxProfiler' => new TransactionProfiler(),
41 'connLogger' => new NullLogger(),
42 'queryLogger' => new NullLogger(),
43 'errorLogger' => null,
44 'deprecationLogger' => null,
45 ] ] )->setMethods( [ 'query' ] )
47 $this->db
->initConnection();
48 $this->db
->method( 'query' )->willReturn( true );
49 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
50 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
56 * @return string|string[]|null
58 private function replaceVars( $sql ) {
59 $wrapper = TestingAccessWrapper
::newFromObject( $this->db
);
60 // normalize spacing to hide implementation details
61 return preg_replace( '/\s+/', ' ', $wrapper->replaceVars( $sql ) );
64 private function assertResultIs( $expected, $res ) {
65 $this->assertNotNull( $res );
67 foreach ( $res as $row ) {
68 foreach ( $expected[$i] as $key => $value ) {
69 $this->assertTrue( isset( $row->$key ) );
70 $this->assertEquals( $value, $row->$key );
74 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
77 public static function provideAddQuotes() {
83 'foo bar', "'foo bar'"
85 [ // #2: including quote
86 'foo\'bar', "'foo''bar'"
88 // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
93 [ // #4: blob object (must be represented as hex)
105 * @dataProvider provideAddQuotes()
106 * @covers DatabaseSqlite::addQuotes
108 public function testAddQuotes( $value, $expected ) {
110 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
111 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
113 // ok, quoting works as expected, now try a round trip.
114 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
116 $this->assertTrue( $re !== false, 'query failed' );
118 $row = $re->fetchRow();
120 if ( $value instanceof Blob
) {
121 $value = $value->fetch();
124 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
126 $this->fail( 'query returned no result' );
131 * @covers DatabaseSqlite::replaceVars
133 public function testReplaceVars() {
134 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
137 "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
138 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
140 "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, "
141 . "foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', "
142 . "foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;"
147 "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
149 "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
153 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
154 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
157 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
158 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
162 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
163 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
165 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
166 $this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
169 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
170 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
173 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
174 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
177 $this->assertEquals( "DROP INDEX foo",
178 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
181 $this->assertEquals( "DROP INDEX foo -- dropping index",
182 $this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
184 $this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
185 $this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
190 * @covers DatabaseSqlite::tableName
192 public function testTableName() {
194 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
195 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
196 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
197 $db->tablePrefix( 'foo_' );
198 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
199 $this->assertEquals( 'foo_bar', $db->tableName( 'bar' ) );
203 * @covers DatabaseSqlite::duplicateTableStructure
205 public function testDuplicateTableStructure() {
206 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
207 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
208 $db->query( 'CREATE INDEX index1 ON foo(foo)' );
209 $db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' );
211 $db->duplicateTableStructure( 'foo', 'bar' );
212 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
213 $db->selectField( 'sqlite_master', 'sql', [ 'name' => 'bar' ] ),
214 'Normal table duplication'
216 $indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' );
217 $index = $indexList->next();
218 $this->assertEquals( 'bar_index1', $index->name
);
219 $this->assertEquals( '0', $index->unique
);
220 $index = $indexList->next();
221 $this->assertEquals( 'bar_index2', $index->name
);
222 $this->assertEquals( '1', $index->unique
);
224 $db->duplicateTableStructure( 'foo', 'baz', true );
225 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
226 $db->selectField( 'sqlite_temp_master', 'sql', [ 'name' => 'baz' ] ),
227 'Creation of temporary duplicate'
229 $indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' );
230 $index = $indexList->next();
231 $this->assertEquals( 'baz_index1', $index->name
);
232 $this->assertEquals( '0', $index->unique
);
233 $index = $indexList->next();
234 $this->assertEquals( 'baz_index2', $index->name
);
235 $this->assertEquals( '1', $index->unique
);
236 $this->assertSame( 0,
237 $db->selectField( 'sqlite_master', 'COUNT(*)', [ 'name' => 'baz' ] ),
238 'Create a temporary duplicate only'
243 * @covers DatabaseSqlite::duplicateTableStructure
245 public function testDuplicateTableStructureVirtual() {
246 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
247 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
248 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
250 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
252 $db->duplicateTableStructure( 'foo', 'bar' );
253 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
254 $db->selectField( 'sqlite_master', 'sql', [ 'name' => 'bar' ] ),
255 'Duplication of virtual tables'
258 $db->duplicateTableStructure( 'foo', 'baz', true );
259 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
260 $db->selectField( 'sqlite_master', 'sql', [ 'name' => 'baz' ] ),
261 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
266 * @covers DatabaseSqlite::deleteJoin
268 public function testDeleteJoin() {
269 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
270 $db->query( 'CREATE TABLE a (a_1)', __METHOD__
);
271 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__
);
280 [ 'b_1' => 2, 'b_2' => 'a' ],
281 [ 'b_1' => 3, 'b_2' => 'b' ],
285 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', [ 'b_2' => 'a' ], __METHOD__
);
286 $res = $db->query( "SELECT * FROM a", __METHOD__
);
287 $this->assertResultIs( [
298 public function testEntireSchema() {
301 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
302 if ( $result !== true ) {
303 $this->fail( $result );
305 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
309 * Runs upgrades of older databases and compares results with current schema
310 * @todo Currently only checks list of tables
313 public function testUpgrades() {
314 global $IP, $wgVersion, $wgProfiler;
318 // '1.13', disabled for now, was totally screwed up
319 // SQLite wasn't included in 1.14
331 // Mismatches for these columns we can safely ignore
333 'user_newtalk.user_last_timestamp', // r84185
336 $currentDB = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
337 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
339 $profileToDb = false;
340 if ( isset( $wgProfiler['output'] ) ) {
341 $out = $wgProfiler['output'];
342 if ( $out === 'db' ) {
344 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
349 if ( $profileToDb ) {
350 $currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
352 $currentTables = $this->getTables( $currentDB );
353 sort( $currentTables );
355 foreach ( $versions as $version ) {
356 $versions = "upgrading from $version to $wgVersion";
357 $db = $this->prepareTestDB( $version );
358 $tables = $this->getTables( $db );
359 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
360 foreach ( $tables as $table ) {
361 $currentCols = $this->getColumns( $currentDB, $table );
362 $cols = $this->getColumns( $db, $table );
364 array_keys( $currentCols ),
366 "Mismatching columns for table \"$table\" $versions"
368 foreach ( $currentCols as $name => $column ) {
369 $fullName = "$table.$name";
372 (bool)$cols[$name]->pk
,
373 "PRIMARY KEY status does not match for column $fullName $versions"
375 if ( !in_array( $fullName, $ignoredColumns ) ) {
377 (bool)$column->notnull
,
378 (bool)$cols[$name]->notnull
,
379 "NOT NULL status does not match for column $fullName $versions"
383 $cols[$name]->dflt_value
,
384 "Default values does not match for column $fullName $versions"
388 $currentIndexes = $this->getIndexes( $currentDB, $table );
389 $indexes = $this->getIndexes( $db, $table );
391 array_keys( $currentIndexes ),
392 array_keys( $indexes ),
393 "mismatching indexes for table \"$table\" $versions"
401 * @covers DatabaseSqlite::insertId
403 public function testInsertIdType() {
404 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
406 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
407 $this->assertInstanceOf( ResultWrapper
::class, $databaseCreation, "Database creation" );
409 $insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__
);
410 $this->assertTrue( $insertion, "Insertion worked" );
412 $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
413 $this->assertTrue( $db->close(), "closing database" );
417 * @covers DatabaseSqlite::insert
419 public function testInsertAffectedRows() {
420 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
421 $db->query( 'CREATE TABLE testInsertAffectedRows ( foo )', __METHOD__
);
423 $insertion = $db->insert(
424 'testInsertAffectedRows',
432 $this->assertTrue( $insertion, "Insertion worked" );
434 $this->assertSame( 3, $db->affectedRows() );
435 $this->assertTrue( $db->close(), "closing database" );
438 private function prepareTestDB( $version ) {
439 static $maint = null;
440 if ( $maint === null ) {
441 $maint = new FakeMaintenance();
442 $maint->loadParamsAndArgs( null, [ 'quiet' => 1 ] );
446 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
447 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
448 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
449 $updater->doUpdates( [ 'core' ] );
454 private function getTables( $db ) {
455 $list = array_flip( $db->listTables() );
457 'external_user', // removed from core in 1.22
458 'math', // moved out of core in 1.18
459 'trackbacks', // removed from core in 1.19
461 'searchindex_content',
462 'searchindex_segments',
463 'searchindex_segdir',
465 'searchindex_docsize',
468 foreach ( $excluded as $t ) {
471 $list = array_flip( $list );
477 private function getColumns( $db, $table ) {
479 $res = $db->query( "PRAGMA table_info($table)" );
480 $this->assertNotNull( $res );
481 foreach ( $res as $col ) {
482 $cols[$col->name
] = $col;
489 private function getIndexes( $db, $table ) {
491 $res = $db->query( "PRAGMA index_list($table)" );
492 $this->assertNotNull( $res );
493 foreach ( $res as $index ) {
494 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
495 $this->assertNotNull( $res2 );
496 $index->columns
= [];
497 foreach ( $res2 as $col ) {
498 $index->columns
[] = $col;
500 $indexes[$index->name
] = $index;
510 public function testCaseInsensitiveLike() {
511 // TODO: Test this for all databases
512 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
513 $res = $db->query( 'SELECT "a" LIKE "A" AS a' );
514 $row = $res->fetchRow();
515 $this->assertFalse( (bool)$row['a'] );
519 * @covers DatabaseSqlite::numFields
521 public function testNumFields() {
522 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
524 $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
);
525 $this->assertInstanceOf( ResultWrapper
::class, $databaseCreation, "Failed to create table a" );
526 $res = $db->select( 'a', '*' );
527 $this->assertSame( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
528 $insertion = $db->insert( 'a', [ 'a_1' => 10 ], __METHOD__
);
529 $this->assertTrue( $insertion, "Insertion failed" );
530 $res = $db->select( 'a', '*' );
531 $this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
533 $this->assertTrue( $db->close(), "closing database" );
537 * @covers \Wikimedia\Rdbms\DatabaseSqlite::__toString
539 public function testToString() {
540 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
542 $toString = (string)$db;
544 $this->assertContains( 'sqlite object', $toString );
548 * @covers \Wikimedia\Rdbms\DatabaseSqlite::getAttributes()
550 public function testsAttributes() {
551 $attributes = Database
::attributesFromType( 'sqlite' );
552 $this->assertTrue( $attributes[Database
::ATTR_DB_LEVEL_LOCKING
] );