Debian Codegraph @

tests/phpunit/integration/includes/db/DatabaseSqliteUpgradeTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Installer\DatabaseUpdater;
4 use MediaWiki\Maintenance\FakeMaintenance;
5 use Psr\Log\NullLogger;
6 use Wikimedia\ObjectCache\HashBagOStuff;
7 use Wikimedia\Rdbms\Database;
8 use Wikimedia\Rdbms\DatabaseSqlite;
9 use Wikimedia\Rdbms\TransactionProfiler;
10
11 /**
12 * @group sqlite
13 * @group Database
14 * @group medium
15 */
16 class DatabaseSqliteUpgradeTest extends \MediaWikiIntegrationTestCase {
17 /** @var DatabaseSqlite */
18 protected $db;
19
20 /** @var array|null */
21 protected $currentSchema;
22
23 protected function setUp(): void {
24 parent::setUp();
25
26 if ( !Sqlite::isPresent() ) {
27 $this->markTestSkipped( 'No SQLite support detected' );
28 }
29 $this->db = $this->newMockDb();
30 if ( version_compare( $this->getDb()->getServerVersion(), '3.6.0', '<' ) ) {
31 $this->markTestSkipped( "SQLite at least 3.6 required, {$this->getDb()->getServerVersion()} found" );
32 }
33 }
34
35 /**
36 * @param string|null $version
37 * @return \PHPUnit\Framework\MockObject\MockObject|DatabaseSqlite
38 */
39 private function newMockDb( $version = null ) {
40 $mock = $this->getMockBuilder( DatabaseSqlite::class )
41 ->setConstructorArgs( [ [
42 'dbFilePath' => ':memory:',
43 'dbname' => 'Foo',
44 'schema' => null,
45 'host' => false,
46 'user' => false,
47 'password' => false,
48 'tablePrefix' => '',
49 'cliMode' => true,
50 'agent' => 'unit-tests',
51 'serverName' => null,
52 'flags' => DBO_DEFAULT,
53 'variables' => [ 'synchronous' => 'NORMAL', 'temp_store' => 'MEMORY' ],
54 'profiler' => null,
55 'topologyRole' => Database::ROLE_STREAMING_MASTER,
56 'trxProfiler' => new TransactionProfiler(),
57 'errorLogger' => null,
58 'deprecationLogger' => new NullLogger(),
59 'srvCache' => new HashBagOStuff(),
60 ] ] )->onlyMethods( array_merge(
61 [ 'query' ],
62 $version ? [ 'getServerVersion' ] : []
63 ) )->getMock();
64
65 $mock->initConnection();
66
67 $mock->method( 'query' )->willReturn( true );
68
69 if ( $version ) {
70 $mock->method( 'getServerVersion' )->willReturn( $version );
71 }
72
73 return $mock;
74 }
75
76 /**
77 * @coversNothing
78 */
79 public function testEntireSchema() {
80 $result = Sqlite::checkSqlSyntax( dirname( __FILE__, 6 ) . "/maintenance/tables.sql" );
81 if ( $result !== true ) {
82 $this->fail( $result );
83 }
84 $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
85 }
86
87 /**
88 * Runs upgrades of older databases and compares results with current schema
89 * @coversNothing
90 * @param string $version
91 * @dataProvider provideSupportedVersions
92 */
93 public function testUpgradeFromVersion( string $version ) {
94 $currentSchema =& $this->currentSchema;
95 if ( $currentSchema === null ) {
96 $currentDB = $this->initAndUpgradeTestDB( null );
97 $currentSchema = [];
98 foreach ( $this->getTables( $currentDB ) as $table ) {
99 $currentSchema[$table] = [
100 'columns' => $this->getColumns( $currentDB, $table ),
101 'indexes' => $this->getIndexes( $currentDB, $table )
102 ];
103 }
104 $currentDB->close();
105 }
106
107 // Mismatches for these columns we can safely ignore
108 $ignoredColumns = [];
109
110 $versions = "upgrading from $version to " . MW_VERSION;
111 $db = $this->initAndUpgradeTestDB( $version );
112 $tables = $this->getTables( $db );
113
114 $this->assertEquals( array_keys( $currentSchema ), $tables, "Different tables $versions" );
115
116 foreach ( $tables as $table ) {
117 $cols = $this->getColumns( $db, $table );
118 $this->assertEquals(
119 array_keys( $currentSchema[$table]['columns'] ),
120 array_keys( $cols ),
121 "Mismatching columns for table \"$table\" $versions"
122 );
123
124 foreach ( $currentSchema[$table]['columns'] as $name => $column ) {
125 $fullName = "$table.$name";
126 $this->assertEquals(
127 (bool)$column->pk,
128 (bool)$cols[$name]->pk,
129 "PRIMARY KEY status does not match for column $fullName $versions"
130 );
131 if ( !in_array( $fullName, $ignoredColumns ) ) {
132 $this->assertEquals(
133 (bool)$column->notnull,
134 (bool)$cols[$name]->notnull,
135 "NOT NULL status does not match for column $fullName $versions"
136 );
137 if ( $cols[$name]->dflt_value === 'NULL' ) {
138 $cols[$name]->dflt_value = null;
139 }
140 if ( $column->dflt_value === 'NULL' ) {
141 $column->dflt_value = null;
142 }
143 $this->assertEquals(
144 $column->dflt_value,
145 $cols[$name]->dflt_value,
146 "Default values does not match for column $fullName $versions"
147 );
148 }
149 }
150
151 $indexes = $this->getIndexes( $db, $table );
152 $this->assertEquals(
153 array_keys( $currentSchema[$table]['indexes'] ),
154 array_keys( $indexes ),
155 "mismatching indexes for table \"$table\" $versions"
156 );
157 }
158 }
159
160 public static function provideSupportedVersions() {
161 return [
162 [ '1.36' ],
163 [ '1.37' ],
164 [ '1.38' ],
165 [ '1.39' ],
166 [ '1.40' ],
167 [ '1.41' ],
168 [ '1.42' ],
169 ];
170 }
171
172 private function initAndUpgradeTestDB( $version = null ) {
173 static $maint = null;
174
175 if ( $maint === null ) {
176 $maint = new FakeMaintenance();
177 $maint->loadParamsAndArgs( null, [ 'quiet' => 1 ] );
178 }
179
180 $p = [ 'variables' => [ 'synchronous' => 'NORMAL', 'temp_store' => 'MEMORY' ] ];
181 $db = DatabaseSqlite::newStandaloneInstance( ':memory:', $p );
182 if ( $version !== null ) {
183 $db->sourceFile( dirname( __FILE__, 6 ) . "/tests/phpunit/data/db/sqlite/tables-$version.sql" );
184 $updater = DatabaseUpdater::newForDB( $db, false, $maint );
185 $updater->doUpdates( [ 'core' ] );
186 } else {
187 $db->sourceFile( dirname( __FILE__, 6 ) . "/maintenance/tables.sql" );
188 $db->sourceFile( dirname( __FILE__, 6 ) . "/maintenance/sqlite/tables-generated.sql" );
189 }
190
191 return $db;
192 }
193
194 private function getTables( $db ) {
195 $list = array_diff(
196 $db->listTables(),
197 [
198 'external_user', // removed from core in 1.22
199 'math', // moved out of core in 1.18
200 'trackbacks', // removed from core in 1.19
201 'searchindex',
202 'searchindex_content',
203 'searchindex_segments',
204 'searchindex_segdir',
205 ]
206 );
207 sort( $list );
208
209 return $list;
210 }
211
212 private function getColumns( $db, $table ) {
213 $cols = [];
214 $res = $db->query( "PRAGMA table_info($table)" );
215 $this->assertNotNull( $res );
216 foreach ( $res as $col ) {
217 $cols[$col->name] = $col;
218 }
219 ksort( $cols );
220
221 return $cols;
222 }
223
224 private function getIndexes( $db, $table ) {
225 $indexes = [];
226 $res = $db->query( "PRAGMA index_list($table)" );
227 $this->assertNotNull( $res );
228 foreach ( $res as $index ) {
229 $res2 = $db->query( "PRAGMA index_info({$index->name})" );
230 $this->assertNotNull( $res2 );
231 $index->columns = [];
232 foreach ( $res2 as $col ) {
233 $index->columns[] = $col;
234 }
235 $indexes[$index->name] = $index;
236 }
237 ksort( $indexes );
238
239 return $indexes;
240 }
241 }