Source:
public String getMySQLScript() { String sql = ""; final String UPDATE_MISSING_WORKFLOW_ASSIGNMENTS = "UPDATE workflow_task SET assigned_to = " + " (SELECT id FROM cms_role WHERE role_name = '" + Config.getStringProperty("CMS_ADMINISTRATOR_ROLE") + "') " + " WHERE assigned_to NOT IN (SELECT id FROM cms_role); "; final String FKWORKFLOWASSIGN = "ALTER TABLE workflow_task ADD CONSTRAINT FK_workflow_assign FOREIGN KEY (assigned_to) REFERENCES cms_role (id);"; final String FKWORKFLOWTASKASSET = "ALTER TABLE workflow_task ADD CONSTRAINT FK_workflow_task_asset FOREIGN KEY (webasset) REFERENCES identifier (id);"; final String FKWORKFLOWSTEP = "ALTER TABLE workflow_task ADD CONSTRAINT FK_workflow_step FOREIGN KEY (status) REFERENCES workflow_step (id);"; Connection conn = null; final List<String> tables = new ArrayList<>( Arrays.asList("workflow_task")); try { sql += UPDATE_MISSING_WORKFLOW_ASSIGNMENTS; conn = DbConnectionFactory.getDataSource().getConnection(); conn.setAutoCommit(true); final List<ForeignKey> listForeignKeys = this.getForeingKeys(conn, tables, false); final List<String> listForeignKeysNames = listForeignKeys.stream().map(ForeignKey::fkName).collect( CollectionsUtils.toImmutableList()); if(!listForeignKeysNames.contains("FK_workflow_assign")){ sql += FKWORKFLOWASSIGN; } if(!listForeignKeysNames.contains("FK_workflow_task_asset")){ sql += FKWORKFLOWTASKASSET; } if(!listForeignKeysNames.contains("FK_workflow_step")){ sql += FKWORKFLOWSTEP; } } catch (Exception e) { Logger.error(this,"Error Running Upgrade Task 4215 " + e.getMessage(),e); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException ex) { Logger.error(this,"Error Closing the Connection " + ex.getMessage(),ex); } } return sql; }

Target:
public String getMySQLScript() { String sql = ""; final String UPDATE_MISSING_WORKFLOW_ASSIGNMENTS = "UPDATE workflow_task SET assigned_to = " + " (SELECT id FROM cms_role WHERE role_name = '" + Config.getStringProperty("CMS_ADMINISTRATOR_ROLE", Role.DEFAULT_CMS_ADMINISTRATOR_ROLE) + "') " + " WHERE NOT EXISTS (SELECT 1 FROM cms_role rl WHERE rl.id = assigned_to); "; final String FKWORKFLOWASSIGN = "ALTER TABLE workflow_task ADD CONSTRAINT FK_workflow_assign FOREIGN KEY (assigned_to) REFERENCES cms_role (id);"; final String FKWORKFLOWTASKASSET = "ALTER TABLE workflow_task ADD CONSTRAINT FK_workflow_task_asset FOREIGN KEY (webasset) REFERENCES identifier (id);"; final String FKWORKFLOWSTEP = "ALTER TABLE workflow_task ADD CONSTRAINT FK_workflow_step FOREIGN KEY (status) REFERENCES workflow_step (id);"; Connection conn = null; final List<String> tables = new ArrayList<>( Arrays.asList("workflow_task")); try { sql += UPDATE_MISSING_WORKFLOW_ASSIGNMENTS; conn = DbConnectionFactory.getDataSource().getConnection(); conn.setAutoCommit(true); final List<ForeignKey> listForeignKeys = this.getForeingKeys(conn, tables, false); final List<String> listForeignKeysNames = listForeignKeys.stream().map(ForeignKey::fkName).collect( CollectionsUtils.toImmutableList()); if(!listForeignKeysNames.contains("FK_workflow_assign")){ sql += FKWORKFLOWASSIGN; } if(!listForeignKeysNames.contains("FK_workflow_task_asset")){ sql += FKWORKFLOWTASKASSET; } if(!listForeignKeysNames.contains("FK_workflow_step")){ sql += FKWORKFLOWSTEP; } } catch (Exception e) { Logger.error(this,"Error Running Upgrade Task 4215 " + e.getMessage(),e); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException ex) { Logger.error(this,"Error Closing the Connection " + ex.getMessage(),ex); } } return sql; }