MediaWiki 1.43.1
PostgresUpdater.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Installer;
25
34
42
46 protected $db;
47
51 protected function getCoreUpdateList() {
52 return [
53 // Exception to the sequential updates. Renaming pagecontent and mwuser.
54 // Introduced in 1.36.
55 [ 'renameTable', 'pagecontent', 'text' ],
56 // Introduced in 1.37.
57 [ 'renameTable', 'mwuser', 'user' ],
58
59 // 1.36
60 [ 'setDefault', 'bot_passwords', 'bp_token', '' ],
61 [ 'changeField', 'comment', 'comment_id', 'BIGINT', '' ],
62 [ 'changeField', 'slots', 'slot_revision_id', 'BIGINT', '' ],
63 [ 'changeField', 'slots', 'slot_content_id', 'BIGINT', '' ],
64 [ 'changeField', 'slots', 'slot_origin', 'BIGINT', '' ],
65 [ 'changeField', 'site_stats', 'ss_total_edits', 'BIGINT', '' ],
66 [ 'changeField', 'site_stats', 'ss_good_articles', 'BIGINT', '' ],
67 [ 'changeField', 'site_stats', 'ss_total_pages', 'BIGINT', '' ],
68 [ 'changeField', 'site_stats', 'ss_users', 'BIGINT', '' ],
69 [ 'changeField', 'site_stats', 'ss_active_users', 'BIGINT', '' ],
70 [ 'changeField', 'site_stats', 'ss_images', 'BIGINT', '' ],
71 [ 'dropFkey', 'user_properties', 'up_user' ],
72 [ 'addIndex', 'user_properties', 'user_properties_pkey', 'patch-user_properties-pk.sql' ],
73 [ 'changeField', 'log_search', 'ls_value', 'VARCHAR(255)', '' ],
74 [ 'changeField', 'content', 'content_id', 'BIGINT', '' ],
75 [ 'changeField', 'l10n_cache', 'lc_value', 'TEXT', '' ],
76 [ 'changeField', 'l10n_cache', 'lc_key', 'VARCHAR(255)', '' ],
77 [ 'addIndex', 'l10n_cache', 'l10n_cache_pkey', 'patch-l10n_cache-pk.sql' ],
78 [ 'addIndex', 'module_deps', 'module_deps_pkey', 'patch-module_deps-pk.sql' ],
79 [ 'changeField', 'redirect', 'rd_namespace', 'INT', 'rd_namespace::INT DEFAULT 0' ],
80 [ 'setDefault', 'redirect', 'rd_title', '' ],
81 [ 'setDefault', 'redirect', 'rd_from', 0 ],
82 [ 'dropFkey', 'redirect', 'rd_from' ],
83 [ 'changeField', 'redirect', 'rd_interwiki', 'VARCHAR(32)', '' ],
84 [ 'dropFkey', 'pagelinks', 'pl_from' ],
85 [ 'dropPgIndex', 'pagelinks', 'pagelink_unique' ],
86 [ 'dropPgIndex', 'pagelinks', 'pagelinks_title' ],
87 [ 'dropFkey', 'templatelinks', 'tl_from' ],
88 [ 'dropPgIndex', 'templatelinks', 'templatelinks_unique' ],
89 [ 'dropPgIndex', 'templatelinks', 'templatelinks_from' ],
90 [ 'dropFkey', 'imagelinks', 'il_from' ],
91 [ 'setDefault', 'imagelinks', 'il_to', '' ],
92 [ 'addPgIndex', 'imagelinks', 'il_to', '(il_to, il_from)' ],
93 [ 'addPgIndex', 'imagelinks', 'il_backlinks_namespace',
94 '(il_from_namespace, il_to, il_from)' ],
95 [ 'dropPgIndex', 'imagelinks', 'il_from' ],
96 [ 'dropFkey', 'langlinks', 'll_from' ],
97 [ 'addIndex', 'langlinks', 'langlinks_pkey', 'patch-langlinks-pk.sql' ],
98 [ 'renameIndex', 'langlinks', 'langlinks_lang_title', 'll_lang' ],
99 [ 'setDefault', 'langlinks', 'll_lang', '' ],
100 [ 'setDefault', 'langlinks', 'll_from', 0 ],
101 [ 'setDefault', 'langlinks', 'll_title', '' ],
102 [ 'changeNullableField', 'langlinks', 'll_lang', 'NOT NULL', true ],
103 [ 'changeNullableField', 'langlinks', 'll_title', 'NOT NULL', true ],
104 [ 'addIndex', 'iwlinks', 'iwlinks_pkey', 'patch-iwlinks-pk.sql' ],
105 [ 'renameIndex', 'category', 'category_title', 'cat_title' ],
106 [ 'renameIndex', 'category', 'category_pages', 'cat_pages' ],
107 [ 'dropSequence', 'watchlist_expiry', 'watchlist_expiry_we_item_seq' ],
108 [ 'changeField', 'change_tag_def', 'ctd_count', 'BIGINT', 'ctd_count::BIGINT DEFAULT 0' ],
109 [ 'dropDefault', 'change_tag_def', 'ctd_user_defined' ],
110 [ 'dropFkey', 'ipblocks_restrictions', 'ir_ipb_id' ],
111 [ 'setDefault', 'querycache', 'qc_value', 0 ],
112 [ 'changeField', 'querycache', 'qc_namespace', 'INT', 'qc_namespace::INT DEFAULT 0' ],
113 [ 'setDefault', 'querycache', 'qc_title', '' ],
114 [ 'renameIndex', 'querycache', 'querycache_type_value', 'qc_type' ],
115 [ 'renameIndex', 'querycachetwo', 'querycachetwo_type_value', 'qcc_type' ],
116 [ 'renameIndex', 'querycachetwo', 'querycachetwo_title', 'qcc_title' ],
117 [ 'renameIndex', 'querycachetwo', 'querycachetwo_titletwo', 'qcc_titletwo' ],
118 [ 'dropFkey', 'page_restrictions', 'pr_page' ],
119 [ 'addPgIndex', 'page_restrictions', 'pr_pagetype', '(pr_page, pr_type)', true ],
120 [ 'addPgIndex', 'page_restrictions', 'pr_typelevel', '(pr_type, pr_level)' ],
121 [ 'addPgIndex', 'page_restrictions', 'pr_level', '(pr_level)' ],
122 [ 'addPgIndex', 'page_restrictions', 'pr_cascade', '(pr_cascade)' ],
123 [ 'changePrimaryKey', 'page_restrictions', [ 'pr_id' ], 'page_restrictions_pk' ],
124 [ 'changeNullableField', 'page_restrictions', 'pr_page', 'NOT NULL', true ],
125 [ 'dropFkey', 'user_groups', 'ug_user' ],
126 [ 'setDefault', 'user_groups', 'ug_user', 0 ],
127 [ 'setDefault', 'user_groups', 'ug_group', '' ],
128 [ 'renameIndex', 'user_groups', 'user_groups_group', 'ug_group' ],
129 [ 'renameIndex', 'user_groups', 'user_groups_expiry', 'ug_expiry' ],
130 [ 'setDefault', 'querycache_info', 'qci_type', '' ],
131 [ 'setDefault', 'querycache_info', 'qci_timestamp', '1970-01-01 00:00:00+00' ],
132 [ 'changeNullableField', 'querycache_info', 'qci_type', 'NOT NULL', true ],
133 [ 'changeNullableField', 'querycache_info', 'qci_timestamp', 'NOT NULL', true ],
134 [ 'addIndex', 'querycache_info', 'querycache_info_pkey', 'patch-querycache_info-pk.sql' ],
135 [ 'setDefault', 'watchlist', 'wl_title', '' ],
136 [ 'changeField', 'watchlist', 'wl_namespace', 'INT', 'wl_namespace::INT DEFAULT 0' ],
137 [ 'dropFkey', 'watchlist', 'wl_user' ],
138 [ 'dropPgIndex', 'watchlist', 'wl_user_namespace_title' ],
139 [ 'addPgIndex', 'watchlist', 'namespace_title', '(wl_namespace, wl_title)' ],
140 [ 'checkIndex', 'wl_user', [
141 [ 'wl_user', 'text_ops', 'btree', 1 ],
142 [ 'wl_namespace', 'int4_ops', 'btree', 1 ],
143 [ 'wl_title', 'text_ops', 'btree', 1 ],
144 ],
145 'CREATE UNIQUE INDEX "wl_user" ON "watchlist" (wl_user, wl_namespace, wl_title)'
146 ],
147 [ 'changeField', 'sites', 'site_domain', 'VARCHAR(255)', '' ],
148 [ 'renameIndex', 'sites', 'site_global_key', 'sites_global_key' ],
149 [ 'renameIndex', 'sites', 'site_type', 'sites_type' ],
150 [ 'renameIndex', 'sites', 'site_group', 'sites_group' ],
151 [ 'renameIndex', 'sites', 'site_source', 'sites_source' ],
152 [ 'renameIndex', 'sites', 'site_language', 'sites_language' ],
153 [ 'renameIndex', 'sites', 'site_protocol', 'sites_protocol' ],
154 [ 'renameIndex', 'sites', 'site_domain', 'sites_domain' ],
155 [ 'renameIndex', 'sites', 'site_forward', 'sites_forward' ],
156 [ 'dropFkey', 'user_newtalk', 'user_id' ],
157 [ 'renameIndex', 'user_newtalk', 'user_newtalk_id', 'un_user_id' ],
158 [ 'renameIndex', 'user_newtalk', 'user_newtalk_ip', 'un_user_ip' ],
159 [ 'changeField', 'interwiki', 'iw_prefix', 'VARCHAR(32)', '' ],
160 [ 'changeField', 'interwiki', 'iw_wikiid', 'VARCHAR(64)', '' ],
161 [ 'dropFkey', 'protected_titles', 'pt_user' ],
162 [ 'changeNullableField', 'protected_titles', 'pt_user', 'NOT NULL', true ],
163 [ 'changeNullableField', 'protected_titles', 'pt_expiry', 'NOT NULL', true ],
164 [ 'changeField', 'protected_titles', 'pt_reason_id', 'BIGINT', '' ],
165 [ 'dropDefault', 'protected_titles', 'pt_create_perm' ],
166 [ 'dropFkey', 'externallinks', 'el_from' ],
167 [ 'renameIndex', 'externallinks', 'externallinks_from_to', 'el_from' ],
168 [ 'renameIndex', 'externallinks', 'externallinks_index', 'el_index' ],
169 [ 'dropSequence', 'ip_changes', 'ip_changes_ipc_rev_id_seq' ],
170 [ 'changeField', 'ip_changes', 'ipc_hex', 'TEXT', "ipc_hex::TEXT DEFAULT ''" ],
171 [ 'setDefault', 'ip_changes', 'ipc_rev_id', 0 ],
172 [ 'renameIndex', 'watchlist', 'namespace_title', 'wl_namespace_title' ],
173 [ 'dropFkey', 'page_props', 'pp_page' ],
174 // page_props primary key change moved from the Schema SQL file to here in 1.36
175 [ 'changePrimaryKey', 'page_props', [ 'pp_page', 'pp_propname' ], 'page_props_pk' ],
176 [ 'setDefault', 'job', 'job_cmd', '' ],
177 [ 'changeField', 'job', 'job_namespace', 'INTEGER', '' ],
178 [ 'dropPgIndex', 'job', 'job_cmd_namespace_title' ],
179 [ 'addPgIndex', 'job', 'job_cmd', '(job_cmd, job_namespace, job_title, job_params)' ],
180 [ 'renameIndex', 'job', 'job_timestamp_idx', 'job_timestamp' ],
181 [ 'changeField', 'slot_roles', 'role_id', 'INTEGER', '' ],
182 [ 'changeField', 'content_models', 'model_id', 'INTEGER', '' ],
183 [ 'renameIndex', 'page', 'page_len_idx', 'page_len' ],
184 [ 'renameIndex', 'page', 'page_random_idx', 'page_random' ],
185 [ 'renameIndex', 'page', 'page_unique_name', 'page_name_title' ],
186 [ 'addPGIndex', 'page', 'page_redirect_namespace_len', '(page_is_redirect, page_namespace, page_len)' ],
187 [ 'dropFkey', 'categorylinks', 'cl_from' ],
188 [ 'setDefault', 'categorylinks', 'cl_from', 0 ],
189 [ 'setDefault', 'categorylinks', 'cl_to', '' ],
190 [ 'setDefault', 'categorylinks', 'cl_sortkey', '' ],
191 [ 'setDefault', 'categorylinks', 'cl_collation', '' ],
192 [ 'changeNullableField', 'categorylinks', 'cl_sortkey', 'NOT NULL', true ],
193 [ 'addIndex', 'categorylinks', 'categorylinks_pkey', 'patch-categorylinks-pk.sql' ],
194 [ 'addPgIndex', 'categorylinks', 'cl_timestamp', '(cl_to, cl_timestamp)' ],
195 [ 'addPgIndex', 'categorylinks', 'cl_collation_ext', '(cl_collation, cl_to, cl_type, cl_from)' ],
196 [ 'checkIndex', 'cl_sortkey', [
197 [ 'cl_to', 'text_ops', 'btree', 1 ],
198 [ 'cl_type', 'text_ops', 'btree', 1 ],
199 [ 'cl_sortkey', 'text_ops', 'btree', 1 ],
200 [ 'cl_from', 'text_ops', 'btree', 1 ],
201 ],
202 'CREATE INDEX cl_sortkey ON categorylinks (cl_to, cl_type, cl_sortkey, cl_from)'
203 ],
204 [ 'renameIndex', 'logging', 'logging_type_name', 'type_time' ],
205 [ 'renameIndex', 'logging', 'logging_actor_time_backwards', 'actor_time' ],
206 [ 'renameIndex', 'logging', 'logging_page_time', 'page_time' ],
207 [ 'renameIndex', 'logging', 'logging_times', 'times' ],
208 [ 'renameIndex', 'logging', 'logging_actor_type_time', 'log_actor_type_time' ],
209 [ 'renameIndex', 'logging', 'logging_page_id_time', 'log_page_id_time' ],
210 [ 'renameIndex', 'logging', 'logging_type_action', 'log_type_action' ],
211 [ 'changeNullableField', 'logging', 'log_params', 'NOT NULL', true ],
212 [ 'setDefault', 'logging', 'log_action', '' ],
213 [ 'setDefault', 'logging', 'log_type', '' ],
214 [ 'setDefault', 'logging', 'log_title', '' ],
215 [ 'setDefault', 'logging', 'log_timestamp', '1970-01-01 00:00:00+00' ],
216 [ 'changeField', 'logging', 'log_actor', 'BIGINT', '' ],
217 [ 'changeField', 'logging', 'log_comment_id', 'BIGINT', '' ],
218 [ 'changeField', 'logging', 'log_namespace', 'INT', 'log_namespace::INT DEFAULT 0' ],
219 [ 'dropPgIndex', 'logging', 'logging_actor_time' ],
220 [ 'changeField', 'uploadstash', 'us_key', 'VARCHAR(255)', '' ],
221 [ 'changeField', 'uploadstash', 'us_orig_path', 'VARCHAR(255)', '' ],
222 [ 'changeField', 'uploadstash', 'us_path', 'VARCHAR(255)', '' ],
223 [ 'changeField', 'uploadstash', 'us_source_type', 'VARCHAR(50)', '' ],
224 [ 'changeField', 'uploadstash', 'us_props', 'TEXT', '' ],
225 [ 'changeField', 'uploadstash', 'us_status', 'VARCHAR(50)', '' ],
226 [ 'changeField', 'uploadstash', 'us_sha1', 'VARCHAR(31)', '' ],
227 [ 'changeField', 'uploadstash', 'us_mime', 'VARCHAR(255)', '' ],
228 [ 'changeNullableField', 'uploadstash', 'us_key', 'NOT NULL', true ],
229 [ 'changeNullableField', 'uploadstash', 'us_user', 'NOT NULL', true ],
230 [ 'changeNullableField', 'uploadstash', 'us_orig_path', 'NOT NULL', true ],
231 [ 'changeNullableField', 'uploadstash', 'us_path', 'NOT NULL', true ],
232 [ 'changeNullableField', 'uploadstash', 'us_timestamp', 'NOT NULL', true ],
233 [ 'changeNullableField', 'uploadstash', 'us_status', 'NOT NULL', true ],
234 [ 'changeNullableField', 'uploadstash', 'us_size', 'NOT NULL', true ],
235 [ 'changeNullableField', 'uploadstash', 'us_sha1', 'NOT NULL', true ],
236 [ 'renameIndex', 'uploadstash', 'us_user_idx', 'us_user' ],
237 [ 'renameIndex', 'uploadstash', 'us_key_idx', 'us_key' ],
238 [ 'renameIndex', 'uploadstash', 'us_timestamp_idx', 'us_timestamp' ],
239 [ 'renameIndex', 'user_properties', 'user_properties_property', 'up_property' ],
240 [ 'renameIndex', 'sites', 'sites_global_key', 'site_global_key' ],
241 [ 'renameIndex', 'sites', 'sites_type', 'site_type' ],
242 [ 'renameIndex', 'sites', 'sites_group, ', 'site_group' ],
243 [ 'renameIndex', 'sites', 'sites_source', 'site_source' ],
244 [ 'renameIndex', 'sites', 'sites_language', 'site_language' ],
245 [ 'renameIndex', 'sites', 'sites_protocol', 'site_protocol' ],
246 [ 'renameIndex', 'sites', 'sites_domain', 'site_domain' ],
247 [ 'renameIndex', 'sites', 'sites_forward', 'site_forward' ],
248 [ 'renameIndex', 'logging', 'type_name', 'log_type_time' ],
249 [ 'renameIndex', 'logging', 'actor_time', 'log_actor_time' ],
250 [ 'renameIndex', 'logging', 'page_time', 'log_page_time' ],
251 [ 'renameIndex', 'logging', 'times', 'log_times' ],
252 [ 'setDefault', 'filearchive', 'fa_name', '' ],
253 [ 'setDefault', 'filearchive', 'fa_archive_name', '' ],
254 [ 'setDefault', 'filearchive', 'fa_storage_key', '' ],
255 [ 'dropFkey', 'filearchive', 'fa_deleted_user' ],
256 [ 'changeField', 'filearchive', 'fa_deleted_reason_id', 'BIGINT', '' ],
257 [ 'changeField', 'filearchive', 'fa_metadata', 'TEXT', '' ],
258 [ 'changeField', 'filearchive', 'fa_bits', 'INTEGER', '' ],
259 [ 'changeField', 'filearchive', 'fa_description_id', 'BIGINT', '' ],
260 [ 'changeField', 'filearchive', 'fa_actor', 'BIGINT', '' ],
261 [ 'renameIndex', 'filearchive', 'fa_name_time', 'fa_name' ],
262 [ 'renameIndex', 'filearchive', 'fa_dupe', 'fa_storage_group' ],
263 [ 'renameIndex', 'filearchive', 'fa_notime', 'fa_deleted_timestamp' ],
264 [ 'dropPgIndex', 'filearchive', 'fa_nouser' ],
265 [ 'addPgIndex', 'filearchive', 'fa_actor_timestamp', '(fa_actor, fa_timestamp)' ],
266 [ 'addPgIndex', 'ipblocks', 'ipb_expiry', '(ipb_expiry)' ],
267 [ 'addPgIndex', 'ipblocks', 'ipb_timestamp', '(ipb_timestamp)' ],
268 [ 'renameIndex', 'text', 'pagecontent_pkey', 'text_pkey' ],
269 [ 'changeNullableField', 'text', 'old_text', 'NOT NULL', true ],
270 [ 'changeNullableField', 'text', 'old_flags', 'NOT NULL', true ],
271 [ 'setDefault', 'oldimage', 'oi_name', '' ],
272 [ 'setDefault', 'oldimage', 'oi_archive_name', '' ],
273 [ 'setDefault', 'oldimage', 'oi_size', 0 ],
274 [ 'setDefault', 'oldimage', 'oi_width', 0 ],
275 [ 'setDefault', 'oldimage', 'oi_height', 0 ],
276 [ 'setDefault', 'oldimage', 'oi_bits', 0 ],
277 [ 'setDefault', 'oldimage', 'oi_name', '' ],
278 [ 'changeField', 'oldimage', 'oi_bits', 'INTEGER', '' ],
279 [ 'changeField', 'oldimage', 'oi_description_id', 'BIGINT', '' ],
280 [ 'changeField', 'oldimage', 'oi_actor', 'BIGINT', '' ],
281 [ 'changeField', 'oldimage', 'oi_metadata', 'TEXT', '' ],
282 [ 'dropDefault', 'oldimage', 'oi_metadata' ],
283 [ 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NOT NULL', true ],
284 [ 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NOT NULL', true ],
285 [ 'dropFkey', 'oldimage', 'oi_name' ],
286 [ 'addPgIndex', 'oldimage', 'oi_actor_timestamp', '(oi_actor, oi_timestamp)' ],
287 [ 'dropPgIndex', 'recentchanges', 'rc_timestamp_bot' ],
288 [ 'addPgIndex', 'recentchanges', 'rc_ns_actor', '(rc_namespace, rc_actor)' ],
289 [ 'addPgIndex', 'recentchanges', 'rc_actor', '(rc_actor, rc_timestamp)' ],
290 [ 'dropIndex', 'objectcache', 'keyname', 'patch-objectcache_keyname-pk.sql' ],
291 [ 'changeField', 'objectcache', 'value', 'TEXT', '' ],
292 [ 'changeNullableField', 'objectcache', 'value', 'NULL', true ],
293 [ 'dropFkey', 'ipblocks', 'ipb_user' ],
294 [ 'dropFkey', 'ipblocks', 'ipb_parent_block_id' ],
295 [ 'setDefault', 'ipblocks', 'ipb_user', 0 ],
296 [ 'changeNullableField', 'ipblocks', 'ipb_user', 'NOT NULL', true ],
297 [ 'changeNullableField', 'ipblocks', 'ipb_range_start', 'NOT NULL', true ],
298 [ 'changeNullableField', 'ipblocks', 'ipb_range_end', 'NOT NULL', true ],
299 [ 'changeField', 'ipblocks', 'ipb_by_actor', 'BIGINT', '' ],
300 [ 'changeField', 'ipblocks', 'ipb_reason_id', 'BIGINT', '' ],
301 [ 'renameIndex', 'archive', 'archive_name_title_timestamp', 'ar_name_title_timestamp' ],
302 [ 'dropPgIndex', 'archive', 'archive_actor' ],
303 [ 'addPgIndex', 'archive', 'ar_actor_timestamp', '(ar_actor,ar_timestamp)' ],
304 [ 'setDefault', 'image', 'img_name', '' ],
305 [ 'setDefault', 'image', 'img_size', 0 ],
306 [ 'setDefault', 'image', 'img_width', 0 ],
307 [ 'setDefault', 'image', 'img_height', 0 ],
308 [ 'setDefault', 'image', 'img_bits', 0 ],
309 [ 'changeField', 'image', 'img_bits', 'INTEGER', '' ],
310 [ 'changeField', 'image', 'img_description_id', 'BIGINT', '' ],
311 [ 'changeField', 'image', 'img_actor', 'BIGINT', '' ],
312 [ 'changeField', 'image', 'img_metadata', 'TEXT', '' ],
313 [ 'dropDefault', 'image', 'img_metadata' ],
314 [ 'changeNullableField', 'image', 'img_major_mime', 'NOT NULL', true ],
315 [ 'changeNullableField', 'image', 'img_minor_mime', 'NOT NULL', true ],
316 [ 'changeNullableField', 'image', 'img_timestamp', 'NOT NULL', true ],
317 [ 'renameIndex', 'image', 'img_size_idx', 'img_size' ],
318 [ 'renameIndex', 'image', 'img_timestamp_idx', 'img_timestamp' ],
319 [ 'addPgIndex', 'image', 'img_actor_timestamp', '(img_actor, img_timestamp)' ],
320 [ 'addPgIndex', 'image', 'img_media_mime', '(img_media_type, img_major_mime, img_minor_mime)' ],
321 [ 'renameIndex', 'site_identifiers', 'site_ids_site', 'si_site' ],
322 [ 'renameIndex', 'site_identifiers', 'site_ids_key', 'si_key' ],
323 [ 'changeField', 'recentchanges', 'rc_actor', 'BIGINT', '' ],
324 [ 'changeField', 'recentchanges', 'rc_comment_id', 'BIGINT', '' ],
325 [ 'changeField', 'recentchanges', 'rc_ip', 'TEXT', '' ],
326 [ 'changeField', 'recentchanges', 'rc_namespace', 'INTEGER', '' ],
327 [ 'setDefault', 'recentchanges', 'rc_title', '' ],
328 [ 'setDefault', 'recentchanges', 'rc_source', '' ],
329 [ 'setDefault', 'recentchanges', 'rc_ip', '' ],
330 [ 'setDefault', 'recentchanges', 'rc_namespace', 0 ],
331 [ 'setDefault', 'recentchanges', 'rc_cur_id', 0 ],
332 [ 'setDefault', 'recentchanges', 'rc_this_oldid', 0 ],
333 [ 'setDefault', 'recentchanges', 'rc_last_oldid', 0 ],
334 [ 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NOT NULL', true ],
335 [ 'changeNullableField', 'recentchanges', 'rc_ip', 'NOT NULL', true ],
336 [ 'renameIndex', 'recentchanges', 'new_name_timestamp', 'rc_new_name_timestamp', false,
337 'patch-recentchanges-rc_new_name_timestamp.sql' ],
338 [ 'changeField', 'archive', 'ar_namespace', 'INTEGER', '' ],
339 [ 'setDefault', 'archive', 'ar_namespace', 0 ],
340 [ 'setDefault', 'archive', 'ar_title', '' ],
341 [ 'changeField', 'archive', 'ar_comment_id', 'BIGINT', '' ],
342 [ 'changeField', 'archive', 'ar_actor', 'BIGINT', '' ],
343 [ 'renameIndex', 'user', 'user_email_token_idx', 'user_email_token' ],
344 [ 'addPgIndex', 'user', 'user_email', '(user_email)' ],
345 [ 'addPgIndex', 'user', 'user_name', '(user_name)', true ],
346 [ 'changeField', 'page', 'page_namespace', 'INTEGER', '' ],
347 [ 'changeNullableField', 'page', 'page_touched', 'NOT NULL', true ],
348 [ 'changeField', 'page', 'page_random', 'FLOAT', '' ],
349 [ 'renameIndex', 'revision', 'revision_unique', 'rev_page_id' ],
350 [ 'renameIndex', 'revision', 'rev_timestamp_idx', 'rev_timestamp' ],
351 [ 'addPgIndex', 'revision', 'rev_page_timestamp', '(rev_page,rev_timestamp)' ],
352 [ 'changeNullableField', 'user', 'user_touched', 'NOT NULL', true ],
353
354 // 1.37
355 [ 'setDefault', 'user', 'user_name', '' ],
356 [ 'setDefault', 'user', 'user_token', '' ],
357 [ 'setDefault', 'user', 'user_real_name', '' ],
358 [ 'setDefault', 'user', 'user_email', '' ],
359 [ 'setDefault', 'user', 'user_newpassword', '' ],
360 [ 'setDefault', 'user', 'user_password', '' ],
361 [ 'changeNullableField', 'user', 'user_token', 'NOT NULL', true ],
362 [ 'changeNullableField', 'user', 'user_real_name', 'NOT NULL', true ],
363 [ 'changeNullableField', 'user', 'user_email', 'NOT NULL', true ],
364 [ 'changeNullableField', 'user', 'user_newpassword', 'NOT NULL', true ],
365 [ 'changeNullableField', 'user', 'user_password', 'NOT NULL', true ],
366 [ 'dropDefault', 'user', 'user_email' ],
367 [ 'dropDefault', 'user', 'user_newpassword' ],
368 [ 'dropDefault', 'user', 'user_password' ],
369 [ 'dropConstraint', 'user', 'user_name', 'unique' ],
370 [ 'addField', 'objectcache', 'modtoken', 'patch-objectcache-modtoken.sql' ],
371 [ 'dropFkey', 'revision', 'rev_page' ],
372 [ 'changeNullableField', 'revision', 'rev_page', 'NOT NULL', true ],
373 [ 'changeField', 'revision', 'rev_comment_id', 'BIGINT', 'rev_comment_id::BIGINT DEFAULT 0' ],
374 [ 'changeField', 'revision', 'rev_actor', 'BIGINT', 'rev_actor::BIGINT DEFAULT 0' ],
375 [ 'checkIndex', 'rev_page_id', [
376 [ 'rev_page', 'int4_ops', 'btree', 1 ],
377 [ 'rev_id', 'int4_ops', 'btree', 1 ],
378 ],
379 'CREATE INDEX rev_page_id ON revision (rev_page,rev_id)'
380 ],
381 [ 'addTable', 'searchindex', 'patch-searchindex-table.sql' ],
382 [ 'addPgIndex', 'oldimage', 'oi_timestamp', '(oi_timestamp)' ],
383 [ 'renameIndex', 'page', 'name_title', 'page_name_title' ],
384 [ 'renameIndex', 'change_tag', 'change_tag_rc_tag_id', 'ct_rc_tag_id' ],
385 [ 'renameIndex', 'change_tag', 'change_tag_log_tag_id', 'ct_log_tag_id' ],
386 [ 'renameIndex', 'change_tag', 'change_tag_rev_tag_id', 'ct_rev_tag_id' ],
387 [ 'renameIndex', 'change_tag', 'change_tag_tag_id_id', 'ct_tag_id_id' ],
388
389 // 1.38
390 [ 'doConvertDjvuMetadata' ],
391 [ 'dropPgField', 'page_restrictions', 'pr_user' ],
392 [ 'addTable', 'linktarget', 'patch-linktarget.sql' ],
393 [ 'dropIndex', 'revision', 'rev_page_id', 'patch-drop-rev_page_id.sql' ],
394 [ 'addField', 'templatelinks', 'tl_target_id', 'patch-templatelinks-target_id.sql' ],
395
396 // 1.39
397 [ 'addTable', 'user_autocreate_serial', 'patch-user_autocreate_serial.sql' ],
398 [ 'runMaintenance', MigrateRevisionActorTemp::class ],
399 [ 'dropTable', 'revision_actor_temp' ],
400 [ 'runMaintenance', UpdateRestrictions::class ],
401 [ 'dropPgField', 'page', 'page_restrictions' ],
402 [ 'migrateTemplatelinks' ],
403 [ 'changeNullableField', 'templatelinks', 'tl_target_id', 'NOT NULL', true ],
404 [ 'changePrimaryKey', 'templatelinks', [ 'tl_from', 'tl_target_id' ] ],
405 [ 'dropField', 'templatelinks', 'tl_title', 'patch-templatelinks-drop-tl_title.sql' ],
406
407 // 1.40
408 [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
409
410 // 1.41
411 [ 'addField', 'user', 'user_is_temp', 'patch-user-user_is_temp.sql' ],
412 [ 'runMaintenance', MigrateRevisionCommentTemp::class ],
413 [ 'dropTable', 'revision_comment_temp' ],
414 [ 'runMaintenance', MigrateExternallinks::class ],
415 [ 'modifyField', 'externallinks', 'el_to', 'patch-externallinks-el_to_default.sql' ],
416 [ 'addField', 'pagelinks', 'pl_target_id', 'patch-pagelinks-target_id.sql' ],
417 [ 'dropField', 'externallinks', 'el_to', 'patch-externallinks-drop-el_to.sql' ],
418 [ 'runMaintenance', FixInconsistentRedirects::class ],
419 [ 'modifyField', 'image', 'img_size', 'patch-image-img_size_to_bigint.sql' ],
420 [ 'modifyField', 'filearchive', 'fa_size', 'patch-filearchive-fa_size_to_bigint.sql' ],
421 [ 'modifyField', 'oldimage', 'oi_size', 'patch-oldimage-oi_size_to_bigint.sql' ],
422 [ 'modifyField', 'uploadstash', 'us_size', 'patch-uploadstash-us_size_to_bigint.sql' ],
423
424 // 1.42
425 [ 'addField', 'user_autocreate_serial', 'uas_year', 'patch-user_autocreate_serial-uas_year.sql' ],
426 [ 'addTable', 'block_target', 'patch-block_target.sql' ],
427 [ 'dropIndex', 'categorylinks', 'cl_collation_ext', 'patch-drop-cl_collation_ext.sql' ],
428 [ 'runMaintenance', PopulateUserIsTemp::class ],
429 [ 'dropIndex', 'sites', 'site_type', 'patch-sites-drop_indexes.sql' ],
430 [ 'dropIndex', 'iwlinks', 'iwl_prefix_from_title', 'patch-iwlinks-drop-iwl_prefix_from_title.sql' ],
431
432 // 1.43
433 [ 'migratePagelinks' ],
434 [ 'dropDefault', 'revision', 'rev_actor' ],
435 [ 'dropDefault', 'revision', 'rev_comment_id' ],
436 [ 'changeField', 'revision', 'rev_id', 'BIGINT', '' ],
437 [ 'changeField', 'revision', 'rev_parent_id', 'BIGINT', '' ],
438 [ 'changeField', 'recentchanges', 'rc_id', 'BIGINT', '' ],
439 [ 'changeField', 'change_tag', 'ct_rc_id', 'BIGINT', '' ],
440 [ 'runMaintenance', \MigrateBlocks::class ],
441 [ 'dropTable', 'ipblocks' ],
442 [ 'dropField', 'pagelinks', 'pl_title', 'patch-pagelinks-drop-pl_title.sql' ],
443 [ 'addPostDatabaseUpdateMaintenance', FixAutoblockLogTitles::class ],
444 [ 'migrateSearchindex' ],
445 ];
446 }
447
448 protected function getInitialUpdateKeys() {
449 return [
450 'filearchive-fa_major_mime-patch-fa_major_mime-chemical.sql',
451 'image-img_major_mime-patch-img_major_mime-chemical.sql',
452 'oldimage-oi_major_mime-patch-oi_major_mime-chemical.sql',
453 'user_groups-ug_group-patch-ug_group-length-increase-255.sql',
454 'user_former_groups-ufg_group-patch-ufg_group-length-increase-255.sql',
455 'user_properties-up_property-patch-up_property.sql',
456 ];
457 }
458
459 protected function describeTable( $table ) {
460 $q = <<<END
461SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
462 WHERE pg_class.relnamespace = pg_namespace.oid
463 AND attrelid=pg_class.oid AND attnum > 0
464 AND relname=%s AND nspname=%s
465END;
466 $res = $this->db->query(
467 sprintf( $q,
468 $this->db->addQuotes( $table ),
469 $this->db->addQuotes( $this->db->getCoreSchema() )
470 ),
471 __METHOD__
472 );
473 if ( !$res ) {
474 return null;
475 }
476
477 $cols = [];
478 foreach ( $res as $r ) {
479 $cols[] = [
480 "name" => $r[0],
481 "ord" => $r[1],
482 ];
483 }
484
485 return $cols;
486 }
487
488 protected function describeIndex( $idx ) {
489 // first fetch the key (which is a list of columns ords) and
490 // the table the index applies to (an oid)
491 $q = <<<END
492SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
493 WHERE nspname=%s
494 AND pg_class.relnamespace = pg_namespace.oid
495 AND relname=%s
496 AND indexrelid=pg_class.oid
497END;
498 $res = $this->db->query(
499 sprintf(
500 $q,
501 $this->db->addQuotes( $this->db->getCoreSchema() ),
502 $this->db->addQuotes( $idx )
503 ),
504 __METHOD__
505 );
506 if ( !$res ) {
507 return null;
508 }
509 $r = $res->fetchRow();
510 if ( !$r ) {
511 return null;
512 }
513
514 $indkey = $r[0];
515 $relid = intval( $r[1] );
516 $indkeys = explode( ' ', $indkey );
517
518 $colnames = [];
519 foreach ( $indkeys as $rid ) {
520 $query = <<<END
521SELECT attname FROM pg_class, pg_attribute
522 WHERE attrelid=$relid
523 AND attnum=%d
524 AND attrelid=pg_class.oid
525END;
526 $r2 = $this->db->query( sprintf( $query, $rid ), __METHOD__ );
527 if ( !$r2 ) {
528 return null;
529 }
530 $row2 = $r2->fetchRow();
531 if ( !$row2 ) {
532 return null;
533 }
534 $colnames[] = $row2[0];
535 }
536
537 return $colnames;
538 }
539
540 protected function fkeyDeltype( $fkey ) {
541 $q = <<<END
542SELECT confdeltype FROM pg_constraint, pg_namespace
543 WHERE connamespace=pg_namespace.oid
544 AND nspname=%s
545 AND conname=%s;
546END;
547 $r = $this->db->query(
548 sprintf(
549 $q,
550 $this->db->addQuotes( $this->db->getCoreSchema() ),
551 $this->db->addQuotes( $fkey )
552 ),
553 __METHOD__
554 );
555 $row = $r->fetchRow();
556 return $row ? $row[0] : null;
557 }
558
559 protected function ruleDef( $table, $rule ) {
560 $q = <<<END
561SELECT definition FROM pg_rules
562 WHERE schemaname = %s
563 AND tablename = %s
564 AND rulename = %s
565END;
566 $r = $this->db->query(
567 sprintf(
568 $q,
569 $this->db->addQuotes( $this->db->getCoreSchema() ),
570 $this->db->addQuotes( $table ),
571 $this->db->addQuotes( $rule )
572 ),
573 __METHOD__
574 );
575 $row = $r->fetchRow();
576 if ( !$row ) {
577 return null;
578 }
579 $d = $row[0];
580
581 return $d;
582 }
583
584 protected function addSequence( $table, $pkey, $ns ) {
585 if ( !$this->db->sequenceExists( $ns ) ) {
586 $this->output( "Creating sequence $ns\n" );
587 if ( $pkey !== false ) {
588 $table = $this->db->addIdentifierQuotes( $table );
589 $this->db->query( "CREATE SEQUENCE $ns OWNED BY $table.$pkey", __METHOD__ );
590 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
591 } else {
592 $this->db->query( "CREATE SEQUENCE $ns", __METHOD__ );
593 }
594 }
595 }
596
597 protected function dropSequence( $table, $ns ) {
598 if ( $this->db->sequenceExists( $ns ) ) {
599 $this->output( "Dropping sequence $ns\n" );
600 $this->db->query( "DROP SEQUENCE $ns CASCADE", __METHOD__ );
601 }
602 }
603
604 protected function renameSequence( $old, $new ) {
605 if ( $this->db->sequenceExists( $new ) ) {
606 $this->output( "...sequence $new already exists.\n" );
607
608 return;
609 }
610 if ( $this->db->sequenceExists( $old ) ) {
611 $this->output( "Renaming sequence $old to $new\n" );
612 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new", __METHOD__ );
613 }
614 }
615
616 protected function setSequenceOwner( $table, $pkey, $seq ) {
617 if ( $this->db->sequenceExists( $seq ) ) {
618 $this->output( "Setting sequence $seq owner to $table.$pkey\n" );
619 $table = $this->db->addIdentifierQuotes( $table );
620 $this->db->query( "ALTER SEQUENCE $seq OWNED BY $table.$pkey", __METHOD__ );
621 }
622 }
623
624 protected function renameTable( $old, $new, $patch = false ) {
625 if ( $this->db->tableExists( $old, __METHOD__ ) ) {
626 $this->output( "Renaming table $old to $new\n" );
627 $old = $this->db->addIdentifierQuotes( $old );
628 $new = $this->db->addIdentifierQuotes( $new );
629 $this->db->query( "ALTER TABLE $old RENAME TO $new", __METHOD__ );
630 if ( $patch !== false ) {
631 $this->applyPatch( $patch );
632 }
633 }
634 }
635
636 protected function renameIndex(
637 $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
638 ) {
639 // First requirement: the table must exist
640 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
641 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
642
643 return true;
644 }
645
646 // Second requirement: the new index must be missing
647 if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
648 $this->output( "...index $new already set on $table table.\n" );
649 if ( !$skipBothIndexExistWarning
650 && $this->db->indexExists( $table, $old, __METHOD__ )
651 ) {
652 $this->output( "...WARNING: $old still exists, despite it has been " .
653 "renamed into $new (which also exists).\n" .
654 " $old should be manually removed if not needed anymore.\n" );
655 }
656
657 return true;
658 }
659
660 // Third requirement: the old index must exist
661 if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
662 $this->output( "...skipping: index $old doesn't exist.\n" );
663
664 return true;
665 }
666
667 $this->db->query( "ALTER INDEX $old RENAME TO $new", __METHOD__ );
668 return true;
669 }
670
671 protected function dropPgField( $table, $field ) {
672 $fi = $this->db->fieldInfo( $table, $field );
673 if ( $fi === null ) {
674 $this->output( "...$table table does not contain $field field.\n" );
675 } else {
676 $this->output( "Dropping column '$table.$field'\n" );
677 $table = $this->db->addIdentifierQuotes( $table );
678 $this->db->query( "ALTER TABLE $table DROP COLUMN $field", __METHOD__ );
679 }
680 }
681
682 protected function addPgField( $table, $field, $type ) {
683 $fi = $this->db->fieldInfo( $table, $field );
684 if ( $fi !== null ) {
685 $this->output( "...column '$table.$field' already exists\n" );
686 } else {
687 $this->output( "Adding column '$table.$field'\n" );
688 $table = $this->db->addIdentifierQuotes( $table );
689 $this->db->query( "ALTER TABLE $table ADD $field $type", __METHOD__ );
690 }
691 }
692
693 protected function changeField( $table, $field, $newtype, $default ) {
694 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
695 $this->output( "...$table table does not exist, skipping default change.\n" );
696 return;
697 }
698 $fi = $this->db->fieldInfo( $table, $field );
699 if ( $fi === null ) {
700 $this->output( "...ERROR: expected column $table.$field to exist\n" );
701 exit( 1 );
702 }
703
704 if ( $fi->type() === strtolower( $newtype ) ) {
705 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
706 } else {
707 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
708 $table = $this->db->addIdentifierQuotes( $table );
709 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
710 if ( strlen( $default ) ) {
711 $res = [];
712 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
713 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
714 $this->db->query( $sqldef, __METHOD__ );
715 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
716 }
717 $sql .= " USING $default";
718 }
719 $this->db->query( $sql, __METHOD__ );
720 }
721 }
722
723 protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
724 # # For a cache table, empty it if the field needs to be changed, because the old contents
725 # # may be corrupted. If the column is already the desired type, refrain from purging.
726 $fi = $this->db->fieldInfo( $table, $field );
727 if ( $fi === null ) {
728 $this->output( "...ERROR: expected column $table.$field to exist\n" );
729 exit( 1 );
730 }
731
732 if ( $fi->type() === $newtype ) {
733 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
734 } else {
735 $this->output( "Purging data from cache table '$table'\n" );
736 $table = $this->db->addIdentifierQuotes( $table );
737 $this->db->query( "DELETE from $table", __METHOD__ );
738 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
739 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
740 if ( strlen( $default ) ) {
741 $res = [];
742 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
743 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
744 $this->db->query( $sqldef, __METHOD__ );
745 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
746 }
747 $sql .= " USING $default";
748 }
749 $this->db->query( $sql, __METHOD__ );
750 }
751 }
752
753 protected function setDefault( $table, $field, $default ) {
754 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
755 $this->output( "...$table table does not exist, skipping default change.\n" );
756 return;
757 }
758 $info = $this->db->fieldInfo( $table, $field );
759 if ( $info && $info->defaultValue() !== $default ) {
760 $this->output( "Changing '$table.$field' default value\n" );
761 $table = $this->db->addIdentifierQuotes( $table );
762 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT "
763 . $this->db->addQuotes( $default ), __METHOD__ );
764 }
765 }
766
773 protected function dropDefault( $table, $field ) {
774 $info = $this->db->fieldInfo( $table, $field );
775 if ( $info->defaultValue() !== false ) {
776 $this->output( "Removing '$table.$field' default value\n" );
777 $table = $this->db->addIdentifierQuotes( $table );
778 $this->db->query( "ALTER TABLE $table ALTER $field DROP DEFAULT", __METHOD__ );
779 }
780 }
781
782 protected function changeNullableField( $table, $field, $null, $update = false ) {
783 $fi = $this->db->fieldInfo( $table, $field );
784 if ( $fi === null ) {
785 return;
786 }
787 if ( $fi->isNullable() ) {
788 # # It's NULL - does it need to be NOT NULL?
789 if ( $null === 'NOT NULL' ) {
790 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
791 $table = $this->db->addIdentifierQuotes( $table );
792 if ( $update ) {
793 $this->db->query( "UPDATE $table SET $field = DEFAULT WHERE $field IS NULL", __METHOD__ );
794 }
795 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL", __METHOD__ );
796 } else {
797 $this->output( "...column '$table.$field' is already set as NULL\n" );
798 }
799 } else {
800 # # It's NOT NULL - does it need to be NULL?
801 if ( $null === 'NULL' ) {
802 $this->output( "Changing '$table.$field' to allow NULLs\n" );
803 $table = $this->db->addIdentifierQuotes( $table );
804 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL", __METHOD__ );
805 } else {
806 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
807 }
808 }
809 }
810
811 protected function addPgIndex( $table, $index, $type, $unique = false ) {
812 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
813 $this->output( "...$table table does not exist, skipping index.\n" );
814 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
815 $this->output( "...index '$index' on table '$table' already exists\n" );
816 } else {
817 $this->output( "Creating index '$index' on table '$table' $type\n" );
818 $table = $this->db->addIdentifierQuotes( $table );
819 $unique = $unique ? 'UNIQUE' : '';
820 $this->db->query( "CREATE $unique INDEX $index ON $table $type", __METHOD__ );
821 }
822 }
823
824 protected function addPgExtIndex( $table, $index, $type ) {
825 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
826 $this->output( "...index '$index' on table '$table' already exists\n" );
827 } elseif ( preg_match( '/^\‍(/', $type ) ) {
828 $this->output( "Creating index '$index' on table '$table'\n" );
829 $table = $this->db->addIdentifierQuotes( $table );
830 $this->db->query( "CREATE INDEX $index ON $table $type", __METHOD__ );
831 } else {
832 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
833 }
834 }
835
836 protected function dropFkey( $table, $field ) {
837 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
838 $this->output( "...$table table does not exist, skipping constraint change.\n" );
839 return;
840 }
841 $fi = $this->db->fieldInfo( $table, $field );
842 if ( $fi === null ) {
843 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
844 "Please report this.\n" );
845 return;
846 }
847
848 if ( $this->dropConstraint( $table, $field, 'foreignkey', $fi->conname() ) ) {
849 $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
850 } else {
851 $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
852 }
853 }
854
855 protected function changeFkeyDeferrable( $table, $field, $clause ) {
856 $fi = $this->db->fieldInfo( $table, $field );
857 if ( $fi === null ) {
858 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
859 "Please report this.\n" );
860
861 return;
862 }
863 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
864 return;
865 }
866 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
867
868 $conname = $fi->conname();
869 $conclause = "CONSTRAINT \"$conname\"";
870
871 if ( !$this->dropConstraint( $table, $field, 'foreignkey', $conname ) ) {
872 $this->output( "Column '$table.$field' does not have a foreign key " .
873 "constraint, will be added\n" );
874 $conclause = "";
875 }
876
877 $command =
878 "ALTER TABLE $table ADD $conclause " .
879 "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
880 $this->db->query( $command, __METHOD__ );
881 }
882
883 protected function dropPgIndex( $table, $index ) {
884 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
885 $this->output( "Dropping obsolete index '$index'\n" );
886 $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
887 }
888 }
889
890 protected function checkIndex( $index, $should_be, $good_def ) {
891 $pu = $this->db->indexAttributes( $index );
892 if ( $pu && $pu != $should_be ) {
893 $this->output( "Dropping obsolete version of index '$index'\n" );
894 $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
895 $pu = [];
896 } else {
897 $this->output( "...no need to drop index '$index'\n" );
898 }
899
900 if ( !$pu ) {
901 $this->output( "Creating index '$index'\n" );
902 $this->db->query( $good_def, __METHOD__ );
903 } else {
904 $this->output( "...index '$index' exists\n" );
905 }
906 }
907
908 protected function changePrimaryKey( $table, $shouldBe, $constraintName = null ) {
909 // https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
910 $result = $this->db->query(
911 "SELECT a.attname as column " .
912 "FROM pg_index i " .
913 "JOIN pg_attribute a ON a.attrelid = i.indrelid " .
914 "AND a.attnum = ANY(i.indkey) " .
915 "WHERE i.indrelid = '\"$table\"'::regclass " .
916 "AND i.indisprimary",
917 __METHOD__
918 );
919 $currentColumns = [];
920 foreach ( $result as $row ) {
921 $currentColumns[] = $row->column;
922 }
923
924 if ( $currentColumns == $shouldBe ) {
925 $this->output( "...no need to change primary key of '$table'\n" );
926 return true;
927 }
928
929 $this->dropConstraint( $table, '', 'primary', $constraintName );
930
931 $table = $this->db->addIdentifierQuotes( $table );
932 $this->db->query(
933 "ALTER TABLE $table" .
934 " ADD PRIMARY KEY (" . implode( ',', $shouldBe ) . ');',
935 __METHOD__
936 );
937 }
938
950 protected function dropConstraint( $table, $field, $type, $conname = null ) {
951 if ( $conname === null ) {
952 if ( $type == 'primary' ) {
953 $conname = "{$table}_pkey";
954 } else {
955 $map = [ 'unique' => 'key', 'check' => 'check', 'foreignkey' => 'fkey' ];
956 $conname = "{$table}_{$field}_{$map[$type]}";
957 }
958 }
959
960 if ( $this->db->constraintExists( $table, $conname ) ) {
961 $table = $this->db->addIdentifierQuotes( $table );
962 $this->db->query(
963 "ALTER TABLE $table DROP CONSTRAINT $conname;",
964 __METHOD__
965 );
966
967 return true;
968 }
969
970 return false;
971 }
972
979 protected function migrateSearchindex() {
980 $updateKey = 'searchindex-pk-titlelength';
981 if ( !$this->tableExists( 'searchindex' ) ) {
982 return;
983 }
984
985 $primaryIndexExists = $this->db->indexExists( 'searchindex', 'searchindex_pkey' );
986 if ( $this->updateRowExists( $updateKey ) || ( $primaryIndexExists ) ) {
987 $this->output( "...searchindex table has already been migrated.\n" );
988 if ( !$this->updateRowExists( $updateKey ) ) {
989 $this->insertUpdateRow( $updateKey );
990 }
991 return;
992 }
993
994 $apply = $this->applyPatch( 'patch-searchindex-pk-titlelength.sql', false, '...migrating searchindex table' );
995
996 if ( $apply ) {
997 $this->insertUpdateRow( $updateKey );
998 }
999 }
1000}
Fix redirect pages with missing or incomplete row in the redirect table.
Class for handling database updates.
applyPatch( $path, $isFullPath=false, $msg=null)
Applies a SQL patch.
updateRowExists( $key)
Helper function: check if the given key is present in the updatelog table.
insertUpdateRow( $key, $val=null)
Helper function: Add a key to the updatelog table.
Class for handling updates to Postgres databases.
getInitialUpdateKeys()
Get an array of update keys to insert into the updatelog table after a new installation.
addPgExtIndex( $table, $index, $type)
checkIndex( $index, $should_be, $good_def)
dropConstraint( $table, $field, $type, $conname=null)
Drop generic constraint.
addPgIndex( $table, $index, $type, $unique=false)
changeNullableField( $table, $field, $null, $update=false)
changeField( $table, $field, $newtype, $default)
changeFkeyDeferrable( $table, $field, $clause)
changeFieldPurgeTable( $table, $field, $newtype, $default)
renameIndex( $table, $old, $new, $skipBothIndexExistWarning=false, $a=false, $b=false)
Rename an index from an existing table.
changePrimaryKey( $table, $shouldBe, $constraintName=null)
setDefault( $table, $field, $default)
renameTable( $old, $new, $patch=false)
migrateSearchindex()
Replaces unique index with primary key,modifies si_title length.
dropDefault( $table, $field)
Drop a default value from a field.
Maintenance script that merges the revision_actor_temp table into the revision table.
Maintenance script that merges the revision_comment_temp table into the revision table.
Maintenance script that Fills the user_is_temp column of the user table for users created before MW 1...
Maintenance script that updates page_restrictions table from old page_restriction column.
Postgres database abstraction layer.