Skip to content

Commit

Permalink
Completeness test for starter (#398)
Browse files Browse the repository at this point in the history
* Add beans completeness test

* Reformat code in starter

* Reformat code in pg-index-health-testing

* Reformat code in pg-index-health-model

* Reformat code in pg-index-health-jdbc-connection

* Reformat code in pg-index-health-generator

* Reformat code in pg-index-health
  • Loading branch information
mfvanek committed May 15, 2024
1 parent 6bafbc6 commit 439a77d
Show file tree
Hide file tree
Showing 233 changed files with 3,109 additions and 3,100 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ indent_style = space
insert_final_newline = true
max_line_length = 199
tab_width = 4
ij_continuation_indent_size = 8
ij_continuation_indent_size = 4

[*.java]
ij_java_imports_layout = *,|,java.**,javax.**,|,$*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* Base class for all migration generators.
*
* @param <T> represents an object in a database associated with a table
*
* @author Ivan Vahrushev
* @since 0.6.2
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ColumnWithSerialTypeMigrationGenerator(@Nonnull final GeneratingOptions o
@Nonnull
protected String generate(@Nonnull final ColumnWithSerialType column) {
return dropDefaultValueGenerator.generate(column) +
System.lineSeparator() +
dropSequenceGenerator.generate(column);
System.lineSeparator() +
dropSequenceGenerator.generate(column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* Database migrations generator.
*
* @param <T> represents an object in a database associated with a table
*
* @author Ivan Vahrushev
* @since 0.5.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ final class DropDefaultValueGenerator extends AbstractOptionsAwareSqlGenerator<C
public String generate(@Nonnull final ColumnNameAware column) {
Objects.requireNonNull(column, "column cannot be null");
return keyword("alter table ") +
keyword("if exists ") +
column.getTableName() +
(options.isBreakLines() ? System.lineSeparator() : " ") +
(options.isBreakLines() ? WHITESPACE.repeat(options.getIndentation()) : "") +
keyword("alter column ") +
column.getColumnName() +
keyword(" drop default") +
';';
keyword("if exists ") +
column.getTableName() +
(options.isBreakLines() ? System.lineSeparator() : " ") +
(options.isBreakLines() ? WHITESPACE.repeat(options.getIndentation()) : "") +
keyword("alter column ") +
column.getColumnName() +
keyword(" drop default") +
';';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ final class DropSequenceGenerator extends AbstractOptionsAwareSqlGenerator<Colum
public String generate(@Nonnull final ColumnWithSerialType column) {
Objects.requireNonNull(column, "column cannot be null");
return keyword("drop sequence ") +
keyword("if exists ") +
column.getSequenceName() +
';';
keyword("if exists ") +
column.getSequenceName() +
';';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ public boolean isNeedToAddIdx() {
@Override
public String toString() {
return GeneratingOptions.class.getSimpleName() + '{' +
"concurrently=" + concurrently +
", excludeNulls=" + excludeNulls +
", breakLines=" + breakLines +
", indentation=" + indentation +
", uppercaseForKeywords=" + uppercaseForKeywords +
", nameWithoutNulls=" + nameWithoutNulls +
", idxPosition=" + idxPosition +
'}';
"concurrently=" + concurrently +
", excludeNulls=" + excludeNulls +
", breakLines=" + breakLines +
", indentation=" + indentation +
", uppercaseForKeywords=" + uppercaseForKeywords +
", nameWithoutNulls=" + nameWithoutNulls +
", idxPosition=" + idxPosition +
'}';
}

@Nonnull
Expand Down Expand Up @@ -230,14 +230,14 @@ public Builder withIdxPosition(@Nonnull final IdxPosition idxPosition) {
@Override
public String toString() {
return Builder.class.getSimpleName() + '{' +
"concurrently=" + concurrently +
", excludeNulls=" + excludeNulls +
", breakLines=" + breakLines +
", indentation=" + indentation +
", uppercaseForKeywords=" + uppercaseForKeywords +
", nameWithoutNulls=" + nameWithoutNulls +
", idxPosition=" + idxPosition +
'}';
"concurrently=" + concurrently +
", excludeNulls=" + excludeNulls +
", breakLines=" + breakLines +
", indentation=" + indentation +
", uppercaseForKeywords=" + uppercaseForKeywords +
", nameWithoutNulls=" + nameWithoutNulls +
", idxPosition=" + idxPosition +
'}';
}

private static int validateIndentation(final int indentation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ private PgIdentifierNameGenerator(@Nonnull final ForeignKey foreignKey, @Nonnull
this.options = Objects.requireNonNull(options, "options cannot be null");
this.tableNameWithoutSchema = NameUtils.getTableNameWithoutSchema(foreignKey);
this.columnsInIndex = foreignKey.getColumnsInConstraint().stream()
.map(Column::getColumnName)
.collect(Collectors.joining(AbstractDbMigrationGenerator.DELIMITER));
.map(Column::getColumnName)
.collect(Collectors.joining(AbstractDbMigrationGenerator.DELIMITER));
this.hasToAddWithoutNullsSuffix = options.isNameWithoutNulls() && options.isExcludeNulls() &&
foreignKey.getColumnsInConstraint().stream().anyMatch(Column::isNullable);
foreignKey.getColumnsInConstraint().stream().anyMatch(Column::isNullable);
}

@Nonnull
Expand All @@ -55,7 +55,7 @@ public String generateFullIndexName() {
addMainPart(fullNameBuilder);
addWithoutNullsIfNeed(fullNameBuilder);
return addIdxIfNeed(fullNameBuilder)
.toString();
.toString();
}

@Nonnull
Expand All @@ -73,8 +73,8 @@ public String generateTruncatedIndexName() {
}
remainingLength = remainingLength - DELIMITER_LENGTH - columnsPart.length();
truncatedNameBuilder.append(StringUtils.truncate(tableNameWithoutSchema, remainingLength))
.append(AbstractDbMigrationGenerator.DELIMITER)
.append(columnsPart);
.append(AbstractDbMigrationGenerator.DELIMITER)
.append(columnsPart);
remainingLength -= tableNameWithoutSchema.length();
} else {
addMainPart(truncatedNameBuilder);
Expand All @@ -84,13 +84,13 @@ public String generateTruncatedIndexName() {
addWithoutNullsIfNeed(truncatedNameBuilder);
}
return addIdxIfNeed(truncatedNameBuilder)
.toString();
.toString();
}

private void addMainPart(@Nonnull final StringBuilder nameBuilder) {
nameBuilder.append(tableNameWithoutSchema)
.append(AbstractDbMigrationGenerator.DELIMITER)
.append(columnsInIndex);
.append(AbstractDbMigrationGenerator.DELIMITER)
.append(columnsInIndex);
}

private int getMainPartLength() {
Expand All @@ -100,7 +100,7 @@ private int getMainPartLength() {
private void addWithoutNullsIfNeed(@Nonnull final StringBuilder nameBuilder) {
if (hasToAddWithoutNullsSuffix) {
nameBuilder.append(AbstractDbMigrationGenerator.DELIMITER)
.append(WITHOUT_NULLS);
.append(WITHOUT_NULLS);
}
}

Expand All @@ -109,7 +109,7 @@ private StringBuilder addIdxIfNeed(@Nonnull final StringBuilder nameBuilder) {
if (options.isNeedToAddIdx()) {
if (options.getIdxPosition() == IdxPosition.SUFFIX) {
nameBuilder.append(AbstractDbMigrationGenerator.DELIMITER)
.append(IDX);
.append(IDX);
} else {
nameBuilder.insert(0, IDX + AbstractDbMigrationGenerator.DELIMITER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,42 @@ public String generate(@Nonnull final ForeignKey foreignKey) {
appendFullIndexNameAsComment(queryBuilder, fullIndexName);
}
queryBuilder.append(keyword("create index "))
.append(options.isConcurrently() ? keyword("concurrently ") : "")
.append(keyword("if not exists "))
.append(hasToTruncate ? nameGenerator.generateTruncatedIndexName() : fullIndexName)
.append(options.isBreakLines() ? System.lineSeparator() : " ")
.append(options.isBreakLines() ? WHITESPACE.repeat(options.getIndentation()) : "")
.append(keyword("on "))
.append(foreignKey.getTableName())
.append(" (")
.append(foreignKey.getColumnsInConstraint().stream().map(Column::getColumnName).collect(Collectors.joining(", ")))
.append(')');
.append(options.isConcurrently() ? keyword("concurrently ") : "")
.append(keyword("if not exists "))
.append(hasToTruncate ? nameGenerator.generateTruncatedIndexName() : fullIndexName)
.append(options.isBreakLines() ? System.lineSeparator() : " ")
.append(options.isBreakLines() ? WHITESPACE.repeat(options.getIndentation()) : "")
.append(keyword("on "))
.append(foreignKey.getTableName())
.append(" (")
.append(foreignKey.getColumnsInConstraint().stream().map(Column::getColumnName).collect(Collectors.joining(", ")))
.append(')');
if (hasToExcludeNulls(foreignKey)) {
excludeNulls(queryBuilder, foreignKey);
}
return queryBuilder.append(';')
.toString();
.toString();
}

private void appendFullIndexNameAsComment(@Nonnull final StringBuilder queryBuilder, @Nonnull final String fullIndexName) {
queryBuilder.append("/* ")
.append(fullIndexName)
.append(" */")
.append(options.isBreakLines() ? System.lineSeparator() : " ");
.append(fullIndexName)
.append(" */")
.append(options.isBreakLines() ? System.lineSeparator() : " ");
}

private boolean hasToExcludeNulls(@Nonnull final ForeignKey foreignKey) {
return options.isExcludeNulls() &&
foreignKey.getColumnsInConstraint().stream().anyMatch(Column::isNullable);
foreignKey.getColumnsInConstraint().stream().anyMatch(Column::isNullable);
}

private void excludeNulls(@Nonnull final StringBuilder queryBuilder, @Nonnull final ForeignKey foreignKey) {
queryBuilder.append(keyword(" where "));
final String columnsList = foreignKey.getColumnsInConstraint().stream()
.filter(Column::isNullable)
.map(Column::getColumnName)
.map(n -> n + keyword(" is not null"))
.collect(Collectors.joining(" and "));
.filter(Column::isNullable)
.map(Column::getColumnName)
.map(n -> n + keyword(" is not null"))
.collect(Collectors.joining(" and "));
queryBuilder.append(columnsList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ class ColumnWithSerialTypeMigrationGeneratorTest {
@Test
void shouldHandleInvalidArguments() {
assertThatThrownBy(() -> new ColumnWithSerialTypeMigrationGenerator(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("options cannot be null");
.isInstanceOf(NullPointerException.class)
.hasMessage("options cannot be null");
}

@Test
void forSingleColumn() {
final ColumnWithSerialTypeMigrationGenerator generator = new ColumnWithSerialTypeMigrationGenerator(GeneratingOptions.builder().build());

assertThat(generator.generate(List.of(column())))
.hasSize(1)
.containsExactly("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;");
.hasSize(1)
.containsExactly("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;");
}

@Test
Expand All @@ -47,13 +47,13 @@ void forSeveralColumns() {
final ColumnWithSerialTypeMigrationGenerator generator = new ColumnWithSerialTypeMigrationGenerator(GeneratingOptions.builder().build());

assertThat(generator.generate(List.of(column(), secondColumn)))
.hasSize(2)
.containsExactly("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;",
"alter table if exists s2.t2" + System.lineSeparator() +
" alter column col2 drop default;" + System.lineSeparator() +
"drop sequence if exists s2.seq2;");
.hasSize(2)
.containsExactly("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;",
"alter table if exists s2.t2" + System.lineSeparator() +
" alter column col2 drop default;" + System.lineSeparator() +
"drop sequence if exists s2.seq2;");
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@ class DropDefaultValueGeneratorTest {
@Test
void shouldHandleInvalidArguments() {
assertThatThrownBy(() -> new DropDefaultValueGenerator(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("options cannot be null");
.isInstanceOf(NullPointerException.class)
.hasMessage("options cannot be null");

final DropDefaultValueGenerator generator = new DropDefaultValueGenerator(GeneratingOptions.builder().build());
assertThatThrownBy(() -> generator.generate(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("column cannot be null");
.isInstanceOf(NullPointerException.class)
.hasMessage("column cannot be null");
}

@Test
void generateForColumnLowerCase() {
final DropDefaultValueGenerator generator = new DropDefaultValueGenerator(GeneratingOptions.builder().build());
assertThat(generator.generate(column()))
.isEqualTo("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;");
.isEqualTo("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;");
}

@Test
void generateWithoutBreakingLines() {
final DropDefaultValueGenerator generator = new DropDefaultValueGenerator(
GeneratingOptions.builder()
.doNotBreakLines()
.build());
GeneratingOptions.builder()
.doNotBreakLines()
.build());
assertThat(generator.generate(column()))
.isEqualTo("alter table if exists s1.t1 alter column col1 drop default;");
.isEqualTo("alter table if exists s1.t1 alter column col1 drop default;");
}

@Test
void generateForColumnUpperCase() {
final DropDefaultValueGenerator generator = new DropDefaultValueGenerator(
GeneratingOptions.builder()
.uppercaseForKeywords()
.doNotBreakLines()
.build());
GeneratingOptions.builder()
.uppercaseForKeywords()
.doNotBreakLines()
.build());
assertThat(generator.generate(column()))
.isEqualTo("ALTER TABLE IF EXISTS s1.t1 ALTER COLUMN col1 DROP DEFAULT;");
.isEqualTo("ALTER TABLE IF EXISTS s1.t1 ALTER COLUMN col1 DROP DEFAULT;");
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ class DropSequenceGeneratorTest {
@Test
void shouldHandleInvalidArguments() {
assertThatThrownBy(() -> new DropSequenceGenerator(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("options cannot be null");
.isInstanceOf(NullPointerException.class)
.hasMessage("options cannot be null");

final DropSequenceGenerator generator = new DropSequenceGenerator(GeneratingOptions.builder().build());
assertThatThrownBy(() -> generator.generate(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("column cannot be null");
.isInstanceOf(NullPointerException.class)
.hasMessage("column cannot be null");
}

@Test
void generateForColumnLowerCase() {
final DropSequenceGenerator generator = new DropSequenceGenerator(GeneratingOptions.builder().build());
assertThat(generator.generate(column()))
.isEqualTo("drop sequence if exists s1.seq1;");
.isEqualTo("drop sequence if exists s1.seq1;");
}

@Test
void generateForColumnUpperCase() {
final DropSequenceGenerator generator = new DropSequenceGenerator(
GeneratingOptions.builder()
.uppercaseForKeywords()
.build());
GeneratingOptions.builder()
.uppercaseForKeywords()
.build());
assertThat(generator.generate(column()))
.isEqualTo("DROP SEQUENCE IF EXISTS s1.seq1;");
.isEqualTo("DROP SEQUENCE IF EXISTS s1.seq1;");
}

@Nonnull
Expand Down
Loading

0 comments on commit 439a77d

Please sign in to comment.