Skip to content

Commit

Permalink
0006192: Adjusting unique indexes to non-unique when columns can be null
Browse files Browse the repository at this point in the history
should be able to be turned off by parameter
  • Loading branch information
Philip Marzullo committed Jan 15, 2024
1 parent a870b6e commit b1614a0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
Expand Up @@ -176,6 +176,7 @@ private ParameterConstants() {
public final static String CREATE_TABLE_WITHOUT_INDEXES = "create.table.without.indexes";
public final static String CREATE_TABLE_WITHOUT_PK_IF_SOURCE_WITHOUT_PK = "create.table.without.pk.if.source.without.pk";
public final static String CREATE_TABLE_NOT_NULL_COLUMNS = "create.table.not.null.columns.supported";
public final static String CREATE_INDEX_CONVERT_UNIQUE_TO_NONUNIQUE_WHEN_COLUMNS_NOT_REQUIRED = "create.index.convert.unique.to.nonunique.when.columns.not.required";
public final static String STREAM_TO_FILE_ENABLED = "stream.to.file.enabled";
public final static String STREAM_TO_FILE_THRESHOLD = "stream.to.file.threshold.bytes";
public final static String STREAM_TO_FILE_TIME_TO_LIVE_MS = "stream.to.file.ttl.ms";
Expand Down
Expand Up @@ -70,6 +70,8 @@ public DatabaseWriterSettings buildParameterDatabaseWriterSettings(List<? extend
parameterService.is(ParameterConstants.DATA_LOADER_USE_PRIMARY_KEYS_FROM_SOURCE));
settings.setIgnoreSqlDataEventFailures(parameterService.is(ParameterConstants.DATA_LOADER_IGNORE_SQL_EVENT_ERRORS, false));
settings.setLogSqlParamsOnError(parameterService.is(ParameterConstants.DATA_LOADER_LOG_SQL_PARAMS_ON_ERROR, true));
settings.setCreateIndexConvertUniqueToNonuniqueWhenColumnsNotRequired(
parameterService.is(ParameterConstants.CREATE_INDEX_CONVERT_UNIQUE_TO_NONUNIQUE_WHEN_COLUMNS_NOT_REQUIRED, true));
Map<String, Conflict> byChannel = new HashMap<String, Conflict>();
Map<String, Conflict> byTable = new HashMap<String, Conflict>();
boolean multipleDefaultSettingsFound = false;
Expand Down
Expand Up @@ -1928,6 +1928,13 @@ create.table.without.pk.if.source.without.pk=true
# Type: boolean
create.table.not.null.columns.supported=true

# If set to true, then convert unique indexes to non-unique when one of the columns is defined
# as not required (defined with the NOT NULL constraint).
#
# DatabaseOverridable: true
# Type: boolean
create.index.convert.unique.to.nonunique.when.columns.not.required=true

# Indicates that the current value of the row should be recorded in the incoming_error table
#
# DatabaseOverridable: true
Expand Down
Expand Up @@ -62,6 +62,7 @@ public class DatabaseWriterSettings {
protected IAlterDatabaseInterceptor[] alterDatabaseInterceptors;
protected Set<String> conflictLosingParentRows;
protected boolean ignoreSqlDataEventFailures = false;
protected boolean createIndexConvertUniqueToNonuniqueWhenColumnsNotRequired = true;

public void setAlterDatabaseInterceptors(IAlterDatabaseInterceptor[] alterDatabaseInterceptors) {
this.alterDatabaseInterceptors = alterDatabaseInterceptors;
Expand Down Expand Up @@ -357,4 +358,12 @@ public boolean isIgnoreSqlDataEventFailures() {
public void setIgnoreSqlDataEventFailures(boolean ignoreSqlDataEventFailures) {
this.ignoreSqlDataEventFailures = ignoreSqlDataEventFailures;
}

public boolean isCreateIndexConvertUniqueToNonuniqueWhenColumnsNotRequired() {
return createIndexConvertUniqueToNonuniqueWhenColumnsNotRequired;
}

public void setCreateIndexConvertUniqueToNonuniqueWhenColumnsNotRequired(boolean createIndexConvertUniqueToNonuniqueWhenColumnsNotRequired) {
this.createIndexConvertUniqueToNonuniqueWhenColumnsNotRequired = createIndexConvertUniqueToNonuniqueWhenColumnsNotRequired;
}
}
Expand Up @@ -39,6 +39,7 @@
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.IndexColumn;
import org.jumpmind.db.model.NonUniqueIndex;
import org.jumpmind.db.model.PlatformIndex;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.DatabaseInfo;
Expand Down Expand Up @@ -629,27 +630,33 @@ protected boolean create(CsvData data, boolean withoutDefaults) {
table.removeAllColumnDefaults();
}
}
if (!(getTargetPlatform().allowsUniqueIndexDuplicatesWithNulls())) {
for (Table table : db.getTables()) {
for (IIndex index : table.getUniqueIndices()) {
boolean needsFixed = false;
for (IndexColumn indexColumn : index.getColumns()) {
Column column = indexColumn.getColumn();
if (column != null && !column.isRequired()) {
needsFixed = true;
log.warn(
"Detected Unique Index with potential for multiple null values in table: {} on column: {}. Adjusting index to be NonUnique.",
table.getName(), column.getName());
break;
}
}
if (needsFixed) {
table.removeIndex(index);
IIndex newIndex = new NonUniqueIndex(index.getName());
if (writerSettings.isCreateIndexConvertUniqueToNonuniqueWhenColumnsNotRequired()) {
if (!(getTargetPlatform().allowsUniqueIndexDuplicatesWithNulls())) {
for (Table table : db.getTables()) {
for (IIndex index : table.getUniqueIndices()) {
boolean needsFixed = false;
for (IndexColumn indexColumn : index.getColumns()) {
newIndex.addColumn(indexColumn);
Column column = indexColumn.getColumn();
if (column != null && !column.isRequired()) {
needsFixed = true;
log.warn(
"Detected Unique Index: {} with potential for multiple null values in table: {} on column: {}. Adjusting index to be NonUnique.",
index.getName(), table.getName(), column.getName());
break;
}
}
if (needsFixed) {
table.removeIndex(index);
IIndex newIndex = new NonUniqueIndex(index.getName());
for (IndexColumn indexColumn : index.getColumns()) {
newIndex.addColumn(indexColumn);
}
// Make sure to add the platform index info to the new non-unique index
for (PlatformIndex platformIndex : index.getPlatformIndexes().values()) {
newIndex.addPlatformIndex(platformIndex);
}
table.addIndex(newIndex);
}
table.addIndex(newIndex);
}
}
}
Expand Down

0 comments on commit b1614a0

Please sign in to comment.