MaintenanceDao { @Transactional public int archiveWorkflows(Collection<Long> workflowIds) { String workflowIdParams = params(workflowIds); int archivedInstances = archiveTable("workflow", "id", getWorkflowColumns(), workflowIdParams); int archivedActions = archiveTable("workflow_action", "workflow_id", getActionColumns(), workflowIdParams); int archivedStates = archiveTable("workflow_state", "workflow_id", getStateColumns(), workflowIdParams); logger.info("Archived {} workflow instances, {} actions and {} states.", archivedInstances, archivedActions, archivedStates); deleteWorkflows(MAIN, workflowIdParams); return archivedInstances; } @Inject  MaintenanceDao(SQLVariants sqlVariants, @NFlow JdbcTemplate jdbcTemplate,
      @NFlow NamedParameterJdbcTemplate nflowNamedParameterJdbcTemplate); List<Long> getOldWorkflowIds(TablePrefix table, DateTime before, int maxWorkflows, Set<String> workflowTypes); @Transactional int archiveWorkflows(Collection<Long> workflowIds); @Transactional int deleteWorkflows(TablePrefix table, Collection<Long> workflowIds); @Transactional void deleteActionAndStateHistory(long workflowInstanceId, DateTime olderThan);  }
@Test public void archiveWorkflowsWorks() { List<Long> workflowIds = new ArrayList<>(); storeActiveWorkflow(archiveTime1); storeActiveWorkflow(prodTime1); storePassiveWorkflow(prodTime1); workflowIds.add(storePassiveWorkflow(archiveTime1)); workflowIds.add(storePassiveWorkflow(archiveTime2)); int workflowCountBefore = getActiveWorkflowCount(); assertEquals(workflowIds.size(), maintenanceDao.archiveWorkflows(workflowIds)); int workflowCountAfter = getActiveWorkflowCount(); assertActiveWorkflowsRemoved(workflowIds); assertArchiveWorkflowsExist(workflowIds); assertEquals(workflowIds.size(), getArchivedWorkflowCount()); assertEquals(workflowCountAfter, workflowCountBefore - workflowIds.size()); }
@Test public void archiveWorkflowsWithActionsWorks() { List<Long> workflowIds = new ArrayList<>(); List<Long> actionIds = new ArrayList<>(); storeActions(storeActiveWorkflow(archiveTime1), 3); storeActions(storeActiveWorkflow(prodTime1), 1); storeActions(storePassiveWorkflow(prodTime1), 2); long archivable1 = storePassiveWorkflow(archiveTime1); long archivable2 = storePassiveWorkflow(archiveTime2); actionIds.addAll(storeActions(archivable1, 1)); actionIds.addAll(storeActions(archivable2, 3)); workflowIds.addAll(asList(archivable1, archivable2)); int actionCountBefore = getActiveActionCount(); assertEquals(workflowIds.size(), maintenanceDao.archiveWorkflows(workflowIds)); int actionCountAfter = getActiveActionCount(); assertActiveWorkflowsRemoved(workflowIds); assertArchiveWorkflowsExist(workflowIds); assertActiveActionsRemoved(actionIds); assertArchiveActionsExist(actionIds, true); assertEquals(actionIds.size(), getArchiveActionCount()); assertEquals(actionCountAfter, actionCountBefore - actionIds.size()); }
@Test public void archiveWorkflowsWithActionsAndStatesWorks() { List<Long> workflowIds = new ArrayList<>(); List<Long> actionIds = new ArrayList<>(); List<StateKey> stateIds = new ArrayList<>(); long nonArchivableWorkflow1 = storeActiveWorkflow(archiveTime1); storeStateVariables(nonArchivableWorkflow1, storeActions(nonArchivableWorkflow1, 3), 1); long nonArchivableWorkflow2 = storeActiveWorkflow(prodTime1); storeStateVariables(nonArchivableWorkflow2, storeActions(nonArchivableWorkflow2, 1), 3); long nonArchivableWorkflow3 = storePassiveWorkflow(prodTime1); storeStateVariables(nonArchivableWorkflow3, storeActions(nonArchivableWorkflow3, 2), 2); long archivable1 = storePassiveWorkflow(archiveTime1); long archivable2 = storePassiveWorkflow(archiveTime2); List<Long> actions1 = storeActions(archivable1, 1); List<Long> actions2 = storeActions(archivable2, 2); actionIds.addAll(actions1); actionIds.addAll(actions2); stateIds.addAll(storeStateVariables(archivable1, actions1, 4)); stateIds.addAll(storeStateVariables(archivable2, actions2, 2)); workflowIds.addAll(asList(archivable1, archivable2)); int variablesCountBefore = getActiveStateCount(); assertEquals(workflowIds.size(), maintenanceDao.archiveWorkflows(workflowIds)); int variablesCountAfter = getActiveStateCount(); assertActiveWorkflowsRemoved(workflowIds); assertArchiveWorkflowsExist(workflowIds); assertActiveActionsRemoved(actionIds); assertArchiveActionsExist(actionIds, true); assertActiveStateVariablesRemoved(stateIds); assertArchiveStateVariablesExist(stateIds, true); int requestDataVariableCount = workflowIds.size(); assertEquals(stateIds.size() + requestDataVariableCount, getArchivedStateCount()); assertEquals(variablesCountAfter, variablesCountBefore - stateIds.size() - requestDataVariableCount); }
@Test public void archiveChildWorkflowWithActiveParentWorks() { List<Long> archivableWorkflows = new ArrayList<>(); long parentId = storeActiveWorkflow(archiveTime1); storeActiveChildWorkflow(archiveTime1, parentId); archivableWorkflows.add(storePassiveChildWorkflow(archiveTime1, parentId)); int activeWorkflowCountBefore = getActiveWorkflowCount(); assertEquals(archivableWorkflows.size(), maintenanceDao.archiveWorkflows(archivableWorkflows)); int activeWorkflowCountAfter = getActiveWorkflowCount(); assertActiveWorkflowsRemoved(archivableWorkflows); assertArchiveWorkflowsExist(archivableWorkflows); assertEquals(archivableWorkflows.size(), getArchivedWorkflowCount()); assertEquals(activeWorkflowCountAfter, activeWorkflowCountBefore - archivableWorkflows.size()); }
@Test public void archiveParentWorkflowWithActiveChildWorks() { List<Long> archivableWorkflows = new ArrayList<>(); long parentId = storeActiveWorkflow(archiveTime1); archivableWorkflows.add(parentId); storeActiveChildWorkflow(archiveTime1, parentId); int activeWorkflowCountBefore = getActiveWorkflowCount(); assertEquals(archivableWorkflows.size(), maintenanceDao.archiveWorkflows(archivableWorkflows)); int activeWorkflowCountAfter = getActiveWorkflowCount(); assertActiveWorkflowsRemoved(archivableWorkflows); assertArchiveWorkflowsExist(archivableWorkflows); assertEquals(archivableWorkflows.size(), getArchivedWorkflowCount()); assertEquals(activeWorkflowCountAfter, activeWorkflowCountBefore - archivableWorkflows.size()); }