From 2fa29d077b11f9032ef494fe4e5364135fae8806 Mon Sep 17 00:00:00 2001 From: Sarah Vaupel Date: Fri, 20 May 2022 20:18:29 +0200 Subject: [PATCH] chore(workflows): migrate archived --- src/Model/Migration/Definitions.hs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Model/Migration/Definitions.hs b/src/Model/Migration/Definitions.hs index 78ac1db9e..8a8515098 100644 --- a/src/Model/Migration/Definitions.hs +++ b/src/Model/Migration/Definitions.hs @@ -103,9 +103,7 @@ data ManualMigration | Migration20210208StudyFeaturesRelevanceCachedUUIDs | Migration20210318CrontabSubmissionRatedNotification | Migration20210608SeparateTermActive - -- TODO: migration regarding authorship statements - -- - apply desired non-default modes for IfI - -- - set authorship statement texts for IfI + | Migration20220519WorkflowArchivation deriving (Eq, Ord, Read, Show, Enum, Bounded, Generic, Typeable) deriving anyclass (Universe, Finite) @@ -1069,6 +1067,21 @@ customMigrations = mapF $ \case ALTER TABLE "term" DROP COLUMN "active"; |] + Migration20220519WorkflowArchivation -> do + now <- liftIO getCurrentTime + mArchiveAfter <- lift $ view _appWorkflowWorkflowArchiveAfter + + whenM (and2M (return $ is _Just mArchiveAfter) (not <$> columnExists "workflow_workflow" "archived")) $ do + [executeQQ| ALTER TABLE "workflow_workflow" ADD "archived" timestamp with time zone; |] + let getWorkflows = [queryQQ| SELECT "workflow_workflow"."id", "workflow_workflow".("state"->>-1)::json->'time', "workflow_workflow".("state"->>-1)::json->'to', "shared_workflow_graph"."graph"->'nodes' FROM "workflow_workflow" INNER JOIN "shared_workflow_graph" ON "workflow_workflow"."graph" = "shared_workflow_graph"."hash"; |] + migrateArchived [ fromPersistValue -> Right (wwId :: WorkflowWorkflowId), fromPersistValue -> Right (wpTime :: UTCTime), fromPersistValue -> Right (wpTo :: WorkflowGraphNodeLabel), fromPersistValue -> Right (wGraph :: DBWorkflowGraph) ] = maybeT (return ()) $ do + archiveAfter <- hoistMaybe mArchiveAfter + WGN{wgnFinal} <- hoistMaybe . Map.lookup wpTo $ wgNodes wGraph + let wwArchived = const (max now $ archiveAfter `addUTCTime` wpTime) <$> wgnFinal + lift [executeQQ| UPDATE workflow_workflow SET archived = #{wwArchived} WHERE id = #{wwId} |] + migrateArchived _ = return () + in runConduit $ getWorkflows .| C.mapM_ migrateArchived + tableExists :: MonadIO m => Text -> ReaderT SqlBackend m Bool tableExists table = do