Search in sources :

Example 6 with MigrationMessageEvent

use of org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent in project nzbhydra2 by theotherp.

the class SqliteMigration method migrateIndexerApiAccesses.

private void migrateIndexerApiAccesses(Map<Integer, IndexerEntity> oldIdToIndexersMap) throws SQLException {
    Statement statement = connection.createStatement();
    int countIndexerApiAccesses = getCount(statement, "INDEXERAPIACCESS");
    logger.info("Migrating {} indexer API accesses from old database", countIndexerApiAccesses);
    eventPublisher.publishEvent(new MigrationMessageEvent("Migrating " + countIndexerApiAccesses + " indexer API access entries"));
    ResultSet oldIndexerApiAccesses = statement.executeQuery("SELECT * FROM INDEXERAPIACCESS");
    int countMigrated = 1;
    IndexerApiAccessEntity entity;
    ProgressLogger progressLogger = new ProgressLogger(logger, 5, TimeUnit.SECONDS);
    progressLogger.expectedUpdates = countIndexerApiAccesses;
    progressLogger.start();
    while (oldIndexerApiAccesses.next()) {
        entity = new IndexerApiAccessEntity();
        entity.setIndexer(oldIdToIndexersMap.get(oldIndexerApiAccesses.getInt("indexer_id")));
        entity.setTime(timestampToInstant(oldIndexerApiAccesses.getString("time")));
        Object responseTime = oldIndexerApiAccesses.getObject("response_time");
        entity.setResponseTime(responseTime != null ? ((Integer) responseTime).longValue() : null);
        String error = oldIndexerApiAccesses.getString("error");
        entity.setError(error != null ? error.substring(0, Math.min(4000, error.length())) : null);
        entity.setAccessType(null);
        // Close enough
        entity.setResult(oldIndexerApiAccesses.getBoolean("response_successful") ? IndexerAccessResult.SUCCESSFUL : IndexerAccessResult.CONNECTION_ERROR);
        entity.setAccessType(IndexerApiAccessType.valueOf(oldIndexerApiAccesses.getString("type").toUpperCase()));
        entityManager.persist(entity);
        progressLogger.lightUpdate();
        if (countMigrated++ % 50 == 0) {
            entityManager.flush();
            entityManager.clear();
        }
    }
    progressLogger.stop();
    statement.close();
    entityManager.flush();
    entityManager.clear();
    eventPublisher.publishEvent(new MigrationMessageEvent("Successfully migrated indexer API accesses from old database"));
    logger.info("Successfully migrated indexer API accesses from old database");
}
Also used : MigrationMessageEvent(org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent) ProgressLogger(org.nzbhydra.logging.ProgressLogger)

Example 7 with MigrationMessageEvent

use of org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent in project nzbhydra2 by theotherp.

the class SqliteMigration method migrateIndexers.

private Map<Integer, IndexerEntity> migrateIndexers() throws SQLException {
    Statement statement = connection.createStatement();
    int countIndexers = getCount(statement, "INDEXER");
    logger.info("Migrating {} indexers from old database", countIndexers);
    eventPublisher.publishEvent(new MigrationMessageEvent("Migrating " + countIndexers + " indexer entries"));
    ResultSet indexersResultSet = statement.executeQuery("SELECT * FROM indexer");
    Map<Integer, IndexerEntity> oldIdToIndexersMap = new HashMap<>();
    while (indexersResultSet.next()) {
        IndexerEntity entity = new IndexerEntity();
        entity.setName(indexersResultSet.getString("name"));
        logger.debug("Migrating indexer {}", entity);
        entity = indexerRepository.save(entity);
        oldIdToIndexersMap.put(indexersResultSet.getInt("id"), entity);
    }
    logger.info("Successfully migrated indexers from old database");
    eventPublisher.publishEvent(new MigrationMessageEvent("Successfully migrated indexers from old database"));
    return oldIdToIndexersMap;
}
Also used : MigrationMessageEvent(org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent)

Aggregations

MigrationMessageEvent (org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent)7 ProgressLogger (org.nzbhydra.logging.ProgressLogger)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Instant (java.time.Instant)2 FileDownloadEntity (org.nzbhydra.downloading.FileDownloadEntity)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 Sets (com.google.common.collect.Sets)1 MalformedURLException (java.net.MalformedURLException)1 java.sql (java.sql)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ChronoUnit (java.time.temporal.ChronoUnit)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1