-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checks for duplicated and intersected foreign keys (#451)
* Add DuplicatedForeignKeysCheckOnHost * Add test for DuplicatedForeignKeysCheckOnHost * Add IntersectedForeignKeysCheckOnHost * Add tests for IntersectedForeignKeysCheckOnHost * Add checks on cluster * Fix error in tests * Add code to HealthLogger * Add new beans to the starter
- Loading branch information
Showing
38 changed files
with
799 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../src/main/java/io/github/mfvanek/pg/checks/extractors/DuplicatedForeignKeysExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.checks.extractors; | ||
|
||
import io.github.mfvanek.pg.common.maintenance.ResultSetExtractor; | ||
import io.github.mfvanek.pg.model.constraint.DuplicatedForeignKeys; | ||
import io.github.mfvanek.pg.model.constraint.ForeignKey; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* A mapper from raw data to {@link DuplicatedForeignKeys} model. | ||
* | ||
* @author Ivan Vahrushev | ||
* @see ForeignKeyExtractor | ||
* @since 0.13.1 | ||
*/ | ||
public class DuplicatedForeignKeysExtractor implements ResultSetExtractor<DuplicatedForeignKeys> { | ||
|
||
private final ResultSetExtractor<ForeignKey> defaultExtractor; | ||
private final ResultSetExtractor<ForeignKey> duplicateKeyExtractor; | ||
|
||
private DuplicatedForeignKeysExtractor(@Nonnull final String prefix) { | ||
this.defaultExtractor = ForeignKeyExtractor.ofDefault(); | ||
this.duplicateKeyExtractor = ForeignKeyExtractor.withPrefix(prefix); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Nonnull | ||
@Override | ||
public DuplicatedForeignKeys extractData(@Nonnull final ResultSet resultSet) throws SQLException { | ||
final ForeignKey first = defaultExtractor.extractData(resultSet); | ||
final ForeignKey second = duplicateKeyExtractor.extractData(resultSet); | ||
return DuplicatedForeignKeys.of(first, second); | ||
} | ||
|
||
/** | ||
* Creates {@code DuplicatedForeignKeysExtractor} instance. | ||
* | ||
* @param prefix given prefix; must be non-null | ||
* @return {@code DuplicatedForeignKeysExtractor} instance | ||
*/ | ||
@Nonnull | ||
public static ResultSetExtractor<DuplicatedForeignKeys> of(@Nonnull final String prefix) { | ||
return new DuplicatedForeignKeysExtractor(prefix); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...health-core/src/main/java/io/github/mfvanek/pg/checks/extractors/ForeignKeyExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.checks.extractors; | ||
|
||
import io.github.mfvanek.pg.common.maintenance.ResultSetExtractor; | ||
import io.github.mfvanek.pg.model.column.Column; | ||
import io.github.mfvanek.pg.model.constraint.ForeignKey; | ||
import io.github.mfvanek.pg.utils.ColumnsInForeignKeyParser; | ||
|
||
import java.sql.Array; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.annotation.Nonnull; | ||
|
||
import static io.github.mfvanek.pg.checks.extractors.TableExtractor.TABLE_NAME; | ||
|
||
/** | ||
* A mapper from raw data to {@link ForeignKey} model. | ||
* | ||
* @author Ivan Vahrushev | ||
* @since 0.13.1 | ||
*/ | ||
public class ForeignKeyExtractor implements ResultSetExtractor<ForeignKey> { | ||
|
||
public static final String CONSTRAINT_NAME = "constraint_name"; | ||
|
||
private final String prefix; | ||
|
||
private ForeignKeyExtractor(@Nonnull final String prefix) { | ||
this.prefix = Objects.requireNonNull(prefix, "prefix cannot be null"); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Nonnull | ||
@Override | ||
public ForeignKey extractData(@Nonnull final ResultSet resultSet) throws SQLException { | ||
final String tableName = resultSet.getString(TABLE_NAME); | ||
final String constraintName = resultSet.getString(getConstraintNameField()); | ||
final Array columnsArray = resultSet.getArray(getColumnsField()); | ||
final String[] rawColumns = (String[]) columnsArray.getArray(); | ||
final List<Column> columns = ColumnsInForeignKeyParser.parseRawColumnData(tableName, rawColumns); | ||
return ForeignKey.of(tableName, constraintName, columns); | ||
} | ||
|
||
@Nonnull | ||
private String getConstraintNameField() { | ||
if (!prefix.isBlank()) { | ||
return prefix + "_" + CONSTRAINT_NAME; | ||
} | ||
return CONSTRAINT_NAME; | ||
} | ||
|
||
@Nonnull | ||
private String getColumnsField() { | ||
if (!prefix.isBlank()) { | ||
return prefix + "_constraint_columns"; | ||
} | ||
return "columns"; | ||
} | ||
|
||
/** | ||
* Creates default {@code ForeignKeyExtractor} instance. | ||
* | ||
* @return {@code ForeignKeyExtractor} instance | ||
*/ | ||
@Nonnull | ||
public static ResultSetExtractor<ForeignKey> ofDefault() { | ||
return new ForeignKeyExtractor(""); | ||
} | ||
|
||
/** | ||
* Creates {@code ForeignKeyExtractor} instance for duplicated/intersected constraint fields with given prefix. | ||
* | ||
* @param prefix given prefix; must be non-null | ||
* @return {@code ForeignKeyExtractor} instance | ||
*/ | ||
@Nonnull | ||
public static ResultSetExtractor<ForeignKey> withPrefix(@Nonnull final String prefix) { | ||
return new ForeignKeyExtractor(prefix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...core/src/main/java/io/github/mfvanek/pg/checks/host/DuplicatedForeignKeysCheckOnHost.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.checks.host; | ||
|
||
import io.github.mfvanek.pg.checks.extractors.DuplicatedForeignKeysExtractor; | ||
import io.github.mfvanek.pg.common.maintenance.Diagnostic; | ||
import io.github.mfvanek.pg.connection.PgConnection; | ||
import io.github.mfvanek.pg.model.PgContext; | ||
import io.github.mfvanek.pg.model.constraint.DuplicatedForeignKeys; | ||
|
||
import java.util.List; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Check for duplicated (completely identical) foreign keys on a specific host. | ||
* | ||
* @author Ivan Vahrushev | ||
* @since 0.13.1 | ||
*/ | ||
public class DuplicatedForeignKeysCheckOnHost extends AbstractCheckOnHost<DuplicatedForeignKeys> { | ||
|
||
/** | ||
* Creates a new {@code DuplicatedForeignKeysCheckOnHost} object. | ||
* | ||
* @param pgConnection connection to the PostgreSQL database, must not be null | ||
*/ | ||
public DuplicatedForeignKeysCheckOnHost(@Nonnull final PgConnection pgConnection) { | ||
super(DuplicatedForeignKeys.class, pgConnection, Diagnostic.DUPLICATED_FOREIGN_KEYS); | ||
} | ||
|
||
/** | ||
* Returns duplicated (completely identical) foreign keys in the specified schema. | ||
* | ||
* @param pgContext check's context with the specified schema | ||
* @return list of duplicated foreign keys | ||
*/ | ||
@Nonnull | ||
@Override | ||
public List<DuplicatedForeignKeys> check(@Nonnull final PgContext pgContext) { | ||
return executeQuery(pgContext, DuplicatedForeignKeysExtractor.of("duplicate")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ore/src/main/java/io/github/mfvanek/pg/checks/host/IntersectedForeignKeysCheckOnHost.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.checks.host; | ||
|
||
import io.github.mfvanek.pg.checks.extractors.DuplicatedForeignKeysExtractor; | ||
import io.github.mfvanek.pg.common.maintenance.Diagnostic; | ||
import io.github.mfvanek.pg.connection.PgConnection; | ||
import io.github.mfvanek.pg.model.PgContext; | ||
import io.github.mfvanek.pg.model.constraint.DuplicatedForeignKeys; | ||
|
||
import java.util.List; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Check for intersected (partially identical) foreign keys on a specific host. | ||
* | ||
* @author Ivan Vahrushev | ||
* @see DuplicatedForeignKeysCheckOnHost | ||
* @since 0.13.1 | ||
*/ | ||
public class IntersectedForeignKeysCheckOnHost extends AbstractCheckOnHost<DuplicatedForeignKeys> { | ||
|
||
/** | ||
* Creates a new {@code IntersectedForeignKeysCheckOnHost} object. | ||
* | ||
* @param pgConnection connection to the PostgreSQL database, must not be null | ||
*/ | ||
public IntersectedForeignKeysCheckOnHost(@Nonnull final PgConnection pgConnection) { | ||
super(DuplicatedForeignKeys.class, pgConnection, Diagnostic.INTERSECTED_FOREIGN_KEYS); | ||
} | ||
|
||
/** | ||
* Returns intersected (partially identical) foreign keys in the specified schema (except completely identical). | ||
* | ||
* @param pgContext check's context with the specified schema | ||
* @return list of intersected foreign keys | ||
* @see DuplicatedForeignKeysCheckOnHost | ||
*/ | ||
@Nonnull | ||
@Override | ||
public List<DuplicatedForeignKeys> check(@Nonnull final PgContext pgContext) { | ||
return executeQuery(pgContext, DuplicatedForeignKeysExtractor.of("intersected")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.