From f6aa9e8108c59862d1a8c7dac9b279ef545eb4c6 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 15 Apr 2021 19:13:29 +0200 Subject: [PATCH 1/2] fix: migration tests --- src/Appwrite/Migration/Migration.php | 8 ++++---- src/Appwrite/Migration/Version/V07.php | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index bcfaae4726..a1370029a5 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -14,22 +14,22 @@ abstract class Migration /** * @var PDO */ - protected PDO $db; + protected $db; /** * @var int */ - protected int $limit = 50; + protected $limit = 50; /** * @var Document */ - protected Document $project; + protected $project; /** * @var Database */ - protected Database $projectDB; + protected $projectDB; /** * Migration constructor. diff --git a/src/Appwrite/Migration/Version/V07.php b/src/Appwrite/Migration/Version/V07.php index 0b97a83f80..7cd3b8fefd 100644 --- a/src/Appwrite/Migration/Version/V07.php +++ b/src/Appwrite/Migration/Version/V07.php @@ -26,10 +26,10 @@ class V07 extends Migration switch ($document->getAttribute('$collection')) { case Database::SYSTEM_COLLECTION_USERS: + /** + * Remove deprecated OAuth2 properties in the Users Documents. + */ foreach ($providers as $key => $provider) { - /** - * Remove deprecated OAuth2 properties in the Users Documents. - */ if (!empty($document->getAttribute('oauth2' . \ucfirst($key)))) { $document->removeAttribute('oauth2' . \ucfirst($key)); } @@ -37,17 +37,17 @@ class V07 extends Migration if (!empty($document->getAttribute('oauth2' . \ucfirst($key) . 'AccessToken'))) { $document->removeAttribute('oauth2' . \ucfirst($key) . 'AccessToken'); } - - /** - * Invalidate all Login Tokens, since they can't be migrated to the new structure. - * Reason for it is the missing distinction between E-Mail and OAuth2 tokens. - */ - $tokens = array_filter($document->getAttribute('tokens', []), function($token) { - return ($token->getAttribute('type') != Auth::TOKEN_TYPE_LOGIN); - }); - - $document->setAttribute('tokens', array_values($tokens)); } + + /** + * Invalidate all Login Tokens, since they can't be migrated to the new structure. + * Reason for it is the missing distinction between E-Mail and OAuth2 tokens. + */ + $tokens = array_filter($document->getAttribute('tokens', []), function ($token) { + return ($token->getAttribute('type') != Auth::TOKEN_TYPE_LOGIN); + }); + $document->setAttribute('tokens', array_values($tokens)); + break; } From 52574d40dd3e7c8afbe18efedd86646fec5a2d65 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 16 Apr 2021 09:08:42 +0200 Subject: [PATCH 2/2] fix: comparing multidimensional array on migration --- src/Appwrite/Migration/Migration.php | 31 ++++++++++++++++++++++++-- src/Appwrite/Migration/Version/V07.php | 1 - 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index a1370029a5..78b0f6acdf 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -89,8 +89,8 @@ abstract class Migration if (empty($new->getId())) { throw new Exception('Missing ID'); } - - if (!array_diff_assoc($new->getArrayCopy(), $old)) { + + if (!$this->check_diff_multi($new->getArrayCopy(), $old)) { return; } @@ -112,6 +112,33 @@ abstract class Migration } } + public function check_diff_multi($array1, $array2){ + $result = array(); + + foreach($array1 as $key => $val) { + if(is_array($val) && isset($array2[$key])) { + $tmp = $this->check_diff_multi($val, $array2[$key]); + if($tmp) { + $result[$key] = $tmp; + } + } + elseif(!isset($array2[$key])) { + $result[$key] = null; + } + elseif($val !== $array2[$key]) { + $result[$key] = $array2[$key]; + } + + if(isset($array2[$key])) { + unset($array2[$key]); + } + } + + $result = array_merge($result, $array2); + + return $result; + } + /** * Executes migration for set project. */ diff --git a/src/Appwrite/Migration/Version/V07.php b/src/Appwrite/Migration/Version/V07.php index 7cd3b8fefd..9c3054a286 100644 --- a/src/Appwrite/Migration/Version/V07.php +++ b/src/Appwrite/Migration/Version/V07.php @@ -38,7 +38,6 @@ class V07 extends Migration $document->removeAttribute('oauth2' . \ucfirst($key) . 'AccessToken'); } } - /** * Invalidate all Login Tokens, since they can't be migrated to the new structure. * Reason for it is the missing distinction between E-Mail and OAuth2 tokens.