modify add index logic to behave like the other jdbc databases

This commit is contained in:
Mike G 2020-07-22 12:21:58 -04:00
parent f285318185
commit 6f1d46dc68
1 changed files with 13 additions and 1 deletions

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 {