use of org.hisp.dhis.scheduling.JobStatus in project dhis2-core by dhis2.
the class V2_34_5__Convert_job_configuration_binary_columns_into_varchar_data_type method migrateJobStatusColumn.
private void migrateJobStatusColumn(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;
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(CHECK_JOB_STATUS_DATA_TYPE_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 jobstatusvarchar VARCHAR(120)");
}
// 3. Move existing jobstatus from bytearray column into varchar
// column
Map<Integer, byte[]> jobStatusByteMap = new HashMap<>();
String sql = "SELECT jobconfigurationid, jobstatus FROM jobconfiguration WHERE jobstatus IS NOT NULL";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
jobStatusByteMap.put(rs.getInt("jobconfigurationid"), rs.getBytes("jobstatus"));
}
}
jobStatusByteMap.forEach((id, jobStatusByteArray) -> {
JobStatus jobStatus = (JobStatus) SerializationUtils.deserialize(jobStatusByteArray);
if (jobStatus == null) {
log.error("Flyway java migration error: Parsing JobStatus byte array failed.");
throw new FlywayException("Parsing JobStatus byte array failed.");
}
try (PreparedStatement ps = context.getConnection().prepareStatement("UPDATE jobconfiguration SET jobstatusvarchar = ? WHERE jobconfigurationid = ?")) {
ps.setObject(1, jobStatus.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 jobstatus");
}
// deleted column
try (Statement stmt = context.getConnection().createStatement()) {
stmt.executeUpdate("ALTER TABLE jobconfiguration RENAME COLUMN jobstatusvarchar TO jobstatus");
}
}
}
use of org.hisp.dhis.scheduling.JobStatus in project dhis2-core by dhis2.
the class V2_34_5__Convert_job_configuration_binary_columns_into_varchar_data_type method migrateLastExecutedStatusColumn.
private void migrateLastExecutedStatusColumn(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;
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(CHECK_LAST_EXECUTED_STATUS_DATA_TYPE_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 lastexecutedstatusvarchar VARCHAR(120)");
}
// 3. Move existing lastexecutedstatus from bytearray column into
// varchar column
Map<Integer, byte[]> lastExecutedStatusByteMap = new HashMap<>();
String sql = "SELECT jobconfigurationid, lastexecutedstatus FROM jobconfiguration " + "WHERE lastexecutedstatus IS NOT NULL";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
lastExecutedStatusByteMap.put(rs.getInt("jobconfigurationid"), rs.getBytes("lastexecutedstatus"));
}
}
lastExecutedStatusByteMap.forEach((id, lastExecutedStatusByteArray) -> {
JobStatus lastExecutedStatus = (JobStatus) SerializationUtils.deserialize(lastExecutedStatusByteArray);
if (lastExecutedStatus == null) {
log.error("Flyway java migration error: Parsing LastExecutedStatus byte array failed.");
throw new FlywayException("Parsing LastExecutedStatus byte array failed.");
}
try (PreparedStatement ps = context.getConnection().prepareStatement("UPDATE jobconfiguration SET lastexecutedstatusvarchar = ? WHERE jobconfigurationid = ?")) {
ps.setObject(1, lastExecutedStatus.name());
ps.setInt(2, id);
ps.execute();
} catch (SQLException e) {
log.error("Flyway java migration error:", e);
throw new FlywayException(e);
}
});
// jobconfiguration table
try (Statement stmt = context.getConnection().createStatement()) {
stmt.executeUpdate("ALTER TABLE jobconfiguration DROP COLUMN lastexecutedstatus");
}
// now deleted column
try (Statement stmt = context.getConnection().createStatement()) {
stmt.executeUpdate("ALTER TABLE jobconfiguration RENAME COLUMN lastexecutedstatusvarchar TO lastexecutedstatus");
}
// 6. Set default values where NULL is present
try (Statement stmt = context.getConnection().createStatement()) {
stmt.executeUpdate("UPDATE jobconfiguration SET lastexecutedstatus = 'NOT_STARTED' WHERE lastexecutedstatus IS NULL");
}
}
}
Aggregations