PostgreSQL: Drop unneeded foreign key constraint
authorJeff Janes <jeff.janes@gmail.com>
Mon, 8 Dec 2014 20:27:11 +0000 (12:27 -0800)
committerKunal Mehta <legoktm@gmail.com>
Fri, 30 Jan 2015 21:15:29 +0000 (13:15 -0800)
Change I1c7f3a84f10df05d6b37dccbad4c8232edf51580 causes
an existing foreign key assumption (under PostgreSQL) to be
violated upon deleting a page.  This foreign key assumption does not
explicitly exist in MySQL, and is not implied via documentation.  So
it was probably never needed in the first place.

Don't create the foreign key constraint in PostgreSQL, and drop it
if it already exists when running update.php.

The constraint was previously created with an implicit name, so
drop the constraint involving the specified column name (rc_cur_id),
rather than hard-coding the name of the constraint itself.

This bug probably exists under Oracle and MSSQL as well, but no attempt
was made to address it there.

Bug: T76254
Change-Id: I2abd650c8ce83c5b725aec0545fff14a927a305a

includes/installer/PostgresUpdater.php
maintenance/postgres/tables.sql

index bc25455..9e41276 100644 (file)
@@ -423,6 +423,7 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'dropTable', 'hitcounter' ),
                        array( 'dropField', 'site_stats', 'ss_total_views', 'patch-drop-ss_total_views.sql' ),
                        array( 'dropField', 'page', 'page_counter', 'patch-drop-page_counter.sql' ),
+                       array( 'dropFkey', 'recentchanges', 'rc_cur_id' )
                );
        }
 
@@ -774,6 +775,24 @@ END;
                }
        }
 
+       protected function dropFkey( $table, $field ) {
+               $fi = $this->db->fieldInfo( $table, $field );
+               if ( is_null( $fi ) ) {
+                       $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
+                               "Please report this.\n" );
+                       return;
+               }
+               $conname = $fi->conname();
+               if ( $fi->conname() ) {
+                       $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
+                       $conclause = "CONSTRAINT \"$conname\"";
+                       $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
+                       $this->db->query( $command );
+               } else {
+                       $this->output( "Foreign key constraint on '$table.$field' already does not exist\n" );
+               };
+       }
+
        protected function changeFkeyDeferrable( $table, $field, $clause ) {
                $fi = $this->db->fieldInfo( $table, $field );
                if ( is_null( $fi ) ) {
index 5391bd9..6076206 100644 (file)
@@ -415,7 +415,7 @@ CREATE TABLE recentchanges (
   rc_minor           SMALLINT     NOT NULL  DEFAULT 0,
   rc_bot             SMALLINT     NOT NULL  DEFAULT 0,
   rc_new             SMALLINT     NOT NULL  DEFAULT 0,
-  rc_cur_id          INTEGER          NULL  REFERENCES page(page_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
+  rc_cur_id          INTEGER          NULL,
   rc_this_oldid      INTEGER      NOT NULL,
   rc_last_oldid      INTEGER      NOT NULL,
   rc_type            SMALLINT     NOT NULL  DEFAULT 0,