2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2015-04-27 19:44:57 +00:00
|
|
|
class DatabaseSqliteMock extends DatabaseSqlite {
|
2014-04-24 16:06:21 +00:00
|
|
|
private $lastQuery;
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
public static function newInstance( array $p = array() ) {
|
|
|
|
|
$p['dbFilePath'] = ':memory:';
|
2015-04-27 19:44:57 +00:00
|
|
|
$p['schema'] = false;
|
2015-03-07 12:31:08 +00:00
|
|
|
|
2015-04-27 19:44:57 +00:00
|
|
|
return DatabaseBase::factory( 'SqliteMock', $p );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query( $sql, $fname = '', $tempIgnore = false ) {
|
|
|
|
|
$this->lastQuery = $sql;
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-28 10:27:15 +00:00
|
|
|
/**
|
|
|
|
|
* Override parent visibility to public
|
|
|
|
|
*/
|
|
|
|
|
public function replaceVars( $s ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
return parent::replaceVars( $s );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group sqlite
|
2012-01-18 15:50:00 +00:00
|
|
|
* @group Database
|
2012-12-03 08:17:24 +00:00
|
|
|
* @group medium
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class DatabaseSqliteTest extends MediaWikiTestCase {
|
2015-04-27 19:44:57 +00:00
|
|
|
/** @var DatabaseSqliteMock */
|
2014-04-24 16:06:21 +00:00
|
|
|
protected $db;
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
if ( !Sqlite::isPresent() ) {
|
|
|
|
|
$this->markTestSkipped( 'No SQLite support detected' );
|
|
|
|
|
}
|
2015-04-27 19:44:57 +00:00
|
|
|
$this->db = DatabaseSqliteMock::newInstance();
|
2011-05-09 16:41:51 +00:00
|
|
|
if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
|
|
|
|
|
$this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function replaceVars( $sql ) {
|
|
|
|
|
// normalize spacing to hide implementation details
|
|
|
|
|
return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-18 20:37:02 +00:00
|
|
|
private function assertResultIs( $expected, $res ) {
|
|
|
|
|
$this->assertNotNull( $res );
|
|
|
|
|
$i = 0;
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
foreach ( $expected[$i] as $key => $value ) {
|
2011-06-18 20:37:02 +00:00
|
|
|
$this->assertTrue( isset( $row->$key ) );
|
|
|
|
|
$this->assertEquals( $value, $row->$key );
|
|
|
|
|
}
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-31 14:27:57 +00:00
|
|
|
public static function provideAddQuotes() {
|
|
|
|
|
return array(
|
|
|
|
|
array( // #0: empty
|
|
|
|
|
'', "''"
|
|
|
|
|
),
|
|
|
|
|
array( // #1: simple
|
|
|
|
|
'foo bar', "'foo bar'"
|
|
|
|
|
),
|
|
|
|
|
array( // #2: including quote
|
|
|
|
|
'foo\'bar', "'foo''bar'"
|
|
|
|
|
),
|
2014-04-24 16:06:21 +00:00
|
|
|
// #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
|
|
|
|
|
array(
|
2012-10-31 14:27:57 +00:00
|
|
|
"x\0y",
|
|
|
|
|
"x'780079'",
|
|
|
|
|
),
|
|
|
|
|
array( // #4: blob object (must be represented as hex)
|
|
|
|
|
new Blob( "hello" ),
|
|
|
|
|
"x'68656c6c6f'",
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideAddQuotes()
|
2013-10-18 10:36:09 +00:00
|
|
|
* @covers DatabaseSqlite::addQuotes
|
2012-10-31 14:27:57 +00:00
|
|
|
*/
|
|
|
|
|
public function testAddQuotes( $value, $expected ) {
|
|
|
|
|
// check quoting
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2012-10-31 14:27:57 +00:00
|
|
|
$this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
|
|
|
|
|
|
|
|
|
|
// ok, quoting works as expected, now try a round trip.
|
|
|
|
|
$re = $db->query( 'select ' . $db->addQuotes( $value ) );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $re !== false, 'query failed' );
|
|
|
|
|
|
|
|
|
|
if ( $row = $re->fetchRow() ) {
|
|
|
|
|
if ( $value instanceof Blob ) {
|
|
|
|
|
$value = $value->fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $value, $row[0], 'string mangled by the database' );
|
|
|
|
|
} else {
|
|
|
|
|
$this->fail( 'query returned no result' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::replaceVars
|
|
|
|
|
*/
|
2010-12-14 16:26:35 +00:00
|
|
|
public function testReplaceVars() {
|
|
|
|
|
$this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
|
|
|
|
|
|
2014-04-24 16:06:21 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
|
2013-02-14 13:10:38 +00:00
|
|
|
. "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
|
2014-04-24 16:06:21 +00:00
|
|
|
$this->replaceVars(
|
|
|
|
|
"CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, "
|
|
|
|
|
. "foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', "
|
|
|
|
|
. "foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;"
|
|
|
|
|
)
|
2013-02-14 13:10:38 +00:00
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2014-04-24 16:06:21 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
|
|
|
|
|
$this->replaceVars(
|
|
|
|
|
"CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );"
|
|
|
|
|
)
|
2013-02-14 13:10:38 +00:00
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
|
2013-02-14 13:10:38 +00:00
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
|
|
|
|
|
'Table name changed'
|
2013-02-14 13:10:38 +00:00
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-04-10 08:44:06 +00:00
|
|
|
$this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
|
2013-02-14 13:10:38 +00:00
|
|
|
$this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
|
|
|
|
|
);
|
2011-04-10 08:44:06 +00:00
|
|
|
$this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
|
2013-02-14 13:10:38 +00:00
|
|
|
$this->replaceVars( "CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
|
|
|
|
|
);
|
2011-04-10 08:44:06 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
|
2013-02-14 13:10:38 +00:00
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
|
|
|
|
|
$this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
|
2013-02-14 13:10:38 +00:00
|
|
|
);
|
2013-10-15 19:46:39 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( "DROP INDEX foo",
|
|
|
|
|
$this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar" )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "DROP INDEX foo -- dropping index",
|
|
|
|
|
$this->replaceVars( "DROP INDEX /*i*/foo ON /*_*/bar -- dropping index" )
|
|
|
|
|
);
|
2014-09-08 19:16:02 +00:00
|
|
|
$this->assertEquals( "INSERT OR IGNORE INTO foo VALUES ('bar')",
|
|
|
|
|
$this->replaceVars( "INSERT OR IGNORE INTO foo VALUES ('bar')" )
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::tableName
|
|
|
|
|
*/
|
2010-12-14 16:26:35 +00:00
|
|
|
public function testTableName() {
|
|
|
|
|
// @todo Moar!
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( 'foo', $db->tableName( 'foo' ) );
|
|
|
|
|
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
|
|
|
|
|
$db->tablePrefix( 'foo' );
|
|
|
|
|
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
|
|
|
|
|
$this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
|
|
|
|
|
}
|
2011-09-28 19:28:19 +00:00
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::duplicateTableStructure
|
|
|
|
|
*/
|
2011-02-26 16:45:35 +00:00
|
|
|
public function testDuplicateTableStructure() {
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2011-02-26 16:45:35 +00:00
|
|
|
$db->query( 'CREATE TABLE foo(foo, barfoo)' );
|
2015-08-06 09:25:16 +00:00
|
|
|
$db->query( 'CREATE INDEX index1 ON foo(foo)' );
|
|
|
|
|
$db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' );
|
2011-02-26 16:45:35 +00:00
|
|
|
|
|
|
|
|
$db->duplicateTableStructure( 'foo', 'bar' );
|
2011-04-12 20:48:19 +00:00
|
|
|
$this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
|
2011-02-26 16:45:35 +00:00
|
|
|
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
|
|
|
|
|
'Normal table duplication'
|
|
|
|
|
);
|
2015-08-06 09:25:16 +00:00
|
|
|
$indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' );
|
|
|
|
|
$index = $indexList->next();
|
|
|
|
|
$this->assertEquals( 'bar_index1', $index->name );
|
|
|
|
|
$this->assertEquals( '0', $index->unique );
|
|
|
|
|
$index = $indexList->next();
|
|
|
|
|
$this->assertEquals( 'bar_index2', $index->name );
|
|
|
|
|
$this->assertEquals( '1', $index->unique );
|
2011-02-26 16:45:35 +00:00
|
|
|
|
|
|
|
|
$db->duplicateTableStructure( 'foo', 'baz', true );
|
2011-04-12 20:48:19 +00:00
|
|
|
$this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
|
2011-02-26 16:45:35 +00:00
|
|
|
$db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
|
|
|
|
|
'Creation of temporary duplicate'
|
|
|
|
|
);
|
2015-08-06 09:25:16 +00:00
|
|
|
$indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' );
|
|
|
|
|
$index = $indexList->next();
|
|
|
|
|
$this->assertEquals( 'baz_index1', $index->name );
|
|
|
|
|
$this->assertEquals( '0', $index->unique );
|
|
|
|
|
$index = $indexList->next();
|
|
|
|
|
$this->assertEquals( 'baz_index2', $index->name );
|
|
|
|
|
$this->assertEquals( '1', $index->unique );
|
2011-02-26 16:45:35 +00:00
|
|
|
$this->assertEquals( 0,
|
|
|
|
|
$db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
|
|
|
|
|
'Create a temporary duplicate only'
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-09-28 19:28:19 +00:00
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::duplicateTableStructure
|
|
|
|
|
*/
|
2011-02-26 16:45:35 +00:00
|
|
|
public function testDuplicateTableStructureVirtual() {
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2011-02-26 16:45:35 +00:00
|
|
|
if ( $db->getFulltextSearchModule() != 'FTS3' ) {
|
|
|
|
|
$this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
|
|
|
|
|
}
|
2011-04-12 20:48:19 +00:00
|
|
|
$db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
|
2011-02-26 16:45:35 +00:00
|
|
|
|
|
|
|
|
$db->duplicateTableStructure( 'foo', 'bar' );
|
2011-04-12 20:48:19 +00:00
|
|
|
$this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
|
2011-02-26 16:45:35 +00:00
|
|
|
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
|
|
|
|
|
'Duplication of virtual tables'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$db->duplicateTableStructure( 'foo', 'baz', true );
|
2011-04-12 20:48:19 +00:00
|
|
|
$this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
|
2011-02-26 16:45:35 +00:00
|
|
|
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
|
|
|
|
|
"Can't create temporary virtual tables, should fall back to non-temporary duplication"
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::deleteJoin
|
|
|
|
|
*/
|
2011-06-18 20:37:02 +00:00
|
|
|
public function testDeleteJoin() {
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2011-06-18 20:37:02 +00:00
|
|
|
$db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
|
|
|
|
|
$db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
|
|
|
|
|
$db->insert( 'a', array(
|
|
|
|
|
array( 'a_1' => 1 ),
|
|
|
|
|
array( 'a_1' => 2 ),
|
|
|
|
|
array( 'a_1' => 3 ),
|
|
|
|
|
),
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$db->insert( 'b', array(
|
|
|
|
|
array( 'b_1' => 2, 'b_2' => 'a' ),
|
|
|
|
|
array( 'b_1' => 3, 'b_2' => 'b' ),
|
|
|
|
|
),
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
|
|
|
|
|
$res = $db->query( "SELECT * FROM a", __METHOD__ );
|
|
|
|
|
$this->assertResultIs( array(
|
|
|
|
|
array( 'a_1' => 1 ),
|
|
|
|
|
array( 'a_1' => 3 ),
|
|
|
|
|
),
|
|
|
|
|
$res
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-09 16:41:51 +00:00
|
|
|
public function testEntireSchema() {
|
2010-12-14 16:26:35 +00:00
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
$result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
|
|
|
|
|
if ( $result !== true ) {
|
|
|
|
|
$this->fail( $result );
|
|
|
|
|
}
|
2011-06-13 19:22:52 +00:00
|
|
|
$this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2011-05-09 16:41:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Runs upgrades of older databases and compares results with current schema
|
2013-05-15 01:12:35 +00:00
|
|
|
* @todo Currently only checks list of tables
|
2011-05-09 16:41:51 +00:00
|
|
|
*/
|
|
|
|
|
public function testUpgrades() {
|
2014-12-03 18:36:12 +00:00
|
|
|
global $IP, $wgVersion, $wgProfiler;
|
2011-05-09 18:49:56 +00:00
|
|
|
|
|
|
|
|
// Versions tested
|
|
|
|
|
$versions = array(
|
2015-09-11 13:44:59 +00:00
|
|
|
// '1.13', disabled for now, was totally screwed up
|
2011-05-09 18:49:56 +00:00
|
|
|
// SQLite wasn't included in 1.14
|
|
|
|
|
'1.15',
|
|
|
|
|
'1.16',
|
|
|
|
|
'1.17',
|
2011-12-11 16:56:37 +00:00
|
|
|
'1.18',
|
2011-05-09 18:49:56 +00:00
|
|
|
);
|
2011-05-09 16:41:51 +00:00
|
|
|
|
2011-05-09 18:49:56 +00:00
|
|
|
// Mismatches for these columns we can safely ignore
|
|
|
|
|
$ignoredColumns = array(
|
|
|
|
|
'user_newtalk.user_last_timestamp', // r84185
|
|
|
|
|
);
|
2011-09-28 19:28:19 +00:00
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
$currentDB = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2011-05-09 16:41:51 +00:00
|
|
|
$currentDB->sourceFile( "$IP/maintenance/tables.sql" );
|
2014-12-03 18:36:12 +00:00
|
|
|
|
|
|
|
|
$profileToDb = false;
|
|
|
|
|
if ( isset( $wgProfiler['output'] ) ) {
|
|
|
|
|
$out = $wgProfiler['output'];
|
|
|
|
|
if ( $out === 'db' ) {
|
|
|
|
|
$profileToDb = true;
|
2014-11-27 17:03:07 +00:00
|
|
|
} elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
|
2014-12-03 18:36:12 +00:00
|
|
|
$profileToDb = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $profileToDb ) {
|
2013-01-02 13:52:02 +00:00
|
|
|
$currentDB->sourceFile( "$IP/maintenance/sqlite/archives/patch-profiling.sql" );
|
|
|
|
|
}
|
2011-05-09 16:41:51 +00:00
|
|
|
$currentTables = $this->getTables( $currentDB );
|
|
|
|
|
sort( $currentTables );
|
|
|
|
|
|
|
|
|
|
foreach ( $versions as $version ) {
|
2011-05-09 18:49:56 +00:00
|
|
|
$versions = "upgrading from $version to $wgVersion";
|
2011-05-09 16:41:51 +00:00
|
|
|
$db = $this->prepareDB( $version );
|
|
|
|
|
$tables = $this->getTables( $db );
|
2011-05-09 18:49:56 +00:00
|
|
|
$this->assertEquals( $currentTables, $tables, "Different tables $versions" );
|
|
|
|
|
foreach ( $tables as $table ) {
|
|
|
|
|
$currentCols = $this->getColumns( $currentDB, $table );
|
|
|
|
|
$cols = $this->getColumns( $db, $table );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
array_keys( $currentCols ),
|
|
|
|
|
array_keys( $cols ),
|
2011-06-15 17:59:02 +00:00
|
|
|
"Mismatching columns for table \"$table\" $versions"
|
2011-05-09 18:49:56 +00:00
|
|
|
);
|
|
|
|
|
foreach ( $currentCols as $name => $column ) {
|
|
|
|
|
$fullName = "$table.$name";
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
(bool)$column->pk,
|
|
|
|
|
(bool)$cols[$name]->pk,
|
|
|
|
|
"PRIMARY KEY status does not match for column $fullName $versions"
|
|
|
|
|
);
|
|
|
|
|
if ( !in_array( $fullName, $ignoredColumns ) ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
(bool)$column->notnull,
|
|
|
|
|
(bool)$cols[$name]->notnull,
|
|
|
|
|
"NOT NULL status does not match for column $fullName $versions"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$column->dflt_value,
|
|
|
|
|
$cols[$name]->dflt_value,
|
|
|
|
|
"Default values does not match for column $fullName $versions"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-15 17:59:02 +00:00
|
|
|
$currentIndexes = $this->getIndexes( $currentDB, $table );
|
|
|
|
|
$indexes = $this->getIndexes( $db, $table );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
array_keys( $currentIndexes ),
|
|
|
|
|
array_keys( $indexes ),
|
|
|
|
|
"mismatching indexes for table \"$table\" $versions"
|
|
|
|
|
);
|
2011-05-09 18:49:56 +00:00
|
|
|
}
|
2011-05-09 16:41:51 +00:00
|
|
|
$db->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::insertId
|
|
|
|
|
*/
|
2012-04-06 18:54:24 +00:00
|
|
|
public function testInsertIdType() {
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2013-10-18 10:36:09 +00:00
|
|
|
|
|
|
|
|
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
|
|
|
|
|
$this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
|
|
|
|
|
|
|
|
|
|
$insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
|
|
|
|
|
$this->assertTrue( $insertion, "Insertion worked" );
|
|
|
|
|
|
2013-03-15 21:50:42 +00:00
|
|
|
$this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
|
2012-04-06 18:54:24 +00:00
|
|
|
$this->assertTrue( $db->close(), "closing database" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-09 16:41:51 +00:00
|
|
|
private function prepareDB( $version ) {
|
|
|
|
|
static $maint = null;
|
|
|
|
|
if ( $maint === null ) {
|
|
|
|
|
$maint = new FakeMaintenance();
|
|
|
|
|
$maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
|
|
|
|
|
}
|
2011-09-28 19:28:19 +00:00
|
|
|
|
2011-12-22 09:18:39 +00:00
|
|
|
global $IP;
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2011-12-22 09:18:39 +00:00
|
|
|
$db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
|
2011-05-09 16:41:51 +00:00
|
|
|
$updater = DatabaseUpdater::newForDB( $db, false, $maint );
|
|
|
|
|
$updater->doUpdates( array( 'core' ) );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-05-09 16:41:51 +00:00
|
|
|
return $db;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-09 18:49:56 +00:00
|
|
|
private function getTables( $db ) {
|
2011-05-09 16:41:51 +00:00
|
|
|
$list = array_flip( $db->listTables() );
|
|
|
|
|
$excluded = array(
|
2013-05-02 19:07:18 +00:00
|
|
|
'external_user', // removed from core in 1.22
|
2011-05-09 16:41:51 +00:00
|
|
|
'math', // moved out of core in 1.18
|
2011-12-11 16:56:37 +00:00
|
|
|
'trackbacks', // removed from core in 1.19
|
2011-05-09 16:41:51 +00:00
|
|
|
'searchindex',
|
|
|
|
|
'searchindex_content',
|
|
|
|
|
'searchindex_segments',
|
|
|
|
|
'searchindex_segdir',
|
|
|
|
|
// FTS4 ready!!1
|
|
|
|
|
'searchindex_docsize',
|
|
|
|
|
'searchindex_stat',
|
|
|
|
|
);
|
|
|
|
|
foreach ( $excluded as $t ) {
|
|
|
|
|
unset( $list[$t] );
|
|
|
|
|
}
|
|
|
|
|
$list = array_flip( $list );
|
|
|
|
|
sort( $list );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-05-09 16:41:51 +00:00
|
|
|
return $list;
|
|
|
|
|
}
|
2011-05-09 18:49:56 +00:00
|
|
|
|
|
|
|
|
private function getColumns( $db, $table ) {
|
|
|
|
|
$cols = array();
|
|
|
|
|
$res = $db->query( "PRAGMA table_info($table)" );
|
|
|
|
|
$this->assertNotNull( $res );
|
|
|
|
|
foreach ( $res as $col ) {
|
|
|
|
|
$cols[$col->name] = $col;
|
|
|
|
|
}
|
|
|
|
|
ksort( $cols );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-05-09 18:49:56 +00:00
|
|
|
return $cols;
|
|
|
|
|
}
|
2011-06-15 17:59:02 +00:00
|
|
|
|
|
|
|
|
private function getIndexes( $db, $table ) {
|
|
|
|
|
$indexes = array();
|
|
|
|
|
$res = $db->query( "PRAGMA index_list($table)" );
|
|
|
|
|
$this->assertNotNull( $res );
|
|
|
|
|
foreach ( $res as $index ) {
|
|
|
|
|
$res2 = $db->query( "PRAGMA index_info({$index->name})" );
|
|
|
|
|
$this->assertNotNull( $res2 );
|
|
|
|
|
$index->columns = array();
|
|
|
|
|
foreach ( $res2 as $col ) {
|
|
|
|
|
$index->columns[] = $col;
|
|
|
|
|
}
|
|
|
|
|
$indexes[$index->name] = $index;
|
|
|
|
|
}
|
|
|
|
|
ksort( $indexes );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-06-15 17:59:02 +00:00
|
|
|
return $indexes;
|
|
|
|
|
}
|
2012-12-24 20:10:22 +00:00
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCaseInsensitiveLike() {
|
2012-12-24 20:10:22 +00:00
|
|
|
// TODO: Test this for all databases
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2013-02-14 13:10:38 +00:00
|
|
|
$res = $db->query( 'SELECT "a" LIKE "A" AS a' );
|
|
|
|
|
$row = $res->fetchRow();
|
|
|
|
|
$this->assertFalse( (bool)$row['a'] );
|
2012-12-24 20:10:22 +00:00
|
|
|
}
|
2014-05-21 11:38:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers DatabaseSqlite::numFields
|
|
|
|
|
*/
|
|
|
|
|
public function testNumFields() {
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
2014-05-21 11:38:56 +00:00
|
|
|
|
|
|
|
|
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
|
|
|
|
|
$this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );
|
2014-07-19 21:12:10 +00:00
|
|
|
$res = $db->select( 'a', '*' );
|
|
|
|
|
$this->assertEquals( 0, $db->numFields( $res ), "expects to get 0 fields for an empty table" );
|
2014-05-21 11:38:56 +00:00
|
|
|
$insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
|
|
|
|
|
$this->assertTrue( $insertion, "Insertion failed" );
|
2014-07-19 21:12:10 +00:00
|
|
|
$res = $db->select( 'a', '*' );
|
|
|
|
|
$this->assertEquals( 1, $db->numFields( $res ), "wrong number of fields" );
|
2014-05-21 11:38:56 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( $db->close(), "closing database" );
|
|
|
|
|
}
|
2015-10-05 20:42:28 +00:00
|
|
|
|
|
|
|
|
public function testToString() {
|
|
|
|
|
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
|
|
|
|
|
|
|
|
|
|
$toString = (string)$db;
|
|
|
|
|
|
|
|
|
|
$this->assertContains( 'SQLite ', $toString );
|
|
|
|
|
}
|
2011-04-12 20:48:19 +00:00
|
|
|
}
|