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");
}
}
}
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");
}
}
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);
}
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));
}
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);
}
Aggregations