Merge "MemoizedCallable: Add test for memoized closure"
[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 /**
25 * Class for handling updates to Postgres databases.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class PostgresUpdater extends DatabaseUpdater {
31
32 /**
33 * @var DatabasePostgres
34 */
35 protected $db;
36
37 /**
38 * @todo FIXME: Postgres should use sequential updates like Mysql, Sqlite
39 * and everybody else. It never got refactored like it should've.
40 * @return array
41 */
42 protected function getCoreUpdateList() {
43 return [
44 # rename tables 1.7.3
45 # r15791 Change reserved word table names "user" and "text"
46 [ 'renameTable', 'user', 'mwuser' ],
47 [ 'renameTable', 'text', 'pagecontent' ],
48 [ 'renameIndex', 'mwuser', 'user_pkey', 'mwuser_pkey' ],
49 [ 'renameIndex', 'mwuser', 'user_user_name_key', 'mwuser_user_name_key' ],
50 [ 'renameIndex', 'pagecontent', 'text_pkey', 'pagecontent_pkey' ],
51
52 # renamed sequences
53 [ 'renameSequence', 'ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq' ],
54 [ 'renameSequence', 'rev_rev_id_val', 'revision_rev_id_seq' ],
55 [ 'renameSequence', 'text_old_id_val', 'text_old_id_seq' ],
56 [ 'renameSequence', 'rc_rc_id_seq', 'recentchanges_rc_id_seq' ],
57 [ 'renameSequence', 'log_log_id_seq', 'logging_log_id_seq' ],
58 [ 'renameSequence', 'pr_id_val', 'page_restrictions_pr_id_seq' ],
59 [ 'renameSequence', 'us_id_seq', 'uploadstash_us_id_seq' ],
60
61 # since r58263
62 [ 'renameSequence', 'category_id_seq', 'category_cat_id_seq' ],
63
64 # new sequences if not renamed above
65 [ 'addSequence', 'logging', false, 'logging_log_id_seq' ],
66 [ 'addSequence', 'page_restrictions', false, 'page_restrictions_pr_id_seq' ],
67 [ 'addSequence', 'filearchive', 'fa_id', 'filearchive_fa_id_seq' ],
68 [ 'addSequence', 'archive', false, 'archive_ar_id_seq' ],
69 [ 'addSequence', 'externallinks', false, 'externallinks_el_id_seq' ],
70
71 # new tables
72 [ 'addTable', 'category', 'patch-category.sql' ],
73 [ 'addTable', 'page', 'patch-page.sql' ],
74 [ 'addTable', 'querycachetwo', 'patch-querycachetwo.sql' ],
75 [ 'addTable', 'page_props', 'patch-page_props.sql' ],
76 [ 'addTable', 'page_restrictions', 'patch-page_restrictions.sql' ],
77 [ 'addTable', 'profiling', 'patch-profiling.sql' ],
78 [ 'addTable', 'protected_titles', 'patch-protected_titles.sql' ],
79 [ 'addTable', 'redirect', 'patch-redirect.sql' ],
80 [ 'addTable', 'updatelog', 'patch-updatelog.sql' ],
81 [ 'addTable', 'change_tag', 'patch-change_tag.sql' ],
82 [ 'addTable', 'tag_summary', 'patch-tag_summary.sql' ],
83 [ 'addTable', 'valid_tag', 'patch-valid_tag.sql' ],
84 [ 'addTable', 'user_properties', 'patch-user_properties.sql' ],
85 [ 'addTable', 'log_search', 'patch-log_search.sql' ],
86 [ 'addTable', 'l10n_cache', 'patch-l10n_cache.sql' ],
87 [ 'addTable', 'iwlinks', 'patch-iwlinks.sql' ],
88 [ 'addTable', 'module_deps', 'patch-module_deps.sql' ],
89 [ 'addTable', 'uploadstash', 'patch-uploadstash.sql' ],
90 [ 'addTable', 'user_former_groups', 'patch-user_former_groups.sql' ],
91 [ 'addTable', 'sites', 'patch-sites.sql' ],
92 [ 'addTable', 'bot_passwords', 'patch-bot_passwords.sql' ],
93
94 # Needed before new field
95 [ 'convertArchive2' ],
96
97 # new fields
98 [ 'addPgField', 'updatelog', 'ul_value', 'TEXT' ],
99 [ 'addPgField', 'archive', 'ar_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
100 [ 'addPgField', 'archive', 'ar_len', 'INTEGER' ],
101 [ 'addPgField', 'archive', 'ar_page_id', 'INTEGER' ],
102 [ 'addPgField', 'archive', 'ar_parent_id', 'INTEGER' ],
103 [ 'addPgField', 'archive', 'ar_content_model', 'TEXT' ],
104 [ 'addPgField', 'archive', 'ar_content_format', 'TEXT' ],
105 [ 'addPgField', 'categorylinks', 'cl_sortkey_prefix', "TEXT NOT NULL DEFAULT ''" ],
106 [ 'addPgField', 'categorylinks', 'cl_collation', "TEXT NOT NULL DEFAULT 0" ],
107 [ 'addPgField', 'categorylinks', 'cl_type', "TEXT NOT NULL DEFAULT 'page'" ],
108 [ 'addPgField', 'image', 'img_sha1', "TEXT NOT NULL DEFAULT ''" ],
109 [ 'addPgField', 'ipblocks', 'ipb_allow_usertalk', 'SMALLINT NOT NULL DEFAULT 0' ],
110 [ 'addPgField', 'ipblocks', 'ipb_anon_only', 'SMALLINT NOT NULL DEFAULT 0' ],
111 [ 'addPgField', 'ipblocks', 'ipb_by_text', "TEXT NOT NULL DEFAULT ''" ],
112 [ 'addPgField', 'ipblocks', 'ipb_block_email', 'SMALLINT NOT NULL DEFAULT 0' ],
113 [ 'addPgField', 'ipblocks', 'ipb_create_account', 'SMALLINT NOT NULL DEFAULT 1' ],
114 [ 'addPgField', 'ipblocks', 'ipb_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
115 [ 'addPgField', 'ipblocks', 'ipb_enable_autoblock', 'SMALLINT NOT NULL DEFAULT 1' ],
116 [ 'addPgField', 'ipblocks', 'ipb_parent_block_id',
117 'INTEGER DEFAULT NULL REFERENCES ipblocks(ipb_id) ' .
118 'ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED' ],
119 [ 'addPgField', 'filearchive', 'fa_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
120 [ 'addPgField', 'filearchive', 'fa_sha1', "TEXT NOT NULL DEFAULT ''" ],
121 [ 'addPgField', 'logging', 'log_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
122 [ 'addPgField', 'logging', 'log_id',
123 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ],
124 [ 'addPgField', 'logging', 'log_params', 'TEXT' ],
125 [ 'addPgField', 'mwuser', 'user_editcount', 'INTEGER' ],
126 [ 'addPgField', 'mwuser', 'user_newpass_time', 'TIMESTAMPTZ' ],
127 [ 'addPgField', 'oldimage', 'oi_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
128 [ 'addPgField', 'oldimage', 'oi_major_mime', "TEXT NOT NULL DEFAULT 'unknown'" ],
129 [ 'addPgField', 'oldimage', 'oi_media_type', 'TEXT' ],
130 [ 'addPgField', 'oldimage', 'oi_metadata', "BYTEA NOT NULL DEFAULT ''" ],
131 [ 'addPgField', 'oldimage', 'oi_minor_mime', "TEXT NOT NULL DEFAULT 'unknown'" ],
132 [ 'addPgField', 'oldimage', 'oi_sha1', "TEXT NOT NULL DEFAULT ''" ],
133 [ 'addPgField', 'page', 'page_content_model', 'TEXT' ],
134 [ 'addPgField', 'page_restrictions', 'pr_id',
135 "INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_seq')" ],
136 [ 'addPgField', 'profiling', 'pf_memory', 'NUMERIC(18,10) NOT NULL DEFAULT 0' ],
137 [ 'addPgField', 'recentchanges', 'rc_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
138 [ 'addPgField', 'recentchanges', 'rc_log_action', 'TEXT' ],
139 [ 'addPgField', 'recentchanges', 'rc_log_type', 'TEXT' ],
140 [ 'addPgField', 'recentchanges', 'rc_logid', 'INTEGER NOT NULL DEFAULT 0' ],
141 [ 'addPgField', 'recentchanges', 'rc_new_len', 'INTEGER' ],
142 [ 'addPgField', 'recentchanges', 'rc_old_len', 'INTEGER' ],
143 [ 'addPgField', 'recentchanges', 'rc_params', 'TEXT' ],
144 [ 'addPgField', 'redirect', 'rd_interwiki', 'TEXT NULL' ],
145 [ 'addPgField', 'redirect', 'rd_fragment', 'TEXT NULL' ],
146 [ 'addPgField', 'revision', 'rev_deleted', 'SMALLINT NOT NULL DEFAULT 0' ],
147 [ 'addPgField', 'revision', 'rev_len', 'INTEGER' ],
148 [ 'addPgField', 'revision', 'rev_parent_id', 'INTEGER DEFAULT NULL' ],
149 [ 'addPgField', 'revision', 'rev_content_model', 'TEXT' ],
150 [ 'addPgField', 'revision', 'rev_content_format', 'TEXT' ],
151 [ 'addPgField', 'site_stats', 'ss_active_users', "INTEGER DEFAULT '-1'" ],
152 [ 'addPgField', 'user_newtalk', 'user_last_timestamp', 'TIMESTAMPTZ' ],
153 [ 'addPgField', 'logging', 'log_user_text', "TEXT NOT NULL DEFAULT ''" ],
154 [ 'addPgField', 'logging', 'log_page', 'INTEGER' ],
155 [ 'addPgField', 'interwiki', 'iw_api', "TEXT NOT NULL DEFAULT ''" ],
156 [ 'addPgField', 'interwiki', 'iw_wikiid', "TEXT NOT NULL DEFAULT ''" ],
157 [ 'addPgField', 'revision', 'rev_sha1', "TEXT NOT NULL DEFAULT ''" ],
158 [ 'addPgField', 'archive', 'ar_sha1', "TEXT NOT NULL DEFAULT ''" ],
159 [ 'addPgField', 'uploadstash', 'us_chunk_inx', "INTEGER NULL" ],
160 [ 'addPgField', 'job', 'job_timestamp', "TIMESTAMPTZ" ],
161 [ 'addPgField', 'job', 'job_random', "INTEGER NOT NULL DEFAULT 0" ],
162 [ 'addPgField', 'job', 'job_attempts', "INTEGER NOT NULL DEFAULT 0" ],
163 [ 'addPgField', 'job', 'job_token', "TEXT NOT NULL DEFAULT ''" ],
164 [ 'addPgField', 'job', 'job_token_timestamp', "TIMESTAMPTZ" ],
165 [ 'addPgField', 'job', 'job_sha1', "TEXT NOT NULL DEFAULT ''" ],
166 [ 'addPgField', 'archive', 'ar_id',
167 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('archive_ar_id_seq')" ],
168 [ 'addPgField', 'externallinks', 'el_id',
169 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('externallinks_el_id_seq')" ],
170 [ 'addPgField', 'uploadstash', 'us_props', "BYTEA" ],
171
172 # type changes
173 [ 'changeField', 'archive', 'ar_deleted', 'smallint', '' ],
174 [ 'changeField', 'archive', 'ar_minor_edit', 'smallint',
175 'ar_minor_edit::smallint DEFAULT 0' ],
176 [ 'changeField', 'filearchive', 'fa_deleted', 'smallint', '' ],
177 [ 'changeField', 'filearchive', 'fa_height', 'integer', '' ],
178 [ 'changeField', 'filearchive', 'fa_metadata', 'bytea', "decode(fa_metadata,'escape')" ],
179 [ 'changeField', 'filearchive', 'fa_size', 'integer', '' ],
180 [ 'changeField', 'filearchive', 'fa_width', 'integer', '' ],
181 [ 'changeField', 'filearchive', 'fa_storage_group', 'text', '' ],
182 [ 'changeField', 'filearchive', 'fa_storage_key', 'text', '' ],
183 [ 'changeField', 'image', 'img_metadata', 'bytea', "decode(img_metadata,'escape')" ],
184 [ 'changeField', 'image', 'img_size', 'integer', '' ],
185 [ 'changeField', 'image', 'img_width', 'integer', '' ],
186 [ 'changeField', 'image', 'img_height', 'integer', '' ],
187 [ 'changeField', 'interwiki', 'iw_local', 'smallint', 'iw_local::smallint' ],
188 [ 'changeField', 'interwiki', 'iw_trans', 'smallint', 'iw_trans::smallint DEFAULT 0' ],
189 [ 'changeField', 'ipblocks', 'ipb_auto', 'smallint', 'ipb_auto::smallint DEFAULT 0' ],
190 [ 'changeField', 'ipblocks', 'ipb_anon_only', 'smallint',
191 "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ],
192 [ 'changeField', 'ipblocks', 'ipb_create_account', 'smallint',
193 "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ],
194 [ 'changeField', 'ipblocks', 'ipb_enable_autoblock', 'smallint',
195 "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ],
196 [ 'changeField', 'ipblocks', 'ipb_block_email', 'smallint',
197 "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ],
198 [ 'changeField', 'ipblocks', 'ipb_address', 'text', 'ipb_address::text' ],
199 [ 'changeField', 'ipblocks', 'ipb_deleted', 'smallint', 'ipb_deleted::smallint DEFAULT 0' ],
200 [ 'changeField', 'mwuser', 'user_token', 'text', '' ],
201 [ 'changeField', 'mwuser', 'user_email_token', 'text', '' ],
202 [ 'changeField', 'objectcache', 'keyname', 'text', '' ],
203 [ 'changeField', 'oldimage', 'oi_height', 'integer', '' ],
204 [ 'changeField', 'oldimage', 'oi_metadata', 'bytea', "decode(img_metadata,'escape')" ],
205 [ 'changeField', 'oldimage', 'oi_size', 'integer', '' ],
206 [ 'changeField', 'oldimage', 'oi_width', 'integer', '' ],
207 [ 'changeField', 'page', 'page_is_redirect', 'smallint',
208 'page_is_redirect::smallint DEFAULT 0' ],
209 [ 'changeField', 'page', 'page_is_new', 'smallint', 'page_is_new::smallint DEFAULT 0' ],
210 [ 'changeField', 'querycache', 'qc_value', 'integer', '' ],
211 [ 'changeField', 'querycachetwo', 'qcc_value', 'integer', '' ],
212 [ 'changeField', 'recentchanges', 'rc_bot', 'smallint', 'rc_bot::smallint DEFAULT 0' ],
213 [ 'changeField', 'recentchanges', 'rc_deleted', 'smallint', '' ],
214 [ 'changeField', 'recentchanges', 'rc_minor', 'smallint', 'rc_minor::smallint DEFAULT 0' ],
215 [ 'changeField', 'recentchanges', 'rc_new', 'smallint', 'rc_new::smallint DEFAULT 0' ],
216 [ 'changeField', 'recentchanges', 'rc_type', 'smallint', 'rc_type::smallint DEFAULT 0' ],
217 [ 'changeField', 'recentchanges', 'rc_patrolled', 'smallint',
218 'rc_patrolled::smallint DEFAULT 0' ],
219 [ 'changeField', 'revision', 'rev_deleted', 'smallint', 'rev_deleted::smallint DEFAULT 0' ],
220 [ 'changeField', 'revision', 'rev_minor_edit', 'smallint',
221 'rev_minor_edit::smallint DEFAULT 0' ],
222 [ 'changeField', 'templatelinks', 'tl_namespace', 'smallint', 'tl_namespace::smallint' ],
223 [ 'changeField', 'user_newtalk', 'user_ip', 'text', 'host(user_ip)' ],
224 [ 'changeField', 'uploadstash', 'us_image_bits', 'smallint', '' ],
225 [ 'changeField', 'profiling', 'pf_time', 'float', '' ],
226 [ 'changeField', 'profiling', 'pf_memory', 'float', '' ],
227
228 # null changes
229 [ 'changeNullableField', 'oldimage', 'oi_bits', 'NULL' ],
230 [ 'changeNullableField', 'oldimage', 'oi_timestamp', 'NULL' ],
231 [ 'changeNullableField', 'oldimage', 'oi_major_mime', 'NULL' ],
232 [ 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NULL' ],
233 [ 'changeNullableField', 'image', 'img_metadata', 'NOT NULL' ],
234 [ 'changeNullableField', 'filearchive', 'fa_metadata', 'NOT NULL' ],
235 [ 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NULL' ],
236 [ 'changeNullableField', 'recentchanges', 'rc_cur_time', 'NULL' ],
237
238 [ 'checkOiDeleted' ],
239
240 # New indexes
241 [ 'addPgIndex', 'archive', 'archive_user_text', '(ar_user_text)' ],
242 [ 'addPgIndex', 'image', 'img_sha1', '(img_sha1)' ],
243 [ 'addPgIndex', 'ipblocks', 'ipb_parent_block_id', '(ipb_parent_block_id)' ],
244 [ 'addPgIndex', 'oldimage', 'oi_sha1', '(oi_sha1)' ],
245 [ 'addPgIndex', 'page', 'page_mediawiki_title', '(page_title) WHERE page_namespace = 8' ],
246 [ 'addPgIndex', 'pagelinks', 'pagelinks_title', '(pl_title)' ],
247 [ 'addPgIndex', 'page_props', 'pp_propname_page', '(pp_propname, pp_page)' ],
248 [ 'addPgIndex', 'revision', 'rev_text_id_idx', '(rev_text_id)' ],
249 [ 'addPgIndex', 'recentchanges', 'rc_timestamp_bot', '(rc_timestamp) WHERE rc_bot = 0' ],
250 [ 'addPgIndex', 'templatelinks', 'templatelinks_from', '(tl_from)' ],
251 [ 'addPgIndex', 'watchlist', 'wl_user', '(wl_user)' ],
252 [ 'addPgIndex', 'watchlist', 'wl_user_notificationtimestamp',
253 '(wl_user, wl_notificationtimestamp)' ],
254 [ 'addPgIndex', 'logging', 'logging_user_type_time',
255 '(log_user, log_type, log_timestamp)' ],
256 [ 'addPgIndex', 'logging', 'logging_page_id_time', '(log_page,log_timestamp)' ],
257 [ 'addPgIndex', 'iwlinks', 'iwl_prefix_from_title', '(iwl_prefix, iwl_from, iwl_title)' ],
258 [ 'addPgIndex', 'iwlinks', 'iwl_prefix_title_from', '(iwl_prefix, iwl_title, iwl_from)' ],
259 [ 'addPgIndex', 'job', 'job_timestamp_idx', '(job_timestamp)' ],
260 [ 'addPgIndex', 'job', 'job_sha1', '(job_sha1)' ],
261 [ 'addPgIndex', 'job', 'job_cmd_token', '(job_cmd, job_token, job_random)' ],
262 [ 'addPgIndex', 'job', 'job_cmd_token_id', '(job_cmd, job_token, job_id)' ],
263 [ 'addPgIndex', 'filearchive', 'fa_sha1', '(fa_sha1)' ],
264 [ 'addPgIndex', 'logging', 'logging_user_text_type_time',
265 '(log_user_text, log_type, log_timestamp)' ],
266 [ 'addPgIndex', 'logging', 'logging_user_text_time', '(log_user_text, log_timestamp)' ],
267
268 [ 'checkIndex', 'pagelink_unique', [
269 [ 'pl_from', 'int4_ops', 'btree', 0 ],
270 [ 'pl_namespace', 'int2_ops', 'btree', 0 ],
271 [ 'pl_title', 'text_ops', 'btree', 0 ],
272 ],
273 'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ],
274 [ 'checkIndex', 'cl_sortkey', [
275 [ 'cl_to', 'text_ops', 'btree', 0 ],
276 [ 'cl_sortkey', 'text_ops', 'btree', 0 ],
277 [ 'cl_from', 'int4_ops', 'btree', 0 ],
278 ],
279 'CREATE INDEX cl_sortkey ON "categorylinks" ' .
280 'USING "btree" ("cl_to", "cl_sortkey", "cl_from")' ],
281 [ 'checkIndex', 'iwl_prefix_title_from', [
282 [ 'iwl_prefix', 'text_ops', 'btree', 0 ],
283 [ 'iwl_title', 'text_ops', 'btree', 0 ],
284 [ 'iwl_from', 'int4_ops', 'btree', 0 ],
285 ],
286 'CREATE INDEX iwl_prefix_title_from ON "iwlinks" ' .
287 'USING "btree" ("iwl_prefix", "iwl_title", "iwl_from")' ],
288 [ 'checkIndex', 'logging_times', [
289 [ 'log_timestamp', 'timestamptz_ops', 'btree', 0 ],
290 ],
291 'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ],
292 [ 'dropIndex', 'oldimage', 'oi_name' ],
293 [ 'checkIndex', 'oi_name_archive_name', [
294 [ 'oi_name', 'text_ops', 'btree', 0 ],
295 [ 'oi_archive_name', 'text_ops', 'btree', 0 ],
296 ],
297 'CREATE INDEX "oi_name_archive_name" ON "oldimage" ' .
298 'USING "btree" ("oi_name", "oi_archive_name")' ],
299 [ 'checkIndex', 'oi_name_timestamp', [
300 [ 'oi_name', 'text_ops', 'btree', 0 ],
301 [ 'oi_timestamp', 'timestamptz_ops', 'btree', 0 ],
302 ],
303 'CREATE INDEX "oi_name_timestamp" ON "oldimage" ' .
304 'USING "btree" ("oi_name", "oi_timestamp")' ],
305 [ 'checkIndex', 'page_main_title', [
306 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
307 ],
308 'CREATE INDEX "page_main_title" ON "page" ' .
309 'USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 0)' ],
310 [ 'checkIndex', 'page_mediawiki_title', [
311 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
312 ],
313 'CREATE INDEX "page_mediawiki_title" ON "page" ' .
314 'USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 8)' ],
315 [ 'checkIndex', 'page_project_title', [
316 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
317 ],
318 'CREATE INDEX "page_project_title" ON "page" ' .
319 'USING "btree" ("page_title" "text_pattern_ops") ' .
320 'WHERE ("page_namespace" = 4)' ],
321 [ 'checkIndex', 'page_talk_title', [
322 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
323 ],
324 'CREATE INDEX "page_talk_title" ON "page" ' .
325 'USING "btree" ("page_title" "text_pattern_ops") ' .
326 'WHERE ("page_namespace" = 1)' ],
327 [ 'checkIndex', 'page_user_title', [
328 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
329 ],
330 'CREATE INDEX "page_user_title" ON "page" ' .
331 'USING "btree" ("page_title" "text_pattern_ops") WHERE ' .
332 '("page_namespace" = 2)' ],
333 [ 'checkIndex', 'page_utalk_title', [
334 [ 'page_title', 'text_pattern_ops', 'btree', 0 ],
335 ],
336 'CREATE INDEX "page_utalk_title" ON "page" ' .
337 'USING "btree" ("page_title" "text_pattern_ops") ' .
338 'WHERE ("page_namespace" = 3)' ],
339 [ 'checkIndex', 'ts2_page_text', [
340 [ 'textvector', 'tsvector_ops', 'gist', 0 ],
341 ],
342 'CREATE INDEX "ts2_page_text" ON "pagecontent" USING "gist" ("textvector")' ],
343 [ 'checkIndex', 'ts2_page_title', [
344 [ 'titlevector', 'tsvector_ops', 'gist', 0 ],
345 ],
346 'CREATE INDEX "ts2_page_title" ON "page" USING "gist" ("titlevector")' ],
347
348 [ 'checkOiNameConstraint' ],
349 [ 'checkPageDeletedTrigger' ],
350 [ 'checkRevUserFkey' ],
351 [ 'dropIndex', 'ipblocks', 'ipb_address' ],
352 [ 'checkIndex', 'ipb_address_unique', [
353 [ 'ipb_address', 'text_ops', 'btree', 0 ],
354 [ 'ipb_user', 'int4_ops', 'btree', 0 ],
355 [ 'ipb_auto', 'int2_ops', 'btree', 0 ],
356 [ 'ipb_anon_only', 'int2_ops', 'btree', 0 ],
357 ],
358 'CREATE UNIQUE INDEX ipb_address_unique ' .
359 'ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only)' ],
360
361 [ 'checkIwlPrefix' ],
362
363 # All FK columns should be deferred
364 [ 'changeFkeyDeferrable', 'archive', 'ar_user', 'mwuser(user_id) ON DELETE SET NULL' ],
365 [ 'changeFkeyDeferrable', 'categorylinks', 'cl_from', 'page(page_id) ON DELETE CASCADE' ],
366 [ 'changeFkeyDeferrable', 'externallinks', 'el_from', 'page(page_id) ON DELETE CASCADE' ],
367 [ 'changeFkeyDeferrable', 'filearchive', 'fa_deleted_user',
368 'mwuser(user_id) ON DELETE SET NULL' ],
369 [ 'changeFkeyDeferrable', 'filearchive', 'fa_user', 'mwuser(user_id) ON DELETE SET NULL' ],
370 [ 'changeFkeyDeferrable', 'image', 'img_user', 'mwuser(user_id) ON DELETE SET NULL' ],
371 [ 'changeFkeyDeferrable', 'imagelinks', 'il_from', 'page(page_id) ON DELETE CASCADE' ],
372 [ 'changeFkeyDeferrable', 'ipblocks', 'ipb_by', 'mwuser(user_id) ON DELETE CASCADE' ],
373 [ 'changeFkeyDeferrable', 'ipblocks', 'ipb_user', 'mwuser(user_id) ON DELETE SET NULL' ],
374 [ 'changeFkeyDeferrable', 'ipblocks', 'ipb_parent_block_id',
375 'ipblocks(ipb_id) ON DELETE SET NULL' ],
376 [ 'changeFkeyDeferrable', 'langlinks', 'll_from', 'page(page_id) ON DELETE CASCADE' ],
377 [ 'changeFkeyDeferrable', 'logging', 'log_user', 'mwuser(user_id) ON DELETE SET NULL' ],
378 [ 'changeFkeyDeferrable', 'oldimage', 'oi_name',
379 'image(img_name) ON DELETE CASCADE ON UPDATE CASCADE' ],
380 [ 'changeFkeyDeferrable', 'oldimage', 'oi_user', 'mwuser(user_id) ON DELETE SET NULL' ],
381 [ 'changeFkeyDeferrable', 'pagelinks', 'pl_from', 'page(page_id) ON DELETE CASCADE' ],
382 [ 'changeFkeyDeferrable', 'page_props', 'pp_page', 'page (page_id) ON DELETE CASCADE' ],
383 [ 'changeFkeyDeferrable', 'page_restrictions', 'pr_page',
384 'page(page_id) ON DELETE CASCADE' ],
385 [ 'changeFkeyDeferrable', 'protected_titles', 'pt_user',
386 'mwuser(user_id) ON DELETE SET NULL' ],
387 [ 'changeFkeyDeferrable', 'recentchanges', 'rc_user',
388 'mwuser(user_id) ON DELETE SET NULL' ],
389 [ 'changeFkeyDeferrable', 'redirect', 'rd_from', 'page(page_id) ON DELETE CASCADE' ],
390 [ 'changeFkeyDeferrable', 'revision', 'rev_page', 'page (page_id) ON DELETE CASCADE' ],
391 [ 'changeFkeyDeferrable', 'revision', 'rev_user', 'mwuser(user_id) ON DELETE RESTRICT' ],
392 [ 'changeFkeyDeferrable', 'templatelinks', 'tl_from', 'page(page_id) ON DELETE CASCADE' ],
393 [ 'changeFkeyDeferrable', 'user_groups', 'ug_user', 'mwuser(user_id) ON DELETE CASCADE' ],
394 [ 'changeFkeyDeferrable', 'user_newtalk', 'user_id', 'mwuser(user_id) ON DELETE CASCADE' ],
395 [ 'changeFkeyDeferrable', 'user_properties', 'up_user',
396 'mwuser(user_id) ON DELETE CASCADE' ],
397 [ 'changeFkeyDeferrable', 'watchlist', 'wl_user', 'mwuser(user_id) ON DELETE CASCADE' ],
398
399 # r81574
400 [ 'addInterwikiType' ],
401 # end
402 [ 'tsearchFixes' ],
403
404 // 1.23
405 [ 'addPgField', 'recentchanges', 'rc_source', "TEXT NOT NULL DEFAULT ''" ],
406 [ 'addPgField', 'page', 'page_links_updated', "TIMESTAMPTZ NULL" ],
407 [ 'addPgField', 'mwuser', 'user_password_expires', 'TIMESTAMPTZ NULL' ],
408 [ 'changeFieldPurgeTable', 'l10n_cache', 'lc_value', 'bytea',
409 "replace(lc_value,'\','\\\\')::bytea" ],
410 // 1.23.9
411 [ 'rebuildTextSearch' ],
412
413 // 1.24
414 [ 'addPgField', 'page_props', 'pp_sortkey', 'float NULL' ],
415 [ 'addPgIndex', 'page_props', 'pp_propname_sortkey_page',
416 '( pp_propname, pp_sortkey, pp_page ) WHERE ( pp_sortkey IS NOT NULL )' ],
417 [ 'addPgField', 'page', 'page_lang', 'TEXT default NULL' ],
418 [ 'addPgField', 'pagelinks', 'pl_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ],
419 [ 'addPgField', 'templatelinks', 'tl_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ],
420 [ 'addPgField', 'imagelinks', 'il_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ],
421
422 // 1.25
423 [ 'dropTable', 'hitcounter' ],
424 [ 'dropField', 'site_stats', 'ss_total_views', 'patch-drop-ss_total_views.sql' ],
425 [ 'dropField', 'page', 'page_counter', 'patch-drop-page_counter.sql' ],
426 [ 'dropFkey', 'recentchanges', 'rc_cur_id' ],
427
428 // 1.27
429 [ 'dropTable', 'msg_resource_links' ],
430 [ 'dropTable', 'msg_resource' ],
431 ];
432 }
433
434 protected function getOldGlobalUpdates() {
435 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
436
437 $updates = [];
438
439 # Add missing extension tables
440 foreach ( $wgExtNewTables as $tableRecord ) {
441 $updates[] = [
442 'addTable', $tableRecord[0], $tableRecord[1], true
443 ];
444 }
445
446 # Add missing extension fields
447 foreach ( $wgExtPGNewFields as $fieldRecord ) {
448 $updates[] = [
449 'addPgField', $fieldRecord[0], $fieldRecord[1],
450 $fieldRecord[2]
451 ];
452 }
453
454 # Change altered columns
455 foreach ( $wgExtPGAlteredFields as $fieldRecord ) {
456 $updates[] = [
457 'changeField', $fieldRecord[0], $fieldRecord[1],
458 $fieldRecord[2]
459 ];
460 }
461
462 # Add missing extension indexes
463 foreach ( $wgExtNewIndexes as $fieldRecord ) {
464 $updates[] = [
465 'addPgExtIndex', $fieldRecord[0], $fieldRecord[1],
466 $fieldRecord[2]
467 ];
468 }
469
470 return $updates;
471 }
472
473 protected function describeTable( $table ) {
474 $q = <<<END
475 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
476 WHERE pg_class.relnamespace = pg_namespace.oid
477 AND attrelid=pg_class.oid AND attnum > 0
478 AND relname=%s AND nspname=%s
479 END;
480 $res = $this->db->query( sprintf( $q,
481 $this->db->addQuotes( $table ),
482 $this->db->addQuotes( $this->db->getCoreSchema() ) ) );
483 if ( !$res ) {
484 return null;
485 }
486
487 $cols = [];
488 foreach ( $res as $r ) {
489 $cols[] = [
490 "name" => $r[0],
491 "ord" => $r[1],
492 ];
493 }
494
495 return $cols;
496 }
497
498 function describeIndex( $idx ) {
499 // first fetch the key (which is a list of columns ords) and
500 // the table the index applies to (an oid)
501 $q = <<<END
502 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
503 WHERE nspname=%s
504 AND pg_class.relnamespace = pg_namespace.oid
505 AND relname=%s
506 AND indexrelid=pg_class.oid
507 END;
508 $res = $this->db->query(
509 sprintf(
510 $q,
511 $this->db->addQuotes( $this->db->getCoreSchema() ),
512 $this->db->addQuotes( $idx )
513 )
514 );
515 if ( !$res ) {
516 return null;
517 }
518 $r = $this->db->fetchRow( $res );
519 if ( !$r ) {
520 return null;
521 }
522
523 $indkey = $r[0];
524 $relid = intval( $r[1] );
525 $indkeys = explode( ' ', $indkey );
526
527 $colnames = [];
528 foreach ( $indkeys as $rid ) {
529 $query = <<<END
530 SELECT attname FROM pg_class, pg_attribute
531 WHERE attrelid=$relid
532 AND attnum=%d
533 AND attrelid=pg_class.oid
534 END;
535 $r2 = $this->db->query( sprintf( $query, $rid ) );
536 if ( !$r2 ) {
537 return null;
538 }
539 $row2 = $this->db->fetchRow( $r2 );
540 if ( !$row2 ) {
541 return null;
542 }
543 $colnames[] = $row2[0];
544 }
545
546 return $colnames;
547 }
548
549 function fkeyDeltype( $fkey ) {
550 $q = <<<END
551 SELECT confdeltype FROM pg_constraint, pg_namespace
552 WHERE connamespace=pg_namespace.oid
553 AND nspname=%s
554 AND conname=%s;
555 END;
556 $r = $this->db->query(
557 sprintf(
558 $q,
559 $this->db->addQuotes( $this->db->getCoreSchema() ),
560 $this->db->addQuotes( $fkey )
561 )
562 );
563 $row = $this->db->fetchRow( $r );
564 if ( !$row ) {
565 return null;
566 }
567
568 return $row[0];
569 }
570
571 function ruleDef( $table, $rule ) {
572 $q = <<<END
573 SELECT definition FROM pg_rules
574 WHERE schemaname = %s
575 AND tablename = %s
576 AND rulename = %s
577 END;
578 $r = $this->db->query(
579 sprintf(
580 $q,
581 $this->db->addQuotes( $this->db->getCoreSchema() ),
582 $this->db->addQuotes( $table ),
583 $this->db->addQuotes( $rule )
584 )
585 );
586 $row = $this->db->fetchRow( $r );
587 if ( !$row ) {
588 return null;
589 }
590 $d = $row[0];
591
592 return $d;
593 }
594
595 protected function addSequence( $table, $pkey, $ns ) {
596 if ( !$this->db->sequenceExists( $ns ) ) {
597 $this->output( "Creating sequence $ns\n" );
598 $this->db->query( "CREATE SEQUENCE $ns" );
599 if ( $pkey !== false ) {
600 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
601 }
602 }
603 }
604
605 protected function renameSequence( $old, $new ) {
606 if ( $this->db->sequenceExists( $new ) ) {
607 $this->output( "...sequence $new already exists.\n" );
608
609 return;
610 }
611 if ( $this->db->sequenceExists( $old ) ) {
612 $this->output( "Renaming sequence $old to $new\n" );
613 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new" );
614 }
615 }
616
617 protected function renameTable( $old, $new, $patch = false ) {
618 if ( $this->db->tableExists( $old ) ) {
619 $this->output( "Renaming table $old to $new\n" );
620 $old = $this->db->realTableName( $old, "quoted" );
621 $new = $this->db->realTableName( $new, "quoted" );
622 $this->db->query( "ALTER TABLE $old RENAME TO $new" );
623 if ( $patch !== false ) {
624 $this->applyPatch( $patch );
625 }
626 }
627 }
628
629 protected function renameIndex(
630 $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
631 ) {
632 // First requirement: the table must exist
633 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
634 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
635
636 return;
637 }
638
639 // Second requirement: the new index must be missing
640 if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
641 $this->output( "...index $new already set on $table table.\n" );
642 if ( !$skipBothIndexExistWarning
643 && $this->db->indexExists( $table, $old, __METHOD__ )
644 ) {
645 $this->output( "...WARNING: $old still exists, despite it has been " .
646 "renamed into $new (which also exists).\n" .
647 " $old should be manually removed if not needed anymore.\n" );
648 }
649
650 return;
651 }
652
653 // Third requirement: the old index must exist
654 if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
655 $this->output( "...skipping: index $old doesn't exist.\n" );
656
657 return;
658 }
659
660 $this->db->query( "ALTER INDEX $old RENAME TO $new" );
661 }
662
663 protected function addPgField( $table, $field, $type ) {
664 $fi = $this->db->fieldInfo( $table, $field );
665 if ( !is_null( $fi ) ) {
666 $this->output( "...column '$table.$field' already exists\n" );
667
668 return;
669 } else {
670 $this->output( "Adding column '$table.$field'\n" );
671 $this->db->query( "ALTER TABLE $table ADD $field $type" );
672 }
673 }
674
675 protected function changeField( $table, $field, $newtype, $default ) {
676 $fi = $this->db->fieldInfo( $table, $field );
677 if ( is_null( $fi ) ) {
678 $this->output( "...ERROR: expected column $table.$field to exist\n" );
679 exit( 1 );
680 }
681
682 if ( $fi->type() === $newtype ) {
683 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
684 } else {
685 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
686 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
687 if ( strlen( $default ) ) {
688 $res = [];
689 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
690 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
691 $this->db->query( $sqldef );
692 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
693 }
694 $sql .= " USING $default";
695 }
696 $this->db->query( $sql );
697 }
698 }
699
700 protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
701 # # For a cache table, empty it if the field needs to be changed, because the old contents
702 # # may be corrupted. If the column is already the desired type, refrain from purging.
703 $fi = $this->db->fieldInfo( $table, $field );
704 if ( is_null( $fi ) ) {
705 $this->output( "...ERROR: expected column $table.$field to exist\n" );
706 exit( 1 );
707 }
708
709 if ( $fi->type() === $newtype ) {
710 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
711 } else {
712 $this->output( "Purging data from cache table '$table'\n" );
713 $this->db->query( "DELETE from $table" );
714 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
715 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
716 if ( strlen( $default ) ) {
717 $res = [];
718 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
719 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
720 $this->db->query( $sqldef );
721 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
722 }
723 $sql .= " USING $default";
724 }
725 $this->db->query( $sql );
726 }
727 }
728
729 protected function setDefault( $table, $field, $default ) {
730
731 $info = $this->db->fieldInfo( $table, $field );
732 if ( $info->defaultValue() !== $default ) {
733 $this->output( "Changing '$table.$field' default value\n" );
734 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT " . $default );
735 }
736 }
737
738 protected function changeNullableField( $table, $field, $null ) {
739 $fi = $this->db->fieldInfo( $table, $field );
740 if ( is_null( $fi ) ) {
741 $this->output( "...ERROR: expected column $table.$field to exist\n" );
742 exit( 1 );
743 }
744 if ( $fi->isNullable() ) {
745 # # It's NULL - does it need to be NOT NULL?
746 if ( 'NOT NULL' === $null ) {
747 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
748 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" );
749 } else {
750 $this->output( "...column '$table.$field' is already set as NULL\n" );
751 }
752 } else {
753 # # It's NOT NULL - does it need to be NULL?
754 if ( 'NULL' === $null ) {
755 $this->output( "Changing '$table.$field' to allow NULLs\n" );
756 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" );
757 } else {
758 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
759 }
760 }
761 }
762
763 public function addPgIndex( $table, $index, $type ) {
764 if ( $this->db->indexExists( $table, $index ) ) {
765 $this->output( "...index '$index' on table '$table' already exists\n" );
766 } else {
767 $this->output( "Creating index '$index' on table '$table' $type\n" );
768 $this->db->query( "CREATE INDEX $index ON $table $type" );
769 }
770 }
771
772 public function addPgExtIndex( $table, $index, $type ) {
773 if ( $this->db->indexExists( $table, $index ) ) {
774 $this->output( "...index '$index' on table '$table' already exists\n" );
775 } else {
776 if ( preg_match( '/^\(/', $type ) ) {
777 $this->output( "Creating index '$index' on table '$table'\n" );
778 $this->db->query( "CREATE INDEX $index ON $table $type" );
779 } else {
780 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
781 }
782 }
783 }
784
785 protected function dropFkey( $table, $field ) {
786 $fi = $this->db->fieldInfo( $table, $field );
787 if ( is_null( $fi ) ) {
788 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
789 "Please report this.\n" );
790 return;
791 }
792 $conname = $fi->conname();
793 if ( $fi->conname() ) {
794 $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
795 $conclause = "CONSTRAINT \"$conname\"";
796 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
797 $this->db->query( $command );
798 } else {
799 $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
800 };
801 }
802
803 protected function changeFkeyDeferrable( $table, $field, $clause ) {
804 $fi = $this->db->fieldInfo( $table, $field );
805 if ( is_null( $fi ) ) {
806 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
807 "Please report this.\n" );
808
809 return;
810 }
811 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
812 return;
813 }
814 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
815 $conname = $fi->conname();
816 if ( $fi->conname() ) {
817 $conclause = "CONSTRAINT \"$conname\"";
818 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
819 $this->db->query( $command );
820 } else {
821 $this->output( "Column '$table.$field' does not have a foreign key " .
822 "constraint, will be added\n" );
823 $conclause = "";
824 }
825 $command =
826 "ALTER TABLE $table ADD $conclause " .
827 "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
828 $this->db->query( $command );
829 }
830
831 protected function convertArchive2() {
832 if ( $this->db->tableExists( "archive2" ) ) {
833 if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) {
834 $this->output( "Dropping rule 'archive_insert'\n" );
835 $this->db->query( 'DROP RULE archive_insert ON archive' );
836 }
837 if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) {
838 $this->output( "Dropping rule 'archive_delete'\n" );
839 $this->db->query( 'DROP RULE archive_delete ON archive' );
840 }
841 $this->applyPatch(
842 'patch-remove-archive2.sql',
843 false,
844 "Converting 'archive2' back to normal archive table"
845 );
846 } else {
847 $this->output( "...obsolete table 'archive2' does not exist\n" );
848 }
849 }
850
851 protected function checkOiDeleted() {
852 if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
853 $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" );
854 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
855 $this->db->query(
856 "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
857 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
858 } else {
859 $this->output( "...column 'oldimage.oi_deleted' is already of type 'smallint'\n" );
860 }
861 }
862
863 protected function checkOiNameConstraint() {
864 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
865 $this->output( "...table 'oldimage' has correct cascading delete/update " .
866 "foreign key to image\n" );
867 } else {
868 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) {
869 $this->db->query(
870 "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
871 }
872 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
873 $this->db->query(
874 "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
875 }
876 $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" );
877 $this->db->query(
878 "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
879 "FOREIGN KEY (oi_name) REFERENCES image(img_name) " .
880 "ON DELETE CASCADE ON UPDATE CASCADE" );
881 }
882 }
883
884 protected function checkPageDeletedTrigger() {
885 if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) {
886 $this->applyPatch(
887 'patch-page_deleted.sql',
888 false,
889 "Adding function and trigger 'page_deleted' to table 'page'"
890 );
891 } else {
892 $this->output( "...table 'page' has 'page_deleted' trigger\n" );
893 }
894 }
895
896 protected function dropIndex( $table, $index, $patch = '', $fullpath = false ) {
897 if ( $this->db->indexExists( $table, $index ) ) {
898 $this->output( "Dropping obsolete index '$index'\n" );
899 $this->db->query( "DROP INDEX \"" . $index . "\"" );
900 }
901 }
902
903 protected function checkIndex( $index, $should_be, $good_def ) {
904 $pu = $this->db->indexAttributes( $index );
905 if ( !empty( $pu ) && $pu != $should_be ) {
906 $this->output( "Dropping obsolete version of index '$index'\n" );
907 $this->db->query( "DROP INDEX \"" . $index . "\"" );
908 $pu = [];
909 } else {
910 $this->output( "...no need to drop index '$index'\n" );
911 }
912
913 if ( empty( $pu ) ) {
914 $this->output( "Creating index '$index'\n" );
915 $this->db->query( $good_def );
916 } else {
917 $this->output( "...index '$index' exists\n" );
918 }
919 }
920
921 protected function checkRevUserFkey() {
922 if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) {
923 $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" );
924 } else {
925 $this->applyPatch(
926 'patch-revision_rev_user_fkey.sql',
927 false,
928 "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT"
929 );
930 }
931 }
932
933 protected function checkIwlPrefix() {
934 if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) {
935 $this->applyPatch(
936 'patch-rename-iwl_prefix.sql',
937 false,
938 "Replacing index 'iwl_prefix' with 'iwl_prefix_title_from'"
939 );
940 }
941 }
942
943 protected function addInterwikiType() {
944 $this->applyPatch( 'patch-add_interwiki.sql', false, "Refreshing add_interwiki()" );
945 }
946
947 protected function tsearchFixes() {
948 # Tweak the page_title tsearch2 trigger to filter out slashes
949 # This is create or replace, so harmless to call if not needed
950 $this->applyPatch( 'patch-ts2pagetitle.sql', false, "Refreshing ts2_page_title()" );
951
952 # If the server is 8.3 or higher, rewrite the tsearch2 triggers
953 # in case they have the old 'default' versions
954 # Gather version numbers in case we need them
955 if ( $this->db->getServerVersion() >= 8.3 ) {
956 $this->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" );
957 }
958 }
959
960 protected function rebuildTextSearch() {
961 if ( $this->updateRowExists( 'patch-textsearch_bug66650.sql' ) ) {
962 $this->output( "...bug 66650 already fixed or not applicable.\n" );
963 return true;
964 };
965 $this->applyPatch( 'patch-textsearch_bug66650.sql', false,
966 'Rebuilding text search for bug 66650' );
967 }
968 }