001package ca.uhn.fhir.jpa.migrate;
002
003/*-
004 * #%L
005 * HAPI FHIR Server - SQL Migration
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
024import org.flywaydb.core.api.MigrationVersion;
025
026import javax.annotation.Nonnull;
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.Iterator;
030import java.util.List;
031import java.util.Set;
032import java.util.function.Consumer;
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 -> !theAppliedMigrationVersions.contains(MigrationVersion.fromVersion(task.getMigrationVersion())))
061                        .collect(Collectors.toList());
062                return new MigrationTaskList(unappliedTasks);
063        }
064
065        public void append(Iterable<BaseTask> theMigrationTasks) {
066                for (BaseTask next : theMigrationTasks) {
067                        myTasks.add(next);
068                }
069        }
070
071        public void add(BaseTask theTask) {
072                myTasks.add(theTask);
073        }
074
075        public void clear() {
076                myTasks.clear();
077        }
078
079        @Nonnull
080        @Override
081        public Iterator<BaseTask> iterator() {
082                return myTasks.iterator();
083        }
084
085        public void forEach(Consumer<? super BaseTask> theAction) {
086                myTasks.forEach(theAction);
087        }
088
089        public String getLastVersion() {
090                return myTasks.stream()
091                        .map(BaseTask::getMigrationVersion)
092                        .map(MigrationVersion::fromVersion)
093                        .sorted()
094                        .map(MigrationVersion::toString)
095                        .reduce((first, second) -> second)
096                        .orElse(null);
097        }
098}