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.i18n.Msg; 023import ca.uhn.fhir.jpa.migrate.JdbcUtils; 024import org.apache.commons.lang3.builder.HashCodeBuilder; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import java.sql.SQLException; 029import java.util.Set; 030 031public class RenameTableTask extends BaseTableTask { 032 033 private static final Logger ourLog = LoggerFactory.getLogger(RenameTableTask.class); 034 035 private final String myOldTableName; 036 private final String myNewTableName; 037 private boolean myDeleteTargetColumnFirstIfExist = true; 038 039 public RenameTableTask( 040 String theProductVersion, String theSchemaVersion, String theOldTableName, String theNewTableName) { 041 super(theProductVersion, theSchemaVersion); 042 myOldTableName = theOldTableName; 043 myNewTableName = theNewTableName; 044 } 045 046 @Override 047 public void validate() { 048 setDescription("Rename table " + getOldTableName()); 049 } 050 051 @Override 052 public void doExecute() throws SQLException { 053 054 Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties()); 055 boolean hasTableWithNewTableName = tableNames.contains(getNewTableName()); 056 057 String sql = buildRenameTableSqlStatement(); 058 logInfo(ourLog, "Renaming table: {}", getOldTableName()); 059 060 executeSql(getOldTableName(), sql); 061 } 062 063 public void setDeleteTargetColumnFirstIfExist(boolean theDeleteTargetColumnFirstIfExist) { 064 myDeleteTargetColumnFirstIfExist = theDeleteTargetColumnFirstIfExist; 065 } 066 067 public String getNewTableName() { 068 return myNewTableName; 069 } 070 071 public String getOldTableName() { 072 return myOldTableName; 073 } 074 075 String buildRenameTableSqlStatement() { 076 String retVal; 077 078 final String oldTableName = getOldTableName(); 079 final String newTableName = getNewTableName(); 080 081 switch (getDriverType()) { 082 case MYSQL_5_7: 083 case DERBY_EMBEDDED: 084 retVal = "rename table " + oldTableName + " to " + newTableName; 085 break; 086 case ORACLE_12C: 087 case MARIADB_10_1: 088 case POSTGRES_9_4: 089 case COCKROACHDB_21_1: 090 case H2_EMBEDDED: 091 retVal = "alter table " + oldTableName + " rename to " + newTableName; 092 break; 093 case MSSQL_2012: 094 retVal = "sp_rename '" + oldTableName + "', '" + newTableName + "'"; 095 break; 096 default: 097 throw new IllegalStateException(Msg.code(2513)); 098 } 099 return retVal; 100 } 101 102 @Override 103 protected void generateHashCode(HashCodeBuilder theBuilder) { 104 super.generateHashCode(theBuilder); 105 theBuilder.append(myOldTableName); 106 theBuilder.append(myNewTableName); 107 } 108}