@Test public void shouldCopyColumnsUsingSingleJoinWithConstants() throws Exception { MigrationBuilder mb = new MigrationBuilder(HibernateHelper.configuration()); Iterable<String> sourceCols = Arrays.asList("id", "bar", "baz"); Iterable<String> destCols = Arrays.asList("foo", "bar2", "baz", "c1", "c2"); Map<String, String> predicate = new HashMap<String, String>(); predicate.put("predicate_column", "predicate_value"); mb.copyTableContents("src", "dest", sourceCols, destCols) .join("join_table", "src_id", "id", Arrays.asList("kol1", "kol2")) .whereSource(predicate) .withConstant("constant_column", "constant_value"); Connection conn = createStrictMock(Connection.class); String sql = "insert into dest(foo,bar2,baz,c1,c2,constant_column) " + "select s.id,s.bar,s.baz,j0.kol1,j0.kol2,'constant_value' from src s, join_table j0 " + "where j0.src_id = s.id and s.predicate_column = 'predicate_value'"; expect(conn.prepareStatement(sql)).andReturn(mockExecutablePreparedStatementForUpdate(1)); replay(conn); mb.apply(conn); verify(conn); }
@Test public void shouldCopyColumnsUsingMulitpleJoinsWithoutUsingSourceColumnsInResultSet() throws Exception { MigrationBuilder mb = new MigrationBuilder(HibernateHelper.configuration()); Iterable<String> sourceCols = Arrays.asList(); Iterable<String> destCols = Arrays.asList("c1", "c2", "c3", "c4"); Map<String, String> predicate = new HashMap<String, String>(); predicate.put("predicate_column", "predicate_value"); mb.copyTableContents("src", "dest", sourceCols, destCols) .join("join_table_0", "0_src_id", "id", Arrays.asList("kol1", "kol2")) .join("join_table_1", "1_src_id", "id", Arrays.asList("kol3", "kol4")) .whereSource(predicate); Connection conn = createStrictMock(Connection.class); String sql = "insert into dest(c1,c2,c3,c4) " + "select j0.kol1,j0.kol2,j1.kol3,j1.kol4 " + "from src s, join_table_0 j0,join_table_1 j1 " + "where j0.0_src_id = s.id " + "and j1.1_src_id = s.id " + "and s.predicate_column = 'predicate_value'"; expect(conn.prepareStatement(sql)).andReturn(mockExecutablePreparedStatementForUpdate(1)); replay(conn); mb.apply(conn); verify(conn); }
@Test(expected = IllegalArgumentException.class) public void shouldDetectWhenWrongNumberOfColumnsAreSpecified() throws Exception { MigrationBuilder mb = new MigrationBuilder(HibernateHelper.configuration()); // "foo", "bar", "baz" come from the source table // "kol1", "kol2" come from the join table // The destination table only specifies "foo", "bar", "baz", therefore it should blow up Iterable<String> sourceCols = Arrays.asList("foo", "bar", "baz"); Iterable<String> destCols = Arrays.asList("bar2", "baz", "c1", "c2"); Map<String, String> predicate = new HashMap<String, String>(); predicate.put("predicate_column", "predicate_value"); mb.copyTableContents("src", "dest", sourceCols, destCols) .join("join_table", "src_id", "id", Arrays.asList("kol1", "kol2")) .whereSource(predicate); Connection conn = createStrictMock(Connection.class); String sql = "insert into dest(foo,bar2,baz,c1,c2) " + "select s.id,s.bar,s.baz,j0.kol1,j0.kol2 from src s, join_table j0 " + "where j0.src_id = s.id and s.predicate_column = 'predicate_value'"; expect(conn.prepareStatement(sql)).andReturn(mockExecutablePreparedStatementForUpdate(1)); replay(conn); mb.apply(conn); verify(conn); }
@Test public void shouldCopyColumns() throws Exception { MigrationBuilder mb = new MigrationBuilder(HibernateHelper.configuration()); Iterable<String> sourceCols = Arrays.asList("foo", "bar", "baz"); Iterable<String> destCols = Arrays.asList("foo", "bar2", "baz"); mb.copyTableContents("src", "dest", sourceCols, destCols); Connection conn = createStrictMock(Connection.class); expect(conn.prepareStatement("insert into dest(foo,bar2,baz) select foo,bar,baz from src")) .andReturn(mockExecutablePreparedStatementForUpdate(1)); replay(conn); mb.apply(conn); verify(conn); }