Skip to content

Commit

Permalink
Add additional constructors to the SequenceState
Browse files Browse the repository at this point in the history
  • Loading branch information
mfvanek committed Dec 8, 2024
1 parent 0c7749f commit 4efa26a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

/**
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down

0 comments on commit 4efa26a

Please sign in to comment.