MediaWiki  master
PostgresUpdater.php
Go to the documentation of this file.
1 <?php
25 
33 
37  protected $db;
38 
42  protected function getCoreUpdateList() {
43  return [
44  // 1.35 but must come first
45  [ 'addPgField', 'revision', 'rev_actor', 'INTEGER NOT NULL DEFAULT 0' ],
46  [ 'addPgIndex', 'revision', 'rev_actor_timestamp', '(rev_actor,rev_timestamp,rev_id)' ],
47  [ 'addPgIndex', 'revision', 'rev_page_actor_timestamp', '(rev_page,rev_actor,rev_timestamp)' ],
48 
49  // Exception to the sequential updates. Renaming pagecontent and mwuser.
50  // Introduced in 1.36.
51  [ 'renameTable', 'pagecontent', 'text' ],
52  // Introduced in 1.37.
53  [ 'renameTable', 'mwuser', 'user' ],
54 
55  // 1.34
56  [ 'dropPgIndex', 'archive', 'archive_user_text' ],
57  [ 'dropPgField', 'archive', 'ar_user' ],
58  [ 'dropPgField', 'archive', 'ar_user_text' ],
59  [ 'dropDefault', 'archive', 'ar_actor' ],
60  [ 'dropPgField', 'ipblocks', 'ipb_by' ],
61  [ 'dropPgField', 'ipblocks', 'ipb_by_text' ],
62  [ 'dropDefault', 'ipblocks', 'ipb_by_actor' ],
63  [ 'dropPgField', 'image', 'img_user' ],
64  [ 'dropPgField', 'image', 'img_user_text' ],
65  [ 'dropDefault', 'image', 'img_actor' ],
66  [ 'dropPgField', 'oldimage', 'oi_user' ],
67  [ 'dropPgField', 'oldimage', 'oi_user_text' ],
68  [ 'dropDefault', 'oldimage', 'oi_actor' ],
69  [ 'dropPgField', 'filearchive', 'fa_user' ],
70  [ 'dropPgField', 'filearchive', 'fa_user_text' ],
71  [ 'dropDefault', 'filearchive', 'fa_actor' ],
72  [ 'dropPgField', 'recentchanges', 'rc_user' ],
73  [ 'dropPgField', 'recentchanges', 'rc_user_text' ],
74  [ 'dropDefault', 'recentchanges', 'rc_actor' ],
75  [ 'dropPgIndex', 'logging', 'logging_user_time' ],
76  [ 'dropPgIndex', 'logging', 'logging_user_type_time' ],
77  [ 'dropPgIndex', 'logging', 'logging_user_text_type_time' ],
78  [ 'dropPgIndex', 'logging', 'logging_user_text_time' ],
79  [ 'dropPgField', 'logging', 'log_user' ],
80  [ 'dropPgField', 'logging', 'log_user_text' ],
81  [ 'dropDefault', 'logging', 'log_actor' ],
82 
83  // 1.35
84  [ 'addIndex', 'redirect', 'redirect_pkey', 'patch-redirect-pk.sql' ],
85  [ 'addTable', 'watchlist_expiry', 'patch-watchlist_expiry.sql' ],
86  [ 'setSequenceOwner', 'watchlist_expiry', 'we_item', 'watchlist_expiry_we_item_seq' ],
87  [ 'setDefault', 'user_newtalk', 'user_ip', '' ],
88  [ 'changeNullableField', 'user_newtalk', 'user_ip', 'NOT NULL', true ],
89  [ 'setDefault', 'user_newtalk', 'user_id', 0 ],
90  [ 'dropPgIndex', 'revision', 'rev_user_idx' ],
91  [ 'dropPgIndex', 'revision', 'rev_user_text_idx' ],
92  [ 'dropPgIndex', 'revision', 'rev_text_id_idx' ],
93  [ 'dropPgField', 'revision', 'rev_user' ],
94  [ 'dropPgField', 'revision', 'rev_user_text' ],
95  [ 'dropPgField', 'revision', 'rev_comment' ],
96  [ 'dropPgField', 'revision', 'rev_text_id' ],
97  [ 'dropPgField', 'revision', 'rev_content_model' ],
98  [ 'dropPgField', 'revision', 'rev_content_format' ],
99  [ 'addPgField', 'revision', 'rev_comment_id', 'INTEGER NOT NULL DEFAULT 0' ],
100  [ 'dropPgField', 'archive', 'ar_text_id' ],
101  [ 'dropPgField', 'archive', 'ar_content_model' ],
102  [ 'dropPgField', 'archive', 'ar_content_format' ],
103  [ 'changeField', 'updatelog', 'ul_key', 'varchar(255)', '' ],
104  [ 'changeField', 'updatelog', 'ul_value', 'TEXT', '' ],
105  [ 'changeField', 'site_identifiers', 'si_type', 'TEXT', '' ],
106  [ 'changeField', 'site_identifiers', 'si_key', 'TEXT', '' ],
107  [ 'changeField', 'actor', 'actor_id', 'BIGINT', '' ],
108  [ 'changeField', 'actor', 'actor_name', 'TEXT', '' ],
109  [ 'changeField', 'user_former_groups', 'ufg_group', 'TEXT', '' ],
110  [ 'dropFkey', 'user_former_groups', 'ufg_user' ],
111  [ 'checkIndex', 'ipb_address_unique', [
112  [ 'ipb_address', 'text_ops', 'btree', 0 ],
113  [ 'ipb_user', 'int4_ops', 'btree', 0 ],
114  [ 'ipb_auto', 'int2_ops', 'btree', 0 ],
115  ],
116  'CREATE UNIQUE INDEX ipb_address_unique ' .
117  'ON ipblocks (ipb_address,ipb_user,ipb_auto)' ],
118 
119  // 1.36
120  [ 'setDefault', 'bot_passwords', 'bp_token', '' ],
121  [ 'changeField', 'comment', 'comment_id', 'BIGINT', '' ],
122  [ 'changeField', 'slots', 'slot_revision_id', 'BIGINT', '' ],
123  [ 'changeField', 'slots', 'slot_content_id', 'BIGINT', '' ],
124  [ 'changeField', 'slots', 'slot_origin', 'BIGINT', '' ],
125  [ 'changeField', 'site_stats', 'ss_total_edits', 'BIGINT', '' ],
126  [ 'changeField', 'site_stats', 'ss_good_articles', 'BIGINT', '' ],
127  [ 'changeField', 'site_stats', 'ss_total_pages', 'BIGINT', '' ],
128  [ 'changeField', 'site_stats', 'ss_users', 'BIGINT', '' ],
129  [ 'changeField', 'site_stats', 'ss_active_users', 'BIGINT', '' ],
130  [ 'changeField', 'site_stats', 'ss_images', 'BIGINT', '' ],
131  [ 'dropFkey', 'user_properties', 'up_user' ],
132  [ 'addIndex', 'user_properties', 'user_properties_pkey', 'patch-user_properties-pk.sql' ],
133  [ 'changeField', 'log_search', 'ls_value', 'VARCHAR(255)', '' ],
134  [ 'changeField', 'content', 'content_id', 'BIGINT', '' ],
135  [ 'changeField', 'l10n_cache', 'lc_value', 'TEXT', '' ],
136  [ 'changeField', 'l10n_cache', 'lc_key', 'VARCHAR(255)', '' ],
137  [ 'addIndex', 'l10n_cache', 'l10n_cache_pkey', 'patch-l10n_cache-pk.sql' ],
138  [ 'addIndex', 'module_deps', 'module_deps_pkey', 'patch-module_deps-pk.sql' ],
139  [ 'changeField', 'redirect', 'rd_namespace', 'INT', 'rd_namespace::INT DEFAULT 0' ],
140  [ 'setDefault', 'redirect', 'rd_title', '' ],
141  [ 'setDefault', 'redirect', 'rd_from', 0 ],
142  [ 'dropFkey', 'redirect', 'rd_from' ],
143  [ 'changeField', 'redirect', 'rd_interwiki', 'VARCHAR(32)', '' ],
144  [ 'dropFkey', 'pagelinks', 'pl_from' ],
145  [ 'changeField', 'pagelinks', 'pl_namespace', 'INT', 'pl_namespace::INT DEFAULT 0' ],
146  [ 'setDefault', 'pagelinks', 'pl_title', '' ],
147  [ 'addPgIndex', 'pagelinks', 'pl_namespace', '(pl_namespace,pl_title,pl_from)' ],
148  [ 'addPgIndex', 'pagelinks', 'pl_backlinks_namespace',
149  '(pl_from_namespace,pl_namespace,pl_title,pl_from)' ],
150  [ 'dropPgIndex', 'pagelinks', 'pagelink_unique' ],
151  [ 'dropPgIndex', 'pagelinks', 'pagelinks_title' ],
152  [ 'dropFkey', 'templatelinks', 'tl_from' ],
153  [ 'dropPgIndex', 'templatelinks', 'templatelinks_unique' ],
154  [ 'dropPgIndex', 'templatelinks', 'templatelinks_from' ],
155  [ 'dropFkey', 'imagelinks', 'il_from' ],
156  [ 'setDefault', 'imagelinks', 'il_to', '' ],
157  [ 'addPgIndex', 'imagelinks', 'il_to', '(il_to, il_from)' ],
158  [ 'addPgIndex', 'imagelinks', 'il_backlinks_namespace',
159  '(il_from_namespace, il_to, il_from)' ],
160  [ 'dropPgIndex', 'imagelinks', 'il_from' ],
161  [ 'dropFkey', 'langlinks', 'll_from' ],
162  [ 'addIndex', 'langlinks', 'langlinks_pkey', 'patch-langlinks-pk.sql' ],
163  [ 'renameIndex', 'langlinks', 'langlinks_lang_title', 'll_lang' ],
164  [ 'setDefault', 'langlinks', 'll_lang', '' ],
165  [ 'setDefault', 'langlinks', 'll_from', 0 ],
166  [ 'setDefault', 'langlinks', 'll_title', '' ],
167  [ 'changeNullableField', 'langlinks', 'll_lang', 'NOT NULL', true ],
168  [ 'changeNullableField', 'langlinks', 'll_title', 'NOT NULL', true ],
169  [ 'addIndex', 'iwlinks', 'iwlinks_pkey', 'patch-iwlinks-pk.sql' ],
170  [ 'renameIndex', 'category', 'category_title', 'cat_title' ],
171  [ 'renameIndex', 'category', 'category_pages', 'cat_pages' ],
172  [ 'dropSequence', 'watchlist_expiry', 'watchlist_expiry_we_item_seq' ],
173  [ 'changeField', 'change_tag_def', 'ctd_count', 'BIGINT', 'ctd_count::BIGINT DEFAULT 0' ],
174  [ 'dropDefault', 'change_tag_def', 'ctd_user_defined' ],
175  [ 'dropFkey', 'ipblocks_restrictions', 'ir_ipb_id' ],
176  [ 'setDefault', 'querycache', 'qc_value', 0 ],
177  [ 'changeField', 'querycache', 'qc_namespace', 'INT', 'qc_namespace::INT DEFAULT 0' ],
178  [ 'setDefault', 'querycache', 'qc_title', '' ],
179  [ 'renameIndex', 'querycache', 'querycache_type_value', 'qc_type' ],
180  [ 'renameIndex', 'querycachetwo', 'querycachetwo_type_value', 'qcc_type' ],
181  [ 'renameIndex', 'querycachetwo', 'querycachetwo_title', 'qcc_title' ],
182  [ 'renameIndex', 'querycachetwo', 'querycachetwo_titletwo', 'qcc_titletwo' ],
183  [ 'dropFkey', 'page_restrictions', 'pr_page' ],
184  [ 'addPgIndex', 'page_restrictions', 'pr_pagetype', '(pr_page, pr_type)', true ],
185  [ 'addPgIndex', 'page_restrictions', 'pr_typelevel', '(pr_type, pr_level)' ],
186  [ 'addPgIndex', 'page_restrictions', 'pr_level', '(pr_level)' ],
187  [ 'addPgIndex', 'page_restrictions', 'pr_cascade', '(pr_cascade)' ],
188  [ 'changePrimaryKey', 'page_restrictions', [ 'pr_id' ], 'page_restrictions_pk' ] ,
189  [ 'changeNullableField', 'page_restrictions', 'pr_page', 'NOT NULL', true ],
190  [ 'dropFkey', 'user_groups', 'ug_user' ],
191  [ 'setDefault', 'user_groups', 'ug_user', 0 ],
192  [ 'setDefault', 'user_groups', 'ug_group', '' ],
193  [ 'renameIndex', 'user_groups', 'user_groups_group', 'ug_group' ],
194  [ 'renameIndex', 'user_groups', 'user_groups_expiry', 'ug_expiry' ],
195  [ 'setDefault', 'querycache_info', 'qci_type', '' ],
196  [ 'setDefault', 'querycache_info', 'qci_timestamp', '1970-01-01 00:00:00+00' ],
197  [ 'changeNullableField', 'querycache_info', 'qci_type', 'NOT NULL', true ],
198  [ 'changeNullableField', 'querycache_info', 'qci_timestamp', 'NOT NULL', true ],
199  [ 'addIndex', 'querycache_info', 'querycache_info_pkey', 'patch-querycache_info-pk.sql' ],
200  [ 'setDefault', 'watchlist', 'wl_title', '' ],
201  [ 'changeField', 'watchlist', 'wl_namespace', 'INT', 'wl_namespace::INT DEFAULT 0' ],
202  [ 'dropFkey', 'watchlist', 'wl_user' ],
203  [ 'dropPgIndex', 'watchlist', 'wl_user_namespace_title' ],
204  [ 'addPgIndex', 'watchlist', 'namespace_title', '(wl_namespace, wl_title)' ],
205  [ 'checkIndex', 'wl_user', [
206  [ 'wl_user', 'text_ops', 'btree', 1 ],
207  [ 'wl_namespace', 'int4_ops', 'btree', 1 ],
208  [ 'wl_title', 'text_ops', 'btree', 1 ],
209  ],
210  'CREATE UNIQUE INDEX "wl_user" ON "watchlist" (wl_user, wl_namespace, wl_title) ' ],
211  [ 'changeField', 'sites', 'site_domain', 'VARCHAR(255)', '' ],
212  [ 'renameIndex', 'sites', 'site_global_key', 'sites_global_key' ],
213  [ 'renameIndex', 'sites', 'site_type', 'sites_type' ],
214  [ 'renameIndex', 'sites', 'site_group', 'sites_group' ],
215  [ 'renameIndex', 'sites', 'site_source', 'sites_source' ],
216  [ 'renameIndex', 'sites', 'site_language', 'sites_language' ],
217  [ 'renameIndex', 'sites', 'site_protocol', 'sites_protocol' ],
218  [ 'renameIndex', 'sites', 'site_domain', 'sites_domain' ],
219  [ 'renameIndex', 'sites', 'site_forward', 'sites_forward' ],
220  [ 'dropFkey', 'user_newtalk', 'user_id ' ],
221  [ 'renameIndex', 'user_newtalk', 'user_newtalk_id', 'un_user_id' ],
222  [ 'renameIndex', 'user_newtalk', 'user_newtalk_ip', 'un_user_ip' ],
223  [ 'changeField', 'interwiki', 'iw_prefix', 'VARCHAR(32)', '' ],
224  [ 'changeField', 'interwiki', 'iw_wikiid', 'VARCHAR(64)', '' ],
225  [ 'dropFkey', 'protected_titles', 'pt_user' ],
226  [ 'changeNullableField', 'protected_titles', 'pt_user', 'NOT NULL', true ],
227  [ 'changeNullableField', 'protected_titles', 'pt_expiry', 'NOT NULL', true ],
228  [ 'changeField', 'protected_titles', 'pt_reason_id', 'BIGINT', '' ],
229  [ 'dropDefault', 'protected_titles', 'pt_create_perm' ],
230  [ 'dropFkey', 'externallinks', 'el_from' ],
231  [ 'setDefault', 'externallinks', 'el_from', 0 ],
232  [ 'changeField', 'externallinks', 'el_index_60', 'TEXT', '' ],
233  [ 'renameIndex', 'externallinks', 'externallinks_from_to', 'el_from' ],
234  [ 'renameIndex', 'externallinks', 'externallinks_index', 'el_index' ],
235  [ 'addPgIndex', 'externallinks', 'el_to', '(el_to, el_from)' ],
236  [ 'dropSequence', 'ip_changes', 'ip_changes_ipc_rev_id_seq' ],
237  [ 'changeField', 'ip_changes', 'ipc_hex', 'TEXT', "ipc_hex::TEXT DEFAULT ''" ],
238  [ 'setDefault', 'ip_changes', 'ipc_rev_id', 0 ],
239  [ 'changeField', 'revision_comment_temp', 'revcomment_comment_id', 'BIGINT', '' ],
240  [ 'renameIndex', 'watchlist', 'namespace_title', 'wl_namespace_title' ],
241  [ 'dropFkey', 'page_props', 'pp_page' ],
242  // page_props primary key change moved from the Schema SQL file to here in 1.36
243  [ 'changePrimaryKey', 'page_props', [ 'pp_page', 'pp_propname' ], 'page_props_pk' ],
244  [ 'setDefault','job', 'job_cmd', '' ],
245  [ 'changeField', 'job', 'job_namespace', 'INTEGER', '' ],
246  [ 'dropPgIndex', 'job', 'job_cmd_namespace_title' ],
247  [ 'addPgIndex', 'job', 'job_cmd', '(job_cmd, job_namespace, job_title, job_params)' ],
248  [ 'renameIndex', 'job', 'job_timestamp_idx', 'job_timestamp' ],
249  [ 'changeField', 'slot_roles', 'role_id', 'INTEGER', '' ],
250  [ 'changeField', 'content_models', 'model_id', 'INTEGER', '' ],
251  [ 'renameIndex', 'page', 'page_len_idx', 'page_len' ],
252  [ 'renameIndex', 'page', 'page_random_idx', 'page_random' ],
253  [ 'renameIndex', 'page', 'page_unique_name', 'page_name_title' ],
254  [ 'addPGIndex', 'page', 'page_redirect_namespace_len', '(page_is_redirect, page_namespace, page_len)' ],
255  [ 'dropFkey', 'categorylinks', 'cl_from' ],
256  [ 'setDefault','categorylinks', 'cl_from', 0 ],
257  [ 'setDefault','categorylinks', 'cl_to', '' ],
258  [ 'setDefault','categorylinks', 'cl_sortkey', '' ],
259  [ 'setDefault','categorylinks', 'cl_collation', '' ],
260  [ 'changeNullableField', 'categorylinks', 'cl_sortkey', 'NOT NULL', true ],
261  [ 'addIndex', 'categorylinks', 'categorylinks_pkey', 'patch-categorylinks-pk.sql' ],
262  [ 'addPgIndex', 'categorylinks', 'cl_timestamp', '(cl_to, cl_timestamp)' ],
263  [ 'addPgIndex', 'categorylinks', 'cl_collation_ext', '(cl_collation, cl_to, cl_type, cl_from)' ],
264  [ 'checkIndex', 'cl_sortkey', [
265  [ 'cl_to', 'text_ops', 'btree', 1 ],
266  [ 'cl_type', 'text_ops', 'btree', 1 ],
267  [ 'cl_sortkey', 'text_ops', 'btree', 1 ],
268  [ 'cl_from', 'text_ops', 'btree', 1 ],
269  ],
270  'CREATE INDEX cl_sortkey ON categorylinks (cl_to, cl_type, cl_sortkey, cl_from)' ],
271  [ 'renameIndex', 'logging', 'logging_type_name', 'type_time' ],
272  [ 'renameIndex', 'logging', 'logging_actor_time_backwards', 'actor_time' ],
273  [ 'renameIndex', 'logging', 'logging_page_time', 'page_time' ],
274  [ 'renameIndex', 'logging', 'logging_times', 'times' ],
275  [ 'renameIndex', 'logging', 'logging_actor_type_time', 'log_actor_type_time' ],
276  [ 'renameIndex', 'logging', 'logging_page_id_time', 'log_page_id_time' ],
277  [ 'renameIndex', 'logging', 'logging_type_action', 'log_type_action' ],
278  [ 'changeNullableField', 'logging', 'log_params', 'NOT NULL', true ],
279  [ 'setDefault', 'logging', 'log_action', '' ],
280  [ 'setDefault', 'logging', 'log_type', '' ],
281  [ 'setDefault', 'logging', 'log_title', '' ],
282  [ 'setDefault', 'logging', 'log_timestamp', '1970-01-01 00:00:00+00' ],
283  [ 'changeField', 'logging', 'log_actor', 'BIGINT', '' ],
284  [ 'changeField', 'logging', 'log_comment_id', 'BIGINT', '' ],
285  [ 'changeField', 'logging', 'log_namespace', 'INT', 'log_namespace::INT DEFAULT 0' ],
286  [ 'dropPgIndex', 'logging', 'logging_actor_time' ],
287  [ 'changeField', 'uploadstash', 'us_key', 'VARCHAR(255)', '' ],
288  [ 'changeField', 'uploadstash', 'us_orig_path', 'VARCHAR(255)', '' ],
289  [ 'changeField', 'uploadstash', 'us_path', 'VARCHAR(255)', '' ],
290  [ 'changeField', 'uploadstash', 'us_source_type', 'VARCHAR(50)', '' ],
291  [ 'changeField', 'uploadstash', 'us_props', 'TEXT', '' ],
292  [ 'changeField', 'uploadstash', 'us_status', 'VARCHAR(50)', '' ],
293  [ 'changeField', 'uploadstash', 'us_sha1', 'VARCHAR(31)', '' ],
294  [ 'changeField', 'uploadstash', 'us_mime', 'VARCHAR(255)', '' ],
295  [ 'changeNullableField', 'uploadstash', 'us_key', 'NOT NULL', true ],
296  [ 'changeNullableField', 'uploadstash', 'us_user', 'NOT NULL', true ],
297  [ 'changeNullableField', 'uploadstash', 'us_orig_path', 'NOT NULL', true ],
298  [ 'changeNullableField', 'uploadstash', 'us_path', 'NOT NULL', true ],
299  [ 'changeNullableField', 'uploadstash', 'us_timestamp', 'NOT NULL', true ],
300  [ 'changeNullableField', 'uploadstash', 'us_status', 'NOT NULL', true ],
301  [ 'changeNullableField', 'uploadstash', 'us_size', 'NOT NULL', true ],
302  [ 'changeNullableField', 'uploadstash', 'us_sha1', 'NOT NULL', true ],
303  [ 'renameIndex', 'uploadstash', 'us_user_idx', 'us_user' ],
304  [ 'renameIndex', 'uploadstash', 'us_key_idx', 'us_key' ],
305  [ 'renameIndex', 'uploadstash', 'us_timestamp_idx', 'us_timestamp' ],
306  [ 'renameIndex', 'user_properties', 'user_properties_property', 'up_property' ],
307  [ 'renameIndex', 'sites', 'sites_global_key', 'site_global_key' ],
308  [ 'renameIndex', 'sites', 'sites_type', 'site_type' ],
309  [ 'renameIndex', 'sites', 'sites_group, ', 'site_group' ],
310  [ 'renameIndex', 'sites', 'sites_source', 'site_source' ],
311  [ 'renameIndex', 'sites', 'sites_language', 'site_language' ],
312  [ 'renameIndex', 'sites', 'sites_protocol', 'site_protocol' ],
313  [ 'renameIndex', 'sites', 'sites_domain', 'site_domain' ],
314  [ 'renameIndex', 'sites', 'sites_forward', 'site_forward' ],
315  [ 'renameIndex', 'logging', 'type_name', 'log_type_time' ],
316  [ 'renameIndex', 'logging', 'actor_time', 'log_actor_time' ],
317  [ 'renameIndex', 'logging', 'page_time', 'log_page_time' ],
318  [ 'renameIndex', 'logging', 'times', 'log_times' ],
319  [ 'setDefault', 'filearchive', 'fa_name', '' ],
320  [ 'setDefault', 'filearchive', 'fa_archive_name', '' ],
321  [ 'setDefault', 'filearchive', 'fa_storage_key', '' ],
322  [ 'dropFkey', 'filearchive', 'fa_deleted_user' ],
323  [ 'changeField', 'filearchive', 'fa_deleted_reason_id', 'BIGINT', '' ],
324  [ 'changeField', 'filearchive', 'fa_metadata', 'TEXT', '' ],
325  [ 'changeField', 'filearchive', 'fa_bits', 'INTEGER', '' ],
326  [ 'changeField', 'filearchive', 'fa_description_id', 'BIGINT', '' ],
327  [ 'changeField', 'filearchive', 'fa_actor', 'BIGINT', '' ],
328  [ 'renameIndex', 'filearchive', 'fa_name_time', 'fa_name' ],
329  [ 'renameIndex', 'filearchive', 'fa_dupe', 'fa_storage_group' ],
330  [ 'renameIndex', 'filearchive', 'fa_notime', 'fa_deleted_timestamp' ],
331  [ 'dropPgIndex', 'filearchive', 'fa_nouser' ],
332  [ 'addPgIndex', 'filearchive', 'fa_actor_timestamp', '(fa_actor, fa_timestamp)' ],
333  [ 'addPgIndex', 'ipblocks', 'ipb_expiry', '(ipb_expiry)' ],
334  [ 'addPgIndex', 'ipblocks', 'ipb_timestamp', '(ipb_timestamp)' ],
335  [ 'renameIndex', 'text', 'pagecontent_pkey', 'text_pkey' ],
336  [ 'changeNullableField', 'text', 'old_text', 'NOT NULL', true ],
337  [ 'changeNullableField', 'text', 'old_flags', 'NOT NULL', true ],
338  [ 'setDefault', 'oldimage', 'oi_name', '' ],
339  [ 'setDefault', 'oldimage', 'oi_archive_name', '' ],
340  [ 'setDefault', 'oldimage', 'oi_size', 0 ],
341  [ 'setDefault', 'oldimage', 'oi_width', 0 ],
342  [ 'setDefault', 'oldimage', 'oi_height', 0 ],
343  [ 'setDefault', 'oldimage', 'oi_bits', 0 ],
344  [ 'setDefault', 'oldimage', 'oi_name', '' ],
345  [ 'changeField', 'oldimage', 'oi_bits', 'INTEGER', '' ],
346  [ 'changeField', 'oldimage', 'oi_description_id', 'BIGINT', '' ],
347  [ 'changeField', 'oldimage', 'oi_actor', 'BIGINT', '' ],
348  [ 'changeField', 'oldimage', 'oi_metadata', 'TEXT', '' ],
349  [ 'dropDefault', 'oldimage', 'oi_metadata' ],
350  [ 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NOT NULL', true ],
351  [ 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NOT NULL', true ],
352  [ 'dropFkey', 'oldimage', 'oi_name' ],
353  [ 'addPgIndex', 'oldimage', 'oi_actor_timestamp', '(oi_actor, oi_timestamp)' ],
354  [ 'dropPgIndex', 'recentchanges', 'rc_timestamp_bot' ],
355  [ 'addPgIndex', 'recentchanges', 'rc_ns_actor', '(rc_namespace, rc_actor)' ],
356  [ 'addPgIndex', 'recentchanges', 'rc_actor', '(rc_actor, rc_timestamp)' ],
357  [ 'dropIndex', 'objectcache', 'keyname', 'patch-objectcache_keyname-pk.sql' ],
358  [ 'changeField', 'objectcache', 'value', 'TEXT', '' ],
359  [ 'changeNullableField', 'objectcache', 'value', 'NULL', true ],
360  [ 'dropFkey', 'ipblocks', 'ipb_user' ],
361  [ 'dropFkey', 'ipblocks', 'ipb_parent_block_id' ],
362  [ 'setDefault', 'ipblocks', 'ipb_user', 0 ],
363  [ 'changeNullableField', 'ipblocks', 'ipb_user', 'NOT NULL', true ],
364  [ 'changeNullableField', 'ipblocks', 'ipb_range_start', 'NOT NULL', true ],
365  [ 'changeNullableField', 'ipblocks', 'ipb_range_end', 'NOT NULL', true ],
366  [ 'changeField', 'ipblocks', 'ipb_by_actor', 'BIGINT', '' ],
367  [ 'changeField', 'ipblocks', 'ipb_reason_id', 'BIGINT', '' ],
368  [ 'renameIndex', 'archive', 'archive_name_title_timestamp', 'ar_name_title_timestamp' ],
369  [ 'dropPgIndex', 'archive', 'archive_actor' ],
370  [ 'addPgIndex', 'archive', 'ar_actor_timestamp', '(ar_actor,ar_timestamp)' ],
371  [ 'setDefault', 'image', 'img_name', '' ],
372  [ 'setDefault', 'image', 'img_size', 0 ],
373  [ 'setDefault', 'image', 'img_width', 0 ],
374  [ 'setDefault', 'image', 'img_height', 0 ],
375  [ 'setDefault', 'image', 'img_bits', 0 ],
376  [ 'changeField', 'image', 'img_bits', 'INTEGER', '' ],
377  [ 'changeField', 'image', 'img_description_id', 'BIGINT', '' ],
378  [ 'changeField', 'image', 'img_actor', 'BIGINT', '' ],
379  [ 'changeField', 'image', 'img_metadata', 'TEXT', '' ],
380  [ 'dropDefault', 'image', 'img_metadata' ],
381  [ 'changeNullableField', 'image', 'img_major_mime', 'NOT NULL', true ],
382  [ 'changeNullableField', 'image', 'img_minor_mime', 'NOT NULL', true ],
383  [ 'changeNullableField', 'image', 'img_timestamp', 'NOT NULL', true ],
384  [ 'renameIndex', 'image', 'img_size_idx', 'img_size' ],
385  [ 'renameIndex', 'image', 'img_timestamp_idx', 'img_timestamp' ],
386  [ 'addPgIndex', 'image', 'img_actor_timestamp', '(img_actor, img_timestamp)' ],
387  [ 'addPgIndex', 'image', 'img_media_mime', '(img_media_type, img_major_mime, img_minor_mime)' ],
388  [ 'renameIndex', 'site_identifiers', 'site_ids_site', 'si_site' ],
389  [ 'renameIndex', 'site_identifiers', 'site_ids_key', 'si_key' ],
390  [ 'changeField', 'recentchanges', 'rc_actor', 'BIGINT', '' ],
391  [ 'changeField', 'recentchanges', 'rc_comment_id', 'BIGINT', '' ],
392  [ 'changeField', 'recentchanges', 'rc_ip', 'TEXT', '' ],
393  [ 'changeField', 'recentchanges', 'rc_namespace', 'INTEGER', '' ],
394  [ 'setDefault', 'recentchanges', 'rc_title', '' ],
395  [ 'setDefault', 'recentchanges', 'rc_source', '' ],
396  [ 'setDefault', 'recentchanges', 'rc_ip', '' ],
397  [ 'setDefault', 'recentchanges', 'rc_namespace', 0 ],
398  [ 'setDefault', 'recentchanges', 'rc_cur_id', 0 ],
399  [ 'setDefault', 'recentchanges', 'rc_this_oldid', 0 ],
400  [ 'setDefault', 'recentchanges', 'rc_last_oldid', 0 ],
401  [ 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NOT NULL', true ],
402  [ 'changeNullableField', 'recentchanges', 'rc_ip', 'NOT NULL', true ],
403  [ 'renameIndex', 'recentchanges', 'new_name_timestamp', 'rc_new_name_timestamp', false,
404  'patch-recentchanges-rc_new_name_timestamp.sql' ],
405  [ 'changeField', 'archive', 'ar_namespace', 'INTEGER', '' ],
406  [ 'setDefault', 'archive', 'ar_namespace', 0 ],
407  [ 'setDefault', 'archive', 'ar_title', '' ],
408  [ 'changeField', 'archive', 'ar_comment_id', 'BIGINT', '' ],
409  [ 'changeField', 'archive', 'ar_actor', 'BIGINT', '' ],
410  [ 'renameIndex', 'user', 'user_email_token_idx', 'user_email_token' ],
411  [ 'addPgIndex', 'user', 'user_email', '(user_email)' ],
412  [ 'addPgIndex', 'user', 'user_name', '(user_name)', true ],
413  [ 'changeField', 'page', 'page_namespace', 'INTEGER', '' ],
414  [ 'changeNullableField', 'page', 'page_touched', 'NOT NULL', true ],
415  [ 'changeField', 'page', 'page_random', 'FLOAT', '' ],
416  [ 'renameIndex', 'revision', 'revision_unique', 'rev_page_id' ],
417  [ 'renameIndex', 'revision', 'rev_timestamp_idx', 'rev_timestamp' ],
418  [ 'addPgIndex', 'revision', 'rev_page_timestamp', '(rev_page,rev_timestamp)' ],
419  [ 'changeNullableField', 'user', 'user_touched', 'NOT NULL', true ],
420 
421  // 1.37
422  [ 'changeNullableField', 'user', 'user_token', 'NOT NULL', true ],
423  [ 'changeNullableField', 'user', 'user_real_name', 'NOT NULL', true ],
424  [ 'changeNullableField', 'user', 'user_email', 'NOT NULL', true ],
425  [ 'changeNullableField', 'user', 'user_newpassword', 'NOT NULL', true ],
426  [ 'changeNullableField', 'user', 'user_password', 'NOT NULL', true ],
427  [ 'setDefault', 'user', 'user_name', '' ],
428  [ 'setDefault', 'user', 'user_token', '' ],
429  [ 'setDefault', 'user', 'user_real_name', '' ],
430  [ 'dropConstraint', 'user', 'user_name', 'unique' ],
431  [ 'addField', 'objectcache', 'modtoken', 'patch-objectcache-modtoken.sql' ],
432  [ 'dropFkey', 'revision', 'rev_page' ],
433  [ 'changeNullableField', 'revision', 'rev_page', 'NOT NULL', true ],
434  [ 'changeField', 'revision', 'rev_comment_id', 'BIGINT', 'rev_comment_id::BIGINT DEFAULT 0' ],
435  [ 'changeField', 'revision', 'rev_actor', 'BIGINT', 'rev_actor::BIGINT DEFAULT 0' ],
436  [ 'checkIndex', 'rev_page_id', [
437  [ 'rev_page', 'int4_ops', 'btree', 1 ],
438  [ 'rev_id', 'int4_ops', 'btree', 1 ],
439  ],
440  'CREATE INDEX rev_page_id ON revision (rev_page,rev_id)' ],
441  [ 'addTable', 'searchindex', 'patch-searchindex-table.sql' ],
442  [ 'addPgIndex', 'oldimage', 'oi_timestamp', '(oi_timestamp)' ],
443  [ 'renameIndex', 'page', 'name_title', 'page_name_title' ],
444  [ 'renameIndex', 'change_tag', 'change_tag_rc_tag_id', 'ct_rc_tag_id' ],
445  [ 'renameIndex', 'change_tag', 'change_tag_log_tag_id', 'ct_log_tag_id' ],
446  [ 'renameIndex', 'change_tag', 'change_tag_rev_tag_id', 'ct_rev_tag_id' ],
447  [ 'renameIndex', 'change_tag', 'change_tag_tag_id_id', 'ct_tag_id_id' ],
448 
449  // 1.38
450  [ 'doConvertDjvuMetadata' ],
451  [ 'dropPgField', 'page_restrictions', 'pr_user' ],
452  [ 'addTable', 'linktarget', 'patch-linktarget.sql' ],
453  [ 'dropIndex', 'revision', 'rev_page_id', 'patch-drop-rev_page_id.sql' ],
454  [ 'addField', 'templatelinks', 'tl_target_id', 'patch-templatelinks-target_id.sql' ],
455 
456  // 1.39
457  [ 'addTable', 'user_autocreate_serial', 'patch-user_autocreate_serial.sql' ],
458  [ 'runMaintenance', MigrateRevisionActorTemp::class, 'maintenance/migrateRevisionActorTemp.php' ],
459  [ 'dropTable', 'revision_actor_temp' ],
460  [ 'runMaintenance', UpdateRestrictions::class, 'maintenance/updateRestrictions.php' ],
461  [ 'dropPgField', 'page', 'page_restrictions' ],
462  [ 'migrateTemplatelinks' ],
463  [ 'changeNullableField', 'templatelinks', 'tl_target_id', 'NOT NULL', true ],
464  [ 'changePrimaryKey', 'templatelinks', [ 'tl_from', 'tl_target_id' ], 'templatelinks_pk' ],
465  [ 'dropField', 'templatelinks', 'tl_title', 'patch-templatelinks-drop-tl_title.sql' ],
466 
467  // 1.40
468  [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
469  ];
470  }
471 
472  protected function describeTable( $table ) {
473  $q = <<<END
474 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
475  WHERE pg_class.relnamespace = pg_namespace.oid
476  AND attrelid=pg_class.oid AND attnum > 0
477  AND relname=%s AND nspname=%s
478 END;
479  $res = $this->db->query(
480  sprintf( $q,
481  $this->db->addQuotes( $table ),
482  $this->db->addQuotes( $this->db->getCoreSchema() )
483  ),
484  __METHOD__
485  );
486  if ( !$res ) {
487  return null;
488  }
489 
490  $cols = [];
491  foreach ( $res as $r ) {
492  $cols[] = [
493  "name" => $r[0],
494  "ord" => $r[1],
495  ];
496  }
497 
498  return $cols;
499  }
500 
501  protected function describeIndex( $idx ) {
502  // first fetch the key (which is a list of columns ords) and
503  // the table the index applies to (an oid)
504  $q = <<<END
505 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
506  WHERE nspname=%s
507  AND pg_class.relnamespace = pg_namespace.oid
508  AND relname=%s
509  AND indexrelid=pg_class.oid
510 END;
511  $res = $this->db->query(
512  sprintf(
513  $q,
514  $this->db->addQuotes( $this->db->getCoreSchema() ),
515  $this->db->addQuotes( $idx )
516  ),
517  __METHOD__
518  );
519  if ( !$res ) {
520  return null;
521  }
522  $r = $res->fetchRow();
523  if ( !$r ) {
524  return null;
525  }
526 
527  $indkey = $r[0];
528  $relid = intval( $r[1] );
529  $indkeys = explode( ' ', $indkey );
530 
531  $colnames = [];
532  foreach ( $indkeys as $rid ) {
533  $query = <<<END
534 SELECT attname FROM pg_class, pg_attribute
535  WHERE attrelid=$relid
536  AND attnum=%d
537  AND attrelid=pg_class.oid
538 END;
539  $r2 = $this->db->query( sprintf( $query, $rid ), __METHOD__ );
540  if ( !$r2 ) {
541  return null;
542  }
543  $row2 = $r2->fetchRow();
544  if ( !$row2 ) {
545  return null;
546  }
547  $colnames[] = $row2[0];
548  }
549 
550  return $colnames;
551  }
552 
553  protected function fkeyDeltype( $fkey ) {
554  $q = <<<END
555 SELECT confdeltype FROM pg_constraint, pg_namespace
556  WHERE connamespace=pg_namespace.oid
557  AND nspname=%s
558  AND conname=%s;
559 END;
560  $r = $this->db->query(
561  sprintf(
562  $q,
563  $this->db->addQuotes( $this->db->getCoreSchema() ),
564  $this->db->addQuotes( $fkey )
565  ),
566  __METHOD__
567  );
568  $row = $r->fetchRow();
569  if ( !$row ) {
570  return null;
571  }
572 
573  return $row[0];
574  }
575 
576  protected function ruleDef( $table, $rule ) {
577  $q = <<<END
578 SELECT definition FROM pg_rules
579  WHERE schemaname = %s
580  AND tablename = %s
581  AND rulename = %s
582 END;
583  $r = $this->db->query(
584  sprintf(
585  $q,
586  $this->db->addQuotes( $this->db->getCoreSchema() ),
587  $this->db->addQuotes( $table ),
588  $this->db->addQuotes( $rule )
589  ),
590  __METHOD__
591  );
592  $row = $r->fetchRow();
593  if ( !$row ) {
594  return null;
595  }
596  $d = $row[0];
597 
598  return $d;
599  }
600 
601  protected function addSequence( $table, $pkey, $ns ) {
602  if ( !$this->db->sequenceExists( $ns ) ) {
603  $this->output( "Creating sequence $ns\n" );
604  if ( $pkey !== false ) {
605  $table = $this->db->addIdentifierQuotes( $table );
606  $this->db->query( "CREATE SEQUENCE $ns OWNED BY $table.$pkey", __METHOD__ );
607  $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
608  } else {
609  $this->db->query( "CREATE SEQUENCE $ns", __METHOD__ );
610  }
611  }
612  }
613 
614  protected function dropSequence( $table, $ns ) {
615  if ( $this->db->sequenceExists( $ns ) ) {
616  $this->output( "Dropping sequence $ns\n" );
617  $this->db->query( "DROP SEQUENCE $ns CASCADE", __METHOD__ );
618  }
619  }
620 
621  protected function renameSequence( $old, $new ) {
622  if ( $this->db->sequenceExists( $new ) ) {
623  $this->output( "...sequence $new already exists.\n" );
624 
625  return;
626  }
627  if ( $this->db->sequenceExists( $old ) ) {
628  $this->output( "Renaming sequence $old to $new\n" );
629  $this->db->query( "ALTER SEQUENCE $old RENAME TO $new", __METHOD__ );
630  }
631  }
632 
633  protected function setSequenceOwner( $table, $pkey, $seq ) {
634  if ( $this->db->sequenceExists( $seq ) ) {
635  $this->output( "Setting sequence $seq owner to $table.$pkey\n" );
636  $table = $this->db->addIdentifierQuotes( $table );
637  $this->db->query( "ALTER SEQUENCE $seq OWNED BY $table.$pkey", __METHOD__ );
638  }
639  }
640 
641  protected function renameTable( $old, $new, $patch = false ) {
642  if ( $this->db->tableExists( $old, __METHOD__ ) ) {
643  $this->output( "Renaming table $old to $new\n" );
644  $old = $this->db->addIdentifierQuotes( $old );
645  $new = $this->db->addIdentifierQuotes( $new );
646  $this->db->query( "ALTER TABLE $old RENAME TO $new", __METHOD__ );
647  if ( $patch !== false ) {
648  $this->applyPatch( $patch );
649  }
650  }
651  }
652 
653  protected function renameIndex(
654  $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
655  ) {
656  // First requirement: the table must exist
657  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
658  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
659 
660  return true;
661  }
662 
663  // Second requirement: the new index must be missing
664  if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
665  $this->output( "...index $new already set on $table table.\n" );
666  if ( !$skipBothIndexExistWarning
667  && $this->db->indexExists( $table, $old, __METHOD__ )
668  ) {
669  $this->output( "...WARNING: $old still exists, despite it has been " .
670  "renamed into $new (which also exists).\n" .
671  " $old should be manually removed if not needed anymore.\n" );
672  }
673 
674  return true;
675  }
676 
677  // Third requirement: the old index must exist
678  if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
679  $this->output( "...skipping: index $old doesn't exist.\n" );
680 
681  return true;
682  }
683 
684  $this->db->query( "ALTER INDEX $old RENAME TO $new", __METHOD__ );
685  return true;
686  }
687 
688  protected function dropPgField( $table, $field ) {
689  $fi = $this->db->fieldInfo( $table, $field );
690  if ( $fi === null ) {
691  $this->output( "...$table table does not contain $field field.\n" );
692  } else {
693  $this->output( "Dropping column '$table.$field'\n" );
694  $table = $this->db->addIdentifierQuotes( $table );
695  $this->db->query( "ALTER TABLE $table DROP COLUMN $field", __METHOD__ );
696  }
697  }
698 
699  protected function addPgField( $table, $field, $type ) {
700  $fi = $this->db->fieldInfo( $table, $field );
701  if ( $fi !== null ) {
702  $this->output( "...column '$table.$field' already exists\n" );
703  } else {
704  $this->output( "Adding column '$table.$field'\n" );
705  $table = $this->db->addIdentifierQuotes( $table );
706  $this->db->query( "ALTER TABLE $table ADD $field $type", __METHOD__ );
707  }
708  }
709 
710  protected function changeField( $table, $field, $newtype, $default ) {
711  $fi = $this->db->fieldInfo( $table, $field );
712  if ( $fi === null ) {
713  $this->output( "...ERROR: expected column $table.$field to exist\n" );
714  exit( 1 );
715  }
716 
717  if ( $fi->type() === strtolower( $newtype ) ) {
718  $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
719  } else {
720  $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
721  $table = $this->db->addIdentifierQuotes( $table );
722  $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
723  if ( strlen( $default ) ) {
724  $res = [];
725  if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
726  $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
727  $this->db->query( $sqldef, __METHOD__ );
728  $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
729  }
730  $sql .= " USING $default";
731  }
732  $this->db->query( $sql, __METHOD__ );
733  }
734  }
735 
736  protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
737  # # For a cache table, empty it if the field needs to be changed, because the old contents
738  # # may be corrupted. If the column is already the desired type, refrain from purging.
739  $fi = $this->db->fieldInfo( $table, $field );
740  if ( $fi === null ) {
741  $this->output( "...ERROR: expected column $table.$field to exist\n" );
742  exit( 1 );
743  }
744 
745  if ( $fi->type() === $newtype ) {
746  $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
747  } else {
748  $this->output( "Purging data from cache table '$table'\n" );
749  $table = $this->db->addIdentifierQuotes( $table );
750  $this->db->query( "DELETE from $table", __METHOD__ );
751  $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
752  $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
753  if ( strlen( $default ) ) {
754  $res = [];
755  if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
756  $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
757  $this->db->query( $sqldef, __METHOD__ );
758  $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
759  }
760  $sql .= " USING $default";
761  }
762  $this->db->query( $sql, __METHOD__ );
763  }
764  }
765 
766  protected function setDefault( $table, $field, $default ) {
767  $info = $this->db->fieldInfo( $table, $field );
768  if ( $info && $info->defaultValue() !== $default ) {
769  $this->output( "Changing '$table.$field' default value\n" );
770  $table = $this->db->addIdentifierQuotes( $table );
771  $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT "
772  . $this->db->addQuotes( $default ), __METHOD__ );
773  }
774  }
775 
782  protected function dropDefault( $table, $field ) {
783  $info = $this->db->fieldInfo( $table, $field );
784  if ( $info->defaultValue() !== false ) {
785  $this->output( "Removing '$table.$field' default value\n" );
786  $table = $this->db->addIdentifierQuotes( $table );
787  $this->db->query( "ALTER TABLE $table ALTER $field DROP DEFAULT", __METHOD__ );
788  }
789  }
790 
791  protected function changeNullableField( $table, $field, $null, $update = false ) {
792  $fi = $this->db->fieldInfo( $table, $field );
793  if ( $fi === null ) {
794  return;
795  }
796  if ( $fi->isNullable() ) {
797  # # It's NULL - does it need to be NOT NULL?
798  if ( $null === 'NOT NULL' ) {
799  $this->output( "Changing '$table.$field' to not allow NULLs\n" );
800  $table = $this->db->addIdentifierQuotes( $table );
801  if ( $update ) {
802  $this->db->query( "UPDATE $table SET $field = DEFAULT WHERE $field IS NULL", __METHOD__ );
803  }
804  $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL", __METHOD__ );
805  } else {
806  $this->output( "...column '$table.$field' is already set as NULL\n" );
807  }
808  } else {
809  # # It's NOT NULL - does it need to be NULL?
810  if ( $null === 'NULL' ) {
811  $this->output( "Changing '$table.$field' to allow NULLs\n" );
812  $table = $this->db->addIdentifierQuotes( $table );
813  $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL", __METHOD__ );
814  } else {
815  $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
816  }
817  }
818  }
819 
820  protected function addPgIndex( $table, $index, $type, $unique = false ) {
821  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
822  $this->output( "...index '$index' on table '$table' already exists\n" );
823  } else {
824  $this->output( "Creating index '$index' on table '$table' $type\n" );
825  $table = $this->db->addIdentifierQuotes( $table );
826  $unique = $unique ? 'UNIQUE' : '';
827  $this->db->query( "CREATE $unique INDEX $index ON $table $type", __METHOD__ );
828  }
829  }
830 
831  protected function addPgExtIndex( $table, $index, $type ) {
832  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
833  $this->output( "...index '$index' on table '$table' already exists\n" );
834  } elseif ( preg_match( '/^\‍(/', $type ) ) {
835  $this->output( "Creating index '$index' on table '$table'\n" );
836  $table = $this->db->addIdentifierQuotes( $table );
837  $this->db->query( "CREATE INDEX $index ON $table $type", __METHOD__ );
838  } else {
839  $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
840  }
841  }
842 
849  protected function addPgEnumValue( $type, $value ) {
850  $row = $this->db->selectRow(
851  [
852  't' => 'pg_catalog.pg_type',
853  'n' => 'pg_catalog.pg_namespace',
854  'e' => 'pg_catalog.pg_enum',
855  ],
856  [ 't.typname', 't.typtype', 'e.enumlabel' ],
857  [
858  't.typname' => $type,
859  'n.nspname' => $this->db->getCoreSchema(),
860  ],
861  __METHOD__,
862  [],
863  [
864  'n' => [ 'JOIN', 't.typnamespace = n.oid' ],
865  'e' => [ 'LEFT JOIN', [ 'e.enumtypid = t.oid', 'e.enumlabel' => $value ] ],
866  ]
867  );
868 
869  if ( !$row ) {
870  $this->output( "...Type $type does not exist, skipping modify enum.\n" );
871  } elseif ( $row->typtype !== 'e' ) {
872  $this->output( "...Type $type does not seem to be an enum, skipping modify enum.\n" );
873  } elseif ( $row->enumlabel === $value ) {
874  $this->output( "...Enum type $type already contains value '$value'.\n" );
875  } else {
876  $this->output( "...Adding value '$value' to enum type $type.\n" );
877  $etype = $this->db->addIdentifierQuotes( $type );
878  $evalue = $this->db->addQuotes( $value );
879  $this->db->query( "ALTER TYPE $etype ADD VALUE $evalue", __METHOD__ );
880  }
881  }
882 
883  protected function dropFkey( $table, $field ) {
884  $fi = $this->db->fieldInfo( $table, $field );
885  if ( $fi === null ) {
886  $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
887  "Please report this.\n" );
888  return;
889  }
890 
891  if ( $this->dropConstraint( $table, $field, 'foreignkey', $fi->conname() ) ) {
892  $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
893  } else {
894  $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
895  }
896  }
897 
898  protected function changeFkeyDeferrable( $table, $field, $clause ) {
899  $fi = $this->db->fieldInfo( $table, $field );
900  if ( $fi === null ) {
901  $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
902  "Please report this.\n" );
903 
904  return;
905  }
906  if ( $fi->is_deferred() && $fi->is_deferrable() ) {
907  return;
908  }
909  $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
910 
911  $conname = $fi->conname();
912  $conclause = "CONSTRAINT \"$conname\"";
913 
914  if ( !$this->dropConstraint( $table, $field, 'foreignkey', $conname ) ) {
915  $this->output( "Column '$table.$field' does not have a foreign key " .
916  "constraint, will be added\n" );
917  $conclause = "";
918  }
919 
920  $command =
921  "ALTER TABLE $table ADD $conclause " .
922  "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
923  $this->db->query( $command, __METHOD__ );
924  }
925 
926  protected function dropPgIndex( $table, $index ) {
927  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
928  $this->output( "Dropping obsolete index '$index'\n" );
929  $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
930  }
931  }
932 
933  protected function checkIndex( $index, $should_be, $good_def ) {
934  $pu = $this->db->indexAttributes( $index );
935  if ( !empty( $pu ) && $pu != $should_be ) {
936  $this->output( "Dropping obsolete version of index '$index'\n" );
937  $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
938  $pu = [];
939  } else {
940  $this->output( "...no need to drop index '$index'\n" );
941  }
942 
943  if ( empty( $pu ) ) {
944  $this->output( "Creating index '$index'\n" );
945  $this->db->query( $good_def, __METHOD__ );
946  } else {
947  $this->output( "...index '$index' exists\n" );
948  }
949  }
950 
951  protected function changePrimaryKey( $table, $shouldBe, $constraintName = null ) {
952  // https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
953  $result = $this->db->query(
954  "SELECT a.attname as column " .
955  "FROM pg_index i " .
956  "JOIN pg_attribute a ON a.attrelid = i.indrelid " .
957  "AND a.attnum = ANY(i.indkey) " .
958  "WHERE i.indrelid = '\"$table\"'::regclass " .
959  "AND i.indisprimary",
960  __METHOD__
961  );
962  $currentColumns = [];
963  foreach ( $result as $row ) {
964  $currentColumns[] = $row->column;
965  }
966 
967  if ( $currentColumns == $shouldBe ) {
968  $this->output( "...no need to change primary key of '$table'\n" );
969  return true;
970  }
971 
972  $this->dropConstraint( $table, '', 'primary', $constraintName );
973 
974  $table = $this->db->addIdentifierQuotes( $table );
975  $this->db->query(
976  "ALTER TABLE $table" .
977  " ADD PRIMARY KEY (" . implode( ',', $shouldBe ) . ');',
978  __METHOD__
979  );
980  }
981 
993  protected function dropConstraint( $table, $field, $type, $conname = null ) {
994  if ( $conname === null ) {
995  if ( $type == 'primary' ) {
996  $conname = "{$table}_pkey";
997  } else {
998  $map = [ 'unique' => 'key', 'check' => 'check', 'foreignkey' => 'fkey' ];
999  $conname = "{$table}_{$field}_{$map[$type]}";
1000  }
1001  }
1002 
1003  if ( $this->db->constraintExists( $table, $conname ) ) {
1004  $table = $this->db->addIdentifierQuotes( $table );
1005  $this->db->query(
1006  "ALTER TABLE $table DROP CONSTRAINT $conname;",
1007  __METHOD__
1008  );
1009 
1010  return true;
1011  }
1012 
1013  return false;
1014  }
1015 }
Class for handling database updates.
output( $str)
Output some text.
applyPatch( $path, $isFullPath=false, $msg=null)
Applies a SQL patch.
Class for handling updates to Postgres databases.
changeNullableField( $table, $field, $null, $update=false)
changePrimaryKey( $table, $shouldBe, $constraintName=null)
changeField( $table, $field, $newtype, $default)
renameTable( $old, $new, $patch=false)
renameIndex( $table, $old, $new, $skipBothIndexExistWarning=false, $a=false, $b=false)
Rename an index from an existing table.
dropDefault( $table, $field)
Drop a default value from a field.
ruleDef( $table, $rule)
dropSequence( $table, $ns)
addPgExtIndex( $table, $index, $type)
addSequence( $table, $pkey, $ns)
addPgEnumValue( $type, $value)
Add a value to an existing PostgreSQL enum type.
dropPgField( $table, $field)
renameSequence( $old, $new)
dropPgIndex( $table, $index)
setDefault( $table, $field, $default)
DatabasePostgres $db
addPgField( $table, $field, $type)
setSequenceOwner( $table, $pkey, $seq)
dropFkey( $table, $field)
dropConstraint( $table, $field, $type, $conname=null)
Drop generic constraint.
changeFkeyDeferrable( $table, $field, $clause)
checkIndex( $index, $should_be, $good_def)
changeFieldPurgeTable( $table, $field, $newtype, $default)
addPgIndex( $table, $index, $type, $unique=false)
Postgres database abstraction layer.
$command
Definition: mcc.php:125
return true
Definition: router.php:90