-
-
Notifications
You must be signed in to change notification settings - Fork 16
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 code for check_not_valid_constraints.sql #374
Conversation
The current test code doesn't seem to be good code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are checkstyle issues (see tests results).
Please ensure that project builds locally: ./gradlew build
See https://github.com/mfvanek/pg-index-health/blob/master/CONTRIBUTING.md
pg-index-health/src/main/java/io/github/mfvanek/pg/common/maintenance/Diagnostic.java
Outdated
Show resolved
Hide resolved
...ex-health/src/main/java/io/github/mfvanek/pg/checks/host/CheckNotValidConstraintsOnHost.java
Outdated
Show resolved
Hide resolved
...lth/src/main/java/io/github/mfvanek/pg/checks/cluster/CheckNotValidConstraintsOnCluster.java
Outdated
Show resolved
Hide resolved
...src/test/java/io/github/mfvanek/pg/checks/cluster/CheckNotValidConstraintsOnClusterTest.java
Outdated
Show resolved
Hide resolved
...ealth/src/test/java/io/github/mfvanek/pg/checks/host/CheckNotValidConstraintsOnHostTest.java
Outdated
Show resolved
Hide resolved
|
...ex-health/src/main/java/io/github/mfvanek/pg/checks/host/CheckNotValidConstraintsOnHost.java
Outdated
Show resolved
Hide resolved
// AddInvalidForeignKeyStatement.java
@Override
public void execute(@Nonnull final Statement statement) throws SQLException {
statement.execute(String.format("alter table if exists %1$s.accounts " +
"add constraint c_accounts_fk_invalid foreign key (invalid_client_id) references %1$s.invalid_clients (invalid_id);",
schemaName));
} // CreateTableWithCheckConstraintOnSerialPrimaryKey.java
@Override
public void execute(@Nonnull final Statement statement) throws SQLException {
statement.execute(String.format("create table if not exists %1$s.another_table(" +
"id bigserial primary key, " +
"invalid_column integer, " +
"constraint not_reserved_id check (id > 1000), " +
"constraint less_than_million check (id < 1000000), " +
"constraint always_false check (invalid_column > 100 AND invalid_column < 0));",
schemaName));
} Is this modified code suitable? |
Unfortunately, no, it isn't. When I say "not valid constraint" it's not about true or false constraint is, it's about whether database uses this constraint or not. Not valid constraint is like a draft. Please take a look at the original article https://habr.com/ru/articles/800121/ It should be like this
In tests after you find all not valid constraints you can can call |
@ParameterizedTest
@ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"})
void onDatabaseWithNotValidatedForeignKey(final String schemaName) {
executeTestOnDatabase(schemaName, DatabasePopulator::withInvalidForeignKey, ctx ->
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(
Constraint.of(ctx.enrichWithSchema("accounts"), "c_accounts_fk_client_id_not_validated_yet",
ConstraintType.FOREIGN_KEY)
)
);
} Is it okay to proceed with the test code like this? |
Hi @BLoHny
I'd suggest a few enhancements:
|
statement.execute(String.format("alter table if exists %1$s.accounts " +
"add constraint c_accounts_chk_client_id_not_validated_yet check (client_id > 0) not valid;",
schemaName)); After add more sql query.. .containsExactly(
Constraint.of(ctx.enrichWithSchema("accounts"), "c_accounts_chk_client_id_not_validated_yet",
ConstraintType.CHECK),
Constraint.of(ctx.enrichWithSchema("accounts"), "c_accounts_fk_client_id_not_validated_yet",
ConstraintType.FOREIGN_KEY)
) add test code proceed like this? |
Yes, looks good |
open new pull request #382 by conflict |
Relates to #362