From 318b62782d49b68724ea11183f881335d08cdaac Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 9 Feb 2016 19:21:50 +0100
Subject: [PATCH] dev #32668: add index on custom field value

---
 cosmogramme/php/_init.php                 |  2 +-
 cosmogramme/sql/patch/patch_287.php       |  7 +++++++
 cosmogramme/sql/patch/patch_287.sql       |  7 +++++++
 tests/db/UpgradeDBTest.php                | 18 ++++++++++++++++++
 tests/library/Class/ArticleLoaderTest.php |  8 ++++++++
 5 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 cosmogramme/sql/patch/patch_287.php
 create mode 100644 cosmogramme/sql/patch/patch_287.sql

diff --git a/cosmogramme/php/_init.php b/cosmogramme/php/_init.php
index 783024abbb2..0f5feab41d9 100644
--- a/cosmogramme/php/_init.php
+++ b/cosmogramme/php/_init.php
@@ -1,7 +1,7 @@
 <?php
 error_reporting(E_ERROR | E_PARSE);
 
-define("PATCH_LEVEL","286");
+define("PATCH_LEVEL","287");
 
 define("APPLI","cosmogramme");
 define("COSMOPATH", "/var/www/html/vhosts/opac2/www/htdocs");
diff --git a/cosmogramme/sql/patch/patch_287.php b/cosmogramme/sql/patch/patch_287.php
new file mode 100644
index 00000000000..be89ca51784
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_287.php
@@ -0,0 +1,7 @@
+<?php
+$adapter = Zend_Db_Table::getDefaultAdapter();
+
+try {
+  $adapter->query(' alter table custom_field_values add index (value(50))');
+} catch(Exception $e) {}
+?>
diff --git a/cosmogramme/sql/patch/patch_287.sql b/cosmogramme/sql/patch/patch_287.sql
new file mode 100644
index 00000000000..d9a88ffb45e
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_287.sql
@@ -0,0 +1,7 @@
+<?php
+$adapter = Zend_Db_Table::getDefaultAdapter();
+
+try {
+  $adapter->query('alter table custom_field_values add index (value(50));');
+} catch(Exception $e) {}
+?>
diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php
index daba6f5e222..4f2ba0e77cb 100644
--- a/tests/db/UpgradeDBTest.php
+++ b/tests/db/UpgradeDBTest.php
@@ -443,4 +443,22 @@ class UpgradeDB_286_Test extends UpgradeDBTestCase {
   public function typeDocShouldExist() {
     $this->assertColumn('notices_avis', 'type_doc');
   }
+}
+
+
+class UpgradeDB_287_Test extends UpgradeDBTestCase {
+
+  public function prepare() {
+    try {
+      $this->query(' drop index value on custom_field_values');
+    } catch(Exception $e) {}
+  }
+
+
+  /** @test */
+  public function indexOnCustomFieldShouldExist() {
+    $this->assertNotNull($this->query("SELECT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS WHERE `TABLE_SCHEMA` = DATABASE() AND `TABLE_NAME` = 'custom_field_values' AND `INDEX_NAME` ='value';"));
+
+  }
+
 }
\ No newline at end of file
diff --git a/tests/library/Class/ArticleLoaderTest.php b/tests/library/Class/ArticleLoaderTest.php
index 965d7a70424..64664a53c11 100644
--- a/tests/library/Class/ArticleLoaderTest.php
+++ b/tests/library/Class/ArticleLoaderTest.php
@@ -248,6 +248,14 @@ class ArticleLoaderGetArticlesByPreferencesTest extends ModelTestCase {
 
   /** @test */
   function withArticleAndCustomFieldValueSelectionSqlShouldFilterByCustomField() {
+    $this->fixture('Class_CustomField',
+                   ['id' => 5,
+                    'priority' => 3,
+                    'label' => 'Options',
+                    'field_type' => Class_CustomField_Meta::MULTI_CHECKBOX,
+                    'options_list' => 'wifi;jardinage;projection',
+                    'model' => 'Article']);
+
     $articles = $this->getArticles(array('display_order' => 'EventDebut',
                                          'custom_fields' => [5 => 'value1',
                                                              12 => 'value2'],
-- 
GitLab