Skip to content

Commit

Permalink
Migrate enum to use varchar (#3627)
Browse files Browse the repository at this point in the history
Co-authored-by: kbirk <[email protected]>
  • Loading branch information
kbirk and kbirk authored May 15, 2024
1 parent d3dbd16 commit e34a166
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package db.migration;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

public class V13__NotificationEventProgress extends BaseJavaMigration {

ObjectMapper objectMapper = new ObjectMapper();

private String mapToString(final int progress) {
switch (progress) {
case 0:
return "CANCELLED";
case 1:
return "COMPLETE";
case 2:
return "ERROR";
case 3:
return "FAILED";
case 4:
return "QUEUED";
case 5:
return "RETRIEVING";
case 6:
return "RUNNING";
}
return null;
}

@Override
public void migrate(final Context context) throws Exception {
try (var statement = context.getConnection().createStatement()) {

// Rename transform to transform_old
statement.execute("ALTER TABLE notification_event RENAME COLUMN state TO state_old;");

// Create new json column transform
statement.execute("ALTER TABLE notification_event ADD COLUMN state varchar(255);");

// Select all rows from the simulation_result_files table
final ResultSet resultSet = statement.executeQuery(
"SELECT id, state_old FROM notification_event WHERE state_old IS NOT NULL ORDER BY id;");

// Prepare the update statement for the simulation table
final PreparedStatement preparedStatement =
context.getConnection().prepareStatement("UPDATE notification_event SET state = ? WHERE id = ?;");

// Iterate through the result set
while (resultSet.next()) {
// Get the id and result_files
final UUID id = (UUID) resultSet.getObject("id");
final int state = resultSet.getInt("state_old");

// Set the parameters for the update statement
preparedStatement.setString(1, mapToString(state));
preparedStatement.setObject(2, id);

// Execute the update statement
preparedStatement.executeUpdate();
}

// Create new json column transform
statement.execute("ALTER TABLE notification_event DROP COLUMN state_old");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
Expand All @@ -30,6 +32,8 @@ public class NotificationEvent extends TerariumEntity {
private static final long serialVersionUID = -3382397588627700379L;

private Double progress = 0.0;

@Enumerated(EnumType.STRING)
private ProgressState state = null;

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import software.uncharted.terarium.hmiserver.configuration.MockUser;
import software.uncharted.terarium.hmiserver.models.ClientEvent;
import software.uncharted.terarium.hmiserver.models.ClientEventType;
import software.uncharted.terarium.hmiserver.models.dataservice.simulation.ProgressState;
import software.uncharted.terarium.hmiserver.models.extractionservice.ExtractionStatusUpdate;
import software.uncharted.terarium.hmiserver.models.notification.NotificationEvent;
import software.uncharted.terarium.hmiserver.models.notification.NotificationGroup;
Expand Down Expand Up @@ -59,11 +60,14 @@ public void testItCanCreateNotificationEvent() throws Exception {
notificationService.createNotificationGroup(new NotificationGroup().setType("test"));

notificationService.createNotificationEvent(
group.getId(), new NotificationEvent().setData(produceClientEvent(1.0, "", "")));
group.getId(),
new NotificationEvent().setData(produceClientEvent(1.0, "", "")).setState(ProgressState.QUEUED));
notificationService.createNotificationEvent(
group.getId(), new NotificationEvent().setData(produceClientEvent(1.0, "", "")));
group.getId(),
new NotificationEvent().setData(produceClientEvent(1.0, "", "")).setState(ProgressState.RUNNING));
notificationService.createNotificationEvent(
group.getId(), new NotificationEvent().setData(produceClientEvent(1.0, "", "")));
group.getId(),
new NotificationEvent().setData(produceClientEvent(1.0, "", "")).setState(ProgressState.CANCELLED));

final NotificationGroup after =
notificationService.getNotificationGroup(group.getId()).orElseThrow();
Expand Down

0 comments on commit e34a166

Please sign in to comment.