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.jpa.migrate.JdbcUtils;
023import org.intellij.lang.annotations.Language;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import java.sql.SQLException;
028import java.util.List;
029import java.util.Set;
030
031public class DropTableTask extends BaseTableTask {
032
033        private static final Logger ourLog = LoggerFactory.getLogger(DropTableTask.class);
034
035        public DropTableTask(String theProductVersion, String theSchemaVersion) {
036                super(theProductVersion, theSchemaVersion);
037        }
038
039        @Override
040        public void validate() {
041                super.validate();
042                setDescription("Drop table " + getTableName());
043        }
044
045        @Override
046        public void doExecute() throws SQLException {
047                Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
048                if (!tableNames.contains(getTableName())) {
049                        return;
050                }
051
052                Set<String> foreignKeys = JdbcUtils.getForeignKeys(getConnectionProperties(), null, getTableName());
053                logInfo(ourLog, "Table {} has the following foreign keys: {}", getTableName(), foreignKeys);
054
055                Set<String> indexNames = JdbcUtils.getIndexNames(getConnectionProperties(), getTableName());
056                logInfo(ourLog, "Table {} has the following indexes: {}", getTableName(), indexNames);
057
058                for (String next : foreignKeys) {
059                        List<String> sql = DropForeignKeyTask.generateSql(getTableName(), next, getDriverType());
060                        for (@Language("SQL") String nextSql : sql) {
061                                executeSql(getTableName(), nextSql);
062                        }
063                }
064
065                DropIndexTask theIndexTask = new DropIndexTask(getProductVersion(), getSchemaVersion());
066                theIndexTask
067                                .setTableName(getTableName())
068                                .setConnectionProperties(getConnectionProperties())
069                                .setDriverType(getDriverType())
070                                .setDryRun(isDryRun());
071                for (String nextIndex : indexNames) {
072                        theIndexTask.setIndexName(nextIndex).execute();
073                }
074
075                logInfo(ourLog, "Dropping table: {}", getTableName());
076
077                @Language("SQL")
078                String sql = "DROP TABLE " + getTableName();
079                executeSql(getTableName(), sql);
080        }
081}