001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2025 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.taskdef; 021 022import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; 023import ca.uhn.fhir.jpa.migrate.JdbcUtils; 024import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; 025import ca.uhn.fhir.jpa.migrate.tasks.api.TaskFlagEnum; 026import org.apache.commons.lang3.builder.EqualsBuilder; 027import org.apache.commons.lang3.builder.HashCodeBuilder; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import java.sql.SQLException; 032import java.util.List; 033import java.util.Set; 034 035public class InitializeSchemaTask extends BaseTask { 036 private static final String DESCRIPTION_PREFIX = "Initialize schema for "; 037 private static final Logger ourLog = LoggerFactory.getLogger(InitializeSchemaTask.class); 038 private final ISchemaInitializationProvider mySchemaInitializationProvider; 039 private boolean myInitializedSchema; 040 041 public InitializeSchemaTask( 042 String theProductVersion, 043 String theSchemaVersion, 044 ISchemaInitializationProvider theSchemaInitializationProvider) { 045 super(theProductVersion, theSchemaVersion); 046 mySchemaInitializationProvider = theSchemaInitializationProvider; 047 setDescription(DESCRIPTION_PREFIX + mySchemaInitializationProvider.getSchemaDescription()); 048 addFlag(TaskFlagEnum.RUN_DURING_SCHEMA_INITIALIZATION); 049 } 050 051 @Override 052 public void validate() { 053 // nothing 054 } 055 056 @Override 057 public void doExecute() throws SQLException { 058 DriverTypeEnum driverType = getDriverType(); 059 060 Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties()); 061 String schemaExistsIndicatorTable = mySchemaInitializationProvider.getSchemaExistsIndicatorTable(); 062 if (tableNames.contains(schemaExistsIndicatorTable)) { 063 logInfo( 064 ourLog, 065 "The table {} already exists. Skipping schema initialization for {}", 066 schemaExistsIndicatorTable, 067 driverType); 068 return; 069 } 070 071 logInfo( 072 ourLog, 073 "Initializing {} schema for {} with dry-run: {}", 074 driverType, 075 mySchemaInitializationProvider.getSchemaDescription(), 076 isDryRun()); 077 078 List<String> sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType); 079 080 for (String nextSql : sqlStatements) { 081 executeSql(null, nextSql); 082 } 083 084 if (mySchemaInitializationProvider.canInitializeSchema()) { 085 myInitializedSchema = true; 086 } 087 088 logInfo( 089 ourLog, 090 "{} schema for {} initialized successfully", 091 driverType, 092 mySchemaInitializationProvider.getSchemaDescription()); 093 } 094 095 @Override 096 public boolean initializedSchema() { 097 return myInitializedSchema; 098 } 099 100 @Override 101 protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { 102 InitializeSchemaTask otherObject = (InitializeSchemaTask) theOtherObject; 103 theBuilder.append(getSchemaInitializationProvider(), otherObject.getSchemaInitializationProvider()); 104 } 105 106 @Override 107 protected void generateHashCode(HashCodeBuilder theBuilder) { 108 theBuilder.append(getSchemaInitializationProvider()); 109 } 110 111 public ISchemaInitializationProvider getSchemaInitializationProvider() { 112 return mySchemaInitializationProvider; 113 } 114}