001package ca.uhn.fhir.jpa.migrate.tasks.api;
002
003/*-
004 * #%L
005 * HAPI FHIR Server - SQL Migration
006 * %%
007 * Copyright (C) 2014 - 2021 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 com.google.common.collect.Multimap;
025import com.google.common.collect.MultimapBuilder;
026import org.apache.commons.lang3.EnumUtils;
027import org.apache.commons.lang3.Validate;
028import org.flywaydb.core.api.MigrationVersion;
029
030import javax.annotation.Nonnull;
031import java.util.ArrayList;
032import java.util.Collection;
033import java.util.List;
034
035public class BaseMigrationTasks<T extends Enum> {
036        MigrationVersion lastVersion;
037        private Multimap<T, BaseTask> myTasks = MultimapBuilder.hashKeys().arrayListValues().build();
038
039        @SuppressWarnings("unchecked")
040        public List<BaseTask> getTasks(@Nonnull T theFrom, @Nonnull T theTo) {
041                Validate.notNull(theFrom);
042                Validate.notNull(theTo);
043                Validate.isTrue(theFrom.ordinal() < theTo.ordinal(), "From version must be lower than to version");
044
045                List<BaseTask> retVal = new ArrayList<>();
046                for (Object nextVersion : EnumUtils.getEnumList(theFrom.getClass())) {
047                        if (((T) nextVersion).ordinal() <= theFrom.ordinal()) {
048                                continue;
049                        }
050                        if (((T) nextVersion).ordinal() > theTo.ordinal()) {
051                                continue;
052                        }
053
054                        Collection<BaseTask> nextValues = myTasks.get((T) nextVersion);
055                        if (nextValues != null) {
056                                retVal.addAll(nextValues);
057                        }
058                }
059
060                return retVal;
061        }
062
063        public Builder forVersion(T theRelease) {
064                IAcceptsTasks sink = theTask -> {
065                        theTask.validate();
066                        myTasks.put(theRelease, theTask);
067                };
068                return new Builder(toReleaseName(theRelease), sink);
069        }
070
071        @Nonnull
072        protected String toReleaseName(T theRelease) {
073                return theRelease.name();
074        }
075
076        public List<BaseTask> getAllTasks(T[] theVersionEnumValues) {
077                List<BaseTask> retval = new ArrayList<>();
078                for (T nextVersion : theVersionEnumValues) {
079                        Collection<BaseTask> nextValues = myTasks.get(nextVersion);
080                        if (nextValues != null) {
081                                validate(nextValues);
082                                retval.addAll(nextValues);
083                        }
084                }
085
086                return retval;
087        }
088
089        protected BaseTask getTaskWithVersion(String theFlywayVersion) {
090                return myTasks.values().stream()
091                        .filter(task -> theFlywayVersion.equals(task.getFlywayVersion()))
092                        .findFirst()
093                        .get();
094        }
095
096        void validate(Collection<BaseTask> theTasks) {
097                for (BaseTask task : theTasks) {
098                        task.validateVersion();
099                        String version = task.getFlywayVersion();
100                        MigrationVersion migrationVersion = MigrationVersion.fromVersion(version);
101                        if (lastVersion != null) {
102                                if (migrationVersion.compareTo(lastVersion) <= 0) {
103                                        throw new IllegalStateException("Migration version " + migrationVersion + " found after migration version " + lastVersion + ".  Migrations need to be in order by version number.");
104                                }
105                        }
106                        lastVersion = migrationVersion;
107                }
108        }
109
110        public interface IAcceptsTasks {
111                void addTask(BaseTask theTask);
112        }
113}