001package ca.uhn.fhir.jpa.migrate; 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.taskdef.BaseTask; 025import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 026import org.flywaydb.core.api.MigrationInfoService; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030import java.sql.SQLException; 031import java.util.ArrayList; 032import java.util.List; 033import java.util.Optional; 034 035/** 036 * This class is an alternative to {@link FlywayMigrator). It doesn't use Flyway, but instead just 037 * executes all tasks. 038 */ 039public class TaskOnlyMigrator extends BaseMigrator { 040 041 private static final Logger ourLog = LoggerFactory.getLogger(TaskOnlyMigrator.class); 042 private List<BaseTask> myTasks = new ArrayList<>(); 043 044 @Override 045 public void migrate() { 046 DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getDataSource()); 047 048 for (BaseTask next : myTasks) { 049 next.setDriverType(getDriverType()); 050 next.setDryRun(isDryRun()); 051 next.setNoColumnShrink(isNoColumnShrink()); 052 next.setConnectionProperties(connectionProperties); 053 054 try { 055 if (isDryRun()) { 056 ourLog.info("Dry run {} {}", next.getFlywayVersion(), next.getDescription()); 057 } else { 058 ourLog.info("Executing {} {}", next.getFlywayVersion(), next.getDescription()); 059 } 060 next.execute(); 061 addExecutedStatements(next.getExecutedStatements()); 062 } catch (SQLException e) { 063 throw new InternalErrorException(Msg.code(48) + e); 064 } 065 } 066 if (isDryRun()) { 067 StringBuilder statementBuilder = buildExecutedStatementsString(); 068 ourLog.info("SQL that would be executed:\n\n***********************************\n{}***********************************", statementBuilder); 069 } 070 } 071 072 @Override 073 public Optional<MigrationInfoService> getMigrationInfo() { 074 return Optional.empty(); 075 } 076 077 @Override 078 public void addTasks(List<BaseTask> theMigrationTasks) { 079 myTasks.addAll(theMigrationTasks); 080 } 081}