Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / installer / OracleUpdater.php
1 <?php
2 /**
3 * Oracle-specific updater.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for handling updates to Oracle databases.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class OracleUpdater extends DatabaseUpdater {
31
32 /**
33 * Handle to the database subclass
34 *
35 * @var DatabaseOracle
36 */
37 protected $db;
38
39 protected function getCoreUpdateList() {
40 return [
41 [ 'disableContentHandlerUseDB' ],
42
43 // 1.17
44 [ 'doNamespaceDefaults' ],
45 [ 'doFKRenameDeferr' ],
46 [ 'doFunctions17' ],
47 [ 'doSchemaUpgrade17' ],
48 [ 'doInsertPage0' ],
49 [ 'doRemoveNotNullEmptyDefaults' ],
50 [ 'addTable', 'user_former_groups', 'patch-user_former_groups.sql' ],
51
52 // 1.18
53 [ 'addIndex', 'user', 'i02', 'patch-user_email_index.sql' ],
54 [ 'modifyField', 'user_properties', 'up_property', 'patch-up_property.sql' ],
55 [ 'addTable', 'uploadstash', 'patch-uploadstash.sql' ],
56 [ 'doRecentchangesFK2Cascade' ],
57
58 // 1.19
59 [ 'addIndex', 'logging', 'i05', 'patch-logging_type_action_index.sql' ],
60 [ 'addField', 'revision', 'rev_sha1', 'patch-rev_sha1_field.sql' ],
61 [ 'addField', 'archive', 'ar_sha1', 'patch-ar_sha1_field.sql' ],
62 [ 'doRemoveNotNullEmptyDefaults2' ],
63 [ 'addIndex', 'page', 'i03', 'patch-page_redirect_namespace_len.sql' ],
64 [ 'addField', 'uploadstash', 'us_chunk_inx', 'patch-us_chunk_inx_field.sql' ],
65 [ 'addField', 'job', 'job_timestamp', 'patch-job_timestamp_field.sql' ],
66 [ 'addIndex', 'job', 'i02', 'patch-job_timestamp_index.sql' ],
67 [ 'doPageRestrictionsPKUKFix' ],
68
69 // 1.20
70 [ 'addIndex', 'ipblocks', 'i05', 'patch-ipblocks_i05_index.sql' ],
71 [ 'addIndex', 'revision', 'i05', 'patch-revision_i05_index.sql' ],
72 [ 'dropField', 'category', 'cat_hidden', 'patch-cat_hidden.sql' ],
73
74 // 1.21
75 [ 'addField', 'revision', 'rev_content_format',
76 'patch-revision-rev_content_format.sql' ],
77 [ 'addField', 'revision', 'rev_content_model',
78 'patch-revision-rev_content_model.sql' ],
79 [ 'addField', 'archive', 'ar_content_format', 'patch-archive-ar_content_format.sql' ],
80 [ 'addField', 'archive', 'ar_content_model', 'patch-archive-ar_content_model.sql' ],
81 [ 'addField', 'archive', 'ar_id', 'patch-archive-ar_id.sql' ],
82 [ 'addField', 'externallinks', 'el_id', 'patch-externallinks-el_id.sql' ],
83 [ 'addField', 'page', 'page_content_model', 'patch-page-page_content_model.sql' ],
84 [ 'enableContentHandlerUseDB' ],
85 [ 'dropField', 'site_stats', 'ss_admins', 'patch-ss_admins.sql' ],
86 [ 'dropField', 'recentchanges', 'rc_moved_to_title', 'patch-rc_moved.sql' ],
87 [ 'addTable', 'sites', 'patch-sites.sql' ],
88 [ 'addField', 'filearchive', 'fa_sha1', 'patch-fa_sha1.sql' ],
89 [ 'addField', 'job', 'job_token', 'patch-job_token.sql' ],
90 [ 'addField', 'job', 'job_attempts', 'patch-job_attempts.sql' ],
91 [ 'addField', 'uploadstash', 'us_props', 'patch-uploadstash-us_props.sql' ],
92 [ 'modifyField', 'user_groups', 'ug_group', 'patch-ug_group-length-increase-255.sql' ],
93 [ 'modifyField', 'user_former_groups', 'ufg_group',
94 'patch-ufg_group-length-increase-255.sql' ],
95
96 // 1.23
97 [ 'addIndex', 'logging', 'i06', 'patch-logging_user_text_type_time_index.sql' ],
98 [ 'addIndex', 'logging', 'i07', 'patch-logging_user_text_time_index.sql' ],
99 [ 'addField', 'user', 'user_password_expires', 'patch-user_password_expire.sql' ],
100 [ 'addField', 'page', 'page_links_updated', 'patch-page_links_updated.sql' ],
101 [ 'addField', 'recentchanges', 'rc_source', 'patch-rc_source.sql' ],
102
103 // 1.24
104 [ 'addField', 'page', 'page_lang', 'patch-page-page_lang.sql' ],
105
106 // 1.25
107 [ 'dropTable', 'hitcounter' ],
108 [ 'dropField', 'site_stats', 'ss_total_views', 'patch-drop-ss_total_views.sql' ],
109 [ 'dropField', 'page', 'page_counter', 'patch-drop-page_counter.sql' ],
110
111 // 1.27
112 [ 'dropTable', 'msg_resource_links' ],
113 [ 'dropTable', 'msg_resource' ],
114 [ 'addField', 'watchlist', 'wl_id', 'patch-watchlist-wl_id.sql' ],
115
116 // 1.28
117 [ 'addIndex', 'recentchanges', 'rc_name_type_patrolled_timestamp',
118 'patch-add-rc_name_type_patrolled_timestamp_index.sql' ],
119 [ 'addField', 'change_tag', 'ct_id', 'patch-change_tag-ct_id.sql' ],
120 [ 'addField', 'tag_summary', 'ts_id', 'patch-tag_summary-ts_id.sql' ],
121
122 // 1.29
123 [ 'addField', 'externallinks', 'el_index_60', 'patch-externallinks-el_index_60.sql' ],
124 [ 'addField', 'user_groups', 'ug_expiry', 'patch-user_groups-ug_expiry.sql' ],
125
126 // 1.30
127 [ 'doAutoIncrementTriggers' ],
128 [ 'addIndex', 'site_stats', 'PRIMARY', 'patch-site_stats-pk.sql' ],
129
130 // Should have been in 1.30
131 [ 'addTable', 'comment', 'patch-comment-table.sql' ],
132 // This field was added in 1.31, but is put here so it can be used by 'migrateComments'
133 [ 'addField', 'image', 'img_description_id', 'patch-image-img_description_id.sql' ],
134 // Should have been in 1.30
135 [ 'migrateComments' ],
136
137 // 1.31
138 [ 'addTable', 'slots', 'patch-slots.sql' ],
139 [ 'addField', 'slots', 'slot_origin', 'patch-slot-origin.sql' ],
140 [ 'addTable', 'content', 'patch-content.sql' ],
141 [ 'addTable', 'slot_roles', 'patch-slot_roles.sql' ],
142 [ 'addTable', 'content_models', 'patch-content_models.sql' ],
143 [ 'migrateArchiveText' ],
144 [ 'addTable', 'actor', 'patch-actor-table.sql' ],
145 [ 'migrateActors' ],
146 [ 'modifyTable', 'site_stats', 'patch-site_stats-modify.sql' ],
147 [ 'populateArchiveRevId' ],
148 [ 'addIndex', 'recentchanges', 'rc_namespace_title_timestamp',
149 'patch-recentchanges-nttindex.sql' ],
150
151 // 1.32
152 [ 'addTable', 'change_tag_def', 'patch-change_tag_def.sql' ],
153 [ 'populateExternallinksIndex60' ],
154 [ 'runMaintenance', DeduplicateArchiveRevId::class, 'maintenance/deduplicateArchiveRevId.php' ],
155 [ 'addField', 'change_tag', 'ct_tag_id', 'patch-change_tag-tag_id.sql' ],
156 [ 'addIndex', 'archive', 'ar_revid_uniq', 'patch-archive-ar_rev_id-unique.sql' ],
157 [ 'populateContentTables' ],
158 [ 'addIndex', 'recentchanges', 'rc_this_oldid', 'patch-recentchanges-rc_this_oldid-index.sql' ],
159 [ 'dropTable', 'transcache' ],
160 [ 'runMaintenance', PopulateChangeTagDef::class, 'maintenance/populateChangeTagDef.php' ],
161 [ 'addIndex', 'change_tag', 'change_tag_i03',
162 'patch-change_tag-change_tag_rc_tag_id.sql' ],
163 [ 'addField', 'ipblocks', 'ipb_sitewide', 'patch-ipb_sitewide.sql' ],
164 [ 'addTable', 'ipblocks_restrictions', 'patch-ipblocks_restrictions-table.sql' ],
165
166 // KEEP THIS AT THE BOTTOM!!
167 [ 'doRebuildDuplicateFunction' ],
168
169 ];
170 }
171
172 /**
173 * MySQL uses datatype defaults for NULL inserted into NOT NULL fields
174 * In namespace case that results into insert of 0 which is default namespace
175 * Oracle inserts NULL, so namespace fields should have a default value
176 */
177 protected function doNamespaceDefaults() {
178 $meta = $this->db->fieldInfo( 'page', 'page_namespace' );
179 if ( $meta->defaultValue() != null ) {
180 return;
181 }
182
183 $this->applyPatch(
184 'patch_namespace_defaults.sql',
185 false,
186 'Altering namespace fields with default value'
187 );
188 }
189
190 /**
191 * Uniform FK names + deferrable state
192 */
193 protected function doFKRenameDeferr() {
194 $meta = $this->db->query( '
195 SELECT COUNT(*) cnt
196 FROM user_constraints
197 WHERE constraint_type = \'R\' AND deferrable = \'DEFERRABLE\''
198 );
199 $row = $meta->fetchRow();
200 if ( $row && $row['cnt'] > 0 ) {
201 return;
202 }
203
204 $this->applyPatch( 'patch_fk_rename_deferred.sql', false, "Altering foreign keys ... " );
205 }
206
207 /**
208 * Recreate functions to 17 schema layout
209 */
210 protected function doFunctions17() {
211 $this->applyPatch( 'patch_create_17_functions.sql', false, "Recreating functions" );
212 }
213
214 /**
215 * Schema upgrade 16->17
216 * there are no incremental patches prior to this
217 */
218 protected function doSchemaUpgrade17() {
219 // check if iwlinks table exists which was added in 1.17
220 if ( $this->db->tableExists( 'iwlinks' ) ) {
221 return;
222 }
223 $this->applyPatch( 'patch_16_17_schema_changes.sql', false, "Updating schema to 17" );
224 }
225
226 /**
227 * Insert page (page_id = 0) to prevent FK constraint violation
228 */
229 protected function doInsertPage0() {
230 $this->output( "Inserting page 0 if missing ... " );
231 $row = [
232 'page_id' => 0,
233 'page_namespace' => 0,
234 'page_title' => ' ',
235 'page_is_redirect' => 0,
236 'page_is_new' => 0,
237 'page_random' => 0,
238 'page_touched' => $this->db->timestamp(),
239 'page_latest' => 0,
240 'page_len' => 0
241 ];
242 $this->db->insert( 'page', $row, 'OracleUpdater:doInserPage0', [ 'IGNORE' ] );
243 $this->output( "ok\n" );
244 }
245
246 /**
247 * Remove DEFAULT '' NOT NULL constraints from fields as '' is internally
248 * converted to NULL in Oracle
249 */
250 protected function doRemoveNotNullEmptyDefaults() {
251 $meta = $this->db->fieldInfo( 'categorylinks', 'cl_sortkey_prefix' );
252 if ( $meta->isNullable() ) {
253 return;
254 }
255 $this->applyPatch(
256 'patch_remove_not_null_empty_defs.sql',
257 false,
258 'Removing not null empty constraints'
259 );
260 }
261
262 protected function doRemoveNotNullEmptyDefaults2() {
263 $meta = $this->db->fieldInfo( 'ipblocks', 'ipb_by_text' );
264 if ( $meta->isNullable() ) {
265 return;
266 }
267 $this->applyPatch(
268 'patch_remove_not_null_empty_defs2.sql',
269 false,
270 'Removing not null empty constraints'
271 );
272 }
273
274 /**
275 * Removed forcing of invalid state on recentchanges_fk2.
276 * cascading taken in account in the deleting function
277 */
278 protected function doRecentchangesFK2Cascade() {
279 $meta = $this->db->query( 'SELECT 1 FROM all_constraints WHERE owner = \'' .
280 strtoupper( $this->db->getDBname() ) .
281 '\' AND constraint_name = \'' .
282 $this->db->tablePrefix() .
283 'RECENTCHANGES_FK2\' AND delete_rule = \'CASCADE\''
284 );
285 $row = $meta->fetchRow();
286 if ( $row ) {
287 return;
288 }
289
290 $this->applyPatch( 'patch_recentchanges_fk2_cascade.sql', false, "Altering RECENTCHANGES_FK2" );
291 }
292
293 /**
294 * Fixed wrong PK, UK definition
295 */
296 protected function doPageRestrictionsPKUKFix() {
297 $this->output( "Altering PAGE_RESTRICTIONS keys ... " );
298
299 $meta = $this->db->query( 'SELECT column_name FROM all_cons_columns WHERE owner = \'' .
300 strtoupper( $this->db->getDBname() ) .
301 '\' AND constraint_name = \'' .
302 $this->db->tablePrefix() .
303 'PAGE_RESTRICTIONS_PK\' AND rownum = 1'
304 );
305 $row = $meta->fetchRow();
306 if ( $row['column_name'] == 'PR_ID' ) {
307 $this->output( "seems to be up to date.\n" );
308
309 return;
310 }
311
312 $this->applyPatch( 'patch-page_restrictions_pkuk_fix.sql', false );
313 $this->output( "ok\n" );
314 }
315
316 /**
317 * Add auto-increment triggers
318 */
319 protected function doAutoIncrementTriggers() {
320 $this->output( "Adding auto-increment triggers ... " );
321
322 $meta = $this->db->query( 'SELECT trigger_name FROM user_triggers WHERE table_owner = \'' .
323 strtoupper( $this->db->getDBname() ) .
324 '\' AND trigger_name = \'' .
325 $this->db->tablePrefix() .
326 'PAGE_DEFAULT_PAGE_ID\''
327 );
328 $row = $meta->fetchRow();
329 if ( $row['column_name'] ) {
330 $this->output( "seems to be up to date.\n" );
331
332 return;
333 }
334
335 $this->applyPatch( 'patch-auto_increment_triggers.sql', false );
336
337 $this->output( "ok\n" );
338 }
339
340 /**
341 * rebuilding of the function that duplicates tables for tests
342 */
343 protected function doRebuildDuplicateFunction() {
344 $this->applyPatch( 'patch_rebuild_dupfunc.sql', false, "Rebuilding duplicate function" );
345 }
346
347 /**
348 * Overload: after this action field info table has to be rebuilt
349 *
350 * @param array $what
351 */
352 public function doUpdates( array $what = [ 'core', 'extensions', 'purge', 'stats' ] ) {
353 parent::doUpdates( $what );
354
355 $this->db->query( 'BEGIN fill_wiki_info; END;' );
356 }
357
358 /**
359 * Overload: because of the DDL_MODE tablename escaping is a bit dodgy
360 */
361 public function purgeCache() {
362 # We can't guarantee that the user will be able to use TRUNCATE,
363 # but we know that DELETE is available to us
364 $this->output( "Purging caches..." );
365 $this->db->delete( '/*Q*/' . $this->db->tableName( 'objectcache' ), '*', __METHOD__ );
366 $this->output( "done.\n" );
367 }
368 }