Skip to content

Commit 2012cda

Browse files
committed
DestinationAppwrite: add overwrite and skip params
Per Jake's spec, migration destinations accept two new behavior flags: - overwrite=true → use upsertDocuments() instead of createDocuments() Replaces existing rows with the imported values. Naturally handles duplicate ids. - skip=true → wrap createDocuments() in skipDuplicates() scope guard. Silently no-ops duplicate ids at the adapter layer (INSERT IGNORE equivalent). Existing rows are preserved. Default (both false): plain createDocuments, fails fast on DuplicateException. Original behavior, unchanged for existing callers. Precedence when both set: overwrite wins (upsert subsumes skip). The existing skipRelationshipsExistCheck() FK-guard wrapper is preserved in all three branches.
1 parent 0d43b66 commit 2012cda

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ class Appwrite extends Destination
9292
* @param UtopiaDatabase $dbForProject
9393
* @param callable(UtopiaDocument $database):UtopiaDatabase $getDatabasesDB
9494
* @param array<array<string, mixed>> $collectionStructure
95+
* @param bool $overwrite When true, replace existing rows by calling upsertDocuments instead of createDocuments.
96+
* @param bool $skip When true, silently ignore duplicate-id rows by wrapping createDocuments in skipDuplicates.
9597
*/
9698
public function __construct(
9799
string $project,
98100
string $endpoint,
99101
string $key,
100102
protected UtopiaDatabase $dbForProject,
101103
callable $getDatabasesDB,
102-
protected array $collectionStructure
104+
protected array $collectionStructure,
105+
protected bool $overwrite = false,
106+
protected bool $skip = false,
103107
) {
104108
$this->project = $project;
105109
$this->endpoint = $endpoint;
@@ -1067,14 +1071,27 @@ protected function createRecord(Row $resource, bool $isLast): bool
10671071
}
10681072
}
10691073
}
1070-
$dbForDatabases->skipDuplicates(
1071-
fn () => $dbForDatabases->skipRelationshipsExistCheck(
1072-
fn () => $dbForDatabases->createDocuments(
1073-
'database_' . $databaseInternalId . '_collection_' . $tableInternalId,
1074-
$this->rowBuffer
1074+
$collectionId = 'database_' . $databaseInternalId . '_collection_' . $tableInternalId;
1075+
1076+
if ($this->overwrite) {
1077+
// Replace existing rows with the imported values. Upsert naturally
1078+
// handles duplicates so skipDuplicates is unnecessary here.
1079+
$dbForDatabases->skipRelationshipsExistCheck(
1080+
fn () => $dbForDatabases->upsertDocuments($collectionId, $this->rowBuffer)
1081+
);
1082+
} elseif ($this->skip) {
1083+
// Silently ignore duplicates via the adapter-level INSERT IGNORE equivalent.
1084+
$dbForDatabases->skipDuplicates(
1085+
fn () => $dbForDatabases->skipRelationshipsExistCheck(
1086+
fn () => $dbForDatabases->createDocuments($collectionId, $this->rowBuffer)
10751087
)
1076-
)
1077-
);
1088+
);
1089+
} else {
1090+
// Default: fail fast on duplicate ids (original behavior).
1091+
$dbForDatabases->skipRelationshipsExistCheck(
1092+
fn () => $dbForDatabases->createDocuments($collectionId, $this->rowBuffer)
1093+
);
1094+
}
10781095

10791096
} finally {
10801097
$this->rowBuffer = [];

0 commit comments

Comments
 (0)