diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index b479a4226..99b3f9236 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -124,21 +124,7 @@ abstract class Migration $old = $document->getArrayCopy(); $new = call_user_func($callback, $document); - foreach ($document as &$attr) { - if ($attr instanceof Document) { - $attr = call_user_func($callback, $attr); - } - - if (\is_array($attr)) { - foreach ($attr as &$child) { - if ($child instanceof Document) { - $child = call_user_func($callback, $child); - } - } - } - } - - if (!$this->check_diff_multi($new->getArrayCopy(), $old)) { + if (!self::hasDifference($new->getArrayCopy(), $old)) { return; } @@ -167,35 +153,28 @@ abstract class Migration /** * Checks 2 arrays for differences. - * - * @param array $array1 - * @param array $array2 - * @return array + * + * @param array $array1 + * @param array $array2 + * @return bool */ - public function check_diff_multi(array $array1, array $array2): array + public static function hasDifference(array $array1, array $array2): bool { - $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; + foreach ($array1 as $key => $value) { + if (is_array($value)) { + if (!isset($array2[$key]) || !is_array($array2[$key])) { + return true; + } else { + if (self::hasDifference($value, $array2[$key])) { + return true; + } } - } elseif (!isset($array2[$key])) { - $result[$key] = null; - } elseif ($val !== $array2[$key]) { - $result[$key] = $array2[$key]; - } - - if (isset($array2[$key])) { - unset($array2[$key]); + } elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) { + return true; } } - $result = array_merge($result, $array2); - - return $result; + return false; } /** diff --git a/tests/unit/Migration/MigrationTest.php b/tests/unit/Migration/MigrationTest.php index 340e33772..0c8c34cbc 100644 --- a/tests/unit/Migration/MigrationTest.php +++ b/tests/unit/Migration/MigrationTest.php @@ -4,6 +4,7 @@ namespace Appwrite\Tests; use Appwrite\Migration\Migration; use PHPUnit\Framework\TestCase; +use ReflectionClass; use ReflectionMethod; use Utopia\Database\Document; @@ -36,12 +37,100 @@ abstract class MigrationTest extends TestCase */ public function testMigrationVersions() { - require_once __DIR__.'/../../../app/init.php'; + require_once __DIR__ . '/../../../app/init.php'; foreach (Migration::$versions as $class) { - $this->assertTrue(class_exists('Appwrite\\Migration\\Version\\'.$class)); + $this->assertTrue(class_exists('Appwrite\\Migration\\Version\\' . $class)); } // Test if current version exists - //$this->assertArrayHasKey(APP_VERSION_STABLE, Migration::$versions); + $this->assertArrayHasKey(APP_VERSION_STABLE, Migration::$versions); + } + + public function testHasDifference() + { + $this->assertFalse(Migration::hasDifference([], [])); + $this->assertFalse(Migration::hasDifference([ + 'bool' => true, + 'string' => 'abc', + 'int' => 123, + 'array' => ['a', 'b', 'c'], + 'assoc' => [ + 'a' => true, + 'b' => 'abc', + 'c' => 123, + 'd' => ['a', 'b', 'c'] + ] + ], [ + 'bool' => true, + 'string' => 'abc', + 'int' => 123, + 'array' => ['a', 'b', 'c'], + 'assoc' => [ + 'a' => true, + 'b' => 'abc', + 'c' => 123, + 'd' => ['a', 'b', 'c'] + ] + ])); + $this->assertFalse(Migration::hasDifference([ + 'bool' => true, + 'string' => 'abc', + 'int' => 123, + 'array' => ['a', 'b', 'c'], + 'assoc' => [ + 'a' => true, + 'b' => 'abc', + 'c' => 123, + 'd' => ['a', 'b', 'c'] + ] + ], [ + 'string' => 'abc', + 'assoc' => [ + 'a' => true, + 'b' => 'abc', + 'c' => 123, + 'd' => ['a', 'b', 'c'] + ], + 'int' => 123, + 'array' => ['a', 'b', 'c'], + 'bool' => true, + + ])); + $this->assertTrue(Migration::hasDifference([ + 'a' => true + ], [ + 'b' => true + ])); + $this->assertTrue(Migration::hasDifference([ + 'a' => 'true' + ], [ + 'a' => true + ])); + $this->assertTrue(Migration::hasDifference([ + 'a' => true + ], [ + 'a' => false + ])); + $this->assertTrue(Migration::hasDifference([ + 'nested' => [ + 'a' => true + ] + ], [ + 'nested' => [] + ])); + $this->assertTrue(Migration::hasDifference([ + 'assoc' => [ + 'bool' => true, + 'string' => 'abc', + 'int' => 123, + 'array' => ['a', 'b', 'c'] + ] + ], [ + 'nested' => [ + 'a' => true, + 'int' => '123', + 'array' => ['a', 'b', 'c'] + ] + ])); } }