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

Breaking change! Now DbMigrationGenerator::generate() method returns a list of strings. #291

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ description = "pg-index-health build"

allprojects {
group = "io.github.mfvanek"
version = "0.9.6"
version = "0.10.0"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import io.github.mfvanek.pg.model.table.TableNameAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
Expand All @@ -33,19 +34,16 @@ abstract class AbstractDbMigrationGenerator<T extends TableNameAware> implements
*/
@Nonnull
@Override
public final String generate(@Nonnull final List<T> rows) {
public final List<String> generate(@Nonnull final List<T> rows) {
Objects.requireNonNull(rows, "rows cannot be null");

final StringBuilder queryBuilder = new StringBuilder();
for (int i = 0; i < rows.size(); ++i) {
if (i != 0) {
queryBuilder.append(System.lineSeparator())
.append(System.lineSeparator());
}
generate(queryBuilder, rows.get(i));
final List<String> migrations = new ArrayList<>(rows.size());
for (final T row : rows) {
migrations.add(generate(row));
}
return queryBuilder.toString();
return migrations;
}

protected abstract void generate(@Nonnull StringBuilder queryBuilder, @Nonnull T row);
@Nonnull
protected abstract String generate(@Nonnull T row);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public ColumnWithSerialTypeMigrationGenerator(@Nonnull final GeneratingOptions o
}

@Override
protected void generate(@Nonnull final StringBuilder queryBuilder, @Nonnull final ColumnWithSerialType column) {
queryBuilder.append(dropDefaultValueGenerator.generate(column))
.append(System.lineSeparator())
.append(dropSequenceGenerator.generate(column));
@Nonnull
protected String generate(@Nonnull final ColumnWithSerialType column) {
return dropDefaultValueGenerator.generate(column) +
System.lineSeparator() +
dropSequenceGenerator.generate(column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
public interface DbMigrationGenerator<T extends TableNameAware> {

/**
* Generates sql migration based on the specified rows.
* Generates sql migrations based on the specified rows.
*
* @param rows a set of data on the basis of which the sql migration will be generated
* @return generated sql migration
* @return a list of generated sql migrations
* @since 0.10.0
*/
@Nonnull
String generate(@Nonnull List<T> rows);
List<String> generate(@Nonnull List<T> rows);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public ForeignKeyMigrationGenerator(@Nonnull final GeneratingOptions options) {
}

@Override
protected void generate(@Nonnull final StringBuilder queryBuilder, @Nonnull final ForeignKey foreignKey) {
queryBuilder.append(generator.generate(foreignKey));
@Nonnull
protected String generate(@Nonnull final ForeignKey foreignKey) {
return generator.generate(foreignKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void forSingleColumn() {
final ColumnWithSerialTypeMigrationGenerator generator = new ColumnWithSerialTypeMigrationGenerator(GeneratingOptions.builder().build());

assertThat(generator.generate(List.of(column())))
.isEqualTo("alter table if exists s1.t1" + System.lineSeparator() +
.hasSize(1)
.containsExactly("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;");
}
Expand All @@ -46,13 +47,13 @@ void forSeveralColumns() {
final ColumnWithSerialTypeMigrationGenerator generator = new ColumnWithSerialTypeMigrationGenerator(GeneratingOptions.builder().build());

assertThat(generator.generate(List.of(column(), secondColumn)))
.isEqualTo("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;" + System.lineSeparator() +
System.lineSeparator() +
.hasSize(2)
.containsExactly("alter table if exists s1.t1" + System.lineSeparator() +
" alter column col1 drop default;" + System.lineSeparator() +
"drop sequence if exists s1.seq1;",
"alter table if exists s2.t2" + System.lineSeparator() +
" alter column col2 drop default;" + System.lineSeparator() +
"drop sequence if exists s2.seq2;");
" alter column col2 drop default;" + System.lineSeparator() +
"drop sequence if exists s2.seq2;");
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class ForeignKeyMigrationGeneratorTest {
@Test
void generateForSingleForeignKey() {
final DbMigrationGenerator<ForeignKey> generator = new ForeignKeyMigrationGenerator(GeneratingOptions.builder().build());
final String result = generator.generate(List.of(nullableColumnWithSchema()));
final var result = generator.generate(List.of(nullableColumnWithSchema()));
assertThat(result)
.isNotBlank()
.isEqualTo("/* table_with_very_very_very_long_name_column_with_very_very_very_long_name_without_nulls_idx */" + System.lineSeparator() +
.hasSize(1)
.containsExactly("/* table_with_very_very_very_long_name_column_with_very_very_very_long_name_without_nulls_idx */" + System.lineSeparator() +
"create index concurrently if not exists table_with_very_very_very_long_name_3202677_without_nulls_idx" + System.lineSeparator() +
" on schema_name_that_should_be_omitted.table_with_very_very_very_long_name (column_with_very_very_very_long_name) " +
"where column_with_very_very_very_long_name is not null;");
Expand All @@ -36,19 +36,17 @@ void generateForSingleForeignKey() {
@Test
void generateForSeveralForeignKeys() {
final DbMigrationGenerator<ForeignKey> generator = new ForeignKeyMigrationGenerator(GeneratingOptions.builder().build());
final String result = generator.generate(
List.of(severalColumnsWithNulls(), severalColumnsWithNulls(), nullableColumnWithSchema()));
final var result = generator.generate(List.of(severalColumnsWithNulls(), severalColumnsWithNulls(), nullableColumnWithSchema()));
assertThat(result)
.isNotBlank()
.isEqualTo("create index concurrently if not exists custom_table_custom_column_1_custom_column_22_without_nulls_idx" + System.lineSeparator() +
" on custom_table (custom_column_1, custom_column_22) where custom_column_22 is not null;" + System.lineSeparator() +
System.lineSeparator() +
.hasSize(3)
.containsExactly(
"create index concurrently if not exists custom_table_custom_column_1_custom_column_22_without_nulls_idx" + System.lineSeparator() +
" on custom_table (custom_column_1, custom_column_22) where custom_column_22 is not null;" + System.lineSeparator() +
System.lineSeparator() +
" on custom_table (custom_column_1, custom_column_22) where custom_column_22 is not null;",
"create index concurrently if not exists custom_table_custom_column_1_custom_column_22_without_nulls_idx" + System.lineSeparator() +
" on custom_table (custom_column_1, custom_column_22) where custom_column_22 is not null;",
"/* table_with_very_very_very_long_name_column_with_very_very_very_long_name_without_nulls_idx */" + System.lineSeparator() +
"create index concurrently if not exists table_with_very_very_very_long_name_3202677_without_nulls_idx" + System.lineSeparator() +
" on schema_name_that_should_be_omitted.table_with_very_very_very_long_name (column_with_very_very_very_long_name) " +
"where column_with_very_very_very_long_name is not null;");
"create index concurrently if not exists table_with_very_very_very_long_name_3202677_without_nulls_idx" + System.lineSeparator() +
" on schema_name_that_should_be_omitted.table_with_very_very_very_long_name (column_with_very_very_very_long_name) " +
"where column_with_very_very_very_long_name is not null;");
}
}