001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2026 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 org.intellij.lang.annotations.Language; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import java.sql.SQLException; 029import java.util.List; 030import java.util.Set; 031 032public class DropTableTask extends BaseTableTask { 033 034 private static final Logger ourLog = LoggerFactory.getLogger(DropTableTask.class); 035 036 public DropTableTask(String theProductVersion, String theSchemaVersion) { 037 super(theProductVersion, theSchemaVersion); 038 } 039 040 @Override 041 public void validate() { 042 super.validate(); 043 setDescription("Drop table " + getTableName()); 044 } 045 046 @Override 047 public void doExecute() throws SQLException { 048 Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties()); 049 if (!tableNames.contains(getTableName())) { 050 return; 051 } 052 053 Set<String> foreignKeys = JdbcUtils.getForeignKeys(getConnectionProperties(), null, getTableName()); 054 logInfo(ourLog, "Table {} has the following foreign keys: {}", getTableName(), foreignKeys); 055 056 Set<String> indexNames = JdbcUtils.getIndexNames(getConnectionProperties(), getTableName()); 057 logInfo(ourLog, "Table {} has the following indexes: {}", getTableName(), indexNames); 058 059 for (String next : foreignKeys) { 060 if (getDriverType() == DriverTypeEnum.DERBY_EMBEDDED && next.contains("-")) { 061 // Derby creates special internal indexes with GUID names that can't be deleted 062 continue; 063 } 064 List<String> sql = DropForeignKeyTask.generateSql(getTableName(), next, getDriverType()); 065 for (@Language("SQL") String nextSql : sql) { 066 executeSql(getTableName(), nextSql); 067 } 068 } 069 070 DropIndexTask theIndexTask = new DropIndexTask(getProductVersion(), getSchemaVersion()); 071 theIndexTask 072 .setTableName(getTableName()) 073 .setConnectionProperties(getConnectionProperties()) 074 .setDriverType(getDriverType()) 075 .setDryRun(isDryRun()); 076 for (String nextIndex : indexNames) { 077 if (getDriverType() == DriverTypeEnum.DERBY_EMBEDDED && nextIndex.contains("-")) { 078 // Derby creates special internal indexes with GUID names that can't be deleted 079 continue; 080 } 081 theIndexTask.setIndexName(nextIndex).execute(); 082 } 083 084 logInfo(ourLog, "Dropping table: {}", getTableName()); 085 086 @Language("SQL") 087 String sql = "DROP TABLE " + getTableName(); 088 executeSql(getTableName(), sql); 089 } 090}