Convert numerous UNIQUE INDEX to PRIMARY KEY
[lhc/web/wiklou.git] / includes / installer / PostgresUpdater.php
1 <?php
2 /**
3 * PostgreSQL-specific updater.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 use Wikimedia\Rdbms\DatabasePostgres;
25
26 /**
27 * Class for handling updates to Postgres databases.
28 *
29 * @ingroup Deployment
30 * @since 1.17
31 */
32 class PostgresUpdater extends DatabaseUpdater {
33
34 /**
35 * @var DatabasePostgres
36 */
37 protected $db;
38
39 /**
40 * @todo FIXME: Postgres should use sequential updates like Mysql, Sqlite
41 * and everybody else. It never got refactored like it should've.
42 * @return array
43 */
44 protected function getCoreUpdateList() {
45 return [
46 # rename tables 1.7.3
47 # r15791 Change reserved word table names "user" and "text"
48 [ 'renameTable', 'user', 'mwuser' ],
49 [ 'renameTable', 'text', 'pagecontent' ],
50 [ 'renameIndex', 'mwuser', 'user_pkey', 'mwuser_pkey' ],
51 [ 'renameIndex', 'mwuser', 'user_user_name_key', 'mwuser_user_name_key' ],
52 [ 'renameIndex', 'pagecontent', 'text_pkey', 'pagecontent_pkey' ],
53
54 # renamed sequences
55 [ 'renameSequence', 'ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq' ],
56 [ 'renameSequence', 'rev_rev_id_val', 'revision_rev_id_seq' ],
57 [ 'renameSequence', 'text_old_id_val', 'text_old_id_seq' ],
58 [ 'renameSequence', 'rc_rc_id_seq', 'recentchanges_rc_id_seq' ],
59 [ 'renameSequence', 'log_log_id_seq', 'logging_log_id_seq' ],
60 [ 'renameSequence', 'pr_id_val', 'page_restrictions_pr_id_seq' ],
61 [ 'renameSequence', 'us_id_seq', 'uploadstash_us_id_seq' ],
62
63 # since r58263
64 [ 'renameSequence', 'category_id_seq', 'category_cat_id_seq' ],
65
66 # new sequences if not renamed above
67 [ 'addSequence', 'logging', false, 'logging_log_id_seq' ],
68 [ 'addSequence', 'page_restrictions', false, 'page_restrictions_pr_id_seq' ],
69 [ 'addSequence', 'filearchive', 'fa_id', 'filearchive_fa_id_seq' ],
70 [ 'addSequence', 'archive', false, 'archive_ar_id_seq' ],
71 [ 'addSequence', 'externallinks', false, 'externallinks_el_id_seq' ],
72 [ 'addSequence', 'watchlist', false, 'watchlist_wl_id_seq' ],
73 [ 'addSequence', 'change_tag', false, 'change_tag_ct_id_seq' ],
74 [ 'addSequence', 'tag_summary', false, 'tag_summary_ts_id_seq' ],
75
76 # new tables
77 [ 'addTable', 'category', 'patch-category.sql' ],
78 [ 'addTable', 'page', 'patch-page.sql' ],
79 [ 'addTable', 'querycachetwo', 'patch-querycachetwo.sql' ],
80 [ 'addTable', 'page_props', 'patch-page_props.sql' ],
81 [ 'addTable', 'page_restrictions', 'patch-page_restrictions.sql' ],
82 [ 'addTable', 'profiling', 'patch-profiling.sql' ],
83 [ 'addTable', 'protected_titles', 'patch-protected_titles.sql' ],
84 [ 'addTable', 'redirect', 'patch-redirect.sql' ],
85 [ 'addTable', 'updatelog', 'patch-updatelog.sql' ],
86 [ 'addTable', 'change_tag', 'patch-change_tag.sql' ],
87 [ 'addTable', 'tag_summary', 'patch-tag_summary.sql' ],
88 [ 'addTable', 'valid_tag', 'patch-valid_tag.sql' ],
89 [ 'addTable', 'user_properties', 'patch-user_properties.sql' ],
90 [ 'addTable', 'log_search', 'patch-log_search.sql' ],
91 [ 'addTable', 'l10n_cache', 'patch-l10n_cache.sql' ],
92 [ 'addTable', 'iwlinks', 'patch-iwlinks.sql' ],
93 [ 'addTable', 'module_deps', 'patch-module_deps.sql' ],
94 [ 'addTable', 'uploadstash', 'patch-uploadstash.sql' ],
95 [ 'addTable', 'user_former_groups', 'patch-user_former_groups.sql' ],
96 [ 'addTable', 'sites', 'patch-sites.sql' ],
97 [ 'addTable', 'bot_passwords', 'patch-bot_passwords.sql' ],
98
99 # Needed before new field
100 [ 'convertArchive2' ],
101
102 # new fields
103 [ 'addPgField', 'updatelog', 'ul_value', 'TEXT' ],
104 [ 'addPgField', 'archive', 'ar_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
105 [ 'addPgField', 'archive', 'ar_len', 'INTEGER' ],
106 [ 'addPgField', 'archive', 'ar_page_id', 'INTEGER' ],
107 [ 'addPgField', 'archive', 'ar_parent_id', 'INTEGER' ],
108 [ 'addPgField', 'archive', 'ar_content_model', 'TEXT' ],
109 [ 'addPgField', 'archive', 'ar_content_format', 'TEXT' ],
110 [ 'addPgField', 'categorylinks', 'cl_sortkey_prefix', "TEXT NOT NULL DEFAULT ''" ],
111 [ 'addPgField', 'categorylinks', 'cl_collation', "TEXT NOT NULL DEFAULT 0" ],
112 [ 'addPgField', 'categorylinks', 'cl_type', "TEXT NOT NULL DEFAULT 'page'" ],
113 [ 'addPgField', 'image', 'img_sha1', "TEXT NOT NULL DEFAULT ''" ],
114 [ 'addPgField', 'ipblocks', 'ipb_allow_usertalk', 'SMALLINT NOT NULL DEFAULT 0' ],
115 [ 'addPgField', 'ipblocks', 'ipb_anon_only', 'SMALLINT NOT NULL DEFAULT 0' ],
116 [ 'addPgField', 'ipblocks', 'ipb_by_text', "TEXT NOT NULL DEFAULT ''" ],
117 [ 'addPgField', 'ipblocks', 'ipb_block_email', 'SMALLINT NOT NULL DEFAULT 0' ],
118 [ 'addPgField', 'ipblocks', 'ipb_create_account', 'SMALLINT NOT NULL DEFAULT 1' ],
119 [ 'addPgField', 'ipblocks', 'ipb_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
120 [ 'addPgField', 'ipblocks', 'ipb_enable_autoblock', 'SMALLINT NOT NULL DEFAULT 1' ],
121 [ 'addPgField', 'ipblocks', 'ipb_parent_block_id',
122 'INTEGER DEFAULT NULL REFERENCES ipblocks(ipb_id) ' .
123 'ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED' ],
124 [ 'addPgField', 'filearchive', 'fa_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
125 [ 'addPgField', 'filearchive', 'fa_sha1', "TEXT NOT NULL DEFAULT ''" ],
126 [ 'addPgField', 'logging', 'log_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
127 [ 'addPgField', 'logging', 'log_id',
128 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ],
129 [ 'addPgField', 'logging', 'log_params', 'TEXT' ],
130 [ 'addPgField', 'mwuser', 'user_editcount', 'INTEGER' ],
131 [ 'addPgField', 'mwuser', 'user_newpass_time', 'TIMESTAMPTZ' ],
132 [ 'addPgField', 'oldimage', 'oi_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
133 [ 'addPgField', 'oldimage', 'oi_major_mime', "TEXT NOT NULL DEFAULT 'unknown'" ],
134 [ 'addPgField', 'oldimage', 'oi_media_type', 'TEXT' ],
135 [ 'addPgField', 'oldimage', 'oi_metadata', "BYTEA NOT NULL DEFAULT ''" ],
136 [ 'addPgField', 'oldimage', 'oi_minor_mime', "TEXT NOT NULL DEFAULT 'unknown'" ],
137 [ 'addPgField', 'oldimage', 'oi_sha1', "TEXT NOT NULL DEFAULT ''" ],
138 [ 'addPgField', 'page', 'page_content_model', 'TEXT' ],
139 [ 'addPgField', 'page_restrictions', 'pr_id',
140 "INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_seq')" ],
141 [ 'addPgField', 'profiling', 'pf_memory', 'NUMERIC(18,10) NOT NULL DEFAULT 0' ],
142 [ 'addPgField', 'recentchanges', 'rc_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
143 [ 'addPgField', 'recentchanges', 'rc_log_action', 'TEXT' ],
144 [ 'addPgField', 'recentchanges', 'rc_log_type', 'TEXT' ],
145 [ 'addPgField', 'recentchanges', 'rc_logid', 'INTEGER NOT NULL DEFAULT 0' ],
146 [ 'addPgField', 'recentchanges', 'rc_new_len', 'INTEGER' ],
147 [ 'addPgField', 'recentchanges', 'rc_old_len', 'INTEGER' ],
148 [ 'addPgField', 'recentchanges', 'rc_params', 'TEXT' ],
149 [ 'addPgField', 'redirect', 'rd_interwiki', 'TEXT NULL' ],
150 [ 'addPgField', 'redirect', 'rd_fragment', 'TEXT NULL' ],
151 [ 'addPgField', 'revision', 'rev_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
152 [ 'addPgField', 'revision', 'rev_len', 'INTEGER' ],
153 [ 'addPgField', 'revision', 'rev_parent_id', 'INTEGER DEFAULT NULL' ],
154 [ 'addPgField', 'revision', 'rev_content_model', 'TEXT' ],
155 [ 'addPgField', 'revision', 'rev_content_format', 'TEXT' ],
156 [ 'addPgField', 'site_stats', 'ss_active_users', "INTEGER DEFAULT '-1'" ],
157 [ 'addPgField', 'user_newtalk', 'user_last_timestamp', 'TIMESTAMPTZ' ],
158 [ 'addPgField', 'logging', 'log_user_text', "TEXT NOT NULL DEFAULT ''" ],
159 [ 'addPgField', 'logging', 'log_page', 'INTEGER' ],
160 [ 'addPgField', 'interwiki', 'iw_api', "TEXT NOT NULL DEFAULT ''" ],
161 [ 'addPgField', 'interwiki', 'iw_wikiid', "TEXT NOT NULL DEFAULT ''" ],
162 [ 'addPgField', 'revision', 'rev_sha1', "TEXT NOT NULL DEFAULT ''" ],
163 [ 'addPgField', 'archive', 'ar_sha1', "TEXT NOT NULL DEFAULT ''" ],
164 [ 'addPgField', 'uploadstash', 'us_chunk_inx', "INTEGER NULL" ],
165 [ 'addPgField', 'job', 'job_timestamp', "TIMESTAMPTZ" ],
166 [ 'addPgField', 'job', 'job_random', "INTEGER NOT NULL DEFAULT 0" ],
167 [ 'addPgField', 'job', 'job_attempts', "INTEGER NOT NULL DEFAULT 0" ],
168 [ 'addPgField', 'job', 'job_token', "TEXT NOT NULL DEFAULT ''" ],
169 [ 'addPgField', 'job', 'job_token_timestamp', "TIMESTAMPTZ" ],
170 [ 'addPgField', 'job', 'job_sha1', "TEXT NOT NULL DEFAULT ''" ],
171 [ 'addPgField', 'archive', 'ar_id',
172 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('archive_ar_id_seq')" ],
173 [ 'addPgField', 'externallinks', 'el_id',
174 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('externallinks_el_id_seq')" ],
175 [ 'addPgField', 'uploadstash', 'us_props', "BYTEA" ],
176
177 # type changes
178 [ 'changeField', 'archive', 'ar_deleted', 'smallint', '' ],
179 [ 'changeField', 'archive', 'ar_minor_edit', 'smallint',
180 'ar_minor_edit::smallint DEFAULT 0' ],
181 [ 'changeField', 'filearchive', 'fa_deleted', 'smallint', '' ],
182 [ 'changeField', 'filearchive', 'fa_height', 'integer', '' ],
183 [ 'changeField', 'filearchive', 'fa_metadata', 'bytea', "decode(fa_metadata,'escape')" ],
184 [ 'changeField', 'filearchive', 'fa_size', 'integer', '' ],
185 [ 'changeField', 'filearchive', 'fa_width', 'integer', '' ],
186 [ 'changeField', 'filearchive', 'fa_storage_group', 'text', '' ],
187 [ 'changeField', 'filearchive', 'fa_storage_key', 'text', '' ],
188 [ 'changeField', 'image', 'img_metadata', 'bytea', "decode(img_metadata,'escape')" ],
189 [ 'changeField', 'image', 'img_size', 'integer', '' ],
190 [ 'changeField', 'image', 'img_width', 'integer', '' ],
191 [ 'changeField', 'image', 'img_height', 'integer', '' ],
192 [ 'changeField', 'interwiki', 'iw_local', 'smallint', 'iw_local::smallint' ],
193 [ 'changeField', 'interwiki', 'iw_trans', 'smallint', 'iw_trans::smallint DEFAULT 0' ],
194 [ 'changeField', 'ipblocks', 'ipb_auto', 'smallint', 'ipb_auto::smallint DEFAULT 0' ],
195 [ 'changeField', 'ipblocks', 'ipb_anon_only', 'smallint',
196 "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ],
197 [ 'changeField', 'ipblocks', 'ipb_create_account', 'smallint',
198 "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ],
199 [ 'changeField', 'ipblocks', 'ipb_enable_autoblock', 'smallint',
200 "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ],
201 [ 'changeField', 'ipblocks', 'ipb_block_email', 'smallint',
202 "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ],
203 [ 'changeField', 'ipblocks', 'ipb_address', 'text', 'ipb_address::text' ],
204 [ 'changeField', 'ipblocks', 'ipb_deleted', 'smallint', 'ipb_deleted::smallint DEFAULT 0' ],
205 [ 'changeField', 'mwuser', 'user_token', 'text', '' ],
206 [ 'changeField', 'mwuser', 'user_email_token', 'text', '' ],
207 [ 'changeField', 'objectcache', 'keyname', 'text', '' ],
208 [ 'changeField', 'oldimage', 'oi_height', 'integer', '' ],
209 [ 'changeField', 'oldimage', 'oi_metadata', 'bytea', "decode(img_metadata,'escape')" ],
210 [ 'changeField', 'oldimage', 'oi_size', 'integer', '' ],
211 [ 'changeField', 'oldimage', 'oi_width', 'integer', '' ],
212 [ 'changeField', 'page', 'page_is_redirect', 'smallint',
213 'page_is_redirect::smallint DEFAULT 0' ],
214 [ 'changeField', 'page', 'page_is_new', 'smallint', 'page_is_new::smallint DEFAULT 0' ],
215 [ 'changeField', 'querycache', 'qc_value', 'integer', '' ],
216 [ 'changeField', 'querycachetwo', 'qcc_value', 'integer', '' ],
217 [ 'changeField', 'recentchanges', 'rc_bot', 'smallint', 'rc_bot::smallint DEFAULT 0' ],
218 [ 'changeField', 'recentchanges', 'rc_deleted', 'smallint', '' ],
219 [ 'changeField', 'recentchanges', 'rc_minor', 'smallint', 'rc_minor::smallint DEFAULT 0' ],
220 [ 'changeField', 'recentchanges', 'rc_new', 'smallint', 'rc_new::smallint DEFAULT 0' ],
221 [ 'changeField', 'recentchanges', 'rc_type', 'smallint', 'rc_type::smallint DEFAULT 0' ],
222 [ 'changeField', 'recentchanges', 'rc_patrolled', 'smallint',
223 'rc_patrolled::smallint DEFAULT 0' ],
224 [ 'changeField', 'revision', 'rev_deleted', 'smallint', 'rev_deleted::smallint DEFAULT 0' ],
225 [ 'changeField', 'revision', 'rev_minor_edit', 'smallint',
226 'rev_minor_edit::smallint DEFAULT 0' ],
227 [ 'changeField', 'templatelinks', 'tl_namespace', 'smallint', 'tl_namespace::smallint' ],
228 [ 'changeField', 'user_newtalk', 'user_ip', 'text', 'host(user_ip)' ],
229 [ 'changeField', 'uploadstash', 'us_image_bits', 'smallint', '' ],
230 [ 'changeField', 'profiling', 'pf_time', 'float', '' ],
231 [ 'changeField', 'profiling', 'pf_memory', 'float', '' ],
232
233 # null changes
234 [ 'changeNullableField', 'oldimage', 'oi_bits', 'NULL' ],
235 [ 'changeNullableField', 'oldimage', 'oi_timestamp', 'NULL' ],
236 [ 'changeNullableField', 'oldimage', 'oi_major_mime', 'NULL' ],
237 [ 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NULL' ],
238 [ 'changeNullableField', 'image', 'img_metadata', 'NOT NULL' ],
239 [ 'changeNullableField', 'filearchive', 'fa_metadata', 'NOT NULL' ],
240 [ 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NULL' ],
241 [ 'changeNullableField', 'recentchanges', 'rc_cur_time', 'NULL' ],
242
243 [ 'checkOiDeleted' ],
244
245 # New indexes
246 [ 'addPgIndex', 'archive', 'archive_user_text', '(ar_user_text)' ],
247 [ 'addPgIndex', 'image', 'img_sha1', '(img_sha1)' ],
248 [ 'addPgIndex', 'ipblocks', 'ipb_parent_block_id', '(ipb_parent_block_id)' ],
249 [ 'addPgIndex', 'oldimage', 'oi_sha1', '(oi_sha1)' ],
250 [ 'addPgIndex', 'page', 'page_mediawiki_title', '(page_title) WHERE page_namespace = 8' ],
251 [ 'addPgIndex', 'pagelinks', 'pagelinks_title', '(pl_title)' ],
252 [ 'addPgIndex', 'page_props', 'pp_propname_page', '(pp_propname, pp_page)' ],
253 [ 'addPgIndex', 'revision', 'rev_text_id_idx', '(rev_text_id)' ],
254 [ 'addPgIndex', 'recentchanges', 'rc_timestamp_bot', '(rc_timestamp) WHERE rc_bot = 0' ],
255 [ 'addPgIndex', 'templatelinks', 'templatelinks_from', '(tl_from)' ],
256 [ 'addPgIndex', 'watchlist', 'wl_user', '(wl_user)' ],
257 [ 'addPgIndex', 'watchlist', 'wl_user_notificationtimestamp',
258 '(wl_user, wl_notificationtimestamp)' ],
259 [ 'addPgIndex', 'logging', 'logging_user_type_time',
260 '(log_user, log_type, log_timestamp)' ],
261 [ 'addPgIndex', 'logging', 'logging_page_id_time', '(log_page,log_timestamp)' ],
262 [ 'addPgIndex', 'iwlinks', 'iwl_prefix_from_title', '(iwl_prefix, iwl_from, iwl_title)' ],
263 [ 'addPgIndex', 'iwlinks', 'iwl_prefix_title_from', '(iwl_prefix, iwl_title, iwl_from)' ],
264 [ 'addPgIndex', 'job', 'job_timestamp_idx', '(job_timestamp)' ],
265 [ 'addPgIndex', 'job', 'job_sha1', '(job_sha1)' ],
266 [ 'addPgIndex', 'job', 'job_cmd_token', '(job_cmd, job_token, job_random)' ],
267 [ 'addPgIndex', 'job', 'job_cmd_token_id', '(job_cmd, job_token, job_id)' ],
268 [ 'addPgIndex', 'filearchive', 'fa_sha1', '(fa_sha1)' ],
269 [ 'addPgIndex', 'logging', 'logging_user_text_type_time',
270 '(log_user_text, log_type, log_timestamp)' ],
271 [ 'addPgIndex', 'logging', 'logging_user_text_time', '(log_user_text, log_timestamp)' ],
272
273 [ 'checkIndex', 'pagelink_unique', [
274 [ 'pl_from', 'int4_ops', 'btree', 0 ],
275 [ 'pl_namespace', 'int2_ops', 'btree', 0 ],
276 [ 'pl_title', 'text_ops', 'btree', 0 ],
277 ],
278 'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ],
279 [ 'checkIndex', 'cl_sortkey', [
280 [ 'cl_to', 'text_ops', 'btree', 0 ],
281 [ 'cl_sortkey', 'text_ops', 'btree', 0 ],
282 [ 'cl_from', 'int4_ops', 'btree', 0 ],
283 ],
284 'CREATE INDEX cl_sortkey ON "categorylinks" ' .
285 'USING "btree" ("cl_to", "cl_sortkey", "cl_from")' ],
286 [ 'checkIndex', 'iwl_prefix_title_from', [
287 [ 'iwl_prefix', 'text_ops', 'btree', 0 ],
288 [ 'iwl_title', 'text_ops', 'btree', 0 ],
289 [ 'iwl_from', 'int4_ops', 'btree', 0 ],
290 ],
291 'CREATE INDEX iwl_prefix_title_from ON "iwlinks" ' .
292 'USING "btree" ("iwl_prefix", "iwl_title", "iwl_from")' ],
293 [ 'checkIndex', 'logging_times', [
294 [ 'log_timestamp', 'timestamptz_ops', 'btree', 0 ],
295 ],
296 'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ],
297 [ 'dropPgIndex', 'oldimage', 'oi_name' ],
298 [ 'checkIndex', 'oi_name_archive_name', [
299 [ 'oi_name', 'text_ops', 'btree', 0 ],
300 [ 'oi_archive_name', 'text_ops', 'btree', 0 ],
301 ],
302 'CREATE INDEX "oi_name_archive_name" ON "oldimage" ' .
303 'USING "btree" ("oi_name", "oi_archive_name")' ],
304 [ 'checkIndex', 'oi_name_timestamp', [
305 [ 'oi_name', 'text_ops', 'btree', 0 ],
306 [ 'oi_timestamp', 'timestamptz_ops', 'btree', 0 ],
307 ],
308 'CREATE INDEX "oi_name_timestamp" ON "oldimage" ' .
309 'USING "btree" ("oi_name", "oi_timestamp")' ],
310 [ 'checkIndex', 'page_main_title', [
311 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
312 ],
313 'CREATE INDEX "page_main_title" ON "page" ' .
314 'USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 0)' ],
315 [ 'checkIndex', 'page_mediawiki_title', [
316 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
317 ],
318 'CREATE INDEX "page_mediawiki_title" ON "page" ' .
319 'USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 8)' ],
320 [ 'checkIndex', 'page_project_title', [
321 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
322 ],
323 'CREATE INDEX "page_project_title" ON "page" ' .
324 'USING "btree" ("page_title" "text_pattern_ops") ' .
325 'WHERE ("page_namespace" = 4)' ],
326 [ 'checkIndex', 'page_talk_title', [
327 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
328 ],
329 'CREATE INDEX "page_talk_title" ON "page" ' .
330 'USING "btree" ("page_title" "text_pattern_ops") ' .
331 'WHERE ("page_namespace" = 1)' ],
332 [ 'checkIndex', 'page_user_title', [
333 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
334 ],
335 'CREATE INDEX "page_user_title" ON "page" ' .
336 'USING "btree" ("page_title" "text_pattern_ops") WHERE ' .
337 '("page_namespace" = 2)' ],
338 [ 'checkIndex', 'page_utalk_title', [
339 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
340 ],
341 'CREATE INDEX "page_utalk_title" ON "page" ' .
342 'USING "btree" ("page_title" "text_pattern_ops") ' .
343 'WHERE ("page_namespace" = 3)' ],
344 [ 'checkIndex', 'ts2_page_text', [
345 [ 'textvector', 'tsvector_ops', 'gist', 0 ],
346 ],
347 'CREATE INDEX "ts2_page_text" ON "pagecontent" USING "gist" ("textvector")' ],
348 [ 'checkIndex', 'ts2_page_title', [
349 [ 'titlevector', 'tsvector_ops', 'gist', 0 ],
350 ],
351 'CREATE INDEX "ts2_page_title" ON "page" USING "gist" ("titlevector")' ],
352
353 [ 'checkOiNameConstraint' ],
354 [ 'checkPageDeletedTrigger' ],
355 [ 'checkRevUserFkey' ],
356 [ 'dropPgIndex', 'ipblocks', 'ipb_address' ],
357 [ 'checkIndex', 'ipb_address_unique', [
358 [ 'ipb_address', 'text_ops', 'btree', 0 ],
359 [ 'ipb_user', 'int4_ops', 'btree', 0 ],
360 [ 'ipb_auto', 'int2_ops', 'btree', 0 ],
361 [ 'ipb_anon_only', 'int2_ops', 'btree', 0 ],
362 ],
363 'CREATE UNIQUE INDEX ipb_address_unique ' .
364 'ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only)' ],
365
366 [ 'checkIwlPrefix' ],
367
368 # All FK columns should be deferred
369 [ 'changeFkeyDeferrable', 'archive', 'ar_user', 'mwuser(user_id) ON DELETE SET NULL' ],
370 [ 'changeFkeyDeferrable', 'categorylinks', 'cl_from', 'page(page_id) ON DELETE CASCADE' ],
371 [ 'changeFkeyDeferrable', 'externallinks', 'el_from', 'page(page_id) ON DELETE CASCADE' ],
372 [ 'changeFkeyDeferrable', 'filearchive', 'fa_deleted_user',
373 'mwuser(user_id) ON DELETE SET NULL' ],
374 [ 'changeFkeyDeferrable', 'filearchive', 'fa_user', 'mwuser(user_id) ON DELETE SET NULL' ],
375 [ 'changeFkeyDeferrable', 'image', 'img_user', 'mwuser(user_id) ON DELETE SET NULL' ],
376 [ 'changeFkeyDeferrable', 'imagelinks', 'il_from', 'page(page_id) ON DELETE CASCADE' ],
377 [ 'changeFkeyDeferrable', 'ipblocks', 'ipb_by', 'mwuser(user_id) ON DELETE CASCADE' ],
378 [ 'changeFkeyDeferrable', 'ipblocks', 'ipb_user', 'mwuser(user_id) ON DELETE SET NULL' ],
379 [ 'changeFkeyDeferrable', 'ipblocks', 'ipb_parent_block_id',
380 'ipblocks(ipb_id) ON DELETE SET NULL' ],
381 [ 'changeFkeyDeferrable', 'langlinks', 'll_from', 'page(page_id) ON DELETE CASCADE' ],
382 [ 'changeFkeyDeferrable', 'logging', 'log_user', 'mwuser(user_id) ON DELETE SET NULL' ],
383 [ 'changeFkeyDeferrable', 'oldimage', 'oi_name',
384 'image(img_name) ON DELETE CASCADE ON UPDATE CASCADE' ],
385 [ 'changeFkeyDeferrable', 'oldimage', 'oi_user', 'mwuser(user_id) ON DELETE SET NULL' ],
386 [ 'changeFkeyDeferrable', 'pagelinks', 'pl_from', 'page(page_id) ON DELETE CASCADE' ],
387 [ 'changeFkeyDeferrable', 'page_props', 'pp_page', 'page (page_id) ON DELETE CASCADE' ],
388 [ 'changeFkeyDeferrable', 'page_restrictions', 'pr_page',
389 'page(page_id) ON DELETE CASCADE' ],
390 [ 'changeFkeyDeferrable', 'protected_titles', 'pt_user',
391 'mwuser(user_id) ON DELETE SET NULL' ],
392 [ 'changeFkeyDeferrable', 'recentchanges', 'rc_user',
393 'mwuser(user_id) ON DELETE SET NULL' ],
394 [ 'changeFkeyDeferrable', 'redirect', 'rd_from', 'page(page_id) ON DELETE CASCADE' ],
395 [ 'changeFkeyDeferrable', 'revision', 'rev_page', 'page (page_id) ON DELETE CASCADE' ],
396 [ 'changeFkeyDeferrable', 'revision', 'rev_user', 'mwuser(user_id) ON DELETE RESTRICT' ],
397 [ 'changeFkeyDeferrable', 'templatelinks', 'tl_from', 'page(page_id) ON DELETE CASCADE' ],
398 [ 'changeFkeyDeferrable', 'user_groups', 'ug_user', 'mwuser(user_id) ON DELETE CASCADE' ],
399 [ 'changeFkeyDeferrable', 'user_newtalk', 'user_id', 'mwuser(user_id) ON DELETE CASCADE' ],
400 [ 'changeFkeyDeferrable', 'user_properties', 'up_user',
401 'mwuser(user_id) ON DELETE CASCADE' ],
402 [ 'changeFkeyDeferrable', 'watchlist', 'wl_user', 'mwuser(user_id) ON DELETE CASCADE' ],
403
404 # r81574
405 [ 'addInterwikiType' ],
406 # end
407 [ 'tsearchFixes' ],
408
409 // 1.23
410 [ 'addPgField', 'recentchanges', 'rc_source', "TEXT NOT NULL DEFAULT ''" ],
411 [ 'addPgField', 'page', 'page_links_updated', "TIMESTAMPTZ NULL" ],
412 [ 'addPgField', 'mwuser', 'user_password_expires', 'TIMESTAMPTZ NULL' ],
413 [ 'changeFieldPurgeTable', 'l10n_cache', 'lc_value', 'bytea',
414 "replace(lc_value,'\','\\\\')::bytea" ],
415 // 1.23.9
416 [ 'rebuildTextSearch' ],
417
418 // 1.24
419 [ 'addPgField', 'page_props', 'pp_sortkey', 'float NULL' ],
420 [ 'addPgIndex', 'page_props', 'pp_propname_sortkey_page',
421 '( pp_propname, pp_sortkey, pp_page ) WHERE ( pp_sortkey IS NOT NULL )' ],
422 [ 'addPgField', 'page', 'page_lang', 'TEXT default NULL' ],
423 [ 'addPgField', 'pagelinks', 'pl_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ],
424 [ 'addPgField', 'templatelinks', 'tl_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ],
425 [ 'addPgField', 'imagelinks', 'il_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ],
426
427 // 1.25
428 [ 'dropTable', 'hitcounter' ],
429 [ 'dropField', 'site_stats', 'ss_total_views', 'patch-drop-ss_total_views.sql' ],
430 [ 'dropField', 'page', 'page_counter', 'patch-drop-page_counter.sql' ],
431 [ 'dropFkey', 'recentchanges', 'rc_cur_id' ],
432
433 // 1.27
434 [ 'dropTable', 'msg_resource_links' ],
435 [ 'dropTable', 'msg_resource' ],
436 [
437 'addPgField', 'watchlist', 'wl_id',
438 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('watchlist_wl_id_seq')"
439 ],
440
441 // 1.28
442 [ 'addPgIndex', 'recentchanges', 'rc_name_type_patrolled_timestamp',
443 '( rc_namespace, rc_type, rc_patrolled, rc_timestamp )' ],
444 [ 'addPgField', 'change_tag', 'ct_id',
445 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('change_tag_ct_id_seq')" ],
446 [ 'addPgField', 'tag_summary', 'ts_id',
447 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('tag_summary_ts_id_seq')" ],
448
449 // 1.29
450 [ 'addPgField', 'externallinks', 'el_index_60', "BYTEA NOT NULL DEFAULT ''" ],
451 [ 'addPgIndex', 'externallinks', 'el_index_60', '( el_index_60, el_id )' ],
452 [ 'addPgIndex', 'externallinks', 'el_from_index_60', '( el_from, el_index_60, el_id )' ],
453 [ 'addPgField', 'user_groups', 'ug_expiry', "TIMESTAMPTZ NULL" ],
454 [ 'addPgIndex', 'user_groups', 'user_groups_expiry', '( ug_expiry )' ],
455
456 // 1.30
457 [ 'addPgEnumValue', 'media_type', '3D' ],
458 [ 'setDefault', 'revision', 'rev_comment', '' ],
459 [ 'changeNullableField', 'revision', 'rev_comment', 'NOT NULL', true ],
460 [ 'setDefault', 'archive', 'ar_comment', '' ],
461 [ 'changeNullableField', 'archive', 'ar_comment', 'NOT NULL', true ],
462 [ 'addPgField', 'archive', 'ar_comment_id', 'INTEGER NOT NULL DEFAULT 0' ],
463 [ 'setDefault', 'ipblocks', 'ipb_reason', '' ],
464 [ 'addPgField', 'ipblocks', 'ipb_reason_id', 'INTEGER NOT NULL DEFAULT 0' ],
465 [ 'setDefault', 'image', 'img_description', '' ],
466 [ 'setDefault', 'oldimage', 'oi_description', '' ],
467 [ 'changeNullableField', 'oldimage', 'oi_description', 'NOT NULL', true ],
468 [ 'addPgField', 'oldimage', 'oi_description_id', 'INTEGER NOT NULL DEFAULT 0' ],
469 [ 'setDefault', 'filearchive', 'fa_deleted_reason', '' ],
470 [ 'changeNullableField', 'filearchive', 'fa_deleted_reason', 'NOT NULL', true ],
471 [ 'addPgField', 'filearchive', 'fa_deleted_reason_id', 'INTEGER NOT NULL DEFAULT 0' ],
472 [ 'setDefault', 'filearchive', 'fa_description', '' ],
473 [ 'addPgField', 'filearchive', 'fa_description_id', 'INTEGER NOT NULL DEFAULT 0' ],
474 [ 'setDefault', 'recentchanges', 'rc_comment', '' ],
475 [ 'changeNullableField', 'recentchanges', 'rc_comment', 'NOT NULL', true ],
476 [ 'addPgField', 'recentchanges', 'rc_comment_id', 'INTEGER NOT NULL DEFAULT 0' ],
477 [ 'setDefault', 'logging', 'log_comment', '' ],
478 [ 'changeNullableField', 'logging', 'log_comment', 'NOT NULL', true ],
479 [ 'addPgField', 'logging', 'log_comment_id', 'INTEGER NOT NULL DEFAULT 0' ],
480 [ 'setDefault', 'protected_titles', 'pt_reason', '' ],
481 [ 'changeNullableField', 'protected_titles', 'pt_reason', 'NOT NULL', true ],
482 [ 'addPgField', 'protected_titles', 'pt_reason_id', 'INTEGER NOT NULL DEFAULT 0' ],
483 [ 'addTable', 'comment', 'patch-comment-table.sql' ],
484
485 // This field was added in 1.31, but is put here so it can be used by 'migrateComments'
486 [ 'addPgField', 'image', 'img_description_id', 'INTEGER NOT NULL DEFAULT 0' ],
487
488 [ 'migrateComments' ],
489 [ 'addIndex', 'site_stats', 'site_stats_pkey', 'patch-site_stats-pk.sql' ],
490 [ 'addTable', 'ip_changes', 'patch-ip_changes.sql' ],
491
492 // 1.31
493 [ 'addTable', 'slots', 'patch-slots-table.sql' ],
494 [ 'dropPgIndex', 'slots', 'slot_role_inherited' ],
495 [ 'dropPgField', 'slots', 'slot_inherited' ],
496 [ 'addPgField', 'slots', 'slot_origin', 'INTEGER NOT NULL' ],
497 [
498 'addPgIndex',
499 'slots',
500 'slot_revision_origin_role',
501 '( slot_revision_id, slot_origin, slot_role_id )',
502 ],
503 [ 'addTable', 'content', 'patch-content-table.sql' ],
504 [ 'addTable', 'content_models', 'patch-content_models-table.sql' ],
505 [ 'addTable', 'slot_roles', 'patch-slot_roles-table.sql' ],
506 [ 'migrateArchiveText' ],
507 [ 'addTable', 'actor', 'patch-actor-table.sql' ],
508 [ 'setDefault', 'revision', 'rev_user', 0 ],
509 [ 'setDefault', 'revision', 'rev_user_text', '' ],
510 [ 'setDefault', 'archive', 'ar_user', 0 ],
511 [ 'changeNullableField', 'archive', 'ar_user', 'NOT NULL', true ],
512 [ 'setDefault', 'archive', 'ar_user_text', '' ],
513 [ 'addPgField', 'archive', 'ar_actor', 'INTEGER NOT NULL DEFAULT 0' ],
514 [ 'addPgIndex', 'archive', 'archive_actor', '( ar_actor )' ],
515 [ 'setDefault', 'ipblocks', 'ipb_by', 0 ],
516 [ 'addPgField', 'ipblocks', 'ipb_by_actor', 'INTEGER NOT NULL DEFAULT 0' ],
517 [ 'setDefault', 'image', 'img_user', 0 ],
518 [ 'changeNullableField', 'image', 'img_user', 'NOT NULL', true ],
519 [ 'setDefault', 'image', 'img_user_text', '' ],
520 [ 'addPgField', 'image', 'img_actor', 'INTEGER NOT NULL DEFAULT 0' ],
521 [ 'setDefault', 'oldimage', 'oi_user', 0 ],
522 [ 'changeNullableField', 'oldimage', 'oi_user', 'NOT NULL', true ],
523 [ 'setDefault', 'oldimage', 'oi_user_text', '' ],
524 [ 'addPgField', 'oldimage', 'oi_actor', 'INTEGER NOT NULL DEFAULT 0' ],
525 [ 'setDefault', 'filearchive', 'fa_user', 0 ],
526 [ 'changeNullableField', 'filearchive', 'fa_user', 'NOT NULL', true ],
527 [ 'setDefault', 'filearchive', 'fa_user_text', '' ],
528 [ 'addPgField', 'filearchive', 'fa_actor', 'INTEGER NOT NULL DEFAULT 0' ],
529 [ 'setDefault', 'recentchanges', 'rc_user', 0 ],
530 [ 'changeNullableField', 'recentchanges', 'rc_user', 'NOT NULL', true ],
531 [ 'setDefault', 'recentchanges', 'rc_user_text', '' ],
532 [ 'addPgField', 'recentchanges', 'rc_actor', 'INTEGER NOT NULL DEFAULT 0' ],
533 [ 'setDefault', 'logging', 'log_user', 0 ],
534 [ 'changeNullableField', 'logging', 'log_user', 'NOT NULL', true ],
535 [ 'addPgField', 'logging', 'log_actor', 'INTEGER NOT NULL DEFAULT 0' ],
536 [ 'addPgIndex', 'logging', 'logging_actor_time_backwards', '( log_timestamp, log_actor )' ],
537 [ 'addPgIndex', 'logging', 'logging_actor_type_time', '( log_actor, log_type, log_timestamp )' ],
538 [ 'addPgIndex', 'logging', 'logging_actor_time', '( log_actor, log_timestamp )' ],
539 [ 'migrateActors' ],
540 [ 'modifyTable', 'site_stats', 'patch-site_stats-modify.sql' ],
541 [ 'populateArchiveRevId' ],
542 [ 'dropPgIndex', 'recentchanges', 'rc_namespace_title' ],
543 [
544 'addPgIndex',
545 'recentchanges',
546 'rc_namespace_title_timestamp', '( rc_namespace, rc_title, rc_timestamp )'
547 ],
548 [ 'setSequenceOwner', 'mwuser', 'user_id', 'user_user_id_seq' ],
549 [ 'setSequenceOwner', 'actor', 'actor_id', 'actor_actor_id_seq' ],
550 [ 'setSequenceOwner', 'page', 'page_id', 'page_page_id_seq' ],
551 [ 'setSequenceOwner', 'revision', 'rev_id', 'revision_rev_id_seq' ],
552 [ 'setSequenceOwner', 'ip_changes', 'ipc_rev_id', 'ip_changes_ipc_rev_id_seq' ],
553 [ 'setSequenceOwner', 'pagecontent', 'old_id', 'text_old_id_seq' ],
554 [ 'setSequenceOwner', 'comment', 'comment_id', 'comment_comment_id_seq' ],
555 [ 'setSequenceOwner', 'page_restrictions', 'pr_id', 'page_restrictions_pr_id_seq' ],
556 [ 'setSequenceOwner', 'archive', 'ar_id', 'archive_ar_id_seq' ],
557 [ 'setSequenceOwner', 'content', 'content_id', 'content_content_id_seq' ],
558 [ 'setSequenceOwner', 'slot_roles', 'role_id', 'slot_roles_role_id_seq' ],
559 [ 'setSequenceOwner', 'content_models', 'model_id', 'content_models_model_id_seq' ],
560 [ 'setSequenceOwner', 'externallinks', 'el_id', 'externallinks_el_id_seq' ],
561 [ 'setSequenceOwner', 'ipblocks', 'ipb_id', 'ipblocks_ipb_id_seq' ],
562 [ 'setSequenceOwner', 'filearchive', 'fa_id', 'filearchive_fa_id_seq' ],
563 [ 'setSequenceOwner', 'uploadstash', 'us_id', 'uploadstash_us_id_seq' ],
564 [ 'setSequenceOwner', 'recentchanges', 'rc_id', 'recentchanges_rc_id_seq' ],
565 [ 'setSequenceOwner', 'watchlist', 'wl_id', 'watchlist_wl_id_seq' ],
566 [ 'setSequenceOwner', 'logging', 'log_id', 'logging_log_id_seq' ],
567 [ 'setSequenceOwner', 'job', 'job_id', 'job_job_id_seq' ],
568 [ 'setSequenceOwner', 'category', 'cat_id', 'category_cat_id_seq' ],
569 [ 'setSequenceOwner', 'change_tag', 'ct_id', 'change_tag_ct_id_seq' ],
570 [ 'setSequenceOwner', 'tag_summary', 'ts_id', 'tag_summary_ts_id_seq' ],
571 [ 'setSequenceOwner', 'sites', 'site_id', 'sites_site_id_seq' ],
572
573 // 1.32
574 [ 'addTable', 'change_tag_def', 'patch-change_tag_def.sql' ],
575 [ 'populateExternallinksIndex60' ],
576 [ 'dropDefault', 'externallinks', 'el_index_60' ],
577 [ 'runMaintenance', DeduplicateArchiveRevId::class, 'maintenance/deduplicateArchiveRevId.php' ],
578 [ 'addPgField', 'change_tag', 'ct_tag_id', 'INTEGER NULL' ],
579 [
580 'addPgIndex',
581 'change_tag',
582 'change_tag_tag_id_id',
583 '( ct_tag_id, ct_rc_id, ct_rev_id, ct_log_id )'
584 ],
585 [ 'addPgIndex', 'archive', 'ar_revid_uniq', '(ar_rev_id)', 'unique' ],
586 [ 'dropPgIndex', 'archive', 'ar_revid' ], // Probably doesn't exist, but do it anyway.
587 [ 'populateContentTables' ],
588 [ 'addPgIndex', 'logging', 'log_type_action', '( log_type, log_action, log_timestamp )' ],
589 [ 'dropPgIndex', 'page_props', 'page_props_propname' ],
590 [ 'addIndex', 'interwiki', 'interwiki_pkey', 'patch-interwiki-pk.sql' ],
591 [ 'addIndex', 'protected_titles', 'protected_titles_pkey', 'patch-protected_titles-pk.sql' ],
592 [ 'addIndex', 'site_identifiers', 'site_identifiers_pkey', 'patch-site_identifiers-pk.sql' ],
593 ];
594 }
595
596 protected function getOldGlobalUpdates() {
597 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
598
599 $updates = [];
600
601 # Add missing extension tables
602 foreach ( $wgExtNewTables as $tableRecord ) {
603 $updates[] = [
604 'addTable', $tableRecord[0], $tableRecord[1], true
605 ];
606 }
607
608 # Add missing extension fields
609 foreach ( $wgExtPGNewFields as $fieldRecord ) {
610 $updates[] = [
611 'addPgField', $fieldRecord[0], $fieldRecord[1],
612 $fieldRecord[2]
613 ];
614 }
615
616 # Change altered columns
617 foreach ( $wgExtPGAlteredFields as $fieldRecord ) {
618 $updates[] = [
619 'changeField', $fieldRecord[0], $fieldRecord[1],
620 $fieldRecord[2]
621 ];
622 }
623
624 # Add missing extension indexes
625 foreach ( $wgExtNewIndexes as $fieldRecord ) {
626 $updates[] = [
627 'addPgExtIndex', $fieldRecord[0], $fieldRecord[1],
628 $fieldRecord[2]
629 ];
630 }
631
632 return $updates;
633 }
634
635 protected function describeTable( $table ) {
636 $q = <<<END
637 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
638 WHERE pg_class.relnamespace = pg_namespace.oid
639 AND attrelid=pg_class.oid AND attnum > 0
640 AND relname=%s AND nspname=%s
641 END;
642 $res = $this->db->query( sprintf( $q,
643 $this->db->addQuotes( $table ),
644 $this->db->addQuotes( $this->db->getCoreSchema() ) ) );
645 if ( !$res ) {
646 return null;
647 }
648
649 $cols = [];
650 foreach ( $res as $r ) {
651 $cols[] = [
652 "name" => $r[0],
653 "ord" => $r[1],
654 ];
655 }
656
657 return $cols;
658 }
659
660 function describeIndex( $idx ) {
661 // first fetch the key (which is a list of columns ords) and
662 // the table the index applies to (an oid)
663 $q = <<<END
664 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
665 WHERE nspname=%s
666 AND pg_class.relnamespace = pg_namespace.oid
667 AND relname=%s
668 AND indexrelid=pg_class.oid
669 END;
670 $res = $this->db->query(
671 sprintf(
672 $q,
673 $this->db->addQuotes( $this->db->getCoreSchema() ),
674 $this->db->addQuotes( $idx )
675 )
676 );
677 if ( !$res ) {
678 return null;
679 }
680 $r = $this->db->fetchRow( $res );
681 if ( !$r ) {
682 return null;
683 }
684
685 $indkey = $r[0];
686 $relid = intval( $r[1] );
687 $indkeys = explode( ' ', $indkey );
688
689 $colnames = [];
690 foreach ( $indkeys as $rid ) {
691 $query = <<<END
692 SELECT attname FROM pg_class, pg_attribute
693 WHERE attrelid=$relid
694 AND attnum=%d
695 AND attrelid=pg_class.oid
696 END;
697 $r2 = $this->db->query( sprintf( $query, $rid ) );
698 if ( !$r2 ) {
699 return null;
700 }
701 $row2 = $this->db->fetchRow( $r2 );
702 if ( !$row2 ) {
703 return null;
704 }
705 $colnames[] = $row2[0];
706 }
707
708 return $colnames;
709 }
710
711 function fkeyDeltype( $fkey ) {
712 $q = <<<END
713 SELECT confdeltype FROM pg_constraint, pg_namespace
714 WHERE connamespace=pg_namespace.oid
715 AND nspname=%s
716 AND conname=%s;
717 END;
718 $r = $this->db->query(
719 sprintf(
720 $q,
721 $this->db->addQuotes( $this->db->getCoreSchema() ),
722 $this->db->addQuotes( $fkey )
723 )
724 );
725 $row = $this->db->fetchRow( $r );
726 if ( !$row ) {
727 return null;
728 }
729
730 return $row[0];
731 }
732
733 function ruleDef( $table, $rule ) {
734 $q = <<<END
735 SELECT definition FROM pg_rules
736 WHERE schemaname = %s
737 AND tablename = %s
738 AND rulename = %s
739 END;
740 $r = $this->db->query(
741 sprintf(
742 $q,
743 $this->db->addQuotes( $this->db->getCoreSchema() ),
744 $this->db->addQuotes( $table ),
745 $this->db->addQuotes( $rule )
746 )
747 );
748 $row = $this->db->fetchRow( $r );
749 if ( !$row ) {
750 return null;
751 }
752 $d = $row[0];
753
754 return $d;
755 }
756
757 protected function addSequence( $table, $pkey, $ns ) {
758 if ( !$this->db->sequenceExists( $ns ) ) {
759 $this->output( "Creating sequence $ns\n" );
760 if ( $pkey !== false ) {
761 $this->db->query( "CREATE SEQUENCE $ns OWNED BY $table.$pkey" );
762 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
763 } else {
764 $this->db->query( "CREATE SEQUENCE $ns" );
765 }
766 }
767 }
768
769 protected function dropSequence( $table, $ns ) {
770 if ( $this->db->sequenceExists( $ns ) ) {
771 $this->output( "Dropping sequence $ns\n" );
772 $this->db->query( "DROP SEQUENCE $ns CASCADE" );
773 }
774 }
775
776 protected function renameSequence( $old, $new ) {
777 if ( $this->db->sequenceExists( $new ) ) {
778 $this->output( "...sequence $new already exists.\n" );
779
780 return;
781 }
782 if ( $this->db->sequenceExists( $old ) ) {
783 $this->output( "Renaming sequence $old to $new\n" );
784 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new" );
785 }
786 }
787
788 protected function setSequenceOwner( $table, $pkey, $seq ) {
789 if ( $this->db->sequenceExists( $seq ) ) {
790 $this->output( "Setting sequence $seq owner to $table.$pkey\n" );
791 $this->db->query( "ALTER SEQUENCE $seq OWNED BY $table.$pkey" );
792 }
793 }
794
795 protected function renameTable( $old, $new, $patch = false ) {
796 if ( $this->db->tableExists( $old ) ) {
797 $this->output( "Renaming table $old to $new\n" );
798 $old = $this->db->realTableName( $old, "quoted" );
799 $new = $this->db->realTableName( $new, "quoted" );
800 $this->db->query( "ALTER TABLE $old RENAME TO $new" );
801 if ( $patch !== false ) {
802 $this->applyPatch( $patch );
803 }
804 }
805 }
806
807 protected function renameIndex(
808 $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
809 ) {
810 // First requirement: the table must exist
811 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
812 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
813
814 return;
815 }
816
817 // Second requirement: the new index must be missing
818 if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
819 $this->output( "...index $new already set on $table table.\n" );
820 if ( !$skipBothIndexExistWarning
821 && $this->db->indexExists( $table, $old, __METHOD__ )
822 ) {
823 $this->output( "...WARNING: $old still exists, despite it has been " .
824 "renamed into $new (which also exists).\n" .
825 " $old should be manually removed if not needed anymore.\n" );
826 }
827
828 return;
829 }
830
831 // Third requirement: the old index must exist
832 if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
833 $this->output( "...skipping: index $old doesn't exist.\n" );
834
835 return;
836 }
837
838 $this->db->query( "ALTER INDEX $old RENAME TO $new" );
839 }
840
841 protected function dropPgField( $table, $field ) {
842 $fi = $this->db->fieldInfo( $table, $field );
843 if ( is_null( $fi ) ) {
844 $this->output( "...$table table does not contain $field field.\n" );
845
846 return;
847 } else {
848 $this->output( "Dropping column '$table.$field'\n" );
849 $this->db->query( "ALTER TABLE $table DROP COLUMN $field" );
850 }
851 }
852
853 protected function addPgField( $table, $field, $type ) {
854 $fi = $this->db->fieldInfo( $table, $field );
855 if ( !is_null( $fi ) ) {
856 $this->output( "...column '$table.$field' already exists\n" );
857
858 return;
859 } else {
860 $this->output( "Adding column '$table.$field'\n" );
861 $this->db->query( "ALTER TABLE $table ADD $field $type" );
862 }
863 }
864
865 protected function changeField( $table, $field, $newtype, $default ) {
866 $fi = $this->db->fieldInfo( $table, $field );
867 if ( is_null( $fi ) ) {
868 $this->output( "...ERROR: expected column $table.$field to exist\n" );
869 exit( 1 );
870 }
871
872 if ( $fi->type() === $newtype ) {
873 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
874 } else {
875 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
876 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
877 if ( strlen( $default ) ) {
878 $res = [];
879 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
880 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
881 $this->db->query( $sqldef );
882 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
883 }
884 $sql .= " USING $default";
885 }
886 $this->db->query( $sql );
887 }
888 }
889
890 protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
891 # # For a cache table, empty it if the field needs to be changed, because the old contents
892 # # may be corrupted. If the column is already the desired type, refrain from purging.
893 $fi = $this->db->fieldInfo( $table, $field );
894 if ( is_null( $fi ) ) {
895 $this->output( "...ERROR: expected column $table.$field to exist\n" );
896 exit( 1 );
897 }
898
899 if ( $fi->type() === $newtype ) {
900 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
901 } else {
902 $this->output( "Purging data from cache table '$table'\n" );
903 $this->db->query( "DELETE from $table" );
904 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
905 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
906 if ( strlen( $default ) ) {
907 $res = [];
908 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
909 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
910 $this->db->query( $sqldef );
911 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
912 }
913 $sql .= " USING $default";
914 }
915 $this->db->query( $sql );
916 }
917 }
918
919 protected function setDefault( $table, $field, $default ) {
920 $info = $this->db->fieldInfo( $table, $field );
921 if ( $info->defaultValue() !== $default ) {
922 $this->output( "Changing '$table.$field' default value\n" );
923 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT "
924 . $this->db->addQuotes( $default ) );
925 }
926 }
927
928 /**
929 * Drop a default value from a field
930 * @since 1.32
931 * @param string $table
932 * @param string $field
933 */
934 protected function dropDefault( $table, $field ) {
935 $info = $this->db->fieldInfo( $table, $field );
936 if ( $info->defaultValue() !== false ) {
937 $this->output( "Removing '$table.$field' default value\n" );
938 $this->db->query( "ALTER TABLE $table ALTER $field DROP DEFAULT" );
939 }
940 }
941
942 protected function changeNullableField( $table, $field, $null, $update = false ) {
943 $fi = $this->db->fieldInfo( $table, $field );
944 if ( is_null( $fi ) ) {
945 $this->output( "...ERROR: expected column $table.$field to exist\n" );
946 exit( 1 );
947 }
948 if ( $fi->isNullable() ) {
949 # # It's NULL - does it need to be NOT NULL?
950 if ( 'NOT NULL' === $null ) {
951 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
952 if ( $update ) {
953 $this->db->query( "UPDATE $table SET $field = DEFAULT WHERE $field IS NULL" );
954 }
955 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" );
956 } else {
957 $this->output( "...column '$table.$field' is already set as NULL\n" );
958 }
959 } else {
960 # # It's NOT NULL - does it need to be NULL?
961 if ( 'NULL' === $null ) {
962 $this->output( "Changing '$table.$field' to allow NULLs\n" );
963 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" );
964 } else {
965 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
966 }
967 }
968 }
969
970 public function addPgIndex( $table, $index, $type, $unique = false ) {
971 if ( $this->db->indexExists( $table, $index ) ) {
972 $this->output( "...index '$index' on table '$table' already exists\n" );
973 } else {
974 $this->output( "Creating index '$index' on table '$table' $type\n" );
975 $unique = $unique ? 'UNIQUE' : '';
976 $this->db->query( "CREATE $unique INDEX $index ON $table $type" );
977 }
978 }
979
980 public function addPgExtIndex( $table, $index, $type ) {
981 if ( $this->db->indexExists( $table, $index ) ) {
982 $this->output( "...index '$index' on table '$table' already exists\n" );
983 } else {
984 if ( preg_match( '/^\(/', $type ) ) {
985 $this->output( "Creating index '$index' on table '$table'\n" );
986 $this->db->query( "CREATE INDEX $index ON $table $type" );
987 } else {
988 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
989 }
990 }
991 }
992
993 /**
994 * Add a value to an existing PostgreSQL enum type
995 * @since 1.31
996 * @param string $type Type name. Must be in the core schema.
997 * @param string $value Value to add.
998 */
999 public function addPgEnumValue( $type, $value ) {
1000 $row = $this->db->selectRow(
1001 [
1002 't' => 'pg_catalog.pg_type',
1003 'n' => 'pg_catalog.pg_namespace',
1004 'e' => 'pg_catalog.pg_enum',
1005 ],
1006 [ 't.typname', 't.typtype', 'e.enumlabel' ],
1007 [
1008 't.typname' => $type,
1009 'n.nspname' => $this->db->getCoreSchema(),
1010 ],
1011 __METHOD__,
1012 [],
1013 [
1014 'n' => [ 'JOIN', 't.typnamespace = n.oid' ],
1015 'e' => [ 'LEFT JOIN', [ 'e.enumtypid = t.oid', 'e.enumlabel' => $value ] ],
1016 ]
1017 );
1018
1019 if ( !$row ) {
1020 $this->output( "...Type $type does not exist, skipping modify enum.\n" );
1021 } elseif ( $row->typtype !== 'e' ) {
1022 $this->output( "...Type $type does not seem to be an enum, skipping modify enum.\n" );
1023 } elseif ( $row->enumlabel === $value ) {
1024 $this->output( "...Enum type $type already contains value '$value'.\n" );
1025 } else {
1026 $this->output( "...Adding value '$value' to enum type $type.\n" );
1027 $etype = $this->db->addIdentifierQuotes( $type );
1028 $evalue = $this->db->addQuotes( $value );
1029 $this->db->query( "ALTER TYPE $etype ADD VALUE $evalue" );
1030 }
1031 }
1032
1033 protected function dropFkey( $table, $field ) {
1034 $fi = $this->db->fieldInfo( $table, $field );
1035 if ( is_null( $fi ) ) {
1036 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
1037 "Please report this.\n" );
1038 return;
1039 }
1040 $conname = $fi->conname();
1041 if ( $fi->conname() ) {
1042 $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
1043 $conclause = "CONSTRAINT \"$conname\"";
1044 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
1045 $this->db->query( $command );
1046 } else {
1047 $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
1048 };
1049 }
1050
1051 protected function changeFkeyDeferrable( $table, $field, $clause ) {
1052 $fi = $this->db->fieldInfo( $table, $field );
1053 if ( is_null( $fi ) ) {
1054 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
1055 "Please report this.\n" );
1056
1057 return;
1058 }
1059 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
1060 return;
1061 }
1062 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
1063 $conname = $fi->conname();
1064 if ( $fi->conname() ) {
1065 $conclause = "CONSTRAINT \"$conname\"";
1066 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
1067 $this->db->query( $command );
1068 } else {
1069 $this->output( "Column '$table.$field' does not have a foreign key " .
1070 "constraint, will be added\n" );
1071 $conclause = "";
1072 }
1073 $command =
1074 "ALTER TABLE $table ADD $conclause " .
1075 "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
1076 $this->db->query( $command );
1077 }
1078
1079 protected function convertArchive2() {
1080 if ( $this->db->tableExists( "archive2" ) ) {
1081 if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) {
1082 $this->output( "Dropping rule 'archive_insert'\n" );
1083 $this->db->query( 'DROP RULE archive_insert ON archive' );
1084 }
1085 if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) {
1086 $this->output( "Dropping rule 'archive_delete'\n" );
1087 $this->db->query( 'DROP RULE archive_delete ON archive' );
1088 }
1089 $this->applyPatch(
1090 'patch-remove-archive2.sql',
1091 false,
1092 "Converting 'archive2' back to normal archive table"
1093 );
1094 } else {
1095 $this->output( "...obsolete table 'archive2' does not exist\n" );
1096 }
1097 }
1098
1099 protected function checkOiDeleted() {
1100 if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
1101 $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" );
1102 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
1103 $this->db->query(
1104 "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
1105 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
1106 } else {
1107 $this->output( "...column 'oldimage.oi_deleted' is already of type 'smallint'\n" );
1108 }
1109 }
1110
1111 protected function checkOiNameConstraint() {
1112 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
1113 $this->output( "...table 'oldimage' has correct cascading delete/update " .
1114 "foreign key to image\n" );
1115 } else {
1116 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) {
1117 $this->db->query(
1118 "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
1119 }
1120 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
1121 $this->db->query(
1122 "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
1123 }
1124 $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" );
1125 $this->db->query(
1126 "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
1127 "FOREIGN KEY (oi_name) REFERENCES image(img_name) " .
1128 "ON DELETE CASCADE ON UPDATE CASCADE" );
1129 }
1130 }
1131
1132 protected function checkPageDeletedTrigger() {
1133 if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) {
1134 $this->applyPatch(
1135 'patch-page_deleted.sql',
1136 false,
1137 "Adding function and trigger 'page_deleted' to table 'page'"
1138 );
1139 } else {
1140 $this->output( "...table 'page' has 'page_deleted' trigger\n" );
1141 }
1142 }
1143
1144 protected function dropPgIndex( $table, $index ) {
1145 if ( $this->db->indexExists( $table, $index ) ) {
1146 $this->output( "Dropping obsolete index '$index'\n" );
1147 $this->db->query( "DROP INDEX \"" . $index . "\"" );
1148 }
1149 }
1150
1151 protected function checkIndex( $index, $should_be, $good_def ) {
1152 $pu = $this->db->indexAttributes( $index );
1153 if ( !empty( $pu ) && $pu != $should_be ) {
1154 $this->output( "Dropping obsolete version of index '$index'\n" );
1155 $this->db->query( "DROP INDEX \"" . $index . "\"" );
1156 $pu = [];
1157 } else {
1158 $this->output( "...no need to drop index '$index'\n" );
1159 }
1160
1161 if ( empty( $pu ) ) {
1162 $this->output( "Creating index '$index'\n" );
1163 $this->db->query( $good_def );
1164 } else {
1165 $this->output( "...index '$index' exists\n" );
1166 }
1167 }
1168
1169 protected function checkRevUserFkey() {
1170 if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) {
1171 $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" );
1172 } else {
1173 $this->applyPatch(
1174 'patch-revision_rev_user_fkey.sql',
1175 false,
1176 "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT"
1177 );
1178 }
1179 }
1180
1181 protected function checkIwlPrefix() {
1182 if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) {
1183 $this->applyPatch(
1184 'patch-rename-iwl_prefix.sql',
1185 false,
1186 "Replacing index 'iwl_prefix' with 'iwl_prefix_title_from'"
1187 );
1188 }
1189 }
1190
1191 protected function addInterwikiType() {
1192 $this->applyPatch( 'patch-add_interwiki.sql', false, "Refreshing add_interwiki()" );
1193 }
1194
1195 protected function tsearchFixes() {
1196 # Tweak the page_title tsearch2 trigger to filter out slashes
1197 # This is create or replace, so harmless to call if not needed
1198 $this->applyPatch( 'patch-ts2pagetitle.sql', false, "Refreshing ts2_page_title()" );
1199
1200 # If the server is 8.3 or higher, rewrite the tsearch2 triggers
1201 # in case they have the old 'default' versions
1202 # Gather version numbers in case we need them
1203 if ( $this->db->getServerVersion() >= 8.3 ) {
1204 $this->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" );
1205 }
1206 }
1207
1208 protected function rebuildTextSearch() {
1209 if ( $this->updateRowExists( 'patch-textsearch_bug66650.sql' ) ) {
1210 $this->output( "...T68650 already fixed or not applicable.\n" );
1211 return;
1212 };
1213 $this->applyPatch( 'patch-textsearch_bug66650.sql', false,
1214 'Rebuilding text search for T68650' );
1215 }
1216 }