Skip to content

Commit f84a064

Browse files
authored
Merge pull request #124 from utopia-php/log-projects
Add: logs
2 parents 18bd7d3 + e8994b9 commit f84a064

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/Migration/Sources/Appwrite.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Appwrite\Services\Storage;
1111
use Appwrite\Services\Teams;
1212
use Appwrite\Services\Users;
13+
use Utopia\Console;
1314
use Utopia\Database\Database as UtopiaDatabase;
1415
use Utopia\Database\DateTime as UtopiaDateTime;
1516
use Utopia\Migration\Exception;
@@ -74,6 +75,12 @@ class Appwrite extends Source
7475
public const SOURCE_API = 'api';
7576
public const SOURCE_DATABASE = 'database';
7677

78+
// Debug logging for specific projects
79+
public static array $debugProjects = [
80+
'67ec0369002bd8a96885' => 'SimpMusic#Maxrave',
81+
'6838382d0014e002589c' => 'Fastwrite#DocuTrust',
82+
];
83+
7784
protected Client $client;
7885

7986
private Reader $reader;
@@ -120,7 +127,7 @@ public function __construct(
120127

121128
$this->reader = match ($this->source) {
122129
static::SOURCE_API => new APIReader(new Databases($this->client)),
123-
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabasesDB),
130+
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabasesDB, $this->project),
124131
default => throw new \Exception('Unknown source'),
125132
};
126133

@@ -131,6 +138,19 @@ public static function getName(): string
131138
return 'Appwrite';
132139
}
133140

141+
/**
142+
* Log migration debug info for tracked projects
143+
*/
144+
private function logDebugTrackedProject(string $message): void
145+
{
146+
$projectTag = self::$debugProjects[$this->project] ?? null;
147+
if ($projectTag === null) {
148+
return;
149+
}
150+
151+
Console::info("MIGRATIONS-SOURCE-$projectTag: $message");
152+
}
153+
134154
/**
135155
* @return array<string>
136156
*/
@@ -972,11 +992,22 @@ private function exportIndexes(string $entityType, int $batchSize): void
972992
private function exportRecords(string $entityName, string $fieldName, int $batchSize): void
973993
{
974994
$entities = $this->cache->get($entityName);
995+
996+
$this->logDebugTrackedProject("exportRecords started | Entity: $entityName | Tables count: " . count($entities));
997+
975998
foreach ($entities as $table) {
976999
/** @var Table $table */
9771000
$lastRow = null;
1001+
$iterationCount = 0;
1002+
1003+
$this->logDebugTrackedProject("Starting table export | Table: {$table->getName()} | ID: {$table->getId()}");
9781004

9791005
while (true) {
1006+
$iterationCount++;
1007+
1008+
$memUsage = round(memory_get_usage(true) / 1024 / 1024, 2);
1009+
$this->logDebugTrackedProject("Table: {$table->getName()} | Iteration: $iterationCount | Memory: {$memUsage}MB | LastRow: " . ($lastRow ? $lastRow->getId() : 'null'));
1010+
9801011
$queries = [
9811012
$this->reader->queryLimit($batchSize),
9821013
...$this->queries,
@@ -1009,8 +1040,14 @@ private function exportRecords(string $entityName, string $fieldName, int $batch
10091040

10101041
$queries[] = $this->reader->querySelect($selects);
10111042

1043+
$timestamp = microtime(true);
1044+
$this->logDebugTrackedProject("BEFORE listRows() | Table: {$table->getName()} | Batch: $batchSize | Timestamp: {$timestamp}");
1045+
10121046
$response = $this->reader->listRows($table, $queries);
10131047

1048+
$timestamp = microtime(true);
1049+
$this->logDebugTrackedProject("AFTER listRows() | Table: {$table->getName()} | Rows: " . count($response) . " | Timestamp: $timestamp");
1050+
10141051
foreach ($response as $row) {
10151052
// HACK: Handle many to many (only for schema-based databases)
10161053
if (!empty($manyToMany)) {
@@ -1069,13 +1106,24 @@ private function exportRecords(string $entityName, string $fieldName, int $batch
10691106
$lastRow = $row;
10701107
}
10711108

1109+
$this->logDebugTrackedProject("Processed rows from response | Table: {$table->getName()} | Rows in batch: " . count($rows));
1110+
1111+
$this->logDebugTrackedProject("BEFORE callback() | Table: {$table->getName()} | Rows: " . count($rows));
1112+
10721113
$this->callback($rows);
10731114

1115+
$this->logDebugTrackedProject("AFTER callback() | Table: {$table->getName()}");
1116+
10741117
if (count($response) < $batchSize) {
1118+
$this->logDebugTrackedProject("Table export completed | Table: {$table->getName()} | Response count: " . count($response) . " < Batch size: $batchSize");
10751119
break;
10761120
}
10771121
}
1122+
1123+
$this->logDebugTrackedProject("Finished table export | Table: {$table->getName()} | Total iterations: {$iterationCount}");
10781124
}
1125+
1126+
$this->logDebugTrackedProject("exportRecords completed | Entity: {$entityName}");
10791127
}
10801128

10811129
protected function exportGroupStorage(int $batchSize, array $resources): void

src/Migration/Sources/Appwrite/Reader/Database.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Utopia\Migration\Sources\Appwrite\Reader;
44

5+
use Utopia\Console;
56
use Utopia\Database\Database as UtopiaDatabase;
67
use Utopia\Database\Document as UtopiaDocument;
78
use Utopia\Database\Exception as DatabaseException;
@@ -15,6 +16,7 @@
1516
use Utopia\Migration\Resources\Database\Index as IndexResource;
1617
use Utopia\Migration\Resources\Database\Row as RowResource;
1718
use Utopia\Migration\Resources\Database\Table as TableResource;
19+
use Utopia\Migration\Sources\Appwrite;
1820
use Utopia\Migration\Sources\Appwrite\Reader;
1921

2022
/**
@@ -29,11 +31,29 @@ class Database implements Reader
2931

3032
public function __construct(
3133
private readonly UtopiaDatabase $dbForProject,
32-
?callable $getDatabasesDB = null
34+
?callable $getDatabasesDB = null,
35+
private readonly ?string $projectId = null
3336
) {
3437
$this->getDatabasesDB = $getDatabasesDB;
3538
}
3639

40+
/**
41+
* Log debug message for tracked projects
42+
*/
43+
private function logDebug(string $message): void
44+
{
45+
if ($this->projectId === null) {
46+
return;
47+
}
48+
49+
$projectTag = Appwrite::$debugProjects[$this->projectId] ?? null;
50+
if ($projectTag === null) {
51+
return;
52+
}
53+
54+
Console::info("MIGRATIONS-READER-$projectTag: $message");
55+
}
56+
3757
/**
3858
* Get the appropriate database instance for the given database DSN
3959
*/
@@ -347,7 +367,9 @@ public function listRows(TableResource $resource, array $queries = []): array
347367
$dbInstance = $this->getDatabase($resource->getDatabase()->getDatabase());
348368

349369
try {
370+
$this->logDebug("BEFORE dbInstance->find() | TableID: $tableId | Queries: " . count($queries));
350371
$rows = $dbInstance->find($tableId, $queries);
372+
$this->logDebug("AFTER dbInstance->find() | TableID: $tableId | Rows returned: " . count($rows));
351373
} catch (DatabaseException $e) {
352374
throw new Exception(
353375
resourceName: $resource->getName(),

0 commit comments

Comments
 (0)