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