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