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