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.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                        List<String> sql = DropForeignKeyTask.generateSql(getTableName(), next, getDriverType());
061                        for (@Language("SQL") String nextSql : sql) {
062                                executeSql(getTableName(), nextSql);
063                        }
064                }
065
066                DropIndexTask theIndexTask = new DropIndexTask(getProductVersion(), getSchemaVersion());
067                theIndexTask
068                        .setTableName(getTableName())
069                        .setConnectionProperties(getConnectionProperties())
070                        .setDriverType(getDriverType())
071                        .setDryRun(isDryRun());
072                for (String nextIndex : indexNames) {
073                        theIndexTask
074                                .setIndexName(nextIndex)
075                                .execute();
076                }
077
078                logInfo(ourLog, "Dropping table: {}", getTableName());
079
080                @Language("SQL")
081                String sql = "DROP TABLE " + getTableName();
082                executeSql(getTableName(), sql);
083
084        }
085
086
087}