From e6b3857337edb996199d8b04e92a0c9fdaf67ff4 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Fri, 10 Jan 2020 17:53:43 +0100
Subject: [PATCH] FEAT #11692 TIME 0:30 migrate linked resources

---
 migration/19.12/migrate.sh                 |  1 +
 migration/19.12/migrateLinkedResources.php | 44 ++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 migration/19.12/migrateLinkedResources.php

diff --git a/migration/19.12/migrate.sh b/migration/19.12/migrate.sh
index 1d82adc5576..a60ab5a74d1 100755
--- a/migration/19.12/migrate.sh
+++ b/migration/19.12/migrate.sh
@@ -23,3 +23,4 @@ php ./migrateCustomValues.php
 php ./migrateVersionAttachments.php
 php ./migrateContacts.php
 php ./migrateTemplates.php
+php ./migrateLinkedResources.php
diff --git a/migration/19.12/migrateLinkedResources.php b/migration/19.12/migrateLinkedResources.php
new file mode 100644
index 00000000000..b7cb0d4206b
--- /dev/null
+++ b/migration/19.12/migrateLinkedResources.php
@@ -0,0 +1,44 @@
+<?php
+
+use Resource\models\ResModel;
+
+require '../../vendor/autoload.php';
+
+chdir('../..');
+
+$customs = scandir('custom');
+
+foreach ($customs as $custom) {
+    if ($custom == 'custom.xml' || $custom == '.' || $custom == '..') {
+        continue;
+    }
+
+    \SrcCore\models\DatabasePDO::reset();
+    new \SrcCore\models\DatabasePDO(['customId' => $custom]);
+
+    $migrated = 0;
+
+    $links = \SrcCore\models\DatabaseModel::select([
+        'select' => ['res_parent', 'res_child'],
+        'table'  => ['res_linked']
+    ]);
+
+    foreach ($links as $link) {
+        $resParent = (string)$link['res_parent'];
+        $resChild = (string)$link['res_child'];
+
+        ResModel::update([
+            'postSet' => ['linked_resources' => "jsonb_insert(linked_resources, '{0}', '\"{$resChild}\"')"],
+            'where'   => ['res_id = ?', "(linked_resources @> ?) = false"],
+            'data'    => [(int)$resParent, "\"{$resChild}\""]
+        ]);
+        ResModel::update([
+            'postSet' => ['linked_resources' => "jsonb_insert(linked_resources, '{0}', '\"{$resParent}\"')"],
+            'where'   => ['res_id = ?', "(linked_resources @> ?) = false"],
+            'data'    => [(int)$resChild, "\"{$resParent}\""]
+        ]);
+        $migrated++;
+    }
+
+    printf("Migration des liaisons dans res_letterbox (CUSTOM {$custom}) : " . $migrated . " liaisons migrés.\n");
+}
-- 
GitLab