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;
021
022import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
023import org.apache.commons.lang3.StringUtils;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import java.util.Collection;
028import java.util.Set;
029import java.util.stream.Collectors;
030import java.util.stream.Stream;
031
032import static org.apache.commons.lang3.StringUtils.isBlank;
033
034public class MigrationTaskSkipper {
035        private static final Logger ourLog = LoggerFactory.getLogger(MigrationTaskSkipper.class);
036
037        public static void setDoNothingOnSkippedTasks(Collection<BaseTask> theTasks, String theSkipVersions) {
038                if (isBlank(theSkipVersions) || theTasks.isEmpty()) {
039                        return;
040                }
041                Set<String> skippedVersionSet = Stream.of(theSkipVersions.split(","))
042                                .map(String::trim)
043                                // TODO KHS filter out all characters that aren't numbers, periods and underscores
044                                .map(s -> s.replace("'", ""))
045                                .map(s -> s.replace("\"", ""))
046                                .filter(StringUtils::isNotBlank)
047                                .collect(Collectors.toSet());
048
049                for (BaseTask task : theTasks) {
050                        if (skippedVersionSet.contains(task.getMigrationVersion())) {
051                                ourLog.info("Will skip {}: {}", task.getMigrationVersion(), task.getDescription());
052                                task.setDoNothing(true);
053                        }
054                }
055        }
056}