Fix Sqlite upgrade test when profiling to database is enabled
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
5
6 function __construct( ) {
7 parent::__construct( ':memory:' );
8 }
9
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery = $sql;
12 return true;
13 }
14
15 function replaceVars( $s ) {
16 return parent::replaceVars( $s );
17 }
18 }
19
20 /**
21 * @group sqlite
22 * @group Database
23 * @group medium
24 */
25 class DatabaseSqliteTest extends MediaWikiTestCase {
26 var $db;
27
28 protected function setUp() {
29 parent::setUp();
30
31 if ( !Sqlite::isPresent() ) {
32 $this->markTestSkipped( 'No SQLite support detected' );
33 }
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" );
37 }
38 }
39
40 private function replaceVars( $sql ) {
41 // normalize spacing to hide implementation details
42 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
43 }
44
45 private function assertResultIs( $expected, $res ) {
46 $this->assertNotNull( $res );
47 $i = 0;
48 foreach( $res as $row ) {
49 foreach( $expected[$i] as $key => $value ) {
50 $this->assertTrue( isset( $row->$key ) );
51 $this->assertEquals( $value, $row->$key );
52 }
53 $i++;
54 }
55 $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
56 }
57
58 public static function provideAddQuotes() {
59 return array(
60 array( // #0: empty
61 '', "''"
62 ),
63 array( // #1: simple
64 'foo bar', "'foo bar'"
65 ),
66 array( // #2: including quote
67 'foo\'bar', "'foo''bar'"
68 ),
69 array( // #3: including \0 (must be represented as hex, per https://bugs.php.net/bug.php?id=63419)
70 "x\0y",
71 "x'780079'",
72 ),
73 array( // #4: blob object (must be represented as hex)
74 new Blob( "hello" ),
75 "x'68656c6c6f'",
76 ),
77 );
78 }
79
80 /**
81 * @dataProvider provideAddQuotes()
82 */
83 public function testAddQuotes( $value, $expected ) {
84 // check quoting
85 $db = new DatabaseSqliteStandalone( ':memory:' );
86 $this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
87
88 // ok, quoting works as expected, now try a round trip.
89 $re = $db->query( 'select ' . $db->addQuotes( $value ) );
90
91 $this->assertTrue( $re !== false, 'query failed' );
92
93 if ( $row = $re->fetchRow() ) {
94 if ( $value instanceof Blob ) {
95 $value = $value->fetch();
96 }
97
98 $this->assertEquals( $value, $row[0], 'string mangled by the database' );
99 } else {
100 $this->fail( 'query returned no result' );
101 }
102 }
103
104 public function testReplaceVars() {
105 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
106
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;" )
111 );
112
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 );" )
115 );
116
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) );" )
119 );
120
121 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
122 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
123 'Table name changed'
124 );
125
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 );" )
128 );
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 );" )
131 );
132
133 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
134 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
135 );
136
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" )
139 );
140 }
141
142 public function testTableName() {
143 // @todo Moar!
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' ) );
150 }
151
152 public function testDuplicateTableStructure() {
153 $db = new DatabaseSqliteStandalone( ':memory:' );
154 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
155
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'
160 );
161
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'
166 );
167 $this->assertEquals( 0,
168 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
169 'Create a temporary duplicate only'
170 );
171 }
172
173 public function testDuplicateTableStructureVirtual() {
174 $db = new DatabaseSqliteStandalone( ':memory:' );
175 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
176 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
177 }
178 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
179
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'
184 );
185
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"
190 );
191 }
192
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(
198 array( 'a_1' => 1 ),
199 array( 'a_1' => 2 ),
200 array( 'a_1' => 3 ),
201 ),
202 __METHOD__
203 );
204 $db->insert( 'b', array(
205 array( 'b_1' => 2, 'b_2' => 'a' ),
206 array( 'b_1' => 3, 'b_2' => 'b' ),
207 ),
208 __METHOD__
209 );
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(
213 array( 'a_1' => 1 ),
214 array( 'a_1' => 3 ),
215 ),
216 $res
217 );
218 }
219
220 public function testEntireSchema() {
221 global $IP;
222
223 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
224 if ( $result !== true ) {
225 $this->fail( $result );
226 }
227 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
228 }
229
230 /**
231 * Runs upgrades of older databases and compares results with current schema
232 * @todo: currently only checks list of tables
233 */
234 public function testUpgrades() {
235 global $IP, $wgVersion, $wgProfileToDatabase;
236
237 // Versions tested
238 $versions = array(
239 //'1.13', disabled for now, was totally screwed up
240 // SQLite wasn't included in 1.14
241 '1.15',
242 '1.16',
243 '1.17',
244 '1.18',
245 );
246
247 // Mismatches for these columns we can safely ignore
248 $ignoredColumns = array(
249 'user_newtalk.user_last_timestamp', // r84185
250 );
251
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" );
256 }
257 $currentTables = $this->getTables( $currentDB );
258 sort( $currentTables );
259
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 );
268 $this->assertEquals(
269 array_keys( $currentCols ),
270 array_keys( $cols ),
271 "Mismatching columns for table \"$table\" $versions"
272 );
273 foreach ( $currentCols as $name => $column ) {
274 $fullName = "$table.$name";
275 $this->assertEquals(
276 (bool)$column->pk,
277 (bool)$cols[$name]->pk,
278 "PRIMARY KEY status does not match for column $fullName $versions"
279 );
280 if ( !in_array( $fullName, $ignoredColumns ) ) {
281 $this->assertEquals(
282 (bool)$column->notnull,
283 (bool)$cols[$name]->notnull,
284 "NOT NULL status does not match for column $fullName $versions"
285 );
286 $this->assertEquals(
287 $column->dflt_value,
288 $cols[$name]->dflt_value,
289 "Default values does not match for column $fullName $versions"
290 );
291 }
292 }
293 $currentIndexes = $this->getIndexes( $currentDB, $table );
294 $indexes = $this->getIndexes( $db, $table );
295 $this->assertEquals(
296 array_keys( $currentIndexes ),
297 array_keys( $indexes ),
298 "mismatching indexes for table \"$table\" $versions"
299 );
300 }
301 $db->close();
302 }
303 }
304
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" );
313 }
314
315 private function prepareDB( $version ) {
316 static $maint = null;
317 if ( $maint === null ) {
318 $maint = new FakeMaintenance();
319 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
320 }
321
322 global $IP;
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' ) );
327 return $db;
328 }
329
330 private function getTables( $db ) {
331 $list = array_flip( $db->listTables() );
332 $excluded = array(
333 'math', // moved out of core in 1.18
334 'trackbacks', // removed from core in 1.19
335 'searchindex',
336 'searchindex_content',
337 'searchindex_segments',
338 'searchindex_segdir',
339 // FTS4 ready!!1
340 'searchindex_docsize',
341 'searchindex_stat',
342 );
343 foreach ( $excluded as $t ) {
344 unset( $list[$t] );
345 }
346 $list = array_flip( $list );
347 sort( $list );
348 return $list;
349 }
350
351 private function getColumns( $db, $table ) {
352 $cols = array();
353 $res = $db->query( "PRAGMA table_info($table)" );
354 $this->assertNotNull( $res );
355 foreach ( $res as $col ) {
356 $cols[$col->name] = $col;
357 }
358 ksort( $cols );
359 return $cols;
360 }
361
362 private function getIndexes( $db, $table ) {
363 $indexes = array();
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;
372 }
373 $indexes[$index->name] = $index;
374 }
375 ksort( $indexes );
376 return $indexes;
377 }
378 }