001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2026 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.migrate; 021 022import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask; 023import jakarta.annotation.Nonnull; 024import org.flywaydb.core.api.MigrationVersion; 025 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.Iterator; 029import java.util.List; 030import java.util.Set; 031import java.util.function.Consumer; 032import java.util.function.Predicate; 033import java.util.stream.Collectors; 034 035public class MigrationTaskList implements Iterable<BaseTask> { 036 private final List<BaseTask> myTasks; 037 038 public MigrationTaskList() { 039 myTasks = new ArrayList<>(); 040 } 041 042 public MigrationTaskList(List<BaseTask> theTasks) { 043 myTasks = theTasks; 044 } 045 046 public void addAll(Collection<BaseTask> theTasks) { 047 myTasks.addAll(theTasks); 048 } 049 050 public void setDoNothingOnSkippedTasks(String theSkipVersions) { 051 MigrationTaskSkipper.setDoNothingOnSkippedTasks(myTasks, theSkipVersions); 052 } 053 054 public int size() { 055 return myTasks.size(); 056 } 057 058 public MigrationTaskList diff(Set<MigrationVersion> theAppliedMigrationVersions) { 059 List<BaseTask> unappliedTasks = myTasks.stream() 060 .filter(task -> 061 !theAppliedMigrationVersions.contains(MigrationVersion.fromVersion(task.getMigrationVersion()))) 062 .collect(Collectors.toList()); 063 return new MigrationTaskList(unappliedTasks); 064 } 065 066 public MigrationTaskList getUnskippableTasks() { 067 List<BaseTask> tasks = 068 myTasks.stream().filter(t -> !t.isHeavyweightSkippableTask()).collect(Collectors.toList()); 069 return new MigrationTaskList(tasks); 070 } 071 072 public void append(Iterable<BaseTask> theMigrationTasks) { 073 for (BaseTask next : theMigrationTasks) { 074 myTasks.add(next); 075 } 076 } 077 078 public void add(BaseTask theTask) { 079 myTasks.add(theTask); 080 } 081 082 public void clear() { 083 myTasks.clear(); 084 } 085 086 @Nonnull 087 @Override 088 public Iterator<BaseTask> iterator() { 089 return myTasks.iterator(); 090 } 091 092 public void forEach(Consumer<? super BaseTask> theAction) { 093 myTasks.forEach(theAction); 094 } 095 096 public String getLastVersion() { 097 return myTasks.stream() 098 .map(BaseTask::getMigrationVersion) 099 .map(MigrationVersion::fromVersion) 100 .sorted() 101 .map(MigrationVersion::toString) 102 .reduce((first, second) -> second) 103 .orElse(null); 104 } 105 106 public void removeIf(Predicate<BaseTask> theFilter) { 107 myTasks.removeIf(theFilter); 108 } 109 110 public BaseTask[] toTaskArray() { 111 return myTasks.toArray(new BaseTask[0]); 112 } 113}