Skip to content

Commit

Permalink
[TABLES_WITHOUT_DESCRIPTION] Consider to skip partitions of table (#538)
Browse files Browse the repository at this point in the history
* Add info about partitioning support

* Update queries

* Add tests
  • Loading branch information
mfvanek authored Dec 10, 2024
1 parent 4aa4e6c commit 5aa2eea
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 30 deletions.
54 changes: 27 additions & 27 deletions README.md

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.9"

services:
postgres:
image: postgres:17.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.List;

import static io.github.mfvanek.pg.core.support.AbstractCheckOnHostAssert.assertThat;

class ColumnsWithoutDescriptionCheckOnHostTest extends DatabaseAwareTestBase {
Expand Down Expand Up @@ -95,4 +97,19 @@ void shouldIgnoreDroppedColumns(final String schemaName) {
.containsExactly(
Column.ofNullable(ctx.enrichWithSchema("clients"), "middle_name")));
}

@ParameterizedTest
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"})
void shouldWorkWithPartitionedTables(final String schemaName) {
final String expectedTableName = "custom_entity_reference_with_very_very_very_long_name";
executeTestOnDatabase(schemaName, DatabasePopulator::withPartitionedTableWithoutComments, ctx ->
assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts", "clients")))
.hasSize(4)
.containsExactly(
Column.ofNotNull(ctx, expectedTableName, "creation_date"),
Column.ofNotNull(ctx, expectedTableName, "entity_id"),
Column.ofNotNull(ctx, expectedTableName, "ref_type"),
Column.ofNotNull(ctx, expectedTableName, "ref_value")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,15 @@ void shouldNotTakingIntoAccountBlankComments(final String schemaName) {
.allMatch(t -> t.getTableSizeInBytes() > 1_234L);
});
}

@ParameterizedTest
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"})
void shouldWorkWithPartitionedTables(final String schemaName) {
executeTestOnDatabase(schemaName, DatabasePopulator::withPartitionedTableWithoutComments, ctx ->
assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts", "clients")))
.hasSize(1)
.containsExactly(
Table.of(ctx, "custom_entity_reference_with_very_very_very_long_name")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.github.mfvanek.pg.core.fixtures.support.statements.CreateIndexesWithDifferentOpclassStatement;
import io.github.mfvanek.pg.core.fixtures.support.statements.CreateMaterializedViewStatement;
import io.github.mfvanek.pg.core.fixtures.support.statements.CreateNotSuitableIndexForForeignKeyStatement;
import io.github.mfvanek.pg.core.fixtures.support.statements.CreatePartitionedTableWithoutComments;
import io.github.mfvanek.pg.core.fixtures.support.statements.CreateProceduresStatement;
import io.github.mfvanek.pg.core.fixtures.support.statements.CreateSchemaStatement;
import io.github.mfvanek.pg.core.fixtures.support.statements.CreateSequenceStatement;
Expand Down Expand Up @@ -309,6 +310,12 @@ public DatabasePopulator withIntersectedForeignKeys() {
return this;
}

@Nonnull
public DatabasePopulator withPartitionedTableWithoutComments() {
statementsToExecuteInSameTransaction.putIfAbsent(110, new CreatePartitionedTableWithoutComments());
return this;
}

public void populate() {
try (SchemaNameHolder ignored = SchemaNameHolder.with(schemaName)) {
ExecuteUtils.executeInTransaction(dataSource, statementsToExecuteInSameTransaction.values());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.core.fixtures.support.statements;

import java.util.List;
import javax.annotation.Nonnull;

public class CreatePartitionedTableWithoutComments extends AbstractDbStatement {

@Nonnull
@Override
protected List<String> getSqlToExecute() {
return List.of(
"create table if not exists {schemaName}.custom_entity_reference_with_very_very_very_long_name(" +
"ref_type varchar(32) not null," +
"ref_value varchar(64) not null," +
"creation_date timestamp with time zone not null," +
"entity_id varchar(64) not null," +
"primary key(ref_type, ref_value, creation_date, entity_id)" +
") partition by range (creation_date);",
"create table if not exists {schemaName}.custom_entity_reference_with_very_very_very_long_name_1_default " +
"partition of {schemaName}.custom_entity_reference_with_very_very_very_long_name default;"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.List;

import static io.github.mfvanek.pg.health.support.AbstractCheckOnClusterAssert.assertThat;

class ColumnsWithoutDescriptionCheckOnClusterTest extends DatabaseAwareTestBase {
Expand Down Expand Up @@ -94,4 +96,19 @@ void shouldIgnoreDroppedColumns(final String schemaName) {
.containsExactly(
Column.ofNullable(ctx.enrichWithSchema("clients"), "middle_name")));
}

@ParameterizedTest
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"})
void shouldWorkWithPartitionedTables(final String schemaName) {
final String expectedTableName = "custom_entity_reference_with_very_very_very_long_name";
executeTestOnDatabase(schemaName, DatabasePopulator::withPartitionedTableWithoutComments, ctx ->
assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts", "clients")))
.hasSize(4)
.containsExactly(
Column.ofNotNull(ctx, expectedTableName, "creation_date"),
Column.ofNotNull(ctx, expectedTableName, "entity_id"),
Column.ofNotNull(ctx, expectedTableName, "ref_type"),
Column.ofNotNull(ctx, expectedTableName, "ref_value")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ void shouldNotTakingIntoAccountBlankComments(final String schemaName) {
.allMatch(t -> t.getTableSizeInBytes() > 1_234L);
});
}

@ParameterizedTest
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"})
void shouldWorkWithPartitionedTables(final String schemaName) {
executeTestOnDatabase(schemaName, DatabasePopulator::withPartitionedTableWithoutComments, ctx ->
assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.of(ctx, List.of("accounts", "clients")))
.hasSize(1)
.containsExactly(
Table.of(ctx, "custom_entity_reference_with_very_very_very_long_name")));
}
}

0 comments on commit 5aa2eea

Please sign in to comment.