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 );
24 class DatabaseSqliteTest
extends MediaWikiTestCase
{
27 protected function setUp() {
30 if ( !Sqlite
::isPresent() ) {
31 $this->markTestSkipped( 'No SQLite support detected' );
33 $this->db
= new MockDatabaseSqlite();
34 if ( version_compare( $this->db
->getServerVersion(), '3.6.0', '<' ) ) {
35 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
39 private function replaceVars( $sql ) {
40 // normalize spacing to hide implementation details
41 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
44 private function assertResultIs( $expected, $res ) {
45 $this->assertNotNull( $res );
47 foreach( $res as $row ) {
48 foreach( $expected[$i] as $key => $value ) {
49 $this->assertTrue( isset( $row->$key ) );
50 $this->assertEquals( $value, $row->$key );
54 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
57 public static function provideAddQuotes() {
63 'foo bar', "'foo bar'"
65 array( // #2: including quote
66 'foo\'bar', "'foo''bar'"
68 array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
72 array( // #4: blob object (must be represented as hex)
80 * @dataProvider provideAddQuotes()
82 public function testAddQuotes( $value, $expected ) {
84 $db = new DatabaseSqliteStandalone( ':memory:' );
85 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
87 // ok, quoting works as expected, now try a round trip.
88 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
90 $this->assertTrue( $re !== false, 'query failed' );
92 if ( $row = $re->fetchRow() ) {
93 if ( $value instanceof Blob
) {
94 $value = $value->fetch();
97 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
99 $this->fail( 'query returned no result' );
103 public function testReplaceVars() {
104 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
106 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
107 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
108 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
109 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
112 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
113 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
116 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
117 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
120 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
121 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
125 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
126 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
128 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
129 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
132 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
133 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
136 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
137 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
141 public function testTableName() {
143 $db = new DatabaseSqliteStandalone( ':memory:' );
144 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
145 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
146 $db->tablePrefix( 'foo' );
147 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
148 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
151 public function testDuplicateTableStructure() {
152 $db = new DatabaseSqliteStandalone( ':memory:' );
153 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
155 $db->duplicateTableStructure( 'foo', 'bar' );
156 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
157 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
158 'Normal table duplication'
161 $db->duplicateTableStructure( 'foo', 'baz', true );
162 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
163 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
164 'Creation of temporary duplicate'
166 $this->assertEquals( 0,
167 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
168 'Create a temporary duplicate only'
172 public function testDuplicateTableStructureVirtual() {
173 $db = new DatabaseSqliteStandalone( ':memory:' );
174 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
175 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
177 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
179 $db->duplicateTableStructure( 'foo', 'bar' );
180 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
181 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
182 'Duplication of virtual tables'
185 $db->duplicateTableStructure( 'foo', 'baz', true );
186 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
187 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
188 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
192 public function testDeleteJoin() {
193 $db = new DatabaseSqliteStandalone( ':memory:' );
194 $db->query( 'CREATE TABLE a (a_1)', __METHOD__
);
195 $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__
);
196 $db->insert( 'a', array(
203 $db->insert( 'b', array(
204 array( 'b_1' => 2, 'b_2' => 'a' ),
205 array( 'b_1' => 3, 'b_2' => 'b' ),
209 $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__
);
210 $res = $db->query( "SELECT * FROM a", __METHOD__
);
211 $this->assertResultIs( array(
219 public function testEntireSchema() {
222 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
223 if ( $result !== true ) {
224 $this->fail( $result );
226 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
230 * Runs upgrades of older databases and compares results with current schema
231 * @todo: currently only checks list of tables
233 public function testUpgrades() {
234 global $IP, $wgVersion;
238 //'1.13', disabled for now, was totally screwed up
239 // SQLite wasn't included in 1.14
246 // Mismatches for these columns we can safely ignore
247 $ignoredColumns = array(
248 'user_newtalk.user_last_timestamp', // r84185
251 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
252 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
253 $currentTables = $this->getTables( $currentDB );
254 sort( $currentTables );
256 foreach ( $versions as $version ) {
257 $versions = "upgrading from $version to $wgVersion";
258 $db = $this->prepareDB( $version );
259 $tables = $this->getTables( $db );
260 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
261 foreach ( $tables as $table ) {
262 $currentCols = $this->getColumns( $currentDB, $table );
263 $cols = $this->getColumns( $db, $table );
265 array_keys( $currentCols ),
267 "Mismatching columns for table \"$table\" $versions"
269 foreach ( $currentCols as $name => $column ) {
270 $fullName = "$table.$name";
273 (bool)$cols[$name]->pk
,
274 "PRIMARY KEY status does not match for column $fullName $versions"
276 if ( !in_array( $fullName, $ignoredColumns ) ) {
278 (bool)$column->notnull
,
279 (bool)$cols[$name]->notnull
,
280 "NOT NULL status does not match for column $fullName $versions"
284 $cols[$name]->dflt_value
,
285 "Default values does not match for column $fullName $versions"
289 $currentIndexes = $this->getIndexes( $currentDB, $table );
290 $indexes = $this->getIndexes( $db, $table );
292 array_keys( $currentIndexes ),
293 array_keys( $indexes ),
294 "mismatching indexes for table \"$table\" $versions"
301 public function testInsertIdType() {
302 $db = new DatabaseSqliteStandalone( ':memory:' );
303 $this->assertInstanceOf( 'ResultWrapper',
304 $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__
), "Database creationg" );
305 $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__
),
306 "Insertion worked" );
307 $this->assertEquals( "integer", gettype( $db->insertId() ), "Actual typecheck" );
308 $this->assertTrue( $db->close(), "closing database" );
311 private function prepareDB( $version ) {
312 static $maint = null;
313 if ( $maint === null ) {
314 $maint = new FakeMaintenance();
315 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
319 $db = new DatabaseSqliteStandalone( ':memory:' );
320 $db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
321 $updater = DatabaseUpdater
::newForDB( $db, false, $maint );
322 $updater->doUpdates( array( 'core' ) );
326 private function getTables( $db ) {
327 $list = array_flip( $db->listTables() );
329 'math', // moved out of core in 1.18
330 'trackbacks', // removed from core in 1.19
332 'searchindex_content',
333 'searchindex_segments',
334 'searchindex_segdir',
336 'searchindex_docsize',
339 foreach ( $excluded as $t ) {
342 $list = array_flip( $list );
347 private function getColumns( $db, $table ) {
349 $res = $db->query( "PRAGMA table_info($table)" );
350 $this->assertNotNull( $res );
351 foreach ( $res as $col ) {
352 $cols[$col->name
] = $col;
358 private function getIndexes( $db, $table ) {
360 $res = $db->query( "PRAGMA index_list($table)" );
361 $this->assertNotNull( $res );
362 foreach ( $res as $index ) {
363 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
364 $this->assertNotNull( $res2 );
365 $index->columns
= array();
366 foreach ( $res2 as $col ) {
367 $index->columns
[] = $col;
369 $indexes[$index->name
] = $index;