### Question:
ReferencedTransactionMigrator { public void migrate() { TransactionalDataSource dataSource = databaseManager.getDataSource(); boolean isInTransaction = false; try { if (dataSource.isInTransaction()) { isInTransaction = true; } else { dataSource.begin(); } Connection con = dataSource.getConnection(); try ( PreparedStatement selectPstmt = con.prepareStatement(( "SELECT rtx.transaction_id as id, tx.height FROM referenced_transaction rtx left join transaction tx on rtx.transaction_id = tx.id where rtx.height = -1")); PreparedStatement updatePstmt = con.prepareStatement("UPDATE referenced_transaction SET height = ? WHERE transaction_id = ?") ) { int counter = 0; try (ResultSet rs = selectPstmt.executeQuery()) { while (rs.next()) { updatePstmt.setInt(1, rs.getInt("height")); updatePstmt.setLong(2, rs.getLong("id")); updatePstmt.executeUpdate(); if (++counter % 1000 == 0) { LOG.info("Migrated {} referenced transactions"); } } } if (counter == 0) { LOG.info("No referenced transactions for migration"); } else { LOG.info("Total migrated: {} referenced transactions", counter); } } dataSource.commit(!isInTransaction); } catch (SQLException e) { dataSource.rollback(!isInTransaction); throw new RuntimeException(e.toString(), e); } } @Inject  ReferencedTransactionMigrator(DatabaseManager databaseManager); void migrate();  }### Answer:
@Test public void testMigrate() throws SQLException { try (Connection connection = dbExtension.getDatabaseManager().getDataSource().getConnection(); Statement stmt = connection.createStatement()) { stmt.executeUpdate("update referenced_transaction set height = -1 where db_id >= 40"); } List<ReferencedTransaction> notMigrated = CollectionUtil.toList(referencedTransactionDao.getManyBy(new DbClause.LongClause("db_id", DbClause.Op.GTE, 40), 0, Integer.MAX_VALUE)); notMigrated.forEach(rtx -> assertEquals(-1, rtx.getHeight().intValue())); migrator.migrate(); List<ReferencedTransaction> referencedTransactions = CollectionUtil.toList(referencedTransactionDao.getAll(0, Integer.MAX_VALUE)); List<ReferencedTransaction> expected = td.REFERENCED_TRANSACTIONS.stream().sorted(Comparator.comparing(ReferencedTransaction::getHeight).thenComparing(ReferencedTransaction::getDbId).reversed()).collect(Collectors.toList()); assertEquals(referencedTransactions, expected); }
@Test void testMigrateNothing() { migrator.migrate(); List<ReferencedTransaction> referencedTransactions = CollectionUtil.toList(referencedTransactionDao.getAll(0, Integer.MAX_VALUE)); List<ReferencedTransaction> expected = td.REFERENCED_TRANSACTIONS.stream().sorted(Comparator.comparing(ReferencedTransaction::getHeight).thenComparing(ReferencedTransaction::getDbId).reversed()).collect(Collectors.toList()); assertEquals(referencedTransactions, expected); }