4995c969f85ce70980bdf24df708166eb61fdfe2
[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 */
23 class DatabaseSqliteTest extends MediaWikiTestCase {
24 var $db;
25
26 public function setUp() {
27 if ( !Sqlite::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
29 }
30 $this->db = new MockDatabaseSqlite();
31 if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
32 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
33 }
34 }
35
36 private function replaceVars( $sql ) {
37 // normalize spacing to hide implementation details
38 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
39 }
40
41 public function testReplaceVars() {
42 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
43
44 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
45 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
46 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
47 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
48 );
49
50 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
51 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
52 );
53
54 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
55 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
56 );
57
58 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
59 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
60 'Table name changed'
61 );
62
63 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
64 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
65 );
66 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
67 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
68 );
69
70 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
71 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
72 );
73
74 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
75 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
76 );
77 }
78
79 public function testTableName() {
80 // @todo Moar!
81 $db = new DatabaseSqliteStandalone( ':memory:' );
82 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
83 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
84 $db->tablePrefix( 'foo' );
85 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
86 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
87 }
88
89 public function testDuplicateTableStructure() {
90 $db = new DatabaseSqliteStandalone( ':memory:' );
91 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
92
93 $db->duplicateTableStructure( 'foo', 'bar' );
94 $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
95 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
96 'Normal table duplication'
97 );
98
99 $db->duplicateTableStructure( 'foo', 'baz', true );
100 $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
101 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
102 'Creation of temporary duplicate'
103 );
104 $this->assertEquals( 0,
105 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
106 'Create a temporary duplicate only'
107 );
108 }
109
110 public function testDuplicateTableStructureVirtual() {
111 $db = new DatabaseSqliteStandalone( ':memory:' );
112 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
113 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
114 }
115 $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
116
117 $db->duplicateTableStructure( 'foo', 'bar' );
118 $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
119 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
120 'Duplication of virtual tables'
121 );
122
123 $db->duplicateTableStructure( 'foo', 'baz', true );
124 $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
125 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
126 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
127 );
128 }
129
130 public function testEntireSchema() {
131 global $IP;
132
133 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
134 if ( $result !== true ) {
135 $this->fail( $result );
136 }
137 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
138 }
139
140 /**
141 * Runs upgrades of older databases and compares results with current schema
142 * @todo: currently only checks list of tables
143 */
144 public function testUpgrades() {
145 global $IP, $wgVersion;
146
147 // Versions tested
148 $versions = array(
149 //'1.13', disabled for now, was totally screwed up
150 // SQLite wasn't included in 1.14
151 '1.15',
152 '1.16',
153 '1.17',
154 );
155
156 // Mismatches for these columns we can safely ignore
157 $ignoredColumns = array(
158 'user_newtalk.user_last_timestamp', // r84185
159 );
160
161 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
162 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
163 $currentTables = $this->getTables( $currentDB );
164 sort( $currentTables );
165
166 foreach ( $versions as $version ) {
167 $versions = "upgrading from $version to $wgVersion";
168 $db = $this->prepareDB( $version );
169 $tables = $this->getTables( $db );
170 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
171 foreach ( $tables as $table ) {
172 $currentCols = $this->getColumns( $currentDB, $table );
173 $cols = $this->getColumns( $db, $table );
174 $this->assertEquals(
175 array_keys( $currentCols ),
176 array_keys( $cols ),
177 "Mismatching columns for table $table $versions"
178 );
179 foreach ( $currentCols as $name => $column ) {
180 $fullName = "$table.$name";
181 $this->assertEquals(
182 (bool)$column->pk,
183 (bool)$cols[$name]->pk,
184 "PRIMARY KEY status does not match for column $fullName $versions"
185 );
186 if ( !in_array( $fullName, $ignoredColumns ) ) {
187 $this->assertEquals(
188 (bool)$column->notnull,
189 (bool)$cols[$name]->notnull,
190 "NOT NULL status does not match for column $fullName $versions"
191 );
192 $this->assertEquals(
193 $column->dflt_value,
194 $cols[$name]->dflt_value,
195 "Default values does not match for column $fullName $versions"
196 );
197 }
198 }
199 }
200 $db->close();
201 }
202 }
203
204 private function prepareDB( $version ) {
205 static $maint = null;
206 if ( $maint === null ) {
207 $maint = new FakeMaintenance();
208 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
209 }
210
211 $db = new DatabaseSqliteStandalone( ':memory:' );
212 $db->sourceFile( dirname( __FILE__ ) . "/sqlite/tables-$version.sql" );
213 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
214 $updater->doUpdates( array( 'core' ) );
215 return $db;
216 }
217
218 private function getTables( $db ) {
219 $list = array_flip( $db->listTables() );
220 $excluded = array(
221 'math', // moved out of core in 1.18
222 'searchindex',
223 'searchindex_content',
224 'searchindex_segments',
225 'searchindex_segdir',
226 // FTS4 ready!!1
227 'searchindex_docsize',
228 'searchindex_stat',
229 );
230 foreach ( $excluded as $t ) {
231 unset( $list[$t] );
232 }
233 $list = array_flip( $list );
234 sort( $list );
235 return $list;
236 }
237
238 private function getColumns( $db, $table ) {
239 $cols = array();
240 $res = $db->query( "PRAGMA table_info($table)" );
241 $this->assertNotNull( $res );
242 foreach ( $res as $col ) {
243 $cols[$col->name] = $col;
244 }
245 ksort( $cols );
246 return $cols;
247 }
248 }