]> git.agnieray.net Git - galette.git/commitdiff
Fix issues on dynamics table removal
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 13 Sep 2020 05:47:55 +0000 (07:47 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Sun, 13 Sep 2020 05:48:40 +0000 (07:48 +0200)
galette/lib/Galette/Controllers/CrudController.php
galette/lib/Galette/Core/Db.php
galette/lib/Galette/DynamicFields/DynamicField.php
galette/lib/Galette/Repository/Reminders.php
tests/Galette/Core/tests/units/Db.php
tests/Galette/IO/tests/units/CsvIn.php

index dda1e2e7202b1eae73768bee95ddd4d77b3394cf..410a75920fd9bbfe150530485ce37c4e9ef6d9aa 100644 (file)
@@ -278,7 +278,6 @@ abstract class CrudController extends AbstractController
             );
         } else {
             try {
-                $this->zdb->connection->beginTransaction();
 
                 $ids = $this->getIdsToRemove($args, $post);
 
@@ -290,9 +289,7 @@ abstract class CrudController extends AbstractController
                     );
                     $success = true;
                 }
-                $this->zdb->connection->commit();
             } catch (\Exception $e) {
-                $this->zdb->connection->rollBack();
                 Analog::log(
                     'An error occurred on delete | ' . $e->getMessage(),
                     Analog::ERROR
index 35aeeb81e2205eb28ddb3a3e8998845110c260d8..e43d513790b286dd4b5ee891ada24d1c5ff601ac 100644 (file)
@@ -947,4 +947,25 @@ class Db
             )
         ;
     }
+
+    /**
+     * Drops a table
+     *
+     * @param string  $table   Table name, without prefix
+     * @param boolean $maymiss Whether the table can be missing, defaults to false
+     *
+     * @return void
+     */
+    public function drop($table, $maymiss = false)
+    {
+        $sql = 'DROP TABLE ';
+        if ($maymiss === true) {
+            $sql .= 'IF EXISTS ';
+        }
+        $sql .= PREFIX_DB . $table;
+        $this->db->query(
+            $sql,
+            \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
+        );
+    }
 }
index 4b2523b938909b48d8742a9337ae0650e8613320..888c4fdea93a4ed29413b7d646b8ae93ba5e0bab 100644 (file)
@@ -749,11 +749,8 @@ abstract class DynamicField
             $contents_table = self::getFixedValuesTableName($this->id, true);
 
             try {
+                $this->zdb->drop($contents_table, true);
                 $this->zdb->connection->beginTransaction();
-                $this->zdb->db->query(
-                    'DROP TABLE IF EXISTS ' . $contents_table,
-                    \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
-                );
                 $field_size = ((int)$this->size > 0) ? $this->size : 1;
                 $this->zdb->db->query(
                     'CREATE TABLE ' . $contents_table .
@@ -763,7 +760,10 @@ abstract class DynamicField
                 );
                 $this->zdb->connection->commit();
             } catch (\Exception $e) {
-                $this->zdb->connection->rollBack();
+                if ($this->zdb->connection->inTransaction()) {
+                    //because of DROP autocommit on mysql...
+                    $this->zdb->connection->rollBack();
+                }
                 Analog::log(
                     'Unable to manage fields values table ' .
                     $contents_table . ' | ' . $e->getMessage(),
@@ -938,6 +938,11 @@ abstract class DynamicField
     public function remove()
     {
         try {
+            if ($this->hasFixedValues()) {
+                $contents_table = self::getFixedValuesTableName($this->id);
+                $this->zdb->drop($contents_table);
+            }
+
             $this->zdb->connection->beginTransaction();
             $old_rank = $this->index;
 
@@ -977,19 +982,16 @@ abstract class DynamicField
                 throw new \RuntimeException('Unable to remove field ' . $this->id . '!');
             }
 
-            if ($this->hasFixedValues()) {
-                $contents_table = self::getFixedValuesTableName($this->id);
-                $this->zdb->db->query(
-                    'DROP TABLE IF EXISTS ' . $contents_table,
-                    \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
-                );
-            }
             $this->deleteTranslation($this->name);
 
             $this->zdb->connection->commit();
+
             return true;
         } catch (\Exception $e) {
-            $this->zdb->connection->rollBack();
+            if ($this->zdb->connection->inTransaction()) {
+                //because of DROP autocommit on mysql...
+                $this->zdb->connection->rollBack();
+            }
             Analog::log(
                 'An error occurred deleting field | ' . $e->getMessage(),
                 Analog::ERROR
index d3686166612546d16f5014aa10c37e6d26c017c2..e1b7f24aa331f00817f422d06f803de81580b46a 100644 (file)
@@ -90,18 +90,19 @@ class Reminders
     {
         $this->toremind = array();
         $select = $zdb->select(Members::TABLE, 'a');
+        $select->columns([Members::PK, 'date_echeance']);
         $select->join(
             array('r' => PREFIX_DB . self::TABLE),
             'a.' . Members::PK . '=r.reminder_dest',
             array(
                 'last_reminder' => new Expression('MAX(reminder_date)'),
-                'reminder_type'
+                'reminder_type' => new Expression('MAX(reminder_type)')
             ),
             $select::JOIN_LEFT
         )->join(
             array('p' => PREFIX_DB . Members::TABLE),
             'a.parent_id=p.' . Members::PK,
-            array('email_adh'),
+            array(),
             $select::JOIN_LEFT
         )->where('(a.email_adh != \'\' OR p.email_adh != \'\')')
             ->where('a.activite_adh=true')
@@ -125,11 +126,17 @@ class Reminders
             );
         }
 
-        $select->group('a.id_adh')->group('r.reminder_type')->group('p.email_adh');
+        $select->group('a.id_adh');
 
         $results = $zdb->execute($select);
 
         foreach ($results as $r) {
+            if ($r->reminder_type < $type) {
+                //sent impending, but is now late. reset last remind.
+                $r->reminder_type = $type;
+                $r->last_reminder = '';
+            }
+
             if ($r->reminder_type === null || (int)$r->reminder_type === $type) {
                 $date_checked = false;
 
index f047f6894535d74911e0e4346b23047aabf41b20..9dd5a5756596290dd1a60409bc5d42af966583e3 100644 (file)
@@ -563,7 +563,7 @@ class Db extends atoum
         sort($expected);
 
         $this->array($tables)
-            ->hasSize(count($expected))
+            ->hasSize(count($expected), print_r($tables, true) . ' ' . print_r($expected, true))
             ->isIdenticalTo($expected);
     }
 
index ee7e90ec0592f0093bec95404f9d82156bd49289..1bf8c1be915a6cd80bacbbf321afda1e1331ceb6 100644 (file)
@@ -71,6 +71,7 @@ class CsvIn extends atoum
     private $request;
     private $response;
     private $mocked_router;
+    private $contents_table = null;
 
     /**
      * Set up tests
@@ -81,6 +82,7 @@ class CsvIn extends atoum
      */
     public function beforeTestMethod($testMethod)
     {
+        $this->contents_table = null;
         $this->mocked_router = new \mock\Slim\Router();
         $this->calling($this->mocked_router)->pathFor = function ($name, $params) {
             return $name;
@@ -181,6 +183,10 @@ class CsvIn extends atoum
             ]
         ]);
         $this->zdb->execute($delete);
+
+        if ($this->contents_table !== null) {
+            $this->zdb->drop($this->contents_table);
+        }
     }
 
     /**
@@ -593,6 +599,8 @@ class CsvIn extends atoum
         $this->zdb->execute($delete);
         $delete = $this->zdb->delete(\Galette\Entity\DynamicFieldsHandle::TABLE);
         $this->zdb->execute($delete);
+        //cleanup dynamic choices table
+        $this->contents_table = $cdf->getFixedValuesTableName($cdf->getId());
 
         //new dynamic field, of type date.
         $cfield_data = [