Add SQL for postgres, and fail gracefully in populateIpChanges
[lhc/web/wiklou.git] / includes / installer / PostgresUpdater.php
index e5a5c94..c38eb6a 100644 (file)
@@ -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', '' ],
@@ -481,6 +481,8 @@ class PostgresUpdater extends DatabaseUpdater {
                        [ 'changeNullableField', 'protected_titles', 'pt_reason', 'NOT NULL', true ],
                        [ 'addPgField', 'protected_titles', 'pt_reason_id', 'INTEGER NOT NULL DEFAULT 0' ],
                        [ 'addTable', 'comment', 'patch-comment-table.sql' ],
+                       [ 'addIndex', 'site_stats', 'site_stats_pkey', 'patch-site_stats-pk.sql' ],
+                       [ 'addTable', 'ip_changes', 'patch-ip_changes.sql' ],
                ];
        }
 
@@ -783,7 +785,8 @@ END;
                $info = $this->db->fieldInfo( $table, $field );
                if ( $info->defaultValue() !== $default ) {
                        $this->output( "Changing '$table.$field' default value\n" );
-                       $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT " . $default );
+                       $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT "
+                               . $this->db->addQuotes( $default ) );
                }
        }
 
@@ -837,6 +840,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 ) ) {