package org.opencb.opencga.app.migrations.v2_4_10.catalog;

import org.opencb.opencga.catalog.migration.Migration;
import org.opencb.opencga.catalog.migration.MigrationTool;

@Migration(id = "AddMissingEndDateOnFinishedJobs" ,
        description = "Add missing end date on finished jobs",
        version = "2.4.10",
        domain = Migration.MigrationDomain.CATALOG,
        language = Migration.MigrationLanguage.JAVA,
        date = 20221026
)
public class AddMissingEndDateOnFinishedJobs extends MigrationTool {
    @Override
    protected void run() throws Exception {
        throw new IllegalStateException("PENDING");
    }
}

@@ -64,113 +64,519 @@
 ort 
-org.opencb.opencga.catalog.migration.Migration;%0Aimport org.opencb.opencga.catalog.migration.MigrationTool
+com.mongodb.client.model.Filters;%0Aimport com.mongodb.client.model.Projections;%0Aimport com.mongodb.client.model.UpdateOneModel;%0Aimport org.bson.Document;%0Aimport org.opencb.opencga.catalog.db.mongodb.MongoDBAdaptorFactory;%0Aimport org.opencb.opencga.catalog.migration.Migration;%0Aimport org.opencb.opencga.catalog.migration.MigrationTool;%0Aimport org.opencb.opencga.core.common.TimeUtils;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport static com.mongodb.client.model.Filters.eq
 ;%0A%0A@
@@ -595,36 +595,41 @@
  = %22
-AddM
+add_m
 issing
-E
+_e
 ndDate
-OnF
+_on_f
 inished
-J
+_j
 obs%22
@@ -922,16 +922,17 @@
 nTool %7B%0A
+%0A
     @Ove
@@ -993,49 +993,2116 @@
     
-throw new IllegalStateException(%22PENDING%22
+migrateCollection(Arrays.asList(MongoDBAdaptorFactory.JOB_COLLECTION, MongoDBAdaptorFactory.DELETED_JOB_COLLECTION),%0A                Filters.and(Arrays.asList(%0A                        Filters.in(%22internal.status.id%22, Arrays.asList(%22DONE%22, %22ERROR%22, %22ABORTED%22, %22UNKNOWN%22, %22READY%22)),%0A                        Filters.or(Arrays.asList(%0A                                Filters.exists(%22execution.end%22, false),%0A                                Filters.eq(%22execution.end%22, null))%0A                        )%0A                )),%0A                Projections.include(Arrays.asList(%22execution%22, %22_modificationDate%22)),%0A                (document, bulk) -%3E %7B%0A                    Document execution = document.get(%22execution%22, Document.class);%0A                    if (execution != null) %7B%0A                        // Add end date%0A                        Object modificationDate = document.get(%22_modificationDate%22);%0A                        if (modificationDate == null) %7B%0A                            modificationDate = TimeUtils.getDate();%0A                        %7D%0A                        execution.put(%22end%22, modificationDate);%0A%0A                        // Add event%0A                        List%3CDocument%3E events = execution.getList(%22events%22, Document.class);%0A                        if (events == null) %7B%0A                            events = new ArrayList%3C%3E();%0A                        %7D else %7B%0A                            events = new ArrayList%3C%3E(events);%0A                        %7D%0A                        events.add(new Document()%0A                                .append(%22type%22, %22WARNING%22)%0A                                .append(%22id%22, %22missing-execution-end-date%22)%0A                                .append(%22message%22, %22Missing execution field 'end'. End date set as part of migration fix.%22));%0A                        execution.put(%22events%22, events);%0A%0A                        bulk.add(new UpdateOneModel%3C%3E(%0A                                eq(%22_id%22, document.get(%22_id%22)),%0A                                new Document(%22$set%22, new Document(%22execution%22, execution)))%0A                        );%0A                    %7D%0A                %7D
 );%0A 
@@ -3106,10 +3106,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

add migration script, #TASK-2237