|
| 1 | +package ch.ergon.adam.integrationtest.testcases; |
| 2 | + |
| 3 | +import ch.ergon.adam.core.db.schema.DbEnum; |
| 4 | +import ch.ergon.adam.core.db.schema.Field; |
| 5 | +import ch.ergon.adam.core.db.schema.Schema; |
| 6 | +import ch.ergon.adam.core.db.schema.Table; |
| 7 | +import ch.ergon.adam.integrationtest.AbstractDbTestBase; |
| 8 | +import ch.ergon.adam.integrationtest.DummySink; |
| 9 | +import ch.ergon.adam.integrationtest.TestDbUrlProvider; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.sql.ResultSet; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static ch.ergon.adam.core.db.schema.DataType.ENUM; |
| 17 | +import static org.hamcrest.CoreMatchers.is; |
| 18 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +public abstract class AddSqlForNewTest extends AbstractDbTestBase { |
| 22 | + |
| 23 | + public AddSqlForNewTest(TestDbUrlProvider testDbUrlProvider) { |
| 24 | + super(testDbUrlProvider); |
| 25 | + } |
| 26 | + |
| 27 | + private static final String CREATE_ENUM_SQL = |
| 28 | + "create type custom_enum as enum ('val1', 'val2')"; |
| 29 | + |
| 30 | + private static final String CREATE_TABLE_SQL = |
| 31 | + "create table test_table (" + |
| 32 | + "col1 int" + |
| 33 | + ")"; |
| 34 | + |
| 35 | + private static final String INSERT_DATA_SQL = |
| 36 | + "insert into test_table values (1)"; |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testAddSqlForNewMigration() throws Exception { |
| 40 | + // Setup db |
| 41 | + getTargetDbConnection().createStatement().execute(CREATE_ENUM_SQL); |
| 42 | + getTargetDbConnection().createStatement().execute(CREATE_TABLE_SQL); |
| 43 | + getTargetDbConnection().createStatement().execute(INSERT_DATA_SQL); |
| 44 | + DummySink dummySink = targetToDummy(); |
| 45 | + Schema schema = dummySink.getTargetSchema(); |
| 46 | + |
| 47 | + // Add field |
| 48 | + Table table = schema.getTable("test_table"); |
| 49 | + List<Field> fields = new ArrayList<>(table.getFields()); |
| 50 | + Field newField = new Field("custom_type"); |
| 51 | + newField.setDataType(ENUM); |
| 52 | + newField.setDbEnum(new DbEnum("custom_enum")); |
| 53 | + newField.setArray(true); |
| 54 | + newField.setDefaultValue("'{val2}'"); |
| 55 | + newField.setSqlForNew("'{val1}'"); |
| 56 | + fields.add(newField); |
| 57 | + table.setFields(fields); |
| 58 | + migrateTargetWithSchema(schema); |
| 59 | + |
| 60 | + // Verify |
| 61 | + ResultSet result = getTargetDbConnection().createStatement().executeQuery("select * from test_table"); |
| 62 | + assertTrue(result.next()); |
| 63 | + assertThat(result.getString(2), is("{val1}")); |
| 64 | + } |
| 65 | +} |
0 commit comments