1010use Appwrite \Services \Storage ;
1111use Appwrite \Services \Teams ;
1212use Appwrite \Services \Users ;
13+ use Utopia \Console ;
1314use Utopia \Database \Database as UtopiaDatabase ;
1415use Utopia \Database \DateTime as UtopiaDateTime ;
1516use 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
0 commit comments