From 3aa937a00091f6c01e8fbc32fb978b6cb0f813b8 Mon Sep 17 00:00:00 2001 From: Mikko Tiihonen Date: Mon, 20 Dec 2021 10:50:29 +0200 Subject: [PATCH] Make h2 trigger more robuts --- .../storage/db/H2ModifiedColumnTrigger.java | 23 +++++++++-- .../resources/scripts/db/h2.create.ddl.sql | 40 +++++++++---------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/nflow-engine/src/main/java/io/nflow/engine/internal/storage/db/H2ModifiedColumnTrigger.java b/nflow-engine/src/main/java/io/nflow/engine/internal/storage/db/H2ModifiedColumnTrigger.java index 8fbbf0d87..525371e59 100644 --- a/nflow-engine/src/main/java/io/nflow/engine/internal/storage/db/H2ModifiedColumnTrigger.java +++ b/nflow-engine/src/main/java/io/nflow/engine/internal/storage/db/H2ModifiedColumnTrigger.java @@ -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"); + } } diff --git a/nflow-engine/src/main/resources/scripts/db/h2.create.ddl.sql b/nflow-engine/src/main/resources/scripts/db/h2.create.ddl.sql index 86a0d29cc..5f17763ec 100644 --- a/nflow-engine/src/main/resources/scripts/db/h2.create.ddl.sql +++ b/nflow-engine/src/main/resources/scripts/db/h2.create.ddl.sql @@ -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 ); @@ -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) ); @@ -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) @@ -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 ); @@ -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) );