001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2024 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.i18n.Msg; 023import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; 024import ca.uhn.fhir.jpa.migrate.JdbcUtils; 025import jakarta.annotation.Nonnull; 026import org.apache.commons.lang3.Validate; 027import org.apache.commons.lang3.builder.EqualsBuilder; 028import org.apache.commons.lang3.builder.HashCodeBuilder; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032import java.sql.SQLException; 033import java.util.ArrayList; 034import java.util.List; 035import java.util.Set; 036 037import static org.apache.commons.lang3.StringUtils.isNotBlank; 038 039public class DropForeignKeyTask extends BaseTableTask { 040 041 private static final Logger ourLog = LoggerFactory.getLogger(DropForeignKeyTask.class); 042 private String myConstraintName; 043 private String myParentTableName; 044 045 public DropForeignKeyTask(String theProductVersion, String theSchemaVersion) { 046 super(theProductVersion, theSchemaVersion); 047 } 048 049 @Nonnull 050 static List<String> generateSql(String theTableName, String theConstraintName, DriverTypeEnum theDriverType) { 051 List<String> sqls = new ArrayList<>(); 052 switch (theDriverType) { 053 case MYSQL_5_7: 054 case MARIADB_10_1: 055 // Lousy MYQL.... 056 sqls.add("alter table " + theTableName + " drop foreign key " + theConstraintName); 057 break; 058 case POSTGRES_9_4: 059 case DERBY_EMBEDDED: 060 case H2_EMBEDDED: 061 case ORACLE_12C: 062 case MSSQL_2012: 063 sqls.add("alter table " + theTableName + " drop constraint " + theConstraintName); 064 break; 065 case COCKROACHDB_21_1: 066 sqls.add("drop index if exists " + theTableName + "@" + theConstraintName + " cascade"); 067 break; 068 default: 069 throw new IllegalStateException(Msg.code(59)); 070 } 071 return sqls; 072 } 073 074 public void setConstraintName(String theConstraintName) { 075 myConstraintName = theConstraintName; 076 } 077 078 public void setParentTableName(String theParentTableName) { 079 myParentTableName = theParentTableName; 080 } 081 082 @Override 083 public void validate() { 084 super.validate(); 085 086 Validate.isTrue(isNotBlank(myConstraintName)); 087 Validate.isTrue(isNotBlank(myParentTableName)); 088 setDescription("Drop foreign key " + myConstraintName + " from table " + getTableName()); 089 } 090 091 @Override 092 public void doExecute() throws SQLException { 093 094 Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), myParentTableName, getTableName()); 095 if (!existing.contains(myConstraintName)) { 096 logInfo(ourLog, "Don't have constraint named {} - No action performed", myConstraintName); 097 return; 098 } 099 100 List<String> sqls = generateSql(getTableName(), myConstraintName, getDriverType()); 101 102 for (String next : sqls) { 103 executeSql(getTableName(), next); 104 } 105 } 106 107 @Override 108 protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { 109 DropForeignKeyTask otherObject = (DropForeignKeyTask) theOtherObject; 110 super.generateEquals(theBuilder, otherObject); 111 theBuilder.append(myConstraintName, otherObject.myConstraintName); 112 theBuilder.append(myParentTableName, otherObject.myParentTableName); 113 } 114 115 @Override 116 protected void generateHashCode(HashCodeBuilder theBuilder) { 117 super.generateHashCode(theBuilder); 118 theBuilder.append(myConstraintName); 119 theBuilder.append(myParentTableName); 120 } 121}