-
-
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 check "Tables are not linked to other tables" (#469)
* Add TablesNotLinkedToOthersCheckOnHost * Add check on cluster and logger * Add new bean to the starter
- Loading branch information
Showing
15 changed files
with
301 additions
and
202 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
48 changes: 48 additions & 0 deletions
48
...re/src/main/java/io/github/mfvanek/pg/checks/host/TablesNotLinkedToOthersCheckOnHost.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,48 @@ | ||
/* | ||
* 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.TableExtractor; | ||
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.table.Table; | ||
|
||
import java.util.List; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Check for tables that are not linked to other tables on a specific host. | ||
* <p> | ||
* These are often service tables that are not part of the project, or | ||
* tables that are no longer in use or were created by mistake, but were not deleted in a timely manner. | ||
* | ||
* @author Ivan Vahrushev | ||
* @since 0.13.2 | ||
*/ | ||
public class TablesNotLinkedToOthersCheckOnHost extends AbstractCheckOnHost<Table> { | ||
|
||
public TablesNotLinkedToOthersCheckOnHost(@Nonnull final PgConnection pgConnection) { | ||
super(Table.class, pgConnection, Diagnostic.TABLES_NOT_LINKED_TO_OTHERS); | ||
} | ||
|
||
/** | ||
* Returns tables that are no longer in use or were created by mistake. | ||
* | ||
* @param pgContext check's context with the specified schema; must not be null | ||
* @return list of tables that are no longer in use or were created by mistake | ||
*/ | ||
@Nonnull | ||
@Override | ||
protected List<Table> doCheck(@Nonnull final PgContext pgContext) { | ||
return executeQuery(pgContext, TableExtractor.of()); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
...rc/test/java/io/github/mfvanek/pg/checks/host/TablesNotLinkedToOthersCheckOnHostTest.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,46 @@ | ||
/* | ||
* 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.common.maintenance.DatabaseCheckOnHost; | ||
import io.github.mfvanek.pg.common.maintenance.Diagnostic; | ||
import io.github.mfvanek.pg.model.PgContext; | ||
import io.github.mfvanek.pg.model.table.Table; | ||
import io.github.mfvanek.pg.support.DatabaseAwareTestBase; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static io.github.mfvanek.pg.support.AbstractCheckOnHostAssert.assertThat; | ||
|
||
class TablesNotLinkedToOthersCheckOnHostTest extends DatabaseAwareTestBase { | ||
|
||
private final DatabaseCheckOnHost<Table> check = new TablesNotLinkedToOthersCheckOnHost(getPgConnection()); | ||
|
||
@Test | ||
void shouldSatisfyContract() { | ||
assertThat(check) | ||
.hasType(Table.class) | ||
.hasDiagnostic(Diagnostic.TABLES_NOT_LINKED_TO_OTHERS) | ||
.hasHost(getHost()) | ||
.isStatic(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"}) | ||
void onDatabaseWithThem(final String schemaName) { | ||
executeTestOnDatabase(schemaName, dbp -> dbp.withReferences().withMaterializedView().withTableWithoutPrimaryKey(), ctx -> | ||
assertThat(check) | ||
.executing(ctx) | ||
.hasSize(1) | ||
.containsExactly(Table.of(ctx.enrichWithSchema("bad_clients"), 0L))); | ||
} | ||
} |
Oops, something went wrong.