5050use Utopia \Migration \Resources \Functions \Deployment ;
5151use Utopia \Migration \Resources \Functions \EnvVar ;
5252use Utopia \Migration \Resources \Functions \Func ;
53+ use Utopia \Migration \Resources \Integrations \Platform ;
5354use Utopia \Migration \Resources \Messaging \Message ;
5455use Utopia \Migration \Resources \Messaging \Provider ;
5556use Utopia \Migration \Resources \Messaging \Subscriber ;
@@ -161,6 +162,8 @@ public function __construct(
161162 protected UtopiaDatabase $ dbForProject ,
162163 callable $ getDatabasesDB ,
163164 protected array $ collectionStructure ,
165+ protected UtopiaDatabase $ dbForPlatform ,
166+ protected string $ projectInternalId ,
164167 protected OnDuplicate $ onDuplicate = OnDuplicate::Fail,
165168 ?callable $ getDatabaseDSN = null ,
166169 ) {
@@ -270,6 +273,9 @@ public static function getSupportedResources(): array
270273 Resource::TYPE_SITE_DEPLOYMENT ,
271274 Resource::TYPE_SITE_VARIABLE ,
272275
276+ // Integrations
277+ Resource::TYPE_PLATFORM ,
278+
273279 // Backups
274280 Resource::TYPE_BACKUP_POLICY ,
275281 ];
@@ -419,6 +425,7 @@ protected function import(array $resources, callable $callback): void
419425
420426 try {
421427 $ this ->dbForProject ->setPreserveDates (true );
428+ $ this ->dbForPlatform ->setPreserveDates (true );
422429
423430 $ responseResource = match ($ resource ->getGroup ()) {
424431 Transfer::GROUP_DATABASES => $ this ->importDatabaseResource ($ resource , $ isLast ),
@@ -427,6 +434,7 @@ protected function import(array $resources, callable $callback): void
427434 Transfer::GROUP_FUNCTIONS => $ this ->importFunctionResource ($ resource ),
428435 Transfer::GROUP_MESSAGING => $ this ->importMessagingResource ($ resource ),
429436 Transfer::GROUP_SITES => $ this ->importSiteResource ($ resource ),
437+ Transfer::GROUP_INTEGRATIONS => $ this ->importIntegrationsResource ($ resource ),
430438 Transfer::GROUP_BACKUPS => $ this ->importBackupResource ($ resource ),
431439 default => throw new \Exception ('Invalid resource group ' , Exception::CODE_VALIDATION ),
432440 };
@@ -445,6 +453,7 @@ protected function import(array $resources, callable $callback): void
445453 $ responseResource = $ resource ;
446454 } finally {
447455 $ this ->dbForProject ->setPreserveDates (false );
456+ $ this ->dbForPlatform ->setPreserveDates (false );
448457 }
449458
450459 $ this ->cache ->update ($ responseResource );
@@ -3055,6 +3064,68 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
30553064 return $ deployment ;
30563065 }
30573066
3067+ /**
3068+ * @throws \Exception
3069+ */
3070+ public function importIntegrationsResource (Resource $ resource ): Resource
3071+ {
3072+ switch ($ resource ->getName ()) {
3073+ case Resource::TYPE_PLATFORM :
3074+ /** @var Platform $resource */
3075+ $ this ->createPlatform ($ resource );
3076+ break ;
3077+ }
3078+
3079+ if ($ resource ->getStatus () !== Resource::STATUS_SKIPPED ) {
3080+ $ resource ->setStatus (Resource::STATUS_SUCCESS );
3081+ }
3082+
3083+ return $ resource ;
3084+ }
3085+
3086+ /**
3087+ * @throws \Throwable
3088+ */
3089+ protected function createPlatform (Platform $ resource ): bool
3090+ {
3091+ $ existing = $ this ->dbForPlatform ->findOne ('platforms ' , [
3092+ Query::equal ('projectId ' , [$ this ->project ]),
3093+ Query::equal ('type ' , [$ resource ->getType ()]),
3094+ Query::equal ('name ' , [$ resource ->getPlatformName ()]),
3095+ ]);
3096+
3097+ if ($ existing !== false && !$ existing ->isEmpty ()) {
3098+ $ resource ->setStatus (Resource::STATUS_SKIPPED , 'Platform already exists ' );
3099+ return false ;
3100+ }
3101+
3102+ $ createdAt = $ this ->normalizeDateTime ($ resource ->getCreatedAt ());
3103+ $ updatedAt = $ this ->normalizeDateTime ($ resource ->getUpdatedAt (), $ createdAt );
3104+
3105+ try {
3106+ $ this ->dbForPlatform ->createDocument ('platforms ' , new UtopiaDocument ([
3107+ '$id ' => ID ::unique (),
3108+ '$permissions ' => $ resource ->getPermissions (),
3109+ 'projectInternalId ' => $ this ->projectInternalId ,
3110+ 'projectId ' => $ this ->project ,
3111+ 'type ' => $ resource ->getType (),
3112+ 'name ' => $ resource ->getPlatformName (),
3113+ 'key ' => $ resource ->getKey (),
3114+ 'store ' => $ resource ->getStore (),
3115+ 'hostname ' => $ resource ->getHostname (),
3116+ '$createdAt ' => $ createdAt ,
3117+ '$updatedAt ' => $ updatedAt ,
3118+ ]));
3119+ } catch (DuplicateException ) {
3120+ $ resource ->setStatus (Resource::STATUS_SKIPPED , 'Platform already exists ' );
3121+ return false ;
3122+ }
3123+
3124+ $ this ->dbForPlatform ->purgeCachedDocument ('projects ' , $ this ->project );
3125+
3126+ return true ;
3127+ }
3128+
30583129 private function validateFieldsForIndexes (Index $ resource , UtopiaDocument $ table , array &$ lengths )
30593130 {
30603131 /**
0 commit comments