Skip to content

Commit

Permalink
Make h2 trigger more robuts
Browse files Browse the repository at this point in the history
  • Loading branch information
gmokki committed Dec 20, 2021
1 parent 69ecce0 commit 3aa937a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,32 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Objects;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import org.h2.tools.TriggerAdapter;

public class H2ModifiedColumnTrigger extends TriggerAdapter {
@Override
public void fire(Connection conn, ResultSet oldRow, ResultSet newRow) throws SQLException {
Timestamp oldModified = oldRow.getTimestamp("modified");
Timestamp newModified = newRow.getTimestamp("modified");
if (Objects.equals(oldModified, newModified)) {
long oldModified = getMillis(oldRow.getObject("modified"));
long newModified = getMillis(newRow.getObject("modified"));
if (oldModified == newModified) {
newRow.updateTimestamp("modified", new Timestamp(currentTimeMillis()));
}
}

private long getMillis(Object h2Time) {
if (h2Time instanceof Timestamp) {
return ((Timestamp) h2Time).getTime();
}
if (h2Time instanceof OffsetDateTime) {
return ((OffsetDateTime) h2Time).toInstant().toEpochMilli();
}
if (h2Time instanceof LocalDateTime) {
return ((LocalDateTime) h2Time).toInstant(ZoneOffset.systemDefault().getRules().getOffset((LocalDateTime) h2Time)).toEpochMilli();
}
throw new UnsupportedOperationException("No support for converting " + h2Time.getClass() + " to milliseconds");
}
}
40 changes: 20 additions & 20 deletions nflow-engine/src/main/resources/scripts/db/h2.create.ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ create table if not exists nflow_workflow (
external_id varchar(64) not null,
state varchar(64) not null,
state_text varchar(128),
next_activation timestamp,
external_next_activation timestamp,
next_activation timestamp with time zone,
external_next_activation timestamp with time zone,
executor_id int,
retries int not null default 0,
created timestamp not null default current_timestamp,
modified timestamp not null default current_timestamp,
started timestamp,
created timestamp with time zone not null default current_timestamp,
modified timestamp with time zone not null default current_timestamp,
started timestamp with time zone,
executor_group varchar(64) not null,
workflow_signal int
);
Expand All @@ -37,8 +37,8 @@ create table if not exists nflow_workflow_action (
state varchar(64) not null,
state_text varchar(128),
retry_no int not null,
execution_start timestamp not null,
execution_end timestamp not null,
execution_start timestamp with time zone not null,
execution_end timestamp with time zone not null,
constraint fk_action_workflow_id foreign key (workflow_id) references nflow_workflow(id)
);

Expand All @@ -58,18 +58,18 @@ create table if not exists nflow_executor (
host varchar(253) not null,
pid int not null,
executor_group varchar(64),
started timestamp not null default current_timestamp,
active timestamp not null,
expires timestamp not null,
stopped timestamp
started timestamp with time zone not null default current_timestamp,
active timestamp with time zone not null,
expires timestamp with time zone not null,
stopped timestamp with time zone
);

create table if not exists nflow_workflow_definition (
type varchar(64) not null,
definition_sha1 varchar(40) not null,
definition text not null,
created timestamp not null default current_timestamp,
modified timestamp not null default current_timestamp,
created timestamp with time zone not null default current_timestamp,
modified timestamp with time zone not null default current_timestamp,
modified_by int not null,
executor_group varchar(64) not null,
constraint pk_workflow_definition primary key (type, executor_group)
Expand All @@ -91,13 +91,13 @@ create table if not exists nflow_archive_workflow (
external_id varchar(64) not null,
state varchar(64) not null,
state_text varchar(128),
next_activation timestamp,
external_next_activation timestamp,
next_activation timestamp with time zone,
external_next_activation timestamp with time zone,
executor_id int,
retries int not null,
created timestamp not null,
modified timestamp not null,
started timestamp,
created timestamp with time zone not null,
modified timestamp with time zone not null,
started timestamp with time zone,
executor_group varchar(64) not null,
workflow_signal int
);
Expand All @@ -113,8 +113,8 @@ create table if not exists nflow_archive_workflow_action (
state varchar(64) not null,
state_text varchar(128),
retry_no int not null,
execution_start timestamp not null,
execution_end timestamp not null,
execution_start timestamp with time zone not null,
execution_end timestamp with time zone not null,
constraint fk_arch_action_wf_id foreign key (workflow_id) references nflow_archive_workflow(id)
);

Expand Down

0 comments on commit 3aa937a

Please sign in to comment.