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