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
Add additional constructors to the Constraint
  • Loading branch information
mfvanek committed Dec 8, 2024
commit ff072fbac7dd88361a6ee0e20ae885032bf56039
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 @@ -10,6 +10,7 @@

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

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.table.TableNameAware;
Expand Down Expand Up @@ -170,4 +171,22 @@ public static Constraint ofType(@Nonnull final String tableName,
@Nonnull final ConstraintType constraintType) {
return new Constraint(tableName, constraintName, constraintType);
}

/**
* Constructs a {@code Constraint} object with given {@code ConstraintType} and context.
*
* @param pgContext the schema context to enrich table name; must be non-null.
* @param tableName table name; should be non-blank.
* @param constraintName constraint name; should be non-blank.
* @param constraintType constraint type; should be non-null.
* @return {@code Constraint}
* @since 0.14.3
*/
@Nonnull
public static Constraint ofType(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String constraintName,
@Nonnull final ConstraintType constraintType) {
return ofType(PgContext.enrichWith(tableName, pgContext), constraintName, constraintType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

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 All @@ -21,13 +22,16 @@ class ConstraintTest {

@Test
void testToString() {
final Constraint constraintWithCheck = Constraint.ofType("t", "not_valid_id", ConstraintType.CHECK);
assertThat(constraintWithCheck)
final PgContext ctx = PgContext.of("tst");
assertThat(Constraint.ofType("t", "not_valid_id", ConstraintType.CHECK))
.hasToString("Constraint{tableName='t', constraintName='not_valid_id', constraintType=CHECK}");
assertThat(Constraint.ofType(ctx, "t", "not_valid_id", ConstraintType.CHECK))
.hasToString("Constraint{tableName='tst.t', constraintName='not_valid_id', constraintType=CHECK}");

final Constraint constraintWithForeignKey = Constraint.ofType("t", "not_valid_id", ConstraintType.FOREIGN_KEY);
assertThat(constraintWithForeignKey)
assertThat(Constraint.ofType("t", "not_valid_id", ConstraintType.FOREIGN_KEY))
.hasToString("Constraint{tableName='t', constraintName='not_valid_id', constraintType=FOREIGN_KEY}");
assertThat(Constraint.ofType(ctx, "t1", "not_valid_id", ConstraintType.FOREIGN_KEY))
.hasToString("Constraint{tableName='tst.t1', constraintName='not_valid_id', constraintType=FOREIGN_KEY}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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
Loading