diff --git a/integration-test/src/test/java/ch/ergon/adam/integrationtest/postgresql/PostgreSqlAddSqlForNewTest.java b/integration-test/src/test/java/ch/ergon/adam/integrationtest/postgresql/PostgreSqlAddSqlForNewTest.java new file mode 100644 index 0000000..a669982 --- /dev/null +++ b/integration-test/src/test/java/ch/ergon/adam/integrationtest/postgresql/PostgreSqlAddSqlForNewTest.java @@ -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()); + } +} diff --git a/integration-test/src/test/java/ch/ergon/adam/integrationtest/testcases/AddSqlForNewTest.java b/integration-test/src/test/java/ch/ergon/adam/integrationtest/testcases/AddSqlForNewTest.java new file mode 100644 index 0000000..087b45a --- /dev/null +++ b/integration-test/src/test/java/ch/ergon/adam/integrationtest/testcases/AddSqlForNewTest.java @@ -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 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}")); + } +} diff --git a/postgresql/src/main/java/ch/ergon/adam/postgresql/PostgreSqlSink.java b/postgresql/src/main/java/ch/ergon/adam/postgresql/PostgreSqlSink.java index 3afe92c..088db6e 100644 --- a/postgresql/src/main/java/ch/ergon/adam/postgresql/PostgreSqlSink.java +++ b/postgresql/src/main/java/ch/ergon/adam/postgresql/PostgreSqlSink.java @@ -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; @@ -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); }