Merge pull request #1994 from jamesagnew/fix_mssql_errors

Fix mssql errors
This commit is contained in:
michelgleeson 2020-07-24 11:26:23 -04:00 committed by GitHub
commit 4e66cb6752
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 3 deletions

View File

@ -0,0 +1,4 @@
---
type: add
issue: 1994
title: "A few issues in the migrator when applying migrations against some versions of MSSQL were resolved"

View File

@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
* #L%
*/
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
@ -78,7 +79,18 @@ public class AddIndexTask extends BaseTableTask {
String unique = myUnique ? "unique " : "";
String columns = String.join(", ", myColumns);
String sql = "create " + unique + "index " + myIndexName + " on " + getTableName() + "(" + columns + ")";
String mssqlWhereClause = "";
if (myUnique && getDriverType() == DriverTypeEnum.MSSQL_2012) {
mssqlWhereClause = " WHERE (";
for (int i = 0; i <myColumns.size(); i++) {
mssqlWhereClause += myColumns.get(i) + " IS NOT NULL ";
if (i < myColumns.size() - 1) {
mssqlWhereClause += "AND ";
}
}
mssqlWhereClause += ")";
}
String sql = "create " + unique + "index " + myIndexName + " on " + getTableName() + "(" + columns + ")" + mssqlWhereClause;
String tableName = getTableName();
try {

View File

@ -53,7 +53,8 @@ public class DropColumnTask extends BaseTableColumnTask {
return;
}
if(getDriverType().equals(DriverTypeEnum.MYSQL_5_7) || getDriverType().equals(DriverTypeEnum.MARIADB_10_1)) {
if(getDriverType().equals(DriverTypeEnum.MYSQL_5_7) || getDriverType().equals(DriverTypeEnum.MARIADB_10_1)
|| getDriverType().equals(DriverTypeEnum.MSSQL_2012)) {
// Some DBs such as MYSQL and Maria DB require that foreign keys depending on the column be dropped before the column itself is dropped.
logInfo(ourLog, "Dropping any foreign keys on table {} depending on column {}", getTableName(), getColumnName());
Set<String> foreignKeys = JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), getColumnName(), getTableName());

View File

@ -116,9 +116,11 @@ public class DropIndexTask extends BaseTableTask {
break;
case DERBY_EMBEDDED:
case ORACLE_12C:
case MSSQL_2012:
sql.add("alter table " + theTableName + " drop constraint " + theIndexName);
break;
case MSSQL_2012:
sql.add("drop index " + theIndexName + " on " + theTableName);
break;
case POSTGRES_9_4:
sql.add("alter table " + theTableName + " drop constraint if exists " + theIndexName + " cascade");
sql.add("drop index if exists " + theIndexName + " cascade");