@@ -19,7 +19,7 @@ import {
1919} from "@dokploy/server/utils/process/execAsync" ;
2020import { TRPCError } from "@trpc/server" ;
2121import { format } from "date-fns" ;
22- import { desc , eq } from "drizzle-orm" ;
22+ import { and , desc , eq , or } from "drizzle-orm" ;
2323import {
2424 type Application ,
2525 findApplicationById ,
@@ -852,110 +852,24 @@ export const findAllDeploymentsByServerId = async (serverId: string) => {
852852 return deploymentsList ;
853853} ;
854854
855- export const clearOldDeploymentsByApplicationId = async (
856- applicationId : string ,
855+ export const clearOldDeployments = async (
856+ id : string ,
857+ type : "application" | "compose" = "application" ,
857858) => {
858859 // Get all deployments ordered by creation date (newest first)
859860 const deploymentsList = await db . query . deployments . findMany ( {
860- where : eq ( deployments . applicationId , applicationId ) ,
861- orderBy : desc ( deployments . createdAt ) ,
862- } ) ;
863-
864- // Find the most recent successful deployment (status "done")
865- const activeDeployment = deploymentsList . find (
866- ( deployment ) => deployment . status === "done" ,
867- ) ;
868-
869- // If there's an active deployment, keep it and remove all others
870- // If there's no active deployment, keep the most recent one and remove the rest
871- const deploymentsToKeep : string [ ] = [ ] ;
872-
873- if ( activeDeployment ) {
874- deploymentsToKeep . push ( activeDeployment . deploymentId ) ;
875- } else if ( deploymentsList . length > 0 ) {
876- // Keep the most recent deployment even if it's not "done"
877- deploymentsToKeep . push ( deploymentsList [ 0 ] ! . deploymentId ) ;
878- }
879-
880- const deploymentsToDelete = deploymentsList . filter (
881- ( deployment ) => ! deploymentsToKeep . includes ( deployment . deploymentId ) ,
882- ) ;
883-
884- // Delete old deployments and their log files
885- for ( const deployment of deploymentsToDelete ) {
886- if ( deployment . rollbackId ) {
887- await removeRollbackById ( deployment . rollbackId ) ;
888- }
889-
890- // Remove log file if it exists
891- const logPath = deployment . logPath ;
892- if ( logPath && logPath !== "." && existsSync ( logPath ) ) {
893- try {
894- await fsPromises . unlink ( logPath ) ;
895- } catch ( error ) {
896- console . error ( `Error removing log file ${ logPath } :` , error ) ;
897- }
898- }
899-
900- // Delete deployment from database
901- await removeDeployment ( deployment . deploymentId ) ;
902- }
903-
904- return {
905- deletedCount : deploymentsToDelete . length ,
906- keptDeployment : deploymentsToKeep [ 0 ] || null ,
907- } ;
908- } ;
909-
910- export const clearOldDeploymentsByComposeId = async ( composeId : string ) => {
911- // Get all deployments ordered by creation date (newest first)
912- const deploymentsList = await db . query . deployments . findMany ( {
913- where : eq ( deployments . composeId , composeId ) ,
861+ where : and (
862+ eq ( deployments [ `${ type } Id` ] , id ) ,
863+ or ( eq ( deployments . status , "done" ) , eq ( deployments . status , "error" ) ) ,
864+ ) ,
914865 orderBy : desc ( deployments . createdAt ) ,
915866 } ) ;
916867
917- // Find the most recent successful deployment (status "done")
918- const activeDeployment = deploymentsList . find (
919- ( deployment ) => deployment . status === "done" ,
920- ) ;
921-
922- // If there's an active deployment, keep it and remove all others
923- // If there's no active deployment, keep the most recent one and remove the rest
924- const deploymentsToKeep : string [ ] = [ ] ;
925-
926- if ( activeDeployment ) {
927- deploymentsToKeep . push ( activeDeployment . deploymentId ) ;
928- } else if ( deploymentsList . length > 0 ) {
929- // Keep the most recent deployment even if it's not "done"
930- deploymentsToKeep . push ( deploymentsList [ 0 ] ! . deploymentId ) ;
931- }
932-
933- const deploymentsToDelete = deploymentsList . filter (
934- ( deployment ) => ! deploymentsToKeep . includes ( deployment . deploymentId ) ,
935- ) ;
936-
937- // Delete old deployments and their log files
938- for ( const deployment of deploymentsToDelete ) {
939- if ( deployment . rollbackId ) {
940- await removeRollbackById ( deployment . rollbackId ) ;
941- }
942-
943- // Remove log file if it exists
944- const logPath = deployment . logPath ;
945- if ( logPath && logPath !== "." && existsSync ( logPath ) ) {
946- try {
947- await fsPromises . unlink ( logPath ) ;
948- } catch ( error ) {
949- console . error ( `Error removing log file ${ logPath } :` , error ) ;
950- }
951- }
952-
953- // Delete deployment from database
868+ for ( const deployment of deploymentsList ) {
954869 await removeDeployment ( deployment . deploymentId ) ;
955870 }
956871
957872 return {
958- deletedCount : deploymentsToDelete . length ,
959- keptDeployment : deploymentsToKeep [ 0 ] || null ,
873+ deletedCount : deploymentsList . length ,
960874 } ;
961875} ;
0 commit comments