Search in sources :

Example 1 with JobType

use of org.hisp.dhis.scheduling.JobType in project dhis2-core by dhis2.

the class V2_33_1__Job_configuration_job_type_column_to_varchar method migrate.

@Override
public void migrate(final Context context) throws Exception {
    // 1. Check whether migration is needed at all. Maybe it was already
    // applied. -> Achieves that script can be
    // run multiple times without worries
    boolean continueWithMigration = false;
    String sql = "SELECT data_type FROM information_schema.columns " + "WHERE table_name = 'jobconfiguration' AND column_name = 'jobtype';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next() && rs.getString("data_type").equals("bytea")) {
            continueWithMigration = true;
        }
    }
    if (continueWithMigration) {
        // jobconfiguration table
        try (Statement stmt = context.getConnection().createStatement()) {
            stmt.executeUpdate("ALTER TABLE jobconfiguration ADD COLUMN IF NOT EXISTS jobtypevarchar VARCHAR(120)");
        }
        // 3. Move existing jobtype from bytearray column into varchar
        // column
        Map<Integer, byte[]> jobTypeByteMap = new HashMap<>();
        sql = "SELECT jobconfigurationid, jobtype FROM jobconfiguration WHERE jobtype IS NOT NULL";
        try (Statement stmt = context.getConnection().createStatement();
            ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                jobTypeByteMap.put(rs.getInt("jobconfigurationid"), rs.getBytes("jobtype"));
            }
        }
        jobTypeByteMap.forEach((id, jobTypeByteArray) -> {
            JobType jobType = (JobType) SerializationUtils.deserialize(jobTypeByteArray);
            if (jobType == null) {
                log.error("Flyway java migration error: Parsing JobType byte array failed.");
                throw new FlywayException("Parsing JobType byte array failed.");
            }
            try (PreparedStatement ps = context.getConnection().prepareStatement("UPDATE jobconfiguration SET jobtypevarchar = ? WHERE jobconfigurationid = ?")) {
                ps.setObject(1, jobType.name());
                ps.setInt(2, id);
                ps.execute();
            } catch (SQLException e) {
                log.error("Flyway java migration error:", e);
                throw new FlywayException(e);
            }
        });
        // table
        try (Statement stmt = context.getConnection().createStatement()) {
            stmt.executeUpdate("ALTER TABLE jobconfiguration DROP COLUMN jobtype");
        }
        // deleted column
        try (Statement stmt = context.getConnection().createStatement()) {
            stmt.executeUpdate("ALTER TABLE jobconfiguration RENAME COLUMN jobtypevarchar TO jobtype");
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) JobType(org.hisp.dhis.scheduling.JobType) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with JobType

use of org.hisp.dhis.scheduling.JobType in project dhis2-core by dhis2.

the class V2_31_2__Job_configuration_param_to_jsonb method migrate.

public void migrate(Context context) throws Exception {
    ObjectMapper MAPPER = new ObjectMapper();
    MAPPER.enableDefaultTyping();
    MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    writer = MAPPER.writerFor(JobParameters.class);
    // 1. Create new jsonb column for jobparameters in jobconfiguration
    try (Statement stmt = context.getConnection().createStatement()) {
        stmt.executeUpdate("ALTER TABLE jobconfiguration ADD COLUMN IF NOT EXISTS jsonbjobparameters jsonb");
    }
    // 2. Move existing jobparameters from bytearray column into jsonb
    // column
    Map<Integer, byte[]> jobParamByteMap = new HashMap<>();
    Map<Integer, byte[]> jobTypeByteMap = new HashMap<>();
    try (Statement stmt = context.getConnection().createStatement()) {
        try (ResultSet rows = stmt.executeQuery("select jobconfigurationid,jobparameters,jobtype from jobconfiguration where jobparameters is not null")) {
            while (rows.next()) {
                jobParamByteMap.put(rows.getInt(1), rows.getBytes(2));
                jobTypeByteMap.put(rows.getInt(1), rows.getBytes(3));
            }
        }
    }
    jobParamByteMap.forEach((id, jobParamByteArray) -> {
        Object jParaB = null;
        JobType jobType = null;
        try {
            jParaB = toObject(jobParamByteArray);
            jobType = (JobType) toObject(jobTypeByteMap.get(id));
        } catch (IOException | ClassNotFoundException e) {
            log.error("Flyway java migration error:", e);
            throw new FlywayException(e);
        }
        try (PreparedStatement ps = context.getConnection().prepareStatement("Update jobconfiguration set jsonbjobparameters =? where  jobconfigurationid = ?")) {
            PGobject pg = new PGobject();
            pg.setType("jsonb");
            pg.setValue(convertObjectToJson(jobType.getJobParameters().cast(jParaB)));
            ps.setObject(1, pg);
            ps.setInt(2, id);
            ps.execute();
        } catch (SQLException e) {
            log.error("Flyway java migration error:", e);
            throw new FlywayException(e);
        }
    });
    // 3. Delete old byte array column for jobparameters in jobconfiguration
    try (Statement stmt = context.getConnection().createStatement()) {
        stmt.executeUpdate("ALTER TABLE jobconfiguration DROP COLUMN IF EXISTS jobparameters");
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) PGobject(org.postgresql.util.PGobject) JobType(org.hisp.dhis.scheduling.JobType) ResultSet(java.sql.ResultSet) JobParameters(org.hisp.dhis.scheduling.JobParameters) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with JobType

use of org.hisp.dhis.scheduling.JobType in project dhis2-core by dhis2.

the class NotificationMap method add.

public void add(JobConfiguration configuration, Notification notification) {
    String jobId = configuration.getUid();
    if (jobId == null) {
        return;
    }
    JobType jobType = configuration.getJobType();
    Deque<String> notifications = notificationsJobIdOrder.get(jobType);
    if (notifications.size() > MAX_POOL_TYPE_SIZE) {
        notificationsWithType.get(jobType).remove(notifications.removeLast());
    }
    notifications.addFirst(jobId);
    notificationsWithType.get(jobType).computeIfAbsent(jobId, key -> new ConcurrentLinkedDeque<>()).addFirst(notification);
}
Also used : EnumMap(java.util.EnumMap) Map(java.util.Map) JobType(org.hisp.dhis.scheduling.JobType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) Deque(java.util.Deque) LinkedList(java.util.LinkedList) Arrays.stream(java.util.Arrays.stream) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) JobType(org.hisp.dhis.scheduling.JobType) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque)

Example 4 with JobType

use of org.hisp.dhis.scheduling.JobType in project dhis2-core by dhis2.

the class NotificationMap method clear.

public void clear(JobConfiguration configuration) {
    JobType jobType = configuration.getJobType();
    String jobId = configuration.getUid();
    notificationsWithType.get(jobType).remove(jobId);
    notificationsJobIdOrder.get(jobType).removeIf(e -> e.equals(jobId));
    summariesWithType.get(jobType).remove(jobId);
    summariesJobIdOrder.get(jobType).removeIf(e -> e.equals(jobId));
}
Also used : JobType(org.hisp.dhis.scheduling.JobType)

Example 5 with JobType

use of org.hisp.dhis.scheduling.JobType in project dhis2-core by dhis2.

the class NotificationMap method addSummary.

public void addSummary(JobConfiguration configuration, Object summary) {
    String jobId = configuration.getUid();
    if (jobId == null) {
        return;
    }
    JobType jobType = configuration.getJobType();
    Deque<String> summaries = summariesJobIdOrder.get(jobType);
    if (summaries.size() >= MAX_POOL_TYPE_SIZE) {
        summariesWithType.get(jobType).remove(summaries.removeLast());
    }
    summaries.addFirst(jobId);
    summariesWithType.get(jobType).put(jobId, summary);
}
Also used : JobType(org.hisp.dhis.scheduling.JobType)

Aggregations

JobType (org.hisp.dhis.scheduling.JobType)5 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 FlywayException (org.flywaydb.core.api.FlywayException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Arrays.stream (java.util.Arrays.stream)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Deque (java.util.Deque)1 EnumMap (java.util.EnumMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentLinkedDeque (java.util.concurrent.ConcurrentLinkedDeque)1 JobConfiguration (org.hisp.dhis.scheduling.JobConfiguration)1 JobParameters (org.hisp.dhis.scheduling.JobParameters)1 PGobject (org.postgresql.util.PGobject)1