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 org.apache.commons.lang3.Validate; 025import org.flywaydb.core.api.callback.Callback; 026 027import javax.annotation.Nonnull; 028import javax.sql.DataSource; 029import java.util.ArrayList; 030import java.util.Collections; 031import java.util.List; 032import java.util.Objects; 033 034public abstract class BaseMigrator implements IMigrator { 035 private final List<BaseTask.ExecutedStatement> myExecutedStatements = new ArrayList<>(); 036 private boolean myDryRun; 037 private boolean myNoColumnShrink; 038 private boolean mySchemaWasInitialized; 039 private DriverTypeEnum myDriverType; 040 private DataSource myDataSource; 041 private boolean myStrictOrder; 042 private List<Callback> myCallbacks = Collections.emptyList(); 043 044 @Nonnull 045 public List<Callback> getCallbacks() { 046 return myCallbacks; 047 } 048 049 public void setCallbacks(@Nonnull List<Callback> theCallbacks) { 050 Validate.notNull(theCallbacks); 051 myCallbacks = theCallbacks; 052 } 053 054 public DataSource getDataSource() { 055 return myDataSource; 056 } 057 058 public void setDataSource(DataSource theDataSource) { 059 myDataSource = theDataSource; 060 } 061 062 public boolean isDryRun() { 063 return myDryRun; 064 } 065 066 public void setDryRun(boolean theDryRun) { 067 myDryRun = theDryRun; 068 } 069 070 public boolean isNoColumnShrink() { 071 return myNoColumnShrink; 072 } 073 074 public void setNoColumnShrink(boolean theNoColumnShrink) { 075 myNoColumnShrink = theNoColumnShrink; 076 } 077 078 public DriverTypeEnum getDriverType() { 079 return myDriverType; 080 } 081 082 public void setDriverType(DriverTypeEnum theDriverType) { 083 myDriverType = theDriverType; 084 } 085 086 public boolean isStrictOrder() { 087 return myStrictOrder; 088 } 089 090 public void setStrictOrder(boolean theStrictOrder) { 091 myStrictOrder = theStrictOrder; 092 } 093 094 public void addExecutedStatements(List theExecutedStatements) { 095 myExecutedStatements.addAll(theExecutedStatements); 096 } 097 098 protected StringBuilder buildExecutedStatementsString() { 099 StringBuilder statementBuilder = new StringBuilder(); 100 String lastTable = null; 101 for (BaseTask.ExecutedStatement next : myExecutedStatements) { 102 if (!Objects.equals(lastTable, next.getTableName())) { 103 statementBuilder.append("\n\n-- Table: ").append(next.getTableName()).append("\n"); 104 lastTable = next.getTableName(); 105 } 106 107 statementBuilder.append(next.getSql()).append(";\n"); 108 109 for (Object nextArg : next.getArguments()) { 110 statementBuilder.append(" -- Arg: ").append(nextArg).append("\n"); 111 } 112 } 113 return statementBuilder; 114 } 115 116 public boolean isSchemaWasInitialized() { 117 return mySchemaWasInitialized; 118 } 119 120 public BaseMigrator setSchemaWasInitialized(boolean theSchemaWasInitialized) { 121 mySchemaWasInitialized = theSchemaWasInitialized; 122 return this; 123 } 124}