2021-01-14 05:51:02 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Migration;
|
|
|
|
|
|
|
|
use Appwrite\Database\Document;
|
|
|
|
use Appwrite\Database\Database;
|
2021-01-15 01:05:49 +13:00
|
|
|
use Utopia\CLI\Console;
|
|
|
|
use Utopia\Exception;
|
2021-01-19 03:43:55 +13:00
|
|
|
use PDO;
|
2021-01-14 05:51:02 +13:00
|
|
|
|
|
|
|
abstract class Migration
|
|
|
|
{
|
2021-01-19 03:43:55 +13:00
|
|
|
/**
|
|
|
|
* @var PDO
|
|
|
|
*/
|
|
|
|
protected PDO $db;
|
2021-01-15 01:05:49 +13:00
|
|
|
|
2021-01-19 03:43:55 +13:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2021-01-15 01:05:49 +13:00
|
|
|
protected int $limit = 30;
|
2021-01-15 03:09:48 +13:00
|
|
|
|
2021-01-19 03:43:55 +13:00
|
|
|
/**
|
|
|
|
* @var Document
|
|
|
|
*/
|
2021-01-15 01:05:49 +13:00
|
|
|
protected Document $project;
|
2021-01-19 03:43:55 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Database
|
|
|
|
*/
|
2021-01-15 01:05:49 +13:00
|
|
|
protected Database $projectDB;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Migration constructor.
|
2021-01-19 03:43:55 +13:00
|
|
|
*
|
|
|
|
* @param PDO $pdo
|
2021-01-15 01:05:49 +13:00
|
|
|
*/
|
2021-01-19 03:43:55 +13:00
|
|
|
public function __construct(PDO $db)
|
2021-01-15 01:05:49 +13:00
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set project for migration.
|
2021-01-19 03:43:55 +13:00
|
|
|
*
|
|
|
|
* @param Document $project
|
|
|
|
* @param Database $projectDB
|
2021-01-21 22:57:15 +13:00
|
|
|
*
|
|
|
|
* @return Migration
|
2021-01-15 01:05:49 +13:00
|
|
|
*/
|
|
|
|
public function setProject(Document $project, Database $projectDB): Migration
|
|
|
|
{
|
|
|
|
$this->project = $project;
|
|
|
|
$this->projectDB = $projectDB;
|
|
|
|
$this->projectDB->setNamespace('app_' . $project->getId());
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates through every document.
|
|
|
|
*
|
2021-01-19 03:43:55 +13:00
|
|
|
* @param callable $callback
|
2021-01-15 01:05:49 +13:00
|
|
|
*/
|
2021-01-21 22:57:15 +13:00
|
|
|
public function forEachDocument(callable $callback): void
|
2021-01-15 01:05:49 +13:00
|
|
|
{
|
2021-01-21 22:57:15 +13:00
|
|
|
|
|
|
|
$sum = $this->limit;
|
2021-01-15 03:09:48 +13:00
|
|
|
$offset = 0;
|
|
|
|
|
2021-01-21 22:57:15 +13:00
|
|
|
while ($sum >= $this->limit) {
|
2021-01-15 01:05:49 +13:00
|
|
|
$all = $this->projectDB->getCollection([
|
|
|
|
'limit' => $this->limit,
|
2021-01-15 03:09:48 +13:00
|
|
|
'offset' => $offset,
|
2021-01-15 01:05:49 +13:00
|
|
|
'orderType' => 'DESC',
|
|
|
|
]);
|
|
|
|
|
2021-01-15 03:09:48 +13:00
|
|
|
$sum = \count($all);
|
2021-01-15 01:05:49 +13:00
|
|
|
|
2021-01-15 03:09:48 +13:00
|
|
|
Console::log('Migrating: ' . $offset . ' / ' . $this->projectDB->getSum());
|
2021-01-15 01:05:49 +13:00
|
|
|
|
|
|
|
foreach ($all as $document) {
|
|
|
|
|
|
|
|
$document = call_user_func($callback, $document);
|
|
|
|
|
|
|
|
if (empty($document->getId())) {
|
|
|
|
throw new Exception('Missing ID');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$new = $this->projectDB->overwriteDocument($document->getArrayCopy());
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
Console::error('Failed to update document: ' . $th->getMessage());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($new->getId() !== $document->getId()) {
|
|
|
|
throw new Exception('Duplication Error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 03:09:48 +13:00
|
|
|
$offset += $this->limit;
|
2021-01-15 01:05:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes migration for set project.
|
|
|
|
*/
|
|
|
|
abstract public function execute(): void;
|
2021-01-14 05:51:02 +13:00
|
|
|
}
|