Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloaded methods to create tables/indexes without specifying zero #531

Merged
merged 14 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add additional constructors to the Table
  • Loading branch information
mfvanek committed Dec 7, 2024
commit 2d538e414b7fd3e9a63d67f5a87ae3aceab6612d
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("bad_clients")));
.containsExactly(Table.of(ctx, "bad_clients"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "bad_clients"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactly(
Table.of(ctx.enrichWithSchema("accounts")),
Table.of(ctx.enrichWithSchema("clients")));
Table.of(ctx, "accounts"),
Table.of(ctx, "clients"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts", "clients")))
Expand All @@ -64,13 +64,13 @@ void shouldNotTakingIntoAccountBlankComments(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactly(
Table.of(ctx.enrichWithSchema("accounts")),
Table.of(ctx.enrichWithSchema("clients")));
Table.of(ctx, "accounts"),
Table.of(ctx, "clients"));

assertThat(check)
.executing(ctx, SkipSmallTablesPredicate.of(1_234L))
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("clients")))
.containsExactly(Table.of(ctx, "clients"))
.allMatch(t -> t.getTableSizeInBytes() > 1_234L);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("bad_clients")));
.containsExactly(Table.of(ctx, "bad_clients"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "bad_clients"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public final List<String> logAll(@Nonnull final Exclusions exclusions,
return logResult;
}

private Predicate<DbObject> prepareFilters(@Nonnull final Exclusions exclusions, @Nonnull final PgContext ctx) {
return SkipTablesByNamePredicate.of(ctx, exclusions.getTableNameExclusions())
.and(SkipIndexesByNamePredicate.of(ctx, exclusions.getIndexNameExclusions()))
.and(SkipBySequenceNamePredicate.of(ctx, exclusions.getSequenceNameExclusions()))
private Predicate<DbObject> prepareFilters(@Nonnull final Exclusions exclusions, @Nonnull final PgContext pgContext) {
return SkipTablesByNamePredicate.of(pgContext, exclusions.getTableNameExclusions())
.and(SkipIndexesByNamePredicate.of(pgContext, exclusions.getIndexNameExclusions()))
.and(SkipBySequenceNamePredicate.of(pgContext, exclusions.getSequenceNameExclusions()))
.and(SkipBloatUnderThresholdPredicate.of(exclusions.getBloatSizeThresholdInBytes(), exclusions.getBloatPercentageThreshold()))
.and(SkipSmallTablesPredicate.of(exclusions.getTableSizeThresholdInBytes()))
.and(SkipSmallIndexesPredicate.of(exclusions.getIndexSizeThresholdInBytes()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ private PgParamImpl(@Nonnull final String name, @Nonnull final String value) {
this.value = ParamValidators.paramValueNotNull(value, "value for '" + name + "' cannot be null");
}

/**
* {@inheritDoc}
*/
@Override
@Nonnull
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

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 @@ -135,6 +136,20 @@ public static Table of(@Nonnull final String tableName, final long tableSizeInBy
return new Table(tableName, tableSizeInBytes);
}

/**
* Constructs a {@code Table} object with given context.
*
* @param pgContext the schema context to enrich table name; must be non-null.
* @param tableName table name; should be non-blank.
* @param tableSizeInBytes table size in bytes; should be positive or zero.
* @return {@code Table}
* @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);
}

/**
* Constructs a {@code Table} object with zero size.
*
Expand All @@ -144,6 +159,26 @@ public static Table of(@Nonnull final String tableName, final long tableSizeInBy
*/
@Nonnull
public static Table of(@Nonnull final String tableName) {
return new Table(tableName, 0L);
return of(tableName, 0L);
}

/**
* Constructs a {@code Table} object with zero size and given context.
*
* @param pgContext the schema context to enrich table name; must be non-null.
* @param tableName table name; should be non-blank.
* @return {@code Table}
* @since 0.14.3
*/
@Nonnull
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void shouldWorkForMultipleSequences() {
void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipBySequenceNamePredicate.of(ctx, Set.of("s1", "s2")))
.accepts(Table.of(ctx.enrichWithSchema("t")))
.accepts(Table.of(ctx, "t"))
.accepts(SequenceState.of(ctx.enrichWithSchema("s11"), "int", 80.0))
.rejects(SequenceState.of(ctx.enrichWithSchema("s1"), "int", 80.0))
.rejects(ColumnWithSerialType.ofSerial(Column.ofNullable(ctx.enrichWithSchema("t"), "c"), ctx.enrichWithSchema("s1")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ void shouldWorkWithDbObjectsList() {
void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipFlywayTablesPredicate.of(ctx))
.accepts(Table.of(ctx.enrichWithSchema("t")))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i")))
.accepts(SequenceState.of(ctx.enrichWithSchema("s"), "int", 100.0))
.rejects(Table.of(ctx.enrichWithSchema("flyway_schema_history")))
.rejects(Table.of(ctx.enrichWithSchema("FLYWAY_SCHEMA_HISTORY")));
.rejects(Table.of(ctx, "flyway_schema_history"))
.rejects(Table.of(ctx, "FLYWAY_SCHEMA_HISTORY"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void shouldWorkForMultipleIndexes() {
void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipIndexesByNamePredicate.of(ctx, Set.of("i1", "i2")))
.accepts(Table.of(ctx.enrichWithSchema("t")))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("t1"), ctx.enrichWithSchema("i11")))
.rejects(Index.of(ctx.enrichWithSchema("t2"), ctx.enrichWithSchema("i2")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ void shouldWorkWithDbObjectsList() {
void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipLiquibaseTablesPredicate.of(ctx))
.accepts(Table.of(ctx.enrichWithSchema("t")))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i")))
.accepts(SequenceState.of(ctx.enrichWithSchema("s"), "int", 100.0))
.rejects(Table.of(ctx.enrichWithSchema("databasechangelog")))
.rejects(Table.of(ctx.enrichWithSchema("DATABASECHANGELOG")))
.rejects(Table.of(ctx.enrichWithSchema("databasechangeloglock")))
.rejects(Table.of(ctx.enrichWithSchema("DATABASECHANGELOGLOCK")));
.rejects(Table.of(ctx, "databasechangelog"))
.rejects(Table.of(ctx, "DATABASECHANGELOG"))
.rejects(Table.of(ctx, "databasechangeloglock"))
.rejects(Table.of(ctx, "DATABASECHANGELOGLOCK"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ void shouldWorkForMultipleTables() {
void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipTablesByNamePredicate.of(ctx, Set.of("t2", "T1")))
.accepts(Table.of(ctx.enrichWithSchema("t")))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("T"), ctx.enrichWithSchema("I")))
.accepts(SequenceState.of(ctx.enrichWithSchema("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.enrichWithSchema("t11")));
.accepts(Table.of(ctx, "t11"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.PgObjectType;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -46,12 +47,22 @@ void withInvalidValues() {
assertThatThrownBy(() -> Table.of("t", -1L))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("tableSizeInBytes cannot be less than zero");
assertThatThrownBy(() -> Table.of(null, "t"))
.isInstanceOf(NullPointerException.class)
.hasMessage("pgContext cannot be null");
assertThatThrownBy(() -> Table.of(null, "t", -1L))
.isInstanceOf(NullPointerException.class)
.hasMessage("pgContext cannot be null");
}

@Test
void testToString() {
assertThat(Table.of("t", 2L))
.hasToString("Table{tableName='t', tableSizeInBytes=2}");
assertThat(Table.of(PgContext.ofPublic(), "t", 2L))
.hasToString("Table{tableName='t', tableSizeInBytes=2}");
assertThat(Table.of(PgContext.of("test_schema"), "t"))
.hasToString("Table{tableName='test_schema.t', tableSizeInBytes=0}");
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("bad_clients")));
.containsExactly(Table.of(ctx, "bad_clients"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "bad_clients"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactly(
Table.of(ctx.enrichWithSchema("accounts")),
Table.of(ctx.enrichWithSchema("clients")));
Table.of(ctx, "accounts"),
Table.of(ctx, "clients"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts")))
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("clients")))
.containsExactly(Table.of(ctx, "clients"))
.allMatch(t -> t.getTableSizeInBytes() > 0L);
});
}
Expand All @@ -65,13 +65,13 @@ void shouldNotTakingIntoAccountBlankComments(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactly(
Table.of(ctx.enrichWithSchema("accounts")),
Table.of(ctx.enrichWithSchema("clients")));
Table.of(ctx, "accounts"),
Table.of(ctx, "clients"));

assertThat(check)
.executing(ctx, SkipSmallTablesPredicate.of(1_234L))
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("clients")))
.containsExactly(Table.of(ctx, "clients"))
.allMatch(t -> t.getTableSizeInBytes() > 1_234L);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("bad_clients")));
.containsExactly(Table.of(ctx, "bad_clients"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "bad_clients"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void check() {
.thenAnswer(invocation -> {
final PgContext ctx = invocation.getArgument(0);
return List.of(
Table.of(ctx.enrichWithSchema("t1"), 1L),
Table.of(ctx.enrichWithSchema("t2"), 1L));
Table.of(ctx, "t1", 1L),
Table.of(ctx, "t2", 1L));
});
final List<Table> tables = check.check(CONTEXTS, item -> true);
assertThat(tables)
Expand Down
Loading