001package ca.uhn.fhir.jpa.migrate.tasks.api;
002
003/*-
004 * #%L
005 * HAPI FHIR Server - SQL Migration
006 * %%
007 * Copyright (C) 2014 - 2022 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.i18n.Msg;
024import ca.uhn.fhir.jpa.migrate.MigrationTaskList;
025import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
026import com.google.common.collect.Multimap;
027import com.google.common.collect.MultimapBuilder;
028import org.apache.commons.lang3.EnumUtils;
029import org.apache.commons.lang3.Validate;
030import org.flywaydb.core.api.MigrationVersion;
031
032import javax.annotation.Nonnull;
033import java.util.Collection;
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 MigrationTaskList getTaskList(@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                MigrationTaskList retVal = new MigrationTaskList();
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                        retVal.addAll(nextValues);
056                }
057
058                return retVal;
059        }
060
061        public Builder forVersion(T theRelease) {
062                IAcceptsTasks sink = theTask -> {
063                        theTask.validate();
064                        myTasks.put(theRelease, theTask);
065                };
066                return new Builder(toReleaseName(theRelease), sink);
067        }
068
069        @Nonnull
070        protected String toReleaseName(T theRelease) {
071                return theRelease.name();
072        }
073
074        public MigrationTaskList getAllTasks(T[] theVersionEnumValues) {
075                MigrationTaskList retval = new MigrationTaskList();
076                for (T nextVersion : theVersionEnumValues) {
077                        Collection<BaseTask> nextValues = myTasks.get(nextVersion);
078                        if (nextValues != null) {
079                                validate(nextValues);
080                                retval.addAll(nextValues);
081                        }
082                }
083
084                return retval;
085        }
086
087        protected BaseTask getTaskWithVersion(String theMigrationVersion) {
088                // First normalize the version number
089                String expectedVersion = MigrationVersion.fromVersion(theMigrationVersion).getVersion();
090
091                return myTasks.values().stream()
092                        .filter(task -> expectedVersion.equals(task.getMigrationVersion()))
093                        .findFirst()
094                        .get();
095        }
096
097        void validate(Collection<BaseTask> theTasks) {
098                for (BaseTask task : theTasks) {
099                        task.validateVersion();
100                        String version = task.getMigrationVersion();
101                        MigrationVersion migrationVersion = MigrationVersion.fromVersion(version);
102                        if (lastVersion != null) {
103                                if (migrationVersion.compareTo(lastVersion) <= 0) {
104                                        throw new IllegalStateException(Msg.code(51) + "Migration version " + migrationVersion + " found after migration version " + lastVersion + ".  Migrations need to be in order by version number.");
105                                }
106                        }
107                        lastVersion = migrationVersion;
108                }
109        }
110
111        public interface IAcceptsTasks {
112                void addTask(BaseTask theTask);
113        }
114}