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