Skip to content

Commit

Permalink
Merge pull request #8 from ergon/bugfix/enum-array-load-default-excep…
Browse files Browse the repository at this point in the history
…tion

fix loading of default values for enum arrays in postgres
  • Loading branch information
jmatj committed Jul 17, 2024
2 parents b1f9060 + 71dd60f commit 9c4a4bb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.ergon.adam.integrationtest.postgresql;

import ch.ergon.adam.integrationtest.testcases.AddSqlForNewTest;

public class PostgreSqlAddSqlForNewTest extends AddSqlForNewTest {
public PostgreSqlAddSqlForNewTest() {
super(new PostgreSqlTestDbUrlProvider());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ch.ergon.adam.integrationtest.testcases;

import ch.ergon.adam.core.db.schema.DbEnum;
import ch.ergon.adam.core.db.schema.Field;
import ch.ergon.adam.core.db.schema.Schema;
import ch.ergon.adam.core.db.schema.Table;
import ch.ergon.adam.integrationtest.AbstractDbTestBase;
import ch.ergon.adam.integrationtest.DummySink;
import ch.ergon.adam.integrationtest.TestDbUrlProvider;
import org.junit.jupiter.api.Test;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import static ch.ergon.adam.core.db.schema.DataType.ENUM;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

public abstract class AddSqlForNewTest extends AbstractDbTestBase {

public AddSqlForNewTest(TestDbUrlProvider testDbUrlProvider) {
super(testDbUrlProvider);
}

private static final String CREATE_ENUM_SQL =
"create type custom_enum as enum ('val1', 'val2')";

private static final String CREATE_TABLE_SQL =
"create table test_table (" +
"col1 int" +
")";

private static final String INSERT_DATA_SQL =
"insert into test_table values (1)";

@Test
public void testAddSqlForNewMigration() throws Exception {
// Setup db
getTargetDbConnection().createStatement().execute(CREATE_ENUM_SQL);
getTargetDbConnection().createStatement().execute(CREATE_TABLE_SQL);
getTargetDbConnection().createStatement().execute(INSERT_DATA_SQL);
DummySink dummySink = targetToDummy();
Schema schema = dummySink.getTargetSchema();

// Add field
Table table = schema.getTable("test_table");
List<Field> fields = new ArrayList<>(table.getFields());
Field newField = new Field("custom_type");
newField.setDataType(ENUM);
newField.setDbEnum(new DbEnum("custom_enum"));
newField.setArray(true);
newField.setDefaultValue("'{val2}'");
newField.setSqlForNew("'{val1}'");
fields.add(newField);
table.setFields(fields);
migrateTargetWithSchema(schema);

// Verify
ResultSet result = getTargetDbConnection().createStatement().executeQuery("select * from test_table");
assertTrue(result.next());
assertThat(result.getString(2), is("{val1}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import ch.ergon.adam.core.db.schema.Field;
import ch.ergon.adam.core.db.schema.Sequence;
import ch.ergon.adam.core.db.schema.Table;
import org.jooq.DSLContext;
import org.jooq.DataType;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.jooq.impl.DefaultDataType;

Expand Down Expand Up @@ -76,8 +73,11 @@ protected DataType<?> mapType(Field field) {
protected DataType<?> mapFieldToJooqType(Field field) {
switch (field.getDataType()) {
case ENUM:
DataType<?> jooqType = new DefaultDataType<>(null, VARCHAR, field.getDbEnum().getName(), field.getDbEnum().getName());
return jooqType;
if (field.isArray()) {
return new DefaultDataType<>(null, String[].class, field.getDbEnum().getName() + "[]", field.getDbEnum().getName() + "[]");
} else {
return new DefaultDataType<>(null, VARCHAR, field.getDbEnum().getName(), field.getDbEnum().getName());
}
default:
return super.mapFieldToJooqType(field);
}
Expand Down

0 comments on commit 9c4a4bb

Please sign in to comment.