From f2b29f6591f5190e36c4b75458da8ec793fcc798 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Wed, 12 Dec 2018 17:50:34 +0100 Subject: [PATCH] Fix moving stored data --- src/Core/StorageManager.php | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Core/StorageManager.php b/src/Core/StorageManager.php index e1bab23aca..ad2c0cb1db 100644 --- a/src/Core/StorageManager.php +++ b/src/Core/StorageManager.php @@ -4,6 +4,7 @@ namespace Friendica\Core; use Friendica\Database\DBA; use Friendica\Core\Config; +use Friendica\Core\Logger; @@ -105,35 +106,55 @@ class StorageManager /** * @brief Move resources to storage $dest * + * Copy existing data to destination storage and delete from source. + * This method cannot move to legacy in-table `data` field. + * * @param string $dest Destination storage class name * @param array $tables Tables to look in for resources. Optional, defaults to ['photo'] * * @retur int Number of moved resources */ - public static function move($dest, $tables = null) + public static function move(string $dest, $tables = null) { + if (is_null($dest) || empty($dest)) { + throw Exception('Can\'t move to NULL storage backend'); + } + if (is_null($tables)) { $tables = ['photo']; } $moved = 0; foreach ($tables as $table) { - $rr = DBA::select($table, ['id', 'data', 'backend-class', 'backend-ref'], ['`backend-class` != ?', $dest]); + // Get the rows where backend class is not the destination backend class + $rr = DBA::select( + $table, + ['id', 'data', 'backend-class', 'backend-ref'], + ['`backend-class` IS NULL or `backend-class` != ?' , $dest ] + ); + if (DBA::isResult($rr)) { while($r = $rr->fetch()) { $id = $r['id']; $data = $r['data']; $backendClass = $r['backend-class']; $backendRef = $r['backend-ref']; - if ($backendClass !== '') { + if (!is_null($backendClass) && $backendClass !== '') { + Logger::log("get data from old backend " . $backendClass . " : " . $backendRef); $data = $backendClass::get($backendRef); } + + Logger::log("save data to new backend " . $dest); $ref = $dest::put($data); + Logger::log("saved data as " . $ref); if ($ref !== '') { + Logger::log("update row"); $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]); + if ($ru) { - if ($backendClass !== '') { + if (!is_null($backendClass) && $backendClass !== '') { + Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef); $backendClass::delete($backendRef); } $moved++; @@ -145,4 +166,5 @@ class StorageManager return $moved; } -} \ No newline at end of file +} +