From 6357341379a5474d108e80117dac2b39c946862e Mon Sep 17 00:00:00 2001
From: Heiko Henning <h.henning@educa.ch>
Date: Fri, 6 May 2011 09:37:47 +0200
Subject: [PATCH] pgsql schema: fix issu of mysql key length

---
 includes/database/pgsql/schema.inc |   26 ++++++++++++++++++++------
 1 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/includes/database/pgsql/schema.inc b/includes/database/pgsql/schema.inc
index 4ceddcb..61ce2a5 100644
--- a/includes/database/pgsql/schema.inc
+++ b/includes/database/pgsql/schema.inc
@@ -125,11 +125,11 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
 
     $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->createKeysSqlHelper($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->createKeysSqlHelper($key) . ')';
       }
     }
 
@@ -159,7 +159,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
         $statements[] = 'COMMENT ON COLUMN {' . $name . '}.' . $field_name . ' IS ' . $this->prepareComment($field['description']);
       }
     }
-
+    file_put_contents('/tmp/create.sql', implode("\n", $statements)."\n\n", FILE_APPEND);
     return $statements;
   }
 
@@ -299,7 +299,21 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
       );
     return $map;
   }
-
+  
+  protected function createKeysSqlHelper($fields) {
+    $return = array(); 
+    file_put_contents('/tmp/foo.sql', print_r($fields, true), FILE_APPEND);
+    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) {
@@ -442,7 +456,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
       throw new DatabaseSchemaObjectExistsException(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->createKeysSqlHelper($fields) . ')');
   }
 
   public function dropPrimaryKey($table) {
@@ -462,7 +476,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
       throw new DatabaseSchemaObjectExistsException(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->createKeysSqlHelper($fields) . ')');
   }
 
   public function dropUniqueKey($table, $name) {
-- 
1.7.0.4

