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 java.util.Collection; 032import javax.annotation.Nonnull; 033 034public class BaseMigrationTasks<T extends Enum> { 035 MigrationVersion lastVersion; 036 private Multimap<T, BaseTask> myTasks = 037 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 = 090 MigrationVersion.fromVersion(theMigrationVersion).getVersion(); 091 092 return myTasks.values().stream() 093 .filter(task -> expectedVersion.equals(task.getMigrationVersion())) 094 .findFirst() 095 .get(); 096 } 097 098 void validate(Collection<BaseTask> theTasks) { 099 for (BaseTask task : theTasks) { 100 task.validateVersion(); 101 String version = task.getMigrationVersion(); 102 MigrationVersion migrationVersion = MigrationVersion.fromVersion(version); 103 if (lastVersion != null) { 104 if (migrationVersion.compareTo(lastVersion) <= 0) { 105 throw new IllegalStateException( 106 Msg.code(51) + "Migration version " + migrationVersion + " found after migration version " 107 + lastVersion + ". Migrations need to be in order by version number."); 108 } 109 } 110 lastVersion = migrationVersion; 111 } 112 } 113 114 public interface IAcceptsTasks { 115 void addTask(BaseTask theTask); 116 } 117}