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