V1_11_6__Missing_Entities extends BaseJavaMigration { @Override public void migrate(Context context) throws Exception { File accountInfoFile = getAccountInfoPath().toFile(); if (!accountInfoFile.exists() || !accountInfoFile.canRead()) { log.warn("Skipping entity import due to missing file {}", accountInfoFile.getAbsoluteFile()); return; } log.info("Importing account file {}", accountInfoFile.getAbsoluteFile()); Stopwatch stopwatch = Stopwatch.createStarted(); long count = 0L; try (BufferedReader reader = new BufferedReader(new FileReader(accountInfoFile))) { String line; while ((line = reader.readLine()) != null) { try { if (StringUtils.isNotBlank(line)) { updateAccount(line); } ++count; } catch (Exception e) { log.error("Unable to load AccountInfo: {}", line, e); } } } log.info("Successfully loaded {} accounts in {}", count, stopwatch); } V1_11_6__Missing_Entities(MirrorProperties mirrorProperties, @Lazy EntityRepository entityRepository,
                                     @Lazy JdbcOperations jdbcOperations); @Override void migrate(Context context);  }
@Test void create() throws Exception { AccountInfo.Builder accountInfo = accountInfo(); write(accountInfo.build()); migration.migrate(new FlywayContext()); Assertions.assertThat(entityRepository.findById(EntityId.of(accountId).getId())) .get() .returns(accountInfo.getAutoRenewPeriod().getSeconds(), from(Entities::getAutoRenewPeriod)) .returns(Utility.protobufKeyToHexIfEd25519OrNull(accountInfo.getKey() .toByteArray()), from(Entities::getEd25519PublicKeyHex)) .returns(Utility.timeStampInNanos(accountInfo.getExpirationTime()), from(Entities::getExpiryTimeNs)) .returns(accountInfo.getKey().toByteArray(), from(Entities::getKey)); }
@Test void nullValues() throws Exception { write(AccountInfo.newBuilder().setAccountID(accountId).build()); migration.migrate(new FlywayContext()); Assertions.assertThat(entityRepository.findById(EntityId.of(accountId).getId())) .get() .returns(null, from(Entities::getAutoRenewPeriod)) .returns(null, from(Entities::getEd25519PublicKeyHex)) .returns(null, from(Entities::getExpiryTimeNs)) .returns(null, from(Entities::getKey)); }
@Test void noUpdateIfCreateTransaction() throws Exception { AccountInfo.Builder accountInfo = accountInfo(); write(accountInfo.build()); Entities entity = entity(accountId.getAccountNum()); Entities node = entity(2); Entities payer = entity(3); long transactionTypeId = jdbcTemplate .queryForObject("select id from t_transaction_types where name = ?", new Object[] { "CRYPTOCREATEACCOUNT"}, Integer.class); jdbcTemplate.update("insert into t_transactions (consensus_ns, entity_id, node_account_id, payer_account_id, " + "fk_trans_type_id, valid_start_ns) " + "values(?,?,?,?,?,?,?)", 1L, entity.getId(), node.getId(), payer.getId(), payer.getEntityNum(), transactionTypeId, 1L); migration.migrate(new FlywayContext()); Assertions.assertThat(entityRepository.findAll()) .extracting(Entities::getEntityNum) .containsExactlyInAnyOrder(entity.getEntityNum(), node.getEntityNum(), payer.getEntityNum()); Assertions.assertThat(entityRepository.findById(entity.getId())) .get() .extracting(Entities::getKey) .withFailMessage("Expecting entity <%d> to not be updated", entity.getEntityNum()) .isNull(); }
@Test void updateIfNoCreateTransaction() throws Exception { Entities entity = entity(accountId.getAccountNum()); AccountInfo.Builder accountInfo = accountInfo(); write(accountInfo.build()); migration.migrate(new FlywayContext()); Assertions.assertThat(entityRepository.findById(entity.getId())) .get() .returns(accountInfo.getAutoRenewPeriod().getSeconds(), from(Entities::getAutoRenewPeriod)) .returns(Utility.protobufKeyToHexIfEd25519OrNull(accountInfo.getKey() .toByteArray()), from(Entities::getEd25519PublicKeyHex)) .returns(Utility.timeStampInNanos(accountInfo.getExpirationTime()), from(Entities::getExpiryTimeNs)) .returns(accountInfo.getKey().toByteArray(), from(Entities::getKey)); }
@Test void deleted() throws Exception { AccountInfo.Builder accountInfo = accountInfo(); accountInfo.setDeleted(true); write(accountInfo.build()); migration.migrate(new FlywayContext()); Assertions.assertThat(entityRepository.findById(EntityId.of(accountId).getId())) .get() .extracting(Entities::isDeleted) .isEqualTo(true); }
@Test void missingFile() throws Exception { migration.migrate(new FlywayContext()); assertThat(entityRepository.count()).isEqualTo(0L); }
@Test void longOverflow() throws Exception { AccountInfo.Builder accountInfo = accountInfo() .setExpirationTime(Timestamp.newBuilder().setSeconds(31556889864403199L).build()); write(accountInfo.build()); migration.migrate(new FlywayContext()); Assertions.assertThat(entityRepository.findAll()) .hasSize(2) .extracting(Entities::getExpiryTimeNs) .containsOnlyNulls(); }