diff --git a/nflow-engine/src/main/java/com/nitorcreations/nflow/engine/internal/dao/WorkflowDefinitionDao.java b/nflow-engine/src/main/java/com/nitorcreations/nflow/engine/internal/dao/WorkflowDefinitionDao.java index 076b09589..2d35b6343 100644 --- a/nflow-engine/src/main/java/com/nitorcreations/nflow/engine/internal/dao/WorkflowDefinitionDao.java +++ b/nflow-engine/src/main/java/com/nitorcreations/nflow/engine/internal/dao/WorkflowDefinitionDao.java @@ -1,10 +1,15 @@ package com.nitorcreations.nflow.engine.internal.dao; +import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Collections.sort; import static org.slf4j.LoggerFactory.getLogger; import static org.springframework.util.CollectionUtils.isEmpty; import java.io.IOException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -57,16 +62,19 @@ public void storeWorkflowDefinition(WorkflowDefinition StoredWorkflowDefinition storedDefinition = convert(definition); MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("type", definition.getType()); - params.addValue("definition", serializeDefinition(storedDefinition)); + String serializedDefinition = serializeDefinition(storedDefinition); + params.addValue("definition_sha1", sha1(serializedDefinition)); + params.addValue("definition", serializedDefinition); params.addValue("modified_by", executorInfo.getExecutorId()); params.addValue("executor_group", executorInfo.getExecutorGroup()); + String sql = "update nflow_workflow_definition " - + "set definition = :definition, modified_by = :modified_by " - + "where type = :type and executor_group = :executor_group and definition <> :definition"; + + "set definition = :definition, modified_by = :modified_by, definition_sha1 = :definition_sha1 " + + "where type = :type and executor_group = :executor_group and definition_sha1 <> :definition_sha1"; int updatedRows = namedJdbc.update(sql, params); if (updatedRows == 0) { - sql = "insert into nflow_workflow_definition(type, definition, modified_by, executor_group) " - + "values (:type, :definition, :modified_by, :executor_group)"; + sql = "insert into nflow_workflow_definition(type, definition_sha1, definition, modified_by, executor_group) " + + "values (:type, :definition_sha1, :definition, :modified_by, :executor_group)"; try { namedJdbc.update(sql, params); } catch (DataIntegrityViolationException dex) { @@ -75,6 +83,16 @@ public void storeWorkflowDefinition(WorkflowDefinition } } + private String sha1(String serializedDefinition) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.update(serializedDefinition.getBytes(UTF_8)); + return format("%040x", new BigInteger(1, digest.digest())); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("SHA1 not supported"); + } + } + public List queryStoredWorkflowDefinitions(Collection types) { String sql = "select definition from nflow_workflow_definition where " + executorInfo.getExecutorGroupCondition(); MapSqlParameterSource params = new MapSqlParameterSource(); 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 875a597a2..b176876d7 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 @@ -51,6 +51,7 @@ create table if not exists nflow_executor ( create table if not exists nflow_workflow_definition ( type varchar(64) not null, + definition_sha1 varchar(40) not null default 'n/a', definition text not null, created timestamp not null default current_timestamp, modified timestamp not null default current_timestamp, diff --git a/nflow-engine/src/main/resources/scripts/db/mysql.create.ddl.sql b/nflow-engine/src/main/resources/scripts/db/mysql.create.ddl.sql index 70ddf88f6..75d964d1d 100644 --- a/nflow-engine/src/main/resources/scripts/db/mysql.create.ddl.sql +++ b/nflow-engine/src/main/resources/scripts/db/mysql.create.ddl.sql @@ -48,6 +48,7 @@ create table if not exists nflow_executor ( create table if not exists nflow_workflow_definition ( type varchar(64) not null, + definition_sha1 varchar(40) not null default 'n/a', definition text not null, created timestamp(3) not null default current_timestamp(3), modified timestamp(3) not null default current_timestamp(3) on update current_timestamp(3), diff --git a/nflow-engine/src/main/resources/scripts/db/mysql.legacy.create.ddl.sql b/nflow-engine/src/main/resources/scripts/db/mysql.legacy.create.ddl.sql index 8f9c0fac9..686715f7f 100644 --- a/nflow-engine/src/main/resources/scripts/db/mysql.legacy.create.ddl.sql +++ b/nflow-engine/src/main/resources/scripts/db/mysql.legacy.create.ddl.sql @@ -53,6 +53,7 @@ create table if not exists nflow_executor ( create table if not exists nflow_workflow_definition ( type varchar(64) not null, + definition_sha1 varchar(40) not null default 'n/a', definition text not null, modified timestamp not null default current_timestamp on update current_timestamp, modified_by int not null, diff --git a/nflow-engine/src/main/resources/scripts/db/postgresql.create.ddl.sql b/nflow-engine/src/main/resources/scripts/db/postgresql.create.ddl.sql index c25803a68..daa762908 100644 --- a/nflow-engine/src/main/resources/scripts/db/postgresql.create.ddl.sql +++ b/nflow-engine/src/main/resources/scripts/db/postgresql.create.ddl.sql @@ -61,6 +61,7 @@ create table if not exists nflow_executor ( create table if not exists nflow_workflow_definition ( type varchar(64) not null, + definition_sha1 varchar(40) not null default 'n/a', definition text not null, created timestamptz not null default current_timestamp, modified timestamptz not null default current_timestamp, diff --git a/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/h2.update.ddl.sql b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/h2.update.ddl.sql new file mode 100644 index 000000000..6f96d7ab8 --- /dev/null +++ b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/h2.update.ddl.sql @@ -0,0 +1,2 @@ + +alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a'; \ No newline at end of file diff --git a/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/mysql.legacy.update.ddl.sql b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/mysql.legacy.update.ddl.sql new file mode 100644 index 000000000..6f96d7ab8 --- /dev/null +++ b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/mysql.legacy.update.ddl.sql @@ -0,0 +1,2 @@ + +alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a'; \ No newline at end of file diff --git a/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/mysql.update.ddl.sql b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/mysql.update.ddl.sql new file mode 100644 index 000000000..6f96d7ab8 --- /dev/null +++ b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/mysql.update.ddl.sql @@ -0,0 +1,2 @@ + +alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a'; \ No newline at end of file diff --git a/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/postgresql.update.ddl.sql b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/postgresql.update.ddl.sql new file mode 100644 index 000000000..6f96d7ab8 --- /dev/null +++ b/nflow-engine/src/main/resources/scripts/db/update-1.2.0-x/postgresql.update.ddl.sql @@ -0,0 +1,2 @@ + +alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a'; \ No newline at end of file