From: Brad Jorsch Date: Fri, 15 Sep 2017 16:12:21 +0000 (-0400) Subject: Fix PostgreSQL patch-add-3d.sql by replacing it X-Git-Tag: 1.31.0-rc.0~1806^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=5ba7fb251e0ac09f3171f41320c2c1b60b8b983c Fix PostgreSQL patch-add-3d.sql by replacing it Follows-up 6260545fee, a2f5d05ae8. (T157348) If updates are run for the first time on an installation that already has '3D' in the enum (e.g. because it's a fresh install), the update fails. Instead of blindly running a patch file, we instead add a method that checks whether the enum type already contains the value before adding it. Bug: T177417 Change-Id: Iad10cb88cf1cb35cfb95ce98a556b33688158a88 --- diff --git a/includes/installer/PostgresUpdater.php b/includes/installer/PostgresUpdater.php index 07aeb13d1d..0475fe4078 100644 --- a/includes/installer/PostgresUpdater.php +++ b/includes/installer/PostgresUpdater.php @@ -454,7 +454,7 @@ class PostgresUpdater extends DatabaseUpdater { [ 'addPgIndex', 'user_groups', 'user_groups_expiry', '( ug_expiry )' ], // 1.30 - [ 'modifyField', 'image', 'img_media_type', 'patch-add-3d.sql' ], + [ 'addPgEnumValue', 'media_type', '3D' ], [ 'setDefault', 'revision', 'rev_comment', '' ], [ 'changeNullableField', 'revision', 'rev_comment', 'NOT NULL', true ], [ 'setDefault', 'archive', 'ar_comment', '' ], @@ -838,6 +838,46 @@ END; } } + /** + * Add a value to an existing PostgreSQL enum type + * @since 1.31 + * @param string $type Type name. Must be in the core schema. + * @param string $value Value to add. + */ + public function addPgEnumValue( $type, $value ) { + $row = $this->db->selectRow( + [ + 't' => 'pg_catalog.pg_type', + 'n' => 'pg_catalog.pg_namespace', + 'e' => 'pg_catalog.pg_enum', + ], + [ 't.typname', 't.typtype', 'e.enumlabel' ], + [ + 't.typname' => $type, + 'n.nspname' => $this->db->getCoreSchema(), + ], + __METHOD__, + [], + [ + 'n' => [ 'JOIN', 't.typnamespace = n.oid' ], + 'e' => [ 'LEFT JOIN', [ 'e.enumtypid = t.oid', 'e.enumlabel' => $value ] ], + ] + ); + + if ( !$row ) { + $this->output( "...Type $type does not exist, skipping modify enum.\n" ); + } elseif ( $row->typtype !== 'e' ) { + $this->output( "...Type $type does not seem to be an enum, skipping modify enum.\n" ); + } elseif ( $row->enumlabel === $value ) { + $this->output( "...Enum type $type already contains value '$value'.\n" ); + } else { + $this->output( "...Adding value '$value' to enum type $type.\n" ); + $etype = $this->db->addIdentifierQuotes( $type ); + $evalue = $this->db->addQuotes( $value ); + $this->db->query( "ALTER TYPE $etype ADD VALUE $evalue" ); + } + } + protected function dropFkey( $table, $field ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { diff --git a/maintenance/postgres/archives/patch-add-3d.sql b/maintenance/postgres/archives/patch-add-3d.sql deleted file mode 100644 index f8927557c3..0000000000 --- a/maintenance/postgres/archives/patch-add-3d.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TYPE media_type ADD VALUE '3D';