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