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
public function setUp() {
28
if ( !Sqlite::isPresent() ) {
29
$this->markTestSkipped( 'No SQLite support detected' );
31
$this->db = new MockDatabaseSqlite();
32
if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
33
$this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
37
private function replaceVars( $sql ) {
38
// normalize spacing to hide implementation details
39
return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
42
private function assertResultIs( $expected, $res ) {
43
$this->assertNotNull( $res );
45
foreach( $res as $row ) {
46
foreach( $expected[$i] as $key => $value ) {
47
$this->assertTrue( isset( $row->$key ) );
48
$this->assertEquals( $value, $row->$key );
52
$this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
55
public function testReplaceVars() {
56
$this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
58
$this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
59
. "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
60
$this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
61
foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
64
$this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
65
$this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
68
$this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
69
$this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
72
$this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
73
$this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
77
$this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
78
$this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
80
$this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
81
$this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
84
$this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
85
$this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
88
$this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
89
$this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
93
public function testTableName() {
95
$db = new DatabaseSqliteStandalone( ':memory:' );
96
$this->assertEquals( 'foo', $db->tableName( 'foo' ) );
97
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
98
$db->tablePrefix( 'foo' );
99
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
100
$this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
103
public function testDuplicateTableStructure() {
104
$db = new DatabaseSqliteStandalone( ':memory:' );
105
$db->query( 'CREATE TABLE foo(foo, barfoo)' );
107
$db->duplicateTableStructure( 'foo', 'bar' );
108
$this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
109
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
110
'Normal table duplication'
113
$db->duplicateTableStructure( 'foo', 'baz', true );
114
$this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
115
$db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
116
'Creation of temporary duplicate'
118
$this->assertEquals( 0,
119
$db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
120
'Create a temporary duplicate only'
124
public function testDuplicateTableStructureVirtual() {
125
$db = new DatabaseSqliteStandalone( ':memory:' );
126
if ( $db->getFulltextSearchModule() != 'FTS3' ) {
127
$this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
129
$db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
131
$db->duplicateTableStructure( 'foo', 'bar' );
132
$this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
133
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
134
'Duplication of virtual tables'
137
$db->duplicateTableStructure( 'foo', 'baz', true );
138
$this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
139
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
140
"Can't create temporary virtual tables, should fall back to non-temporary duplication"
144
public function testDeleteJoin() {
145
$db = new DatabaseSqliteStandalone( ':memory:' );
146
$db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
147
$db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
148
$db->insert( 'a', array(
155
$db->insert( 'b', array(
156
array( 'b_1' => 2, 'b_2' => 'a' ),
157
array( 'b_1' => 3, 'b_2' => 'b' ),
161
$db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
162
$res = $db->query( "SELECT * FROM a", __METHOD__ );
163
$this->assertResultIs( array(
171
public function testEntireSchema() {
174
$result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
175
if ( $result !== true ) {
176
$this->fail( $result );
178
$this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
182
* Runs upgrades of older databases and compares results with current schema
183
* @todo: currently only checks list of tables
185
public function testUpgrades() {
186
global $IP, $wgVersion;
190
//'1.13', disabled for now, was totally screwed up
191
// SQLite wasn't included in 1.14
198
// Mismatches for these columns we can safely ignore
199
$ignoredColumns = array(
200
'user_newtalk.user_last_timestamp', // r84185
203
$currentDB = new DatabaseSqliteStandalone( ':memory:' );
204
$currentDB->sourceFile( "$IP/maintenance/tables.sql" );
205
$currentTables = $this->getTables( $currentDB );
206
sort( $currentTables );
208
foreach ( $versions as $version ) {
209
$versions = "upgrading from $version to $wgVersion";
210
$db = $this->prepareDB( $version );
211
$tables = $this->getTables( $db );
212
$this->assertEquals( $currentTables, $tables, "Different tables $versions" );
213
foreach ( $tables as $table ) {
214
$currentCols = $this->getColumns( $currentDB, $table );
215
$cols = $this->getColumns( $db, $table );
217
array_keys( $currentCols ),
219
"Mismatching columns for table \"$table\" $versions"
221
foreach ( $currentCols as $name => $column ) {
222
$fullName = "$table.$name";
225
(bool)$cols[$name]->pk,
226
"PRIMARY KEY status does not match for column $fullName $versions"
228
if ( !in_array( $fullName, $ignoredColumns ) ) {
230
(bool)$column->notnull,
231
(bool)$cols[$name]->notnull,
232
"NOT NULL status does not match for column $fullName $versions"
236
$cols[$name]->dflt_value,
237
"Default values does not match for column $fullName $versions"
241
$currentIndexes = $this->getIndexes( $currentDB, $table );
242
$indexes = $this->getIndexes( $db, $table );
244
array_keys( $currentIndexes ),
245
array_keys( $indexes ),
246
"mismatching indexes for table \"$table\" $versions"
253
private function prepareDB( $version ) {
254
static $maint = null;
255
if ( $maint === null ) {
256
$maint = new FakeMaintenance();
257
$maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
261
$db = new DatabaseSqliteStandalone( ':memory:' );
262
$db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
263
$updater = DatabaseUpdater::newForDB( $db, false, $maint );
264
$updater->doUpdates( array( 'core' ) );
268
private function getTables( $db ) {
269
$list = array_flip( $db->listTables() );
271
'math', // moved out of core in 1.18
272
'trackbacks', // removed from core in 1.19
274
'searchindex_content',
275
'searchindex_segments',
276
'searchindex_segdir',
278
'searchindex_docsize',
281
foreach ( $excluded as $t ) {
284
$list = array_flip( $list );
289
private function getColumns( $db, $table ) {
291
$res = $db->query( "PRAGMA table_info($table)" );
292
$this->assertNotNull( $res );
293
foreach ( $res as $col ) {
294
$cols[$col->name] = $col;
300
private function getIndexes( $db, $table ) {
302
$res = $db->query( "PRAGMA index_list($table)" );
303
$this->assertNotNull( $res );
304
foreach ( $res as $index ) {
305
$res2 = $db->query( "PRAGMA index_info({$index->name})" );
306
$this->assertNotNull( $res2 );
307
$index->columns = array();
308
foreach ( $res2 as $col ) {
309
$index->columns[] = $col;
311
$indexes[$index->name] = $index;