From 3f19cc38001085d9e75456fe68e63d30c4e546d8 Mon Sep 17 00:00:00 2001
From: Ben Coleman <b.coleman@accelerateddesign.com>
Date: Sun, 20 Oct 2013 14:40:19 -0400
Subject: [PATCH] Redo patches for key length

---
 .../Drupal/Core/Database/Driver/pgsql/Schema.php   | 29 +++++++++++++++++++---
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
index 8b4f913..0734278 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
@@ -130,11 +130,11 @@ protected function createTableSql($name, $table) {
 
     $sql_keys = array();
     if (isset($table['primary key']) && is_array($table['primary key'])) {
-      $sql_keys[] = 'PRIMARY KEY (' . implode(', ', $table['primary key']) . ')';
+      $sql_keys[] = 'PRIMARY KEY (' . $this->_createPrimaryKeySql($table['primary key']) . ')';
     }
     if (isset($table['unique keys']) && is_array($table['unique keys'])) {
       foreach ($table['unique keys'] as $key_name => $key) {
-        $sql_keys[] = 'CONSTRAINT ' . $this->prefixNonTable($name, $key_name, 'key') . ' UNIQUE (' . implode(', ', $key) . ')';
+        $sql_keys[] = 'CONSTRAINT ' . $this->prefixNonTable($name, $key_name, 'key') . ' UNIQUE (' . $this->_createKeySql($key) . ')';
       }
     }
 
@@ -305,6 +305,27 @@ function getFieldTypeMap() {
     return $map;
   }
 
+  /**
+   * Create an SQL string for a field to be used in primary key
+   * constraints.
+   *
+   * Any specification of length will be ignored since PostgreSQL
+   * supports out of the box indexing of text fields.
+   * Use _createKeySql() instead for unique key constraints.
+   */
+  protected function _createPrimaryKeySql($fields) {
+    $return = array();
+    foreach ($fields as $field) {
+      if (is_array($field)) {
+        $return[] = $field[0];
+      }
+      else {
+        $return[] = $field;
+      }
+    }
+    return implode(', ', $return);
+  }
+
   protected function _createKeySql($fields) {
     $return = array();
     foreach ($fields as $field) {
@@ -457,7 +478,7 @@ public function addPrimaryKey($table, $fields) {
       throw new SchemaObjectExistsException(t("Cannot add primary key to table @table: primary key already exists.", array('@table' => $table)));
     }
 
-    $this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')');
+    $this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->_createPrimaryKeySql($fields) . ')');
   }
 
   public function dropPrimaryKey($table) {
@@ -477,7 +498,7 @@ function addUniqueKey($table, $name, $fields) {
       throw new SchemaObjectExistsException(t("Cannot add unique key @name to table @table: unique key already exists.", array('@table' => $table, '@name' => $name)));
     }
 
-    $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $this->prefixNonTable($table, $name, 'key') . '" UNIQUE (' . implode(',', $fields) . ')');
+    $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $this->prefixNonTable($table, $name, 'key') . '" UNIQUE (' . $this->_createKeySql($fields) . ')');
   }
 
   public function dropUniqueKey($table, $name) {
-- 
1.8.1.2

