Skip to content

Commit

Permalink
Add ability to skip DuplicatedIndexes by name (#494)
Browse files Browse the repository at this point in the history
* Add ability to skip DuplicatedIndexes by name

* Refactoring

* Bloody Java Generics

* Add ConstraintNameAware and ConstraintsAware
  • Loading branch information
mfvanek authored Nov 15, 2024
1 parent 3b65e9c commit d1473dc
Show file tree
Hide file tree
Showing 47 changed files with 257 additions and 73 deletions.
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<module name="RecordComponentNumber"/>
<module name="RequireThis"/>
<module name="ReturnCount">
<property name="max" value="3"/>
<property name="max" value="4"/>
</module>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
Expand Down
4 changes: 4 additions & 0 deletions config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@
<Bug pattern="ACEM_ABSTRACT_CLASS_EMPTY_METHODS"/>
<Class name="io.github.mfvanek.pg.support.statements.AbstractDbStatement"/>
</Match>
<Match>
<Bug pattern="ITC_INHERITANCE_TYPE_CHECKING"/>
<Class name="io.github.mfvanek.pg.model.predicates.SkipIndexesByNamePredicate"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public enum Diagnostic implements CheckTypeAware {
}

/**
* Gets the place where the diagnostic should be executed.
* Retrieves the place where the diagnostic should be executed.
*
* @return {@code ExecutionTopology}
*/
Expand All @@ -117,7 +117,7 @@ public ExecutionTopology getExecutionTopology() {
}

/**
* Gets the associated sql query file name.
* Retrieves the associated sql query file name.
*
* @return sql query file name
*/
Expand All @@ -127,7 +127,7 @@ public String getSqlQueryFileName() {
}

/**
* Gets the lambda which executes the associated sql query.
* Retrieves the lambda which executes the associated sql query.
*
* @return {@code QueryExecutor}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public interface DiagnosticAware {

/**
* Gets the diagnostic - a rule related to the check.
* Retrieves the diagnostic - a rule related to the check.
*
* @return diagnostic
* @see Diagnostic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public interface RawTypeAware<T extends DbObject> {

/**
* Gets original java type.
* Retrieves original java type.
*
* @return java type representing database object
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface StatisticsAware {
boolean resetStatistics();

/**
* Gets time at which database statistics were last reset.
* Retrieves the time at which database statistics were last reset.
*
* @return {@code Optional} of null or time at which database statistics were last reset.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface StatisticsMaintenanceOnHost extends StatisticsAware, HostAware
boolean resetStatistics();

/**
* Gets time at which database statistics were last reset on current host.
* Retrieves the time at which database statistics were last reset on current host.
*
* @return {@code Optional} of null or time at which database statistics were last reset.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.github.mfvanek.pg.model.PgContext;
import io.github.mfvanek.pg.model.index.DuplicatedIndexes;
import io.github.mfvanek.pg.model.index.IndexWithSize;
import io.github.mfvanek.pg.model.predicates.SkipIndexesByNamePredicate;
import io.github.mfvanek.pg.model.predicates.SkipTablesByNamePredicate;
import io.github.mfvanek.pg.support.DatabaseAwareTestBase;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -52,6 +53,14 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "accounts"))
.isEmpty();

assertThat(check)
.executing(ctx, SkipIndexesByNamePredicate.ofName(ctx, "accounts_account_number_key"))
.isEmpty();

assertThat(check)
.executing(ctx, SkipIndexesByNamePredicate.ofName(ctx, "i_accounts_account_number"))
.isEmpty();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.github.mfvanek.pg.model.PgContext;
import io.github.mfvanek.pg.model.index.DuplicatedIndexes;
import io.github.mfvanek.pg.model.index.IndexWithSize;
import io.github.mfvanek.pg.model.predicates.SkipIndexesByNamePredicate;
import io.github.mfvanek.pg.model.predicates.SkipTablesByNamePredicate;
import io.github.mfvanek.pg.support.DatabaseAwareTestBase;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -58,21 +59,34 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts", "clients")))
.isEmpty();

assertThat(check)
.executing(ctx, SkipIndexesByNamePredicate.of(ctx, List.of("i_clients_last_first", "i_accounts_number_balance_not_deleted")))
.isEmpty();
});
}

@ParameterizedTest
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"})
void shouldFindHashIndex(final String schemaName) {
executeTestOnDatabase(schemaName, dbp -> dbp.withReferences().withData().withDuplicatedHashIndex(), ctx ->
executeTestOnDatabase(schemaName, dbp -> dbp.withReferences().withData().withDuplicatedHashIndex(), ctx -> {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(
DuplicatedIndexes.of(
IndexWithSize.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_first"), 0L),
IndexWithSize.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name"), 0L)))
.allMatch(d -> d.getTotalSize() >= 106_496L));
.allMatch(d -> d.getTotalSize() >= 106_496L);

assertThat(check)
.executing(ctx, SkipIndexesByNamePredicate.ofName(ctx, "i_clients_last_first"))
.isEmpty();

assertThat(check)
.executing(ctx, SkipIndexesByNamePredicate.ofName(ctx, "i_clients_last_name"))
.isEmpty();
});
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private ConnectionCredentials(@Nonnull final Collection<String> connectionUrls,
}

/**
* Gets a set of connection strings for accessing all hosts in the database cluster.
* Retrieves a set of connection strings for accessing all hosts in the database cluster.
*
* @return connection urls
*/
Expand All @@ -54,7 +54,7 @@ public Collection<String> getConnectionUrls() {
}

/**
* Gets the name of the user to connect to the database.
* Retrieves the name of the user to connect to the database.
*
* @return the name of the user
*/
Expand All @@ -64,7 +64,7 @@ public String getUserName() {
}

/**
* Gets the user's password for connecting to the database.
* Retrieves the user's password for connecting to the database.
*
* @return the user's password
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
public interface HighAvailabilityPgConnection {

/**
* Gets connection to a primary host in the cluster.
* Retrieves connection to a primary host in the cluster.
*
* @return {@code PgConnection} to a primary host in the cluster
*/
@Nonnull
PgConnection getConnectionToPrimary();

/**
* Gets connections to all hosts in the cluster (including a connection to a primary host).
* Retrieves connections to all hosts in the cluster (including a connection to a primary host).
*
* @return {@code Set} of connections to all hosts in target cluster
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public interface HostAware {

/**
* Gets information about host in the cluster.
* Retrieves information about host in the cluster.
*
* @return {@code PgHost}
* @see PgHost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public interface PgConnection extends HostAware {

/**
* Gets a standard {@code DataSource} object to access the database.
* Retrieves a standard {@code DataSource} object to access the database.
*
* @return {@code DataSource}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
public interface PgHost {

/**
* Gets a valid connection string to this host.
* Retrieves a valid connection string to this host.
*
* @return connection url to the host
*/
@Nonnull
String getPgUrl();

/**
* Gets the name of this host.
* Retrieves the name of this host.
*
* @return host name
*/
@Nonnull
String getName();

/**
* Gets the port of this host.
* Retrieves the port of this host.
*
* @return port
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package io.github.mfvanek.pg.common.health.logger;

import io.github.mfvanek.pg.checks.predicates.FilterDuplicatedIndexesByNamePredicate;
import io.github.mfvanek.pg.common.maintenance.DatabaseCheckOnCluster;
import io.github.mfvanek.pg.common.maintenance.DatabaseChecks;
import io.github.mfvanek.pg.common.maintenance.Diagnostic;
Expand Down Expand Up @@ -130,13 +129,13 @@ private String writeZeroToLog(@Nonnull final LoggingKey key) {
@Nonnull
private String logDuplicatedIndexes(@Nonnull final Exclusions exclusions) {
return logCheckResult(databaseChecksHolder.get().getCheck(Diagnostic.DUPLICATED_INDEXES, DuplicatedIndexes.class),
FilterDuplicatedIndexesByNamePredicate.of(exclusions.getDuplicatedIndexesExclusions()), SimpleLoggingKey.DUPLICATED_INDEXES);
SkipIndexesByNamePredicate.of(pgContextHolder.get(), exclusions.getDuplicatedIndexesExclusions()), SimpleLoggingKey.DUPLICATED_INDEXES);
}

@Nonnull
private String logIntersectedIndexes(@Nonnull final Exclusions exclusions) {
return logCheckResult(databaseChecksHolder.get().getCheck(Diagnostic.INTERSECTED_INDEXES, DuplicatedIndexes.class),
FilterDuplicatedIndexesByNamePredicate.of(exclusions.getIntersectedIndexesExclusions()), SimpleLoggingKey.INTERSECTED_INDEXES);
SkipIndexesByNamePredicate.of(pgContextHolder.get(), exclusions.getIntersectedIndexesExclusions()), SimpleLoggingKey.INTERSECTED_INDEXES);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
public interface BloatAware {

/**
* Gets bloat amount in bytes.
* Retrieves bloat amount in bytes.
*
* @return bloat amount
*/
long getBloatSizeInBytes();

/**
* Gets bloat percentage (in the range from 0 to 100 inclusive).
* Retrieves bloat percentage (in the range from 0 to 100 inclusive).
*
* @return bloat percentage
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
public interface DbObject {

/**
* Gets database object name.
* Retrieves database object name.
*
* @return database object name
*/
@Nonnull
String getName();

/**
* Gets database object type.
* Retrieves database object type.
*
* @return database object type
* @since 0.13.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public interface ColumnNameAware extends TableNameAware {

/**
* Gets column name in the table.
* Retrieves column name in the table.
*
* @return column name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public boolean isNotNull() {
}

/**
* Gets raw type of serial column.
* Retrieves raw type of serial column.
*
* @return type of serial column
*/
Expand All @@ -97,7 +97,7 @@ public SerialType getSerialType() {
}

/**
* Gets name of the associated sequence.
* Retrieves name of the associated sequence.
*
* @return name of the associated sequence
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String toString() {
}

/**
* Gets {@code SerialType} from PostgreSQL serial column type.
* Retrieves {@code SerialType} from PostgreSQL serial column type.
*
* @param pgColumnType PostgreSQL serial column type; should be non-null.
* @return {@code SerialType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @since 0.11.0
*/
@Immutable
public class Constraint implements DbObject, TableNameAware {
public class Constraint implements DbObject, TableNameAware, ConstraintNameAware {

private final String tableName;
private final String constraintName;
Expand Down Expand Up @@ -78,17 +78,16 @@ public String getTableName() {
}

/**
* Gets the name of constraint.
*
* @return the name of constraint
* {@inheritDoc}
*/
@Nonnull
@Override
public String getConstraintName() {
return constraintName;
}

/**
* Gets type of constraint.
* Retrieves type of constraint.
*
* @return type of constraint
* @see ConstraintType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019-2024. Ivan Vakhrushev and others.
* https://github.com/mfvanek/pg-index-health
*
* This file is a part of "pg-index-health" - a Java library for
* analyzing and maintaining indexes health in PostgreSQL databases.
*
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.model.constraint;

import javax.annotation.Nonnull;

/**
* Represents an entity that is aware of a database constraint name.
* Classes implementing this interface should provide the name of a specific database constraint.
*
* @author Ivan Vakhrushev
* @since 0.13.3
*/
public interface ConstraintNameAware {

/**
* Retrieves the name of the database constraint associated with this entity.
*
* @return the name of the constraint as a non-null {@link String}.
*/
@Nonnull
String getConstraintName();
}
Loading

0 comments on commit d1473dc

Please sign in to comment.