Work on migrator task support for dropping tables in postgres
This commit is contained in:
parent
80bfb9af37
commit
a5dceca075
|
@ -23,12 +23,12 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
|
|||
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
|
||||
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.intellij.lang.annotations.Language;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class DropIndexTask extends BaseTableTask<DropIndexTask> {
|
||||
|
||||
|
@ -57,10 +57,12 @@ public class DropIndexTask extends BaseTableTask<DropIndexTask> {
|
|||
boolean isUnique = JdbcUtils.isIndexUnique(getConnectionProperties(), getTableName(), myIndexName);
|
||||
String uniquenessString = isUnique ? "unique" : "non-unique";
|
||||
|
||||
Optional<String> sql = createDropIndexSql(getConnectionProperties(), getTableName(), myIndexName, getDriverType());
|
||||
if (sql.isPresent()) {
|
||||
List<String> sqls = createDropIndexSql(getConnectionProperties(), getTableName(), myIndexName, getDriverType());
|
||||
if (!sqls.isEmpty()) {
|
||||
ourLog.info("Dropping {} index {} on table {}", uniquenessString, myIndexName, getTableName());
|
||||
executeSql(getTableName(), sql.get());
|
||||
}
|
||||
for (@Language("SQL") String sql : sqls) {
|
||||
executeSql(getTableName(), sql);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,35 +71,36 @@ public class DropIndexTask extends BaseTableTask<DropIndexTask> {
|
|||
return this;
|
||||
}
|
||||
|
||||
static Optional<String> createDropIndexSql(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theIndexName, DriverTypeEnum theDriverType) throws SQLException {
|
||||
static List<String> createDropIndexSql(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theIndexName, DriverTypeEnum theDriverType) throws SQLException {
|
||||
Validate.notBlank(theIndexName, "theIndexName must not be blank");
|
||||
Validate.notBlank(theTableName, "theTableName must not be blank");
|
||||
|
||||
if (!JdbcUtils.getIndexNames(theConnectionProperties, theTableName).contains(theIndexName)) {
|
||||
return Optional.empty();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
boolean isUnique = JdbcUtils.isIndexUnique(theConnectionProperties, theTableName, theIndexName);
|
||||
|
||||
String sql = null;
|
||||
List<String> sql = new ArrayList<>();
|
||||
|
||||
if (isUnique) {
|
||||
// Drop constraint
|
||||
switch (theDriverType) {
|
||||
case MYSQL_5_7:
|
||||
case MARIADB_10_1:
|
||||
sql = "alter table " + theTableName + " drop index " + theIndexName;
|
||||
sql.add("alter table " + theTableName + " drop index " + theIndexName);
|
||||
break;
|
||||
case H2_EMBEDDED:
|
||||
case DERBY_EMBEDDED:
|
||||
sql = "drop index " + theIndexName;
|
||||
sql.add("drop index " + theIndexName);
|
||||
break;
|
||||
case POSTGRES_9_4:
|
||||
sql = "drop index " + theIndexName + " cascade";
|
||||
sql.add("alter table " + theTableName + " drop constraint if exists " + theIndexName + " cascade");
|
||||
sql.add("drop index if exists " + theIndexName + " cascade");
|
||||
break;
|
||||
case ORACLE_12C:
|
||||
case MSSQL_2012:
|
||||
sql = "alter table " + theTableName + " drop constraint " + theIndexName;
|
||||
sql.add("alter table " + theTableName + " drop constraint " + theIndexName);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
|
@ -105,19 +108,19 @@ public class DropIndexTask extends BaseTableTask<DropIndexTask> {
|
|||
switch (theDriverType) {
|
||||
case MYSQL_5_7:
|
||||
case MARIADB_10_1:
|
||||
sql = "alter table " + theTableName + " drop index " + theIndexName;
|
||||
sql.add("alter table " + theTableName + " drop index " + theIndexName);
|
||||
break;
|
||||
case POSTGRES_9_4:
|
||||
case DERBY_EMBEDDED:
|
||||
case H2_EMBEDDED:
|
||||
case ORACLE_12C:
|
||||
sql = "drop index " + theIndexName;
|
||||
sql.add("drop index " + theIndexName);
|
||||
break;
|
||||
case MSSQL_2012:
|
||||
sql = "drop index " + theTableName + "." + theIndexName;
|
||||
sql.add("drop index " + theTableName + "." + theIndexName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Optional.of(sql);
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
|
|||
*/
|
||||
|
||||
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
|
||||
import org.intellij.lang.annotations.Language;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -48,21 +49,24 @@ public class DropTableTask extends BaseTableTask<DropTableTask> {
|
|||
|
||||
for (String next : foreignKeys) {
|
||||
List<String> sql = DropForeignKeyTask.generateSql(getTableName(), next, getDriverType());
|
||||
for (String nextSql : sql) {
|
||||
for (@Language("SQL") String nextSql : sql) {
|
||||
executeSql(getTableName(), nextSql);
|
||||
}
|
||||
}
|
||||
|
||||
for (String nextIndex : indexNames) {
|
||||
Optional<String> sql = DropIndexTask.createDropIndexSql(getConnectionProperties(), getTableName(), nextIndex, getDriverType());
|
||||
if (sql.isPresent()) {
|
||||
List<String> sqls = DropIndexTask.createDropIndexSql(getConnectionProperties(), getTableName(), nextIndex, getDriverType());
|
||||
if (!sqls.isEmpty()) {
|
||||
ourLog.info("Dropping index {} on table {} in preparation for table delete", nextIndex, getTableName());
|
||||
executeSql(getTableName(), sql.get());
|
||||
}
|
||||
for (@Language("SQL") String sql : sqls) {
|
||||
executeSql(getTableName(), sql);
|
||||
}
|
||||
}
|
||||
|
||||
ourLog.info("Dropping table: {}", getTableName());
|
||||
|
||||
@Language("SQL")
|
||||
String sql = "DROP TABLE " + getTableName();
|
||||
executeSql(getTableName(), sql);
|
||||
|
||||
|
|
Loading…
Reference in New Issue