From 54ab39c95300f3162575b256de9c295eca27356e Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 7 Jul 2020 17:59:02 +0200
Subject: [PATCH] FEAT #14249 TIME 0:20 Minor sql update

---
 .../controllers/VersionUpdateController.php   | 19 +++++++++++++++++
 src/core/models/DatabaseModel.php             | 14 +++++++++++++
 src/core/models/DatabasePDO.php               | 21 ++++++++++++++-----
 3 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/src/app/versionUpdate/controllers/VersionUpdateController.php b/src/app/versionUpdate/controllers/VersionUpdateController.php
index 405bbba4727..5def0341bf4 100755
--- a/src/app/versionUpdate/controllers/VersionUpdateController.php
+++ b/src/app/versionUpdate/controllers/VersionUpdateController.php
@@ -19,6 +19,7 @@ use Group\controllers\PrivilegeController;
 use Slim\Http\Request;
 use Slim\Http\Response;
 use SrcCore\models\CoreConfigModel;
+use SrcCore\models\DatabaseModel;
 
 class VersionUpdateController
 {
@@ -155,6 +156,24 @@ class VersionUpdateController
             return $response->withStatus(400)->withJson(['errors' => 'Some files are modified. Can not update application', 'lang' => 'canNotUpdateApplication']);
         }
 
+        $minorVersions = explode('.', $minorVersion);
+        $currentVersionTag = (int)$currentVersionTag;
+        $currentVersionTag++;
+        $sqlFiles = [];
+        while ($currentVersionTag <= (int)$minorVersions[2]) {
+            if (is_file("migration/{$versions[0]}.{$versions[1]}/{$versions[0]}{$versions[1]}{$currentVersionTag}.sql")) {
+                $sqlFiles[] = "migration/{$versions[0]}.{$versions[1]}/{$versions[0]}{$versions[1]}{$currentVersionTag}.sql";
+            }
+            $currentVersionTag++;
+        }
+
+        if (!empty($sqlFiles)) {
+            foreach ($sqlFiles as $sqlFile) {
+                $fileContent = file_get_contents($sqlFile);
+                DatabaseModel::exec($fileContent);
+            }
+        }
+
         $output = [];
         exec('git fetch');
         exec("git checkout {$minorVersion} 2>&1", $output, $returnCode);
diff --git a/src/core/models/DatabaseModel.php b/src/core/models/DatabaseModel.php
index 25370bf17ee..d8cec7661e7 100755
--- a/src/core/models/DatabaseModel.php
+++ b/src/core/models/DatabaseModel.php
@@ -317,6 +317,20 @@ class DatabaseModel
         return $db->rollbackTransaction();
     }
 
+    /**
+     * Database Exec Function
+     * @param string $query
+     *
+     * @return bool
+     * @throws \Exception
+     */
+    public static function exec(string $query)
+    {
+        $db = new DatabasePDO();
+
+        return $db->exec($query);
+    }
+
     /**
      * Database Select column_name
      * @param array $args
diff --git a/src/core/models/DatabasePDO.php b/src/core/models/DatabasePDO.php
index 593c66a3881..9ed108be610 100755
--- a/src/core/models/DatabasePDO.php
+++ b/src/core/models/DatabasePDO.php
@@ -170,6 +170,22 @@ class DatabasePDO
         return $query;
     }
 
+    public function exec(string $query)
+    {
+        try {
+            self::$pdo->exec($query);
+        } catch (\PDOException $PDOException) {
+            $file = fopen('queries_error.log', 'a');
+            fwrite($file, '[' . date('Y-m-d H:i:s') . '] ' . $query . PHP_EOL);
+            fwrite($file, '[' . date('Y-m-d H:i:s') . "] [{$PDOException->getMessage()}]" . PHP_EOL);
+            fclose($file);
+
+            throw new \Exception($PDOException->getMessage());
+        }
+
+        return true;
+    }
+
     public function setLimit(array $args)
     {
         ValidatorModel::notEmpty($args, ['limit']);
@@ -216,9 +232,4 @@ class DatabasePDO
     {
         return self::$pdo->rollBack();
     }
-
-    public function pgsqlCopyFromArray(string $table_name, array $rows, string $delimiter = '\t', string $null_as = "\\\\N", string $fields)
-    {
-        return self::$pdo->pgsqlCopyFromArray($table_name, $rows, $delimiter, $null_as, $fields);
-    }
 }
-- 
GitLab