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 IndexWithColumns and Column
  • Loading branch information
mfvanek committed Dec 8, 2024
commit a496aabbf414fce3482d3eccd5efef27960439a9
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 @@ -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 @@ -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 @@ -10,6 +10,7 @@

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

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 @@ -151,10 +152,27 @@ public int compareTo(@Nonnull final Column other) {
* @return {@code Column}
*/
@Nonnull
public static Column ofNotNull(@Nonnull final String tableName, @Nonnull final String columnName) {
public static Column ofNotNull(@Nonnull final String tableName,
@Nonnull final String columnName) {
return new Column(tableName, columnName, true);
}

/**
* Constructs a not null {@code Column} 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 columnName column name; should be non-blank.
* @return {@code Column}
* @since 0.14.3
*/
@Nonnull
public static Column ofNotNull(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String columnName) {
return ofNotNull(PgContext.enrichWith(tableName, pgContext), columnName);
}

/**
* Constructs a nullable {@code Column} object.
*
Expand All @@ -163,7 +181,24 @@ public static Column ofNotNull(@Nonnull final String tableName, @Nonnull final S
* @return {@code Column}
*/
@Nonnull
public static Column ofNullable(@Nonnull final String tableName, @Nonnull final String columnName) {
public static Column ofNullable(@Nonnull final String tableName,
@Nonnull final String columnName) {
return new Column(tableName, columnName, false);
}

/**
* Constructs a nullable {@code Column} 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 columnName column name; should be non-blank.
* @return {@code Column}
* @since 0.14.3
*/
@Nonnull
public static Column ofNullable(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String columnName) {
return ofNullable(PgContext.enrichWith(tableName, pgContext), columnName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static Index of(@Nonnull final String tableName,
/**
* Constructs an {@code Index} object with given context.
*
* @param pgContext the schema context to enrich index name; must be non-null.
* @param pgContext the schema context to enrich table and index name; must be non-null.
* @param tableName table name; should be non-blank.
* @param indexName index name; should be non-blank.
* @return {@code Index}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static IndexWithBloat of(@Nonnull final String tableName,
/**
* Constructs a {@code IndexWithBloat} object with given context.
*
* @param pgContext the schema context to enrich index name; must be non-null.
* @param pgContext the schema context to enrich table and index name; must be non-null.
* @param tableName table name; should be non-blank.
* @param indexName index name; should be non-blank.
* @param indexSizeInBytes index size in bytes; should be positive or zero.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package io.github.mfvanek.pg.model.index;

import io.github.mfvanek.pg.model.column.Column;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.validation.Validators;

import java.util.List;
Expand Down Expand Up @@ -94,6 +95,26 @@ public static IndexWithColumns ofSingle(@Nonnull final String tableName,
return new IndexWithColumns(tableName, indexName, indexSizeInBytes, columns);
}

/**
* Constructs an {@code IndexWithColumns} object with one column and given context.
*
* @param pgContext the schema context to enrich table and index name; must be non-null.
* @param tableName table name; should be non-blank.
* @param indexName index name; should be non-blank.
* @param indexSizeInBytes index size in bytes; should be positive or zero.
* @param column column in index.
* @return {@code IndexWithColumns}
* @since 0.14.3
*/
@Nonnull
public static IndexWithColumns ofSingle(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String indexName,
final long indexSizeInBytes,
@Nonnull final Column column) {
return ofSingle(PgContext.enrichWith(tableName, pgContext), PgContext.enrichWith(indexName, pgContext), indexSizeInBytes, column);
}

/**
* Constructs an {@code IndexWithColumns} object with given columns.
*
Expand All @@ -110,4 +131,24 @@ public static IndexWithColumns ofColumns(@Nonnull final String tableName,
@Nonnull final List<Column> columns) {
return new IndexWithColumns(tableName, indexName, indexSizeInBytes, columns);
}

/**
* Constructs an {@code IndexWithColumns} object with given columns and context.
*
* @param pgContext the schema context to enrich table and index name; must be non-null.
* @param tableName table name; should be non-blank.
* @param indexName index name; should be non-blank.
* @param indexSizeInBytes index size in bytes; should be positive or zero.
* @param columns columns in index.
* @return {@code IndexWithColumns}
* @since 0.14.3
*/
@Nonnull
public static IndexWithColumns ofColumns(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String indexName,
final long indexSizeInBytes,
@Nonnull final List<Column> columns) {
return ofColumns(PgContext.enrichWith(tableName, pgContext), PgContext.enrichWith(indexName, pgContext), indexSizeInBytes, columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

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 @@ -79,9 +80,14 @@ void withInvalidValuesShouldThrowException() {
void testToString() {
assertThat(Column.ofNotNull("t1", "c1"))
.hasToString("Column{tableName='t1', columnName='c1', notNull=true}");

assertThat(Column.ofNullable("t2", "c2"))
.hasToString("Column{tableName='t2', columnName='c2', notNull=false}");

final PgContext ctx = PgContext.of("tst");
assertThat(Column.ofNotNull(ctx, "t1", "c1"))
.hasToString("Column{tableName='tst.t1', columnName='c1', notNull=true}");
assertThat(Column.ofNullable(ctx, "t2", "c2"))
.hasToString("Column{tableName='tst.t2', columnName='c2', notNull=false}");
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package io.github.mfvanek.pg.model.index;

import io.github.mfvanek.pg.model.column.Column;
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 @@ -75,9 +76,14 @@ void tableShouldBeTheSame() {

@Test
void testToString() {
final Column column = Column.ofNullable("t", "f");
assertThat(IndexWithColumns.ofSingle("t", "i", 22L, column))
.hasToString("IndexWithColumns{tableName='t', indexName='i', " + "indexSizeInBytes=22, columns=[Column{tableName='t', columnName='f', notNull=false}]}");
assertThat(IndexWithColumns.ofSingle("t", "i", 22L, Column.ofNullable("t", "f")))
.hasToString("IndexWithColumns{tableName='t', indexName='i', indexSizeInBytes=22, columns=[Column{tableName='t', columnName='f', notNull=false}]}");
final PgContext ctx = PgContext.of("tst");
final Column column = Column.ofNullable(ctx, "t", "f");
assertThat(IndexWithColumns.ofSingle(ctx, "t", "i", 22L, column))
.hasToString("IndexWithColumns{tableName='tst.t', indexName='tst.i', indexSizeInBytes=22, columns=[Column{tableName='tst.t', columnName='f', notNull=false}]}");
assertThat(IndexWithColumns.ofColumns(ctx, "t", "i", 22L, List.of(column)))
.hasToString("IndexWithColumns{tableName='tst.t', indexName='tst.i', indexSizeInBytes=22, columns=[Column{tableName='tst.t', columnName='f', notNull=false}]}");
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,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 @@ -72,7 +72,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 @@ -103,7 +103,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 @@ -116,11 +116,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 @@ -78,8 +78,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 @@ -44,8 +44,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)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "accounts"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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
Loading