1
0
Fork 0
mirror of synced 2024-07-01 12:40:34 +12:00

Merge pull request #3248 from appwrite/feat-improve-migration-speed

feat: improve migration speed
This commit is contained in:
Torsten Dittmann 2022-05-19 15:26:27 +02:00 committed by GitHub
commit ba7f655de9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 41 deletions

View file

@ -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;
}
/**

View file

@ -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']
]
]));
}
}