@@ -37,7 +37,7 @@ class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
3737
3838 public ?Team $ team = null ;
3939
40- public Server $ server ;
40+ public ? Server $ server = null ;
4141
4242 public StandalonePostgresql |StandaloneMongodb |StandaloneMysql |StandaloneMariadb |ServiceDatabase $ database ;
4343
@@ -47,7 +47,7 @@ class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
4747
4848 public ?ScheduledDatabaseBackupExecution $ backup_log = null ;
4949
50- public string $ backup_status = ' failed ' ;
50+ public string $ backup_status = ScheduledDatabaseBackupExecution:: STATUS_FAILED ;
5151
5252 public ?string $ backup_location = null ;
5353
@@ -101,7 +101,13 @@ public function handle(): void
101101 }
102102 if (data_get ($ this ->backup , 'database_type ' ) === ServiceDatabase::class) {
103103 $ this ->database = data_get ($ this ->backup , 'database ' );
104- $ this ->server = $ this ->database ->service ->server ;
104+ if ($ this ->database ->service_id ) {
105+ $ this ->server = $ this ->database ->service ->server ;
106+ } elseif ($ this ->database ->application_id ) {
107+ $ this ->server = data_get ($ this ->database , 'application.destination.server ' );
108+ } elseif ($ this ->database ->application_preview_id ) {
109+ $ this ->server = data_get ($ this ->database , 'application_preview.application.destination.server ' );
110+ }
105111 $ this ->s3 = $ this ->backup ->s3 ;
106112 } else {
107113 $ this ->database = data_get ($ this ->backup , 'database ' );
@@ -115,6 +121,28 @@ public function handle(): void
115121 throw new \Exception ('Database not found?! ' );
116122 }
117123
124+ if ($ this ->database instanceof ServiceDatabase) {
125+ $ applicationId = $ this ->database ->application_id ;
126+ $ pullRequestId = 0 ;
127+ if ($ this ->database ->application_preview_id ) {
128+ $ applicationId = data_get ($ this ->database ->application_preview , 'application_id ' );
129+ $ pullRequestId = data_get ($ this ->database ->application_preview , 'pull_request_id ' );
130+ }
131+
132+ if ($ applicationId ) {
133+ $ deploymentInProgress = \App \Models \ApplicationDeploymentQueue::where ('application_id ' , $ applicationId )
134+ ->where ('pull_request_id ' , $ pullRequestId )
135+ ->where ('status ' , \App \Enums \ApplicationDeploymentStatus::IN_PROGRESS ->value )
136+ ->exists ();
137+
138+ if ($ deploymentInProgress ) {
139+ $ this ->release (60 );
140+
141+ return ;
142+ }
143+ }
144+ }
145+
118146 $ this ->markStaleExecutionsAsFailed ();
119147
120148 BackupCreated::dispatch ($ this ->team ->id );
@@ -131,8 +159,19 @@ public function handle(): void
131159 }
132160 if (data_get ($ this ->backup , 'database_type ' ) === ServiceDatabase::class) {
133161 $ databaseType = $ this ->database ->databaseType ();
134- $ serviceUuid = $ this ->database ->service ->uuid ;
135- $ serviceName = str ($ this ->database ->service ->name )->slug ();
162+ if ($ this ->database ->service_id ) {
163+ $ serviceUuid = $ this ->database ->service ->uuid ;
164+ $ serviceName = str ($ this ->database ->service ->name )->slug ();
165+ } else {
166+ if ($ this ->database ->application_id ) {
167+ $ serviceUuid = generateApplicationContainerName ($ this ->database ->application );
168+ $ serviceName = str ($ this ->database ->application ->name )->slug ();
169+ } else {
170+ $ preview = $ this ->database ->application_preview ;
171+ $ serviceUuid = generateApplicationContainerName ($ preview ->application , $ preview ->pull_request_id );
172+ $ serviceName = str ($ preview ->application ->name )->slug ();
173+ }
174+ }
136175 if (str ($ databaseType )->contains ('postgres ' )) {
137176 $ this ->container_name = "{$ this ->database ->name }- $ serviceUuid " ;
138177 $ this ->directory_name = $ serviceName .'- ' .$ this ->container_name ;
@@ -489,6 +528,18 @@ public function handle(): void
489528 ]);
490529 }
491530 }
531+
532+ if ($ this ->database instanceof ServiceDatabase) {
533+ if ($ this ->database ->application_id || $ this ->database ->application_preview_id ) {
534+ $ application = $ this ->database ->application_id
535+ ? $ this ->database ->application
536+ : $ this ->database ->application_preview ?->application;
537+
538+ if ($ application ) {
539+ queue_next_deployment ($ application );
540+ }
541+ }
542+ }
492543 }
493544
494545 private function backup_standalone_mongodb (string $ databaseWithCollections ): void
@@ -684,7 +735,13 @@ private function upload_to_s3(): void
684735 $ endpoint = $ this ->s3 ->endpoint ;
685736 $ this ->s3 ->testConnection (shouldSave: true );
686737 if (data_get ($ this ->backup , 'database_type ' ) === ServiceDatabase::class) {
687- $ network = $ this ->database ->service ->destination ->network ;
738+ if ($ this ->database ->service_id ) {
739+ $ network = $ this ->database ->service ->destination ->network ;
740+ } elseif ($ this ->database ->application_id ) {
741+ $ network = $ this ->database ->application ->destination ->network ;
742+ } elseif ($ this ->database ->application_preview_id ) {
743+ $ network = $ this ->database ->application_preview ->application ->destination ->network ;
744+ }
688745 } else {
689746 $ network = $ this ->database ->destination ->network ;
690747 }
@@ -743,13 +800,13 @@ private function markStaleExecutionsAsFailed(): void
743800 $ timeoutSeconds = ($ this ->backup ->timeout ?? 3600 ) * 2 ;
744801
745802 $ staleExecutions = $ this ->backup ->executions ()
746- ->where ('status ' , ' running ' )
803+ ->where ('status ' , ScheduledDatabaseBackupExecution:: STATUS_RUNNING )
747804 ->where ('created_at ' , '< ' , now ()->subSeconds ($ timeoutSeconds ))
748805 ->get ();
749806
750807 foreach ($ staleExecutions as $ execution ) {
751808 $ execution ->update ([
752- 'status ' => ' failed ' ,
809+ 'status ' => ScheduledDatabaseBackupExecution:: STATUS_FAILED ,
753810 'message ' => 'Marked as failed - backup execution exceeded maximum allowed time ' ,
754811 'finished_at ' => now (),
755812 ]);
@@ -781,9 +838,9 @@ public function failed(?Throwable $exception): void
781838 // Don't overwrite a successful backup status — a post-backup error
782839 // (e.g. notification failure) should not retroactively mark the backup
783840 // as failed (see GitHub issue #9088)
784- if ($ log ->status !== ' success ' ) {
841+ if ($ log ->status !== ScheduledDatabaseBackupExecution:: STATUS_SUCCESS ) {
785842 $ log ->update ([
786- 'status ' => ' failed ' ,
843+ 'status ' => ScheduledDatabaseBackupExecution:: STATUS_FAILED ,
787844 'message ' => 'Job permanently failed after ' .$ this ->attempts ().' attempts: ' .($ exception ?->getMessage() ?? 'Unknown error ' ),
788845 'size ' => 0 ,
789846 'filename ' => null ,
@@ -793,7 +850,7 @@ public function failed(?Throwable $exception): void
793850 }
794851
795852 // Notify team about permanent failure (only if backup didn't already succeed)
796- if ($ this ->team && $ log ?->status !== ' success ' ) {
853+ if ($ this ->team && $ log ?->status !== ScheduledDatabaseBackupExecution:: STATUS_SUCCESS ) {
797854 $ databaseName = $ log ?->database_name ?? 'unknown ' ;
798855 $ output = $ this ->backup_output ?? $ exception ?->getMessage() ?? 'Unknown error ' ;
799856 try {
0 commit comments