From debbf2f059f8dd4a1571168f56ef75929239ece8 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.afi-sa.local>
Date: Fri, 12 Aug 2016 13:08:18 +0200
Subject: [PATCH] hotline 46627 remove old index in notices_paniers

---
 cosmogramme/sql/patch/patch_304.php |  6 ++++
 tests/db/UpgradeDBTest.php          | 48 +++++++++++++++++++++++++++--
 2 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 cosmogramme/sql/patch/patch_304.php

diff --git a/cosmogramme/sql/patch/patch_304.php b/cosmogramme/sql/patch/patch_304.php
new file mode 100644
index 00000000000..658c7cbb069
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_304.php
@@ -0,0 +1,6 @@
+<?php
+$adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
+
+try {
+  $adapter->query('ALTER TABLE notices_paniers DROP INDEX user_panier, DROP COLUMN id_panier');
+} catch(Exception $e) {}
diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php
index 626e0e54053..5c0dcaca962 100644
--- a/tests/db/UpgradeDBTest.php
+++ b/tests/db/UpgradeDBTest.php
@@ -129,7 +129,7 @@ abstract class UpgradeDBTestCase extends PHPUnit_Framework_TestCase {
   protected function assertIndex($table, $name, $type, $message='') {
     try {
       foreach($this->query(sprintf('show keys in `%s`', $table))->fetchAll() as $row) {
-         if ($name == $row['Key_name'] && $type == $row['Index_type'])
+        if ($name == $row['Key_name'] && $type == $row['Index_type'])
           return true;
       }
     } catch (Exception $e) {}
@@ -143,6 +143,25 @@ abstract class UpgradeDBTestCase extends PHPUnit_Framework_TestCase {
   }
 
 
+  protected function assertNotIndex($table, $name, $message = '') {
+    $message = $message
+      ? $message
+      : sprintf('Failed asserting that "%s" table NOT CONTAINS index "%s" .',
+                $table, $name);
+
+    try {
+      foreach($this->query(sprintf('show keys in `%s`', $table))->fetchAll() as $row) {
+        if ($name == $row['Key_name'])
+          throw new Exception();
+      }
+    } catch (Exception $e) {
+      $this->fail($message);
+    }
+
+    return true;
+  }
+
+
   protected function assertFieldType($table, $name, $type, $message='') {
     return $this->assertField($table, $name, $type, 'Type', $message);
   }
@@ -683,7 +702,7 @@ class UpgradeDB_295_Test extends UpgradeDBTestCase {
 class UpgradeDB_296_Test extends UpgradeDBTestCase {
   public function prepare() {
     try {
-    $this->query("ALTER TABLE `ouvertures` MODIFY `jour` date NOT NULL, DROP INDEX `id_site`");
+      $this->query("ALTER TABLE `ouvertures` MODIFY `jour` date NOT NULL, DROP INDEX `id_site`");
     } catch(Exception $e) {}
   }
 
@@ -837,4 +856,29 @@ class UpgradeDB_303_Test extends UpgradeDBTestCase {
   public function locationIdShouldBeIndexed() {
     $this->assertIndex('bib_c_site', 'id_lieu', 'BTREE');
   }
+}
+
+
+
+class UpgradeDB_304_Test extends UpgradeDBTestCase {
+  public function prepare() {
+    try {
+      $this->query('ALTER TABLE notices_paniers ADD COLUMN id_panier int(11) NOT NULL');
+      $this->query('UPDATE notices_paniers SET id_panier = id');
+      $this->query('ALTER TABLE notices_paniers ADD CONSTRAINT user_panier UNIQUE (id_user, id_panier)');
+    } catch(Exception $e) {
+    }
+  }
+
+
+  /** @test */
+  public function columnIdPanierShouldNotExist() {
+    $this->assertNotColumn('notices_paniers', 'id_panier');
+  }
+
+
+  /** @test */
+  public function uniqueIndexUserPanierShouldNotExist() {
+    $this->assertNotIndex('notices_paniers', 'user_panier');
+  }
 }
\ No newline at end of file
-- 
GitLab