From 4efa26a62dd1c1e6d36645d0862e0db31029e9b6 Mon Sep 17 00:00:00 2001 From: Ivan Vakhrushev Date: Sun, 8 Dec 2024 12:42:40 +0400 Subject: [PATCH] Add additional constructors to the SequenceState --- .../host/SequenceOverflowCheckOnHostTest.java | 6 +++--- .../mfvanek/pg/model/context/PgContext.java | 15 ++++++++++++++ .../pg/model/sequence/SequenceState.java | 19 ++++++++++++++++++ .../github/mfvanek/pg/model/table/Table.java | 20 +++++++++---------- .../pg/model/context/PgContextTest.java | 7 +++++++ .../SkipBySequenceNamePredicateTest.java | 4 ++-- .../SkipFlywayTablesPredicateTest.java | 2 +- .../SkipLiquibaseTablesPredicateTest.java | 2 +- .../SkipTablesByNamePredicateTest.java | 2 +- .../SequenceOverflowCheckOnClusterTest.java | 6 +++--- 10 files changed, 61 insertions(+), 22 deletions(-) diff --git a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/SequenceOverflowCheckOnHostTest.java b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/SequenceOverflowCheckOnHostTest.java index e43b6671..b7dfb4eb 100644 --- a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/SequenceOverflowCheckOnHostTest.java +++ b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/SequenceOverflowCheckOnHostTest.java @@ -46,9 +46,9 @@ void onDatabaseWithSequences(final String schemaName) { .executing(ctx) .hasSize(3) .containsExactlyInAnyOrder( - SequenceState.of(ctx.enrichWithSchema("seq_1"), "smallint", 8.08), - SequenceState.of(ctx.enrichWithSchema("seq_3"), "integer", 8.08), - SequenceState.of(ctx.enrichWithSchema("seq_5"), "bigint", 8.08)); + SequenceState.of(ctx, "seq_1", "smallint", 8.08), + SequenceState.of(ctx, "seq_3", "integer", 8.08), + SequenceState.of(ctx, "seq_5", "bigint", 8.08)); assertThat(check) .executing(ctx, SkipBySequenceNamePredicate.of(ctx, List.of("seq_1", "seq_3", "seq_5"))) diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/context/PgContext.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/context/PgContext.java index a5bb5c17..78bce144 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/context/PgContext.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/context/PgContext.java @@ -13,6 +13,7 @@ import io.github.mfvanek.pg.model.validation.Validators; import java.util.Locale; +import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; @@ -178,4 +179,18 @@ public static PgContext of(@Nonnull final String schemaName) { public static PgContext ofPublic() { return of(DEFAULT_SCHEMA_NAME); } + + /** + * Complement the given object name with the specified schema name if it is necessary. + * + * @param objectName the name of the object to be enriched with schema information; must not be {@code null} + * @param pgContext the schema context to enrich object name; must be non-null. + * @return the fully qualified object name with schema information + * @since 0.14.3 + */ + @Nonnull + public static String enrichWith(@Nonnull final String objectName, @Nonnull final PgContext pgContext) { + Objects.requireNonNull(pgContext, "pgContext cannot be null"); + return pgContext.enrichWithSchema(objectName); + } } diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/sequence/SequenceState.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/sequence/SequenceState.java index 07757a7d..06755779 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/sequence/SequenceState.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/sequence/SequenceState.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.sequence; +import io.github.mfvanek.pg.model.context.PgContext; import io.github.mfvanek.pg.model.dbobject.DbObject; import io.github.mfvanek.pg.model.dbobject.PgObjectType; import io.github.mfvanek.pg.model.validation.Validators; @@ -148,4 +149,22 @@ public static SequenceState of(@Nonnull final String sequenceName, final double remainingPercentage) { return new SequenceState(sequenceName, dataType, remainingPercentage); } + + /** + * Constructs a {@code SequenceState} object with given context. + * + * @param pgContext the schema context to enrich sequence name; must be non-null. + * @param sequenceName sequence name; should be non-blank. + * @param dataType data type; should be non-blank. + * @param remainingPercentage remaining percentage; in the range from 0 to 100 inclusive. + * @return {@code SequenceState} + * @since 0.14.3 + */ + @Nonnull + public static SequenceState of(@Nonnull final PgContext pgContext, + @Nonnull final String sequenceName, + @Nonnull final String dataType, + final double remainingPercentage) { + return of(PgContext.enrichWith(sequenceName, pgContext), dataType, remainingPercentage); + } } diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/Table.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/Table.java index 6682235d..97687769 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/Table.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/Table.java @@ -132,7 +132,8 @@ public int compareTo(@Nonnull final Table other) { * @return {@code Table} */ @Nonnull - public static Table of(@Nonnull final String tableName, final long tableSizeInBytes) { + public static Table of(@Nonnull final String tableName, + final long tableSizeInBytes) { return new Table(tableName, tableSizeInBytes); } @@ -146,8 +147,10 @@ public static Table of(@Nonnull final String tableName, final long tableSizeInBy * @since 0.14.3 */ @Nonnull - public static Table of(@Nonnull final PgContext pgContext, @Nonnull final String tableName, final long tableSizeInBytes) { - return of(enrichWithSchema(pgContext, tableName), tableSizeInBytes); + public static Table of(@Nonnull final PgContext pgContext, + @Nonnull final String tableName, + final long tableSizeInBytes) { + return of(PgContext.enrichWith(tableName, pgContext), tableSizeInBytes); } /** @@ -171,14 +174,9 @@ public static Table of(@Nonnull final String tableName) { * @since 0.14.3 */ @Nonnull - public static Table of(@Nonnull final PgContext pgContext, @Nonnull final String tableName) { + public static Table of(@Nonnull final PgContext pgContext, + @Nonnull final String tableName) { Objects.requireNonNull(pgContext, "pgContext cannot be null"); - return of(enrichWithSchema(pgContext, tableName)); - } - - @Nonnull - private static String enrichWithSchema(@Nonnull final PgContext pgContext, @Nonnull final String tableName) { - Objects.requireNonNull(pgContext, "pgContext cannot be null"); - return pgContext.enrichWithSchema(tableName); + return of(PgContext.enrichWith(tableName, pgContext)); } } diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/context/PgContextTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/context/PgContextTest.java index 33e6bfdf..ebc0a131 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/context/PgContextTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/context/PgContextTest.java @@ -93,6 +93,8 @@ void complementWithCustomSchema() { .isEqualTo("test.table2"); assertThat(pgContext.enrichWithSchema("TEST.table2")) .isEqualTo("TEST.table2"); + assertThat(PgContext.enrichWith("table1", pgContext)) + .isEqualTo("test.table1"); } @Test @@ -106,6 +108,8 @@ void complementWithPublicSchema() { .isEqualTo("public.table2"); assertThat(pgContext.enrichWithSchema("PUBLIC.table2")) .isEqualTo("PUBLIC.table2"); + assertThat(PgContext.enrichWith("table1", pgContext)) + .isEqualTo("table1"); } @SuppressWarnings("ConstantConditions") @@ -121,5 +125,8 @@ void complementWithSchemaWithInvalidArguments() { assertThatThrownBy(() -> pgContext.enrichWithSchema(" ")) .isInstanceOf(IllegalArgumentException.class) .hasMessage("objectName cannot be blank"); + assertThatThrownBy(() -> PgContext.enrichWith(null, null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("pgContext cannot be null"); } } diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipBySequenceNamePredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipBySequenceNamePredicateTest.java index 68f602ed..811165b4 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipBySequenceNamePredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipBySequenceNamePredicateTest.java @@ -110,8 +110,8 @@ void shouldWorkWithCustomSchema(final String schemaName) { final PgContext ctx = PgContext.of(schemaName); assertThat(SkipBySequenceNamePredicate.of(ctx, Set.of("s1", "s2"))) .accepts(Table.of(ctx, "t")) - .accepts(SequenceState.of(ctx.enrichWithSchema("s11"), "int", 80.0)) - .rejects(SequenceState.of(ctx.enrichWithSchema("s1"), "int", 80.0)) + .accepts(SequenceState.of(ctx, "s11", "int", 80.0)) + .rejects(SequenceState.of(ctx, "s1", "int", 80.0)) .rejects(ColumnWithSerialType.ofSerial(Column.ofNullable(ctx.enrichWithSchema("t"), "c"), ctx.enrichWithSchema("s1"))); } } diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java index 77a81c40..0ab53a82 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java @@ -54,7 +54,7 @@ void shouldWorkWithCustomSchema(final String schemaName) { assertThat(SkipFlywayTablesPredicate.of(ctx)) .accepts(Table.of(ctx, "t")) .accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i"))) - .accepts(SequenceState.of(ctx.enrichWithSchema("s"), "int", 100.0)) + .accepts(SequenceState.of(ctx, "s", "int", 100.0)) .rejects(Table.of(ctx, "flyway_schema_history")) .rejects(Table.of(ctx, "FLYWAY_SCHEMA_HISTORY")); } diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java index 6e2a27b4..8c4dacf8 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java @@ -54,7 +54,7 @@ void shouldWorkWithCustomSchema(final String schemaName) { assertThat(SkipLiquibaseTablesPredicate.of(ctx)) .accepts(Table.of(ctx, "t")) .accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i"))) - .accepts(SequenceState.of(ctx.enrichWithSchema("s"), "int", 100.0)) + .accepts(SequenceState.of(ctx, "s", "int", 100.0)) .rejects(Table.of(ctx, "databasechangelog")) .rejects(Table.of(ctx, "DATABASECHANGELOG")) .rejects(Table.of(ctx, "databasechangeloglock")) diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java index 2d619157..bd13e928 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java @@ -96,7 +96,7 @@ void shouldWorkWithCustomSchema(final String schemaName) { assertThat(SkipTablesByNamePredicate.of(ctx, Set.of("t2", "T1"))) .accepts(Table.of(ctx, "t")) .accepts(Index.of(ctx.enrichWithSchema("T"), ctx.enrichWithSchema("I"))) - .accepts(SequenceState.of(ctx.enrichWithSchema("s"), "int", 100.0)) + .accepts(SequenceState.of(ctx, "s", "int", 100.0)) .rejects(Index.of(ctx.enrichWithSchema("t1"), ctx.enrichWithSchema("i1"))) .rejects(Index.of(ctx.enrichWithSchema("T2"), ctx.enrichWithSchema("i2"))) .accepts(Table.of(ctx, "t11")); diff --git a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/SequenceOverflowCheckOnClusterTest.java b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/SequenceOverflowCheckOnClusterTest.java index 7f1b49e2..ceca1842 100644 --- a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/SequenceOverflowCheckOnClusterTest.java +++ b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/SequenceOverflowCheckOnClusterTest.java @@ -45,9 +45,9 @@ void onDatabaseWithSequences(final String schemaName) { .executing(ctx) .hasSize(3) .containsExactlyInAnyOrder( - SequenceState.of(ctx.enrichWithSchema("seq_1"), "smallint", 8.08), - SequenceState.of(ctx.enrichWithSchema("seq_3"), "integer", 8.08), - SequenceState.of(ctx.enrichWithSchema("seq_5"), "bigint", 8.08)); + SequenceState.of(ctx, "seq_1", "smallint", 8.08), + SequenceState.of(ctx, "seq_3", "integer", 8.08), + SequenceState.of(ctx, "seq_5", "bigint", 8.08)); assertThat(check) .executing(ctx, SkipBySequenceNamePredicate.of(ctx, List.of("seq_1", "seq_3", "seq_5")))