Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
DESTINATION_APPWRITE_TEST_PROJECT=testProject
DESTINATION_APPWRITE_TEST_ENDPOINT=http://localhost/v1
DESTINATION_APPWRITE_TEST_KEY=xxxxxxxxxxxxxxxxxx
# Providers
SOURCE_PROVIDER=appwrite
DESTINATION_PROVIDER=appwrite

# Appwrite (Database mode)
SOURCE_APPWRITE_TEST_DSN=mysql:host=127.0.0.1;port=3306;dbname=appwrite
SOURCE_APPWRITE_TEST_USER=user
SOURCE_APPWRITE_TEST_PASSWORD=password
SOURCE_APPWRITE_TEST_NAMESPACE=1

DESTINATION_APPWRITE_TEST_DSN=mysql:host=127.0.0.1;port=3306;dbname=appwrite
DESTINATION_APPWRITE_TEST_USER=user
DESTINATION_APPWRITE_TEST_PASSWORD=password
DESTINATION_APPWRITE_TEST_NAMESPACE=2

# Appwrite (API Key mode)
SOURCE_APPWRITE_TEST_PROJECT=testProject
SOURCE_APPWRITE_TEST_ENDPOINT=http://localhost/v1
SOURCE_APPWRITE_TEST_KEY=xxxxxxxxxxxxxxxxxx

DESTINATION_APPWRITE_TEST_PROJECT=testProject
DESTINATION_APPWRITE_TEST_ENDPOINT=http://localhost/v1
DESTINATION_APPWRITE_TEST_KEY=xxxxxxxxxxxxxxxxxx

# Firebase
FIREBASE_TEST_PROJECT=testProject
FIREBASE_TEST_ACCOUNT='{type: "service_account", ...}'

# Supabase
SUPABASE_TEST_ENDPOINT=https://xxxxxxxxxxxx.supabase.co
SUPABASE_TEST_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SUPABASE_TEST_HOST=db.xxxxxxxxxxxxxxxx.supabase.co
SUPABASE_TEST_DATABASE=postgres
SUPABASE_TEST_USERNAME=postgres
SUPABASE_TEST_PASSWORD=xxxxxxxxxxxxxxxxxxxxx

# NHost
NHOST_TEST_SUBDOMAIN=xxxxxxxxxxx
NHOST_TEST_REGION=eu-central-1
NHOST_TEST_SECRET=xxxxxxxxxxxxxxxxx
Expand Down
41 changes: 28 additions & 13 deletions bin/MigrationCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function getSource(): Source
$_ENV['SOURCE_APPWRITE_TEST_ENDPOINT'],
$_ENV['SOURCE_APPWRITE_TEST_KEY'],
Appwrite::SOURCE_DATABASE,
$this->getDatabase(),
$this->getDatabase('source'),
);
case 'supabase':
return new Supabase(
Expand Down Expand Up @@ -252,7 +252,7 @@ public function getDestination(): Destination
$_ENV['DESTINATION_APPWRITE_TEST_PROJECT'],
$_ENV['DESTINATION_APPWRITE_TEST_ENDPOINT'],
$_ENV['DESTINATION_APPWRITE_TEST_KEY'],
$this->getDatabase(),
$this->getDatabase('destination'),
self::STRUCTURE
);
case 'local':
Expand All @@ -262,7 +262,7 @@ public function getDestination(): Destination
}
}

public function getDatabase(): Database
public function getDatabase(string $type): Database
{
Database::addFilter(
'subQueryAttributes',
Expand All @@ -277,12 +277,21 @@ function (mixed $value, Document $document, Database $database) {
]);

foreach ($attributes as $attribute) {
if ($attribute->getAttribute('type') === Database::VAR_RELATIONSHIP) {
$options = $attribute->getAttribute('options');
foreach ($options as $key => $value) {
$attribute->setAttribute($key, $value);
}
$attribute->removeAttribute('options');
$attributeType = $attribute->getAttribute('type');

switch ($attributeType) {
case Database::VAR_RELATIONSHIP:
$options = $attribute->getAttribute('options');
foreach ($options as $key => $value) {
$attribute->setAttribute($key, $value);
}
$attribute->removeAttribute('options');
break;

case Database::VAR_STRING:
$filters = $attribute->getAttribute('filters', []);
$attribute->setAttribute('encrypt', in_array('encrypt', $filters));
break;
}
}

Expand Down Expand Up @@ -363,10 +372,16 @@ function (mixed $value, Document $attribute) {
}
);

$prefix = match ($type) {
'source' => 'SOURCE_APPWRITE_TEST_',
'destination' => 'DESTINATION_APPWRITE_TEST_',
default => throw new Exception('Invalid type for database'),
};

$database = new Database(new MariaDB(new PDO(
$_ENV['DESTINATION_APPWRITE_TEST_DSN'],
$_ENV['DESTINATION_APPWRITE_TEST_USER'],
$_ENV['DESTINATION_APPWRITE_TEST_PASSWORD'],
$_ENV[$prefix . 'DSN'],
$_ENV[$prefix . 'USER'],
$_ENV[$prefix . 'PASSWORD'],
[
PDO::ATTR_TIMEOUT => 3,
PDO::ATTR_PERSISTENT => true,
Expand All @@ -378,7 +393,7 @@ function (mixed $value, Document $attribute) {

$database
->setDatabase('appwrite')
->setNamespace('_' . $_ENV['DESTINATION_APPWRITE_TEST_NAMESPACE']);
->setNamespace('_' . $_ENV[$prefix . 'NAMESPACE']);

return $database;
}
Expand Down
18 changes: 9 additions & 9 deletions src/Migration/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function add(Resource $resource): void
$resource->setSequence(uniqid());
}

if ($resource->getName() == Resource::TYPE_DOCUMENT) {
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
$status = $resource->getStatus();
$documentId = $resource->getSequence();
$this->cache[$resource->getName()][$documentId] = $status;
$rowId = $resource->getSequence();
$this->cache[$resource->getName()][$rowId] = $status;
return;
}

Expand Down Expand Up @@ -78,14 +78,14 @@ public function addAll(array $resources): void
*/
public function update(Resource $resource): void
{
// if documents then updating the status counter only
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
$documentId = $resource->getSequence();
if (!isset($this->cache[$resource->getName()][$documentId])) {
// if rows then updating the status counter only
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
$rowId = $resource->getSequence();
if (!isset($this->cache[$resource->getName()][$rowId])) {
$this->add($resource);
} else {
$status = $resource->getStatus();
$this->cache[$resource->getName()][$documentId] = $status;
$this->cache[$resource->getName()][$rowId] = $status;
}
return;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public function updateAll(array $resources): void
*/
public function remove(Resource $resource): void
{
if ($resource->getName() === Resource::TYPE_DOCUMENT) {
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
if (! isset($this->cache[$resource->getName()][$resource->getSequence()])) {
throw new \Exception('Resource does not exist in cache');
}
Expand Down
Loading