001package ca.uhn.fhir.jpa.migrate.taskdef; 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.DriverTypeEnum; 024import ca.uhn.fhir.jpa.migrate.JdbcUtils; 025import org.apache.commons.lang3.Validate; 026import org.apache.commons.lang3.builder.EqualsBuilder; 027import org.apache.commons.lang3.builder.HashCodeBuilder; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import javax.annotation.Nonnull; 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 default: 066 throw new IllegalStateException(); 067 } 068 return sqls; 069 } 070 071 public void setConstraintName(String theConstraintName) { 072 myConstraintName = theConstraintName; 073 } 074 075 public void setParentTableName(String theParentTableName) { 076 myParentTableName = theParentTableName; 077 } 078 079 @Override 080 public void validate() { 081 super.validate(); 082 083 Validate.isTrue(isNotBlank(myConstraintName)); 084 Validate.isTrue(isNotBlank(myParentTableName)); 085 setDescription("Drop foreign key " + myConstraintName + " from table " + getTableName()); 086 087 } 088 089 @Override 090 public void doExecute() throws SQLException { 091 092 Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), myParentTableName, getTableName()); 093 if (!existing.contains(myConstraintName)) { 094 logInfo(ourLog, "Don't have constraint named {} - No action performed", myConstraintName); 095 return; 096 } 097 098 List<String> sqls = generateSql(getTableName(), myConstraintName, getDriverType()); 099 100 for (String next : sqls) { 101 executeSql(getTableName(), next); 102 } 103 104 } 105 106 @Override 107 protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { 108 DropForeignKeyTask otherObject = (DropForeignKeyTask) theOtherObject; 109 super.generateEquals(theBuilder, otherObject); 110 theBuilder.append(myConstraintName, otherObject.myConstraintName); 111 theBuilder.append(myParentTableName, otherObject.myParentTableName); 112 } 113 114 @Override 115 protected void generateHashCode(HashCodeBuilder theBuilder) { 116 super.generateHashCode(theBuilder); 117 theBuilder.append(myConstraintName); 118 theBuilder.append(myParentTableName); 119 } 120}