Skip to content

Commit

Permalink
Add overloaded methods to create tables/indexes without specifying ze…
Browse files Browse the repository at this point in the history
…ro (#531)

* Creation of Table with zero size

* Creation of Indexes with zero size

* Add javadoc

* Add additional constructors to the Table

* Add additional constructors to the TableWithBloat and TableWithMissingIndex

* Add additional constructors to the SequenceState

* Add additional constructors to the StoredFunction

* Add additional constructors to the Index and IndexWithBloat

* Add additional constructors to the IndexWithColumns and Column

* Add additional constructors to the IndexWithNulls

* Add additional constructors to the IndexWithSize

* Add additional constructors to the UnusedIndex

* Add additional constructors to the IndexWithBloat and TableWithBloat

* Add additional constructors to the Constraint
  • Loading branch information
mfvanek authored Dec 8, 2024
1 parent f1f42a9 commit d9143e0
Show file tree
Hide file tree
Showing 78 changed files with 724 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactlyInAnyOrder(
IndexWithColumns.ofSingle(accountsTableName, ctx.enrichWithSchema("accounts_roles_btree_idx"), 0L,
IndexWithColumns.ofSingle(ctx, accountsTableName, "accounts_roles_btree_idx", 0L,
Column.ofNotNull(accountsTableName, "roles")),
IndexWithColumns.ofSingle(accountsTableName, ctx.enrichWithSchema("accounts_account_number_roles_btree_idx"), 0L,
IndexWithColumns.ofSingle(ctx, accountsTableName, "accounts_account_number_roles_btree_idx", 0L,
Column.ofNotNull(accountsTableName, "roles"))
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void shouldIgnoreDroppedColumns(final String schemaName) {
.hasSize(1)
.containsExactly(
ColumnWithSerialType.ofBigSerial(
Column.ofNotNull(ctx.enrichWithSchema("bad_accounts"), "real_client_id"), ctx.enrichWithSchema("bad_accounts_real_client_id_seq"))
Column.ofNotNull(ctx, "bad_accounts", "real_client_id"), ctx.enrichWithSchema("bad_accounts_real_client_id_seq"))
);

assertThat(check)
Expand Down Expand Up @@ -105,7 +105,7 @@ void shouldDetectSerialColumnsWithUniqueConstraints(final String schemaName) {
.hasSize(1)
.containsExactly(
ColumnWithSerialType.ofBigSerial(
Column.ofNotNull(ctx.enrichWithSchema("one_more_table"), "id"), ctx.enrichWithSchema("one_more_table_id_seq"))
Column.ofNotNull(ctx, "one_more_table", "id"), ctx.enrichWithSchema("one_more_table_id_seq"))
));
}

Expand All @@ -118,11 +118,11 @@ void shouldDetectPrimaryKeysThatAreForeignKeysAsWell(final String schemaName) {
.hasSize(3)
.containsExactly(
ColumnWithSerialType.ofBigSerial(
Column.ofNotNull(ctx.enrichWithSchema("one_more_table"), "id"), ctx.enrichWithSchema("one_more_table_id_seq")),
Column.ofNotNull(ctx, "one_more_table", "id"), ctx.enrichWithSchema("one_more_table_id_seq")),
ColumnWithSerialType.ofBigSerial(
Column.ofNotNull(ctx.enrichWithSchema("test_table"), "id"), ctx.enrichWithSchema("test_table_id_seq")),
Column.ofNotNull(ctx, "test_table", "id"), ctx.enrichWithSchema("test_table_id_seq")),
ColumnWithSerialType.ofBigSerial(
Column.ofNotNull(ctx.enrichWithSchema("test_table"), "num"), ctx.enrichWithSchema("test_table_num_seq"))
Column.ofNotNull(ctx, "test_table", "num"), ctx.enrichWithSchema("test_table_num_seq"))
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void shouldNotTakingIntoAccountBlankComments(final String schemaName) {
.filteredOn(c -> "id".equalsIgnoreCase(c.getColumnName()))
.hasSize(2)
.containsExactly(
Column.ofNotNull(ctx.enrichWithSchema("accounts"), "id"),
Column.ofNotNull(ctx.enrichWithSchema("clients"), "id")));
Column.ofNotNull(ctx, "accounts", "id"),
Column.ofNotNull(ctx, "clients", "id")));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void onDatabaseWithThem(final String schemaName) {
.hasSize(1)
.containsExactly(
DuplicatedIndexes.of(
IndexWithSize.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("accounts_account_number_key"), 0L),
IndexWithSize.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_account_number"), 0L)))
IndexWithSize.of(ctx, "accounts", "accounts_account_number_key"),
IndexWithSize.of(ctx, "accounts", "i_accounts_account_number")))
.allMatch(d -> d.getTotalSize() >= 16_384L);

assertThat(check)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void shouldNotTakingIntoAccountBlankComments(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactly(
StoredFunction.of(ctx.enrichWithSchema("add"), "a integer, b integer"),
StoredFunction.of(ctx.enrichWithSchema("add"), "a integer, b integer, c integer")
StoredFunction.of(ctx, "add", "a integer, b integer"),
StoredFunction.of(ctx, "add", "a integer, b integer, c integer")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(4)
.containsExactlyInAnyOrder(
IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_account_number_key"), 0L, 0L, 0),
IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_pkey"), 0L, 0L, 0),
IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("clients_pkey"), 0L, 0L, 0),
IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("i_clients_email_phone"), 0L, 0L, 0))
IndexWithBloat.of(ctx, accountsTableName, "accounts_account_number_key"),
IndexWithBloat.of(ctx, accountsTableName, "accounts_pkey"),
IndexWithBloat.of(ctx, clientsTableName, "clients_pkey"),
IndexWithBloat.of(ctx, clientsTableName, "i_clients_email_phone"))
.allMatch(i -> i.getIndexSizeInBytes() > 1L)
.allMatch(i -> i.getBloatSizeInBytes() > 1L && i.getBloatPercentage() >= 14);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(1)
.containsExactly(
IndexWithColumns.ofSingle(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_deleted"), 0L,
Column.ofNotNull(ctx.enrichWithSchema("accounts"), "deleted"))
IndexWithColumns.ofSingle(ctx, "accounts", "i_accounts_deleted", 0L,
Column.ofNotNull(ctx, "accounts", "deleted"))
);

assertThat(check)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(
IndexWithNulls.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_middle_name"), 0L, "middle_name"))
.containsExactly(IndexWithNulls.of(ctx, "clients", "i_clients_middle_name", 0L, "middle_name"))
.allMatch(i -> i.getNullableColumn().isNullable());

assertThat(check)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ void onDatabaseWithThem(final String schemaName) {
.hasSize(2)
.containsExactly(
DuplicatedIndexes.of(
IndexWithSize.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_account_number_not_deleted"), 0L),
IndexWithSize.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_number_balance_not_deleted"), 0L)
IndexWithSize.of(ctx, "accounts", "i_accounts_account_number_not_deleted"),
IndexWithSize.of(ctx, "accounts", "i_accounts_number_balance_not_deleted")
),
DuplicatedIndexes.of(
IndexWithSize.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_first"), 0L),
IndexWithSize.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name"), 0L)))
IndexWithSize.of(ctx, "clients", "i_clients_last_first"),
IndexWithSize.of(ctx, "clients", "i_clients_last_name")
))
.allMatch(d -> d.getTotalSize() >= 106_496L);

assertThat(check)
Expand All @@ -75,8 +76,8 @@ void shouldFindHashIndex(final String schemaName) {
.hasSize(1)
.containsExactly(
DuplicatedIndexes.of(
IndexWithSize.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_first"), 0L),
IndexWithSize.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name"), 0L)))
IndexWithSize.of(ctx, "clients", "i_clients_last_first"),
IndexWithSize.of(ctx, "clients", "i_clients_last_name")))
.allMatch(d -> d.getTotalSize() >= 106_496L);

assertThat(check)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(
Index.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name_first_name")));
.containsExactly(Index.of(ctx, "clients", "i_clients_last_name_first_name"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "clients"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void onDatabaseWithThem(final String schemaName) {
Assertions.assertThat(notValidConstraints)
.hasSize(2)
.containsExactly(
Constraint.ofType(ctx.enrichWithSchema("accounts"), "c_accounts_chk_client_id_not_validated_yet", ConstraintType.CHECK),
Constraint.ofType(ctx.enrichWithSchema("accounts"), "c_accounts_fk_client_id_not_validated_yet", ConstraintType.FOREIGN_KEY));
Constraint.ofType(ctx, "accounts", "c_accounts_chk_client_id_not_validated_yet", ConstraintType.CHECK),
Constraint.ofType(ctx, "accounts", "c_accounts_fk_client_id_not_validated_yet", ConstraintType.FOREIGN_KEY));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "accounts"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void onDatabaseWithThem(final String schemaName) {
.hasSize(1)
.containsExactlyInAnyOrder(
ColumnWithSerialType.of(
Column.ofNotNull(ctx.enrichWithSchema("bad_accounts"), "id"), SerialType.BIG_SERIAL, ctx.enrichWithSchema("bad_accounts_id_seq")
Column.ofNotNull(ctx, "bad_accounts", "id"), SerialType.BIG_SERIAL, ctx.enrichWithSchema("bad_accounts_id_seq")
));

assertThat(check)
Expand Down
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 @@ -42,7 +42,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("bad_clients"), 0L));
.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 @@ -51,8 +51,8 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactlyInAnyOrder(
TableWithBloat.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0),
TableWithBloat.of(ctx.enrichWithSchema("clients"), 0L, 0L, 0))
TableWithBloat.of(ctx, "accounts"),
TableWithBloat.of(ctx, "clients"))
.allMatch(t -> t.getTableSizeInBytes() > 0L) // real size doesn't matter
.allMatch(t -> t.getBloatPercentage() == 0 && t.getBloatSizeInBytes() == 0L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(1)
.containsExactly(
TableWithMissingIndex.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0L))
TableWithMissingIndex.of(ctx, "accounts", 0L, 0L, 0L))
.allMatch(t -> t.getSeqScans() >= AMOUNT_OF_TRIES)
.allMatch(t -> t.getIndexScans() == 0)
.allMatch(t -> t.getTableSizeInBytes() > 1L);
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"), 0L),
Table.of(ctx.enrichWithSchema("clients"), 0L));
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"), 0L),
Table.of(ctx.enrichWithSchema("clients"), 0L));
Table.of(ctx, "accounts"),
Table.of(ctx, "clients"));

assertThat(check)
.executing(ctx, SkipSmallTablesPredicate.of(1_234L))
.hasSize(1)
.containsExactly(Table.of(ctx.enrichWithSchema("clients"), 0L))
.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"), 0L));
.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,12 +47,12 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(6)
.containsExactlyInAnyOrder(
UnusedIndex.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_first"), 0L, 0),
UnusedIndex.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name"), 0L, 0),
UnusedIndex.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_account_number"), 0L, 0),
UnusedIndex.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_number_balance_not_deleted"), 0L, 0),
UnusedIndex.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_account_number_not_deleted"), 0L, 0),
UnusedIndex.of(ctx.enrichWithSchema("accounts"), ctx.enrichWithSchema("i_accounts_id_account_number_not_deleted"), 0L, 0))
UnusedIndex.of(ctx, "clients", "i_clients_last_first"),
UnusedIndex.of(ctx, "clients", "i_clients_last_name"),
UnusedIndex.of(ctx, "accounts", "i_accounts_account_number"),
UnusedIndex.of(ctx, "accounts", "i_accounts_number_balance_not_deleted"),
UnusedIndex.of(ctx, "accounts", "i_accounts_account_number_not_deleted"),
UnusedIndex.of(ctx, "accounts", "i_accounts_id_account_number_not_deleted"))
.allMatch(i -> i.getIndexSizeInBytes() > 0L)
.allMatch(i -> i.getIndexScans() == 0);

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
Loading

0 comments on commit d9143e0

Please sign in to comment.