Skip to content

Commit

Permalink
Breaking change! Changed DbMigrationGenerator: now generate() method …
Browse files Browse the repository at this point in the history
…returns a list of strings. (#291)
  • Loading branch information
mfvanek committed Oct 1, 2023
1 parent 717c5c8 commit 06704fc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
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;");
}
}

0 comments on commit 06704fc

Please sign in to comment.