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