Skip to content

Commit 9726690

Browse files
authored
Merge pull request #169 from utopia-php/feat/skip-duplicates
feat: add onDuplicate option to DestinationAppwrite
2 parents 7a86aea + 74f166b commit 9726690

3 files changed

Lines changed: 67 additions & 79 deletions

File tree

composer.lock

Lines changed: 28 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Migration/Destinations/Appwrite.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ class Appwrite extends Destination
9292
* @param UtopiaDatabase $dbForProject
9393
* @param callable(UtopiaDocument $database):UtopiaDatabase $getDatabasesDB
9494
* @param array<array<string, mixed>> $collectionStructure
95+
* @param OnDuplicate $onDuplicate Behavior when a row with an existing $id is encountered.
9596
*/
9697
public function __construct(
9798
string $project,
9899
string $endpoint,
99100
string $key,
100101
protected UtopiaDatabase $dbForProject,
101102
callable $getDatabasesDB,
102-
protected array $collectionStructure
103+
protected array $collectionStructure,
104+
protected OnDuplicate $onDuplicate = OnDuplicate::Fail,
103105
) {
104106
$this->project = $project;
105107
$this->endpoint = $endpoint;
@@ -1067,10 +1069,21 @@ protected function createRecord(Row $resource, bool $isLast): bool
10671069
}
10681070
}
10691071
}
1070-
$dbForDatabases->skipRelationshipsExistCheck(fn () => $dbForDatabases->createDocuments(
1071-
'database_' . $databaseInternalId . '_collection_' . $tableInternalId,
1072-
$this->rowBuffer
1073-
));
1072+
$collectionId = 'database_' . $databaseInternalId . '_collection_' . $tableInternalId;
1073+
1074+
match ($this->onDuplicate) {
1075+
OnDuplicate::Upsert => $dbForDatabases->skipRelationshipsExistCheck(
1076+
fn () => $dbForDatabases->upsertDocuments($collectionId, $this->rowBuffer)
1077+
),
1078+
OnDuplicate::Skip => $dbForDatabases->skipDuplicates(
1079+
fn () => $dbForDatabases->skipRelationshipsExistCheck(
1080+
fn () => $dbForDatabases->createDocuments($collectionId, $this->rowBuffer)
1081+
)
1082+
),
1083+
OnDuplicate::Fail => $dbForDatabases->skipRelationshipsExistCheck(
1084+
fn () => $dbForDatabases->createDocuments($collectionId, $this->rowBuffer)
1085+
),
1086+
};
10741087

10751088
} finally {
10761089
$this->rowBuffer = [];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Utopia\Migration\Destinations;
4+
5+
/**
6+
* Behavior when a destination row with an existing ID is encountered.
7+
*/
8+
enum OnDuplicate: string
9+
{
10+
case Fail = 'fail';
11+
case Skip = 'skip';
12+
case Upsert = 'upsert';
13+
14+
/**
15+
* @return array<string>
16+
*/
17+
public static function values(): array
18+
{
19+
return \array_map(fn (self $case) => $case->value, self::cases());
20+
}
21+
}

0 commit comments

Comments
 (0)