import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
+import javax.annotation.Nullable;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.PostgreSql;
import org.sonar.server.platform.db.migration.def.ColumnDef;
* Add a column to the scope of index. Order of calls to this
* method is important and is kept as-is when creating the index.
*
- * @deprecated use {@link CreateIndexBuilder#addColumn(String, boolean) instead}
*/
- @Deprecated(since = "10.3")
public CreateIndexBuilder addColumn(String column) {
requireNonNull(column, COLUMN_CANNOT_BE_NULL);
- columns.add(new NullableColumn(column, false));
+ columns.add(new NullableColumn(column, null));
return this;
}
public List<String> build() {
validateTableName(tableName);
validateIndexName(indexName);
+ validateColumnsForUniqueIndex(unique, columns);
checkArgument(!columns.isEmpty(), "at least one column must be specified");
return singletonList(createSqlStatement());
}
+ private static void validateColumnsForUniqueIndex(boolean unique, List<NullableColumn> columns) {
+ checkArgument(!unique || columns.stream().allMatch(c->c.isNullable() != null), "Nullability of column should be provided for unique indexes");
+ }
+
/**
*
*/
return sql.toString();
}
- private record NullableColumn(String name, boolean isNullable) {
+ private record NullableColumn(String name, @Nullable Boolean isNullable) {
}
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn(COLUMN_NAME)
+ .addColumn(COLUMN_NAME, false)
.setUnique(true)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(SCM_ACCOUNTS_TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn(SCM_ACCOUNT_COLUMN_NAME)
+ .addColumn(SCM_ACCOUNT_COLUMN_NAME, false)
.setUnique(false)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn(EXTERNAL_IDENTITY_PROVIDER_COLUMN_NAME)
- .addColumn(EXTERNAL_GROUP_ID_COLUMN_NAME)
+ .addColumn(EXTERNAL_IDENTITY_PROVIDER_COLUMN_NAME, false)
+ .addColumn(EXTERNAL_GROUP_ID_COLUMN_NAME, false)
.setUnique(true)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn(COLUMN_NAME_PORTFOLIO)
- .addColumn(COLUMN_NAME_BRANCH)
+ .addColumn(COLUMN_NAME_PORTFOLIO, true)
+ .addColumn(COLUMN_NAME_BRANCH, true)
.setUnique(true)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn(COLUMN_NAME_PORTFOLIO)
- .addColumn(COLUMN_NAME_BRANCH)
- .addColumn(COLUMN_NAME_USER)
+ .addColumn(COLUMN_NAME_PORTFOLIO, true)
+ .addColumn(COLUMN_NAME_BRANCH, true)
+ .addColumn(COLUMN_NAME_USER, false)
.setUnique(true)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn("issue_key")
- .addColumn("software_quality")
+ .addColumn("issue_key", false)
+ .addColumn("software_quality", false)
.setUnique(true)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn("rule_uuid")
- .addColumn("software_quality")
+ .addColumn("rule_uuid", false)
+ .addColumn("software_quality", false)
.setUnique(true)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn("rule_change_uuid")
+ .addColumn("rule_change_uuid", false)
.setUnique(false)
.build());
}
context.execute(new CreateIndexBuilder(getDialect())
.setTable(GITHUB_PERMISSIONS_MAPPING_TABLE_NAME)
.setName(INDEX_NAME)
- .addColumn(GITHUB_ROLE_COLUMN)
- .addColumn(SONARQUBE_PERMISSION_COLUMN)
+ .addColumn(GITHUB_ROLE_COLUMN, false)
+ .addColumn(SONARQUBE_PERMISSION_COLUMN, false)
.setUnique(true)
.build());
}
.hasMessage("Index name can't be null");
}
+ @Test
+ public void build_shouldThrowException_whenUniqueAndColumnNullabilityIsNotProvided() {
+ assertThatThrownBy(() -> {
+ new CreateIndexBuilder(new H2())
+ .setTable("issues")
+ .setName("name")
+ .addColumn("columnName")
+ .setUnique(true)
+ .build();
+ })
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Nullability of column should be provided for unique indexes");
+ }
+
@Test
public void throw_IAE_if_columns_are_missing() {
assertThatThrownBy(() -> {