Column checks for updater
[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 }
138
139 /**
140 * Runs upgrades of older databases and compares results with current schema
141 * @todo: currently only checks list of tables
142 */
143 public function testUpgrades() {
144 global $IP, $wgVersion;
145
146 // Versions tested
147 $versions = array(
148 //'1.13', disabled for now, was totally screwed up
149 // SQLite wasn't included in 1.14
150 '1.15',
151 '1.16',
152 '1.17',
153 );
154
155 // Mismatches for these columns we can safely ignore
156 $ignoredColumns = array(
157 'user_newtalk.user_last_timestamp', // r84185
158 );
159
160 $currentDB = new DatabaseSqliteStandalone( ':memory:' );
161 $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
162 $currentTables = $this->getTables( $currentDB );
163 sort( $currentTables );
164
165 foreach ( $versions as $version ) {
166 $versions = "upgrading from $version to $wgVersion";
167 $db = $this->prepareDB( $version );
168 $tables = $this->getTables( $db );
169 $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
170 foreach ( $tables as $table ) {
171 $currentCols = $this->getColumns( $currentDB, $table );
172 $cols = $this->getColumns( $db, $table );
173 $this->assertEquals(
174 array_keys( $currentCols ),
175 array_keys( $cols ),
176 "Mismatching columns for table $table $versions"
177 );
178 foreach ( $currentCols as $name => $column ) {
179 $fullName = "$table.$name";
180 $this->assertEquals(
181 (bool)$column->pk,
182 (bool)$cols[$name]->pk,
183 "PRIMARY KEY status does not match for column $fullName $versions"
184 );
185 if ( !in_array( $fullName, $ignoredColumns ) ) {
186 $this->assertEquals(
187 (bool)$column->notnull,
188 (bool)$cols[$name]->notnull,
189 "NOT NULL status does not match for column $fullName $versions"
190 );
191 $this->assertEquals(
192 $column->dflt_value,
193 $cols[$name]->dflt_value,
194 "Default values does not match for column $fullName $versions"
195 );
196 }
197 }
198 }
199 $db->close();
200 }
201 }
202
203 private function prepareDB( $version ) {
204 static $maint = null;
205 if ( $maint === null ) {
206 $maint = new FakeMaintenance();
207 $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
208 }
209
210 $db = new DatabaseSqliteStandalone( ':memory:' );
211 $db->sourceFile( dirname( __FILE__ ) . "/sqlite/tables-$version.sql" );
212 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
213 $updater->doUpdates( array( 'core' ) );
214 return $db;
215 }
216
217 private function getTables( $db ) {
218 $list = array_flip( $db->listTables() );
219 $excluded = array(
220 'math', // moved out of core in 1.18
221 'searchindex',
222 'searchindex_content',
223 'searchindex_segments',
224 'searchindex_segdir',
225 // FTS4 ready!!1
226 'searchindex_docsize',
227 'searchindex_stat',
228 );
229 foreach ( $excluded as $t ) {
230 unset( $list[$t] );
231 }
232 $list = array_flip( $list );
233 sort( $list );
234 return $list;
235 }
236
237 private function getColumns( $db, $table ) {
238 $cols = array();
239 $res = $db->query( "PRAGMA table_info($table)" );
240 $this->assertNotNull( $res );
241 foreach ( $res as $col ) {
242 $cols[$col->name] = $col;
243 }
244 ksort( $cols );
245 return $cols;
246 }
247 }