Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / installer / PostgresUpdater.php
blob729d11566ccc8774fb52861c11d36aea42885da2
1 <?php
2 /**
3 * PostgreSQL-specific updater.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Installer
24 namespace MediaWiki\Installer;
26 use FixInconsistentRedirects;
27 use MediaWiki\Maintenance\FixAutoblockLogTitles;
28 use MigrateExternallinks;
29 use MigrateRevisionActorTemp;
30 use MigrateRevisionCommentTemp;
31 use PopulateUserIsTemp;
32 use UpdateRestrictions;
33 use Wikimedia\Rdbms\DatabasePostgres;
35 /**
36 * Class for handling updates to Postgres databases.
38 * @ingroup Installer
39 * @since 1.17
41 class PostgresUpdater extends DatabaseUpdater {
43 /**
44 * @var DatabasePostgres
46 protected $db;
48 /**
49 * @return array
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' ],
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 ],
145 'CREATE UNIQUE INDEX "wl_user" ON "watchlist" (wl_user, wl_namespace, wl_title)'
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 ],
202 'CREATE INDEX cl_sortkey ON categorylinks (cl_to, cl_type, cl_sortkey, cl_from)'
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 ],
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 ],
379 'CREATE INDEX rev_page_id ON revision (rev_page,rev_id)'
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' ],
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' ],
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' ],
407 // 1.40
408 [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
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' ],
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' ],
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' ],
446 // 1.44
447 [ 'addTable', 'file', 'patch-file.sql' ],
451 protected function getInitialUpdateKeys() {
452 return [
453 'filearchive-fa_major_mime-patch-fa_major_mime-chemical.sql',
454 'image-img_major_mime-patch-img_major_mime-chemical.sql',
455 'oldimage-oi_major_mime-patch-oi_major_mime-chemical.sql',
456 'user_groups-ug_group-patch-ug_group-length-increase-255.sql',
457 'user_former_groups-ufg_group-patch-ufg_group-length-increase-255.sql',
458 'user_properties-up_property-patch-up_property.sql',
462 protected function describeTable( $table ) {
463 $q = <<<END
464 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
465 WHERE pg_class.relnamespace = pg_namespace.oid
466 AND attrelid=pg_class.oid AND attnum > 0
467 AND relname=%s AND nspname=%s
468 END;
469 $res = $this->db->query(
470 sprintf( $q,
471 $this->db->addQuotes( $table ),
472 $this->db->addQuotes( $this->db->getCoreSchema() )
474 __METHOD__
476 if ( !$res ) {
477 return null;
480 $cols = [];
481 foreach ( $res as $r ) {
482 $cols[] = [
483 "name" => $r[0],
484 "ord" => $r[1],
488 return $cols;
491 protected function describeIndex( $idx ) {
492 // first fetch the key (which is a list of columns ords) and
493 // the table the index applies to (an oid)
494 $q = <<<END
495 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
496 WHERE nspname=%s
497 AND pg_class.relnamespace = pg_namespace.oid
498 AND relname=%s
499 AND indexrelid=pg_class.oid
500 END;
501 $res = $this->db->query(
502 sprintf(
504 $this->db->addQuotes( $this->db->getCoreSchema() ),
505 $this->db->addQuotes( $idx )
507 __METHOD__
509 if ( !$res ) {
510 return null;
512 $r = $res->fetchRow();
513 if ( !$r ) {
514 return null;
517 $indkey = $r[0];
518 $relid = intval( $r[1] );
519 $indkeys = explode( ' ', $indkey );
521 $colnames = [];
522 foreach ( $indkeys as $rid ) {
523 $query = <<<END
524 SELECT attname FROM pg_class, pg_attribute
525 WHERE attrelid=$relid
526 AND attnum=%d
527 AND attrelid=pg_class.oid
528 END;
529 $r2 = $this->db->query( sprintf( $query, $rid ), __METHOD__ );
530 if ( !$r2 ) {
531 return null;
533 $row2 = $r2->fetchRow();
534 if ( !$row2 ) {
535 return null;
537 $colnames[] = $row2[0];
540 return $colnames;
543 protected function fkeyDeltype( $fkey ) {
544 $q = <<<END
545 SELECT confdeltype FROM pg_constraint, pg_namespace
546 WHERE connamespace=pg_namespace.oid
547 AND nspname=%s
548 AND conname=%s;
549 END;
550 $r = $this->db->query(
551 sprintf(
553 $this->db->addQuotes( $this->db->getCoreSchema() ),
554 $this->db->addQuotes( $fkey )
556 __METHOD__
558 $row = $r->fetchRow();
559 return $row ? $row[0] : null;
562 protected function ruleDef( $table, $rule ) {
563 $q = <<<END
564 SELECT definition FROM pg_rules
565 WHERE schemaname = %s
566 AND tablename = %s
567 AND rulename = %s
568 END;
569 $r = $this->db->query(
570 sprintf(
572 $this->db->addQuotes( $this->db->getCoreSchema() ),
573 $this->db->addQuotes( $table ),
574 $this->db->addQuotes( $rule )
576 __METHOD__
578 $row = $r->fetchRow();
579 if ( !$row ) {
580 return null;
582 $d = $row[0];
584 return $d;
587 protected function addSequence( $table, $pkey, $ns ) {
588 if ( !$this->db->sequenceExists( $ns ) ) {
589 $this->output( "Creating sequence $ns\n" );
590 if ( $pkey !== false ) {
591 $table = $this->db->addIdentifierQuotes( $table );
592 $this->db->query( "CREATE SEQUENCE $ns OWNED BY $table.$pkey", __METHOD__ );
593 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
594 } else {
595 $this->db->query( "CREATE SEQUENCE $ns", __METHOD__ );
600 protected function dropSequence( $table, $ns ) {
601 if ( $this->db->sequenceExists( $ns ) ) {
602 $this->output( "Dropping sequence $ns\n" );
603 $this->db->query( "DROP SEQUENCE $ns CASCADE", __METHOD__ );
607 protected function renameSequence( $old, $new ) {
608 if ( $this->db->sequenceExists( $new ) ) {
609 $this->output( "...sequence $new already exists.\n" );
611 return;
613 if ( $this->db->sequenceExists( $old ) ) {
614 $this->output( "Renaming sequence $old to $new\n" );
615 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new", __METHOD__ );
619 protected function setSequenceOwner( $table, $pkey, $seq ) {
620 if ( $this->db->sequenceExists( $seq ) ) {
621 $this->output( "Setting sequence $seq owner to $table.$pkey\n" );
622 $table = $this->db->addIdentifierQuotes( $table );
623 $this->db->query( "ALTER SEQUENCE $seq OWNED BY $table.$pkey", __METHOD__ );
627 protected function renameTable( $old, $new, $patch = false ) {
628 if ( $this->db->tableExists( $old, __METHOD__ ) ) {
629 $this->output( "Renaming table $old to $new\n" );
630 $old = $this->db->addIdentifierQuotes( $old );
631 $new = $this->db->addIdentifierQuotes( $new );
632 $this->db->query( "ALTER TABLE $old RENAME TO $new", __METHOD__ );
633 if ( $patch !== false ) {
634 $this->applyPatch( $patch );
639 protected function renameIndex(
640 $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
642 // First requirement: the table must exist
643 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
644 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
646 return true;
649 // Second requirement: the new index must be missing
650 if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
651 $this->output( "...index $new already set on $table table.\n" );
652 if ( !$skipBothIndexExistWarning
653 && $this->db->indexExists( $table, $old, __METHOD__ )
655 $this->output( "...WARNING: $old still exists, despite it has been " .
656 "renamed into $new (which also exists).\n" .
657 " $old should be manually removed if not needed anymore.\n" );
660 return true;
663 // Third requirement: the old index must exist
664 if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
665 $this->output( "...skipping: index $old doesn't exist.\n" );
667 return true;
670 $this->db->query( "ALTER INDEX $old RENAME TO $new", __METHOD__ );
671 return true;
674 protected function dropPgField( $table, $field ) {
675 $fi = $this->db->fieldInfo( $table, $field );
676 if ( $fi === null ) {
677 $this->output( "...$table table does not contain $field field.\n" );
678 } else {
679 $this->output( "Dropping column '$table.$field'\n" );
680 $table = $this->db->addIdentifierQuotes( $table );
681 $this->db->query( "ALTER TABLE $table DROP COLUMN $field", __METHOD__ );
685 protected function addPgField( $table, $field, $type ) {
686 $fi = $this->db->fieldInfo( $table, $field );
687 if ( $fi !== null ) {
688 $this->output( "...column '$table.$field' already exists\n" );
689 } else {
690 $this->output( "Adding column '$table.$field'\n" );
691 $table = $this->db->addIdentifierQuotes( $table );
692 $this->db->query( "ALTER TABLE $table ADD $field $type", __METHOD__ );
696 protected function changeField( $table, $field, $newtype, $default ) {
697 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
698 $this->output( "...$table table does not exist, skipping default change.\n" );
699 return;
701 $fi = $this->db->fieldInfo( $table, $field );
702 if ( $fi === null ) {
703 $this->output( "...ERROR: expected column $table.$field to exist\n" );
704 exit( 1 );
707 if ( $fi->type() === strtolower( $newtype ) ) {
708 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
709 } else {
710 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
711 $table = $this->db->addIdentifierQuotes( $table );
712 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
713 if ( strlen( $default ) ) {
714 $res = [];
715 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
716 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
717 $this->db->query( $sqldef, __METHOD__ );
718 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
720 $sql .= " USING $default";
722 $this->db->query( $sql, __METHOD__ );
726 protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
727 # # For a cache table, empty it if the field needs to be changed, because the old contents
728 # # may be corrupted. If the column is already the desired type, refrain from purging.
729 $fi = $this->db->fieldInfo( $table, $field );
730 if ( $fi === null ) {
731 $this->output( "...ERROR: expected column $table.$field to exist\n" );
732 exit( 1 );
735 if ( $fi->type() === $newtype ) {
736 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
737 } else {
738 $this->output( "Purging data from cache table '$table'\n" );
739 $table = $this->db->addIdentifierQuotes( $table );
740 $this->db->query( "DELETE from $table", __METHOD__ );
741 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
742 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
743 if ( strlen( $default ) ) {
744 $res = [];
745 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
746 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
747 $this->db->query( $sqldef, __METHOD__ );
748 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
750 $sql .= " USING $default";
752 $this->db->query( $sql, __METHOD__ );
756 protected function setDefault( $table, $field, $default ) {
757 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
758 $this->output( "...$table table does not exist, skipping default change.\n" );
759 return;
761 $info = $this->db->fieldInfo( $table, $field );
762 if ( $info && $info->defaultValue() !== $default ) {
763 $this->output( "Changing '$table.$field' default value\n" );
764 $table = $this->db->addIdentifierQuotes( $table );
765 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT "
766 . $this->db->addQuotes( $default ), __METHOD__ );
771 * Drop a default value from a field
772 * @since 1.32
773 * @param string $table
774 * @param string $field
776 protected function dropDefault( $table, $field ) {
777 $info = $this->db->fieldInfo( $table, $field );
778 if ( $info->defaultValue() !== false ) {
779 $this->output( "Removing '$table.$field' default value\n" );
780 $table = $this->db->addIdentifierQuotes( $table );
781 $this->db->query( "ALTER TABLE $table ALTER $field DROP DEFAULT", __METHOD__ );
785 protected function changeNullableField( $table, $field, $null, $update = false ) {
786 $fi = $this->db->fieldInfo( $table, $field );
787 if ( $fi === null ) {
788 return;
790 if ( $fi->isNullable() ) {
791 # # It's NULL - does it need to be NOT NULL?
792 if ( $null === 'NOT NULL' ) {
793 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
794 $table = $this->db->addIdentifierQuotes( $table );
795 if ( $update ) {
796 $this->db->query( "UPDATE $table SET $field = DEFAULT WHERE $field IS NULL", __METHOD__ );
798 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL", __METHOD__ );
799 } else {
800 $this->output( "...column '$table.$field' is already set as NULL\n" );
802 } else {
803 # # It's NOT NULL - does it need to be NULL?
804 if ( $null === 'NULL' ) {
805 $this->output( "Changing '$table.$field' to allow NULLs\n" );
806 $table = $this->db->addIdentifierQuotes( $table );
807 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL", __METHOD__ );
808 } else {
809 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
814 protected function addPgIndex( $table, $index, $type, $unique = false ) {
815 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
816 $this->output( "...$table table does not exist, skipping index.\n" );
817 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
818 $this->output( "...index '$index' on table '$table' already exists\n" );
819 } else {
820 $this->output( "Creating index '$index' on table '$table' $type\n" );
821 $table = $this->db->addIdentifierQuotes( $table );
822 $unique = $unique ? 'UNIQUE' : '';
823 $this->db->query( "CREATE $unique INDEX $index ON $table $type", __METHOD__ );
827 protected function addPgExtIndex( $table, $index, $type ) {
828 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
829 $this->output( "...index '$index' on table '$table' already exists\n" );
830 } elseif ( preg_match( '/^\(/', $type ) ) {
831 $this->output( "Creating index '$index' on table '$table'\n" );
832 $table = $this->db->addIdentifierQuotes( $table );
833 $this->db->query( "CREATE INDEX $index ON $table $type", __METHOD__ );
834 } else {
835 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
839 protected function dropFkey( $table, $field ) {
840 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
841 $this->output( "...$table table does not exist, skipping constraint change.\n" );
842 return;
844 $fi = $this->db->fieldInfo( $table, $field );
845 if ( $fi === null ) {
846 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
847 "Please report this.\n" );
848 return;
851 if ( $this->dropConstraint( $table, $field, 'foreignkey', $fi->conname() ) ) {
852 $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
853 } else {
854 $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
858 protected function changeFkeyDeferrable( $table, $field, $clause ) {
859 $fi = $this->db->fieldInfo( $table, $field );
860 if ( $fi === null ) {
861 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
862 "Please report this.\n" );
864 return;
866 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
867 return;
869 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
871 $conname = $fi->conname();
872 $conclause = "CONSTRAINT \"$conname\"";
874 if ( !$this->dropConstraint( $table, $field, 'foreignkey', $conname ) ) {
875 $this->output( "Column '$table.$field' does not have a foreign key " .
876 "constraint, will be added\n" );
877 $conclause = "";
880 $command =
881 "ALTER TABLE $table ADD $conclause " .
882 "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
883 $this->db->query( $command, __METHOD__ );
886 protected function dropPgIndex( $table, $index ) {
887 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
888 $this->output( "Dropping obsolete index '$index'\n" );
889 $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
893 protected function checkIndex( $index, $should_be, $good_def ) {
894 $pu = $this->db->indexAttributes( $index );
895 if ( $pu && $pu != $should_be ) {
896 $this->output( "Dropping obsolete version of index '$index'\n" );
897 $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
898 $pu = [];
899 } else {
900 $this->output( "...no need to drop index '$index'\n" );
903 if ( !$pu ) {
904 $this->output( "Creating index '$index'\n" );
905 $this->db->query( $good_def, __METHOD__ );
906 } else {
907 $this->output( "...index '$index' exists\n" );
911 protected function changePrimaryKey( $table, $shouldBe, $constraintName = null ) {
912 // https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
913 $result = $this->db->query(
914 "SELECT a.attname as column " .
915 "FROM pg_index i " .
916 "JOIN pg_attribute a ON a.attrelid = i.indrelid " .
917 "AND a.attnum = ANY(i.indkey) " .
918 "WHERE i.indrelid = '\"$table\"'::regclass " .
919 "AND i.indisprimary",
920 __METHOD__
922 $currentColumns = [];
923 foreach ( $result as $row ) {
924 $currentColumns[] = $row->column;
927 if ( $currentColumns == $shouldBe ) {
928 $this->output( "...no need to change primary key of '$table'\n" );
929 return true;
932 $this->dropConstraint( $table, '', 'primary', $constraintName );
934 $table = $this->db->addIdentifierQuotes( $table );
935 $this->db->query(
936 "ALTER TABLE $table" .
937 " ADD PRIMARY KEY (" . implode( ',', $shouldBe ) . ');',
938 __METHOD__
943 * Drop generic constraint. If the constraint was created with a custom name,
944 * then the name must be queried and supplied as $conname, otherwise standard
945 * system suffixes and format would be assumed.
947 * @param string $table
948 * @param string $field
949 * @param string $type
950 * @param string|null $conname
951 * @return bool
953 protected function dropConstraint( $table, $field, $type, $conname = null ) {
954 if ( $conname === null ) {
955 if ( $type == 'primary' ) {
956 $conname = "{$table}_pkey";
957 } else {
958 $map = [ 'unique' => 'key', 'check' => 'check', 'foreignkey' => 'fkey' ];
959 $conname = "{$table}_{$field}_{$map[$type]}";
963 if ( $this->db->constraintExists( $table, $conname ) ) {
964 $table = $this->db->addIdentifierQuotes( $table );
965 $this->db->query(
966 "ALTER TABLE $table DROP CONSTRAINT $conname;",
967 __METHOD__
970 return true;
973 return false;
977 * Replaces unique index with primary key,modifies si_title length
979 * @since 1.43
980 * @return void
982 protected function migrateSearchindex() {
983 $updateKey = 'searchindex-pk-titlelength';
984 if ( !$this->tableExists( 'searchindex' ) ) {
985 return;
988 $primaryIndexExists = $this->db->indexExists( 'searchindex', 'searchindex_pkey' );
989 if ( $this->updateRowExists( $updateKey ) || ( $primaryIndexExists ) ) {
990 $this->output( "...searchindex table has already been migrated.\n" );
991 if ( !$this->updateRowExists( $updateKey ) ) {
992 $this->insertUpdateRow( $updateKey );
994 return;
997 $apply = $this->applyPatch( 'patch-searchindex-pk-titlelength.sql', false, '...migrating searchindex table' );
999 if ( $apply ) {
1000 $this->insertUpdateRow( $updateKey );