diff --git a/src/OddJobs/Job/Query.hs b/src/OddJobs/Job/Query.hs index 46c9938..4725907 100644 --- a/src/OddJobs/Job/Query.hs +++ b/src/OddJobs/Job/Query.hs @@ -95,28 +95,27 @@ concatJobDbColumnsInternal ys = go ys "" go [col] x = x <> col go (col:cols) x = go cols (x <> col <> ", ") --- | TODO -{-# INLINE concatJobDbColumnsWorkflow #-} -concatJobDbColumnsWorkflow :: (IsString s, Semigroup s) => s -concatJobDbColumnsWorkflow = concatJobDbColumnsInternal jobDbColumnsWorkflow - where - --- | TODO -jobDbColumnsWorkflow :: (IsString s, Semigroup s) => [s] -jobDbColumnsWorkflow = - [ "id" - , "created_at" - , "updated_at" - , "run_at" - , "status" - , "payload" - , "last_error" - , "attempts" - , "locked_at" - , "locked_by" - , "result" - , "parent_job_id" - ] +-- -- | TODO +-- {-# INLINE concatJobDbColumnsWorkflow #-} +-- concatJobDbColumnsWorkflow :: (IsString s, Semigroup s) => s +-- concatJobDbColumnsWorkflow = concatJobDbColumnsInternal jobDbColumnsWorkflow + +-- -- | TODO +-- jobDbColumnsWorkflow :: (IsString s, Semigroup s) => [s] +-- jobDbColumnsWorkflow = +-- [ "id" +-- , "created_at" +-- , "updated_at" +-- , "run_at" +-- , "status" +-- , "payload" +-- , "last_error" +-- , "attempts" +-- , "locked_at" +-- , "locked_by" +-- , "result" +-- , "parent_job_id" +-- ] saveJobQuery :: Query saveJobQuery = diff --git a/src/OddJobs/Migrations.hs b/src/OddJobs/Migrations.hs index 1e522c8..1ae7be0 100644 --- a/src/OddJobs/Migrations.hs +++ b/src/OddJobs/Migrations.hs @@ -11,20 +11,18 @@ import Database.PostgreSQL.Simple.ToRow as PGS import Data.Functor (void) import OddJobs.Types -createJobTableQuery :: Query -createJobTableQuery = createJobTableQueryInternal False +-- createJobTableQuery :: Query +-- createJobTableQuery = createJobTableQueryInternal False -createJobTable :: Connection -> TableName -> IO () -createJobTable = createJobTableInternal False +-- createJobTable :: Connection -> TableName -> IO () +-- createJobTable = createJobTableInternal False -createJobTableWithWorkflow :: Connection -> TableName -> IO () -createJobTableWithWorkflow = createJobTableInternal True +-- createJobTableWithWorkflow :: Connection -> TableName -> IO () +-- createJobTableWithWorkflow = createJobTableInternal True -createJobTableQueryInternal :: - Bool -> - -- ^ whether to enable job-results and job-workflow features +createJobTableQuery :: Query -createJobTableQueryInternal enableWorkflows = "CREATE TABLE IF NOT EXISTS ?" <> +createJobTableQuery = "CREATE TABLE IF NOT EXISTS ?" <> "( id serial primary key" <> ", created_at timestamp with time zone default now() not null" <> ", updated_at timestamp with time zone default now() not null" <> @@ -35,7 +33,7 @@ createJobTableQueryInternal enableWorkflows = "CREATE TABLE IF NOT EXISTS ?" <> ", attempts int not null default 0" <> ", locked_at timestamp with time zone null" <> ", locked_by text null" <> - if enableWorkflows then ", result jsonb, parent_job_id int references ?(id)" else "" <> + ", result jsonb, parent_job_id int references ?(id)" <> ", constraint incorrect_locking_info CHECK (" <> "(locked_at is null and locked_by is null and status <> 'locked') or " <> "(locked_at is not null and locked_by is not null and (status = 'locked' or status = 'cancelled')))" <> @@ -46,7 +44,7 @@ createJobTableQueryInternal enableWorkflows = "CREATE TABLE IF NOT EXISTS ?" <> "create index if not exists ? on ?(locked_by);" <> "create index if not exists ? on ?(status);" <> "create index if not exists ? on ?(run_at);" <> - if enableWorkflows then "create index if not exists ? on ?(parent_job_id);" else "" + "create index if not exists ? on ?(parent_job_id);" createNotificationTrigger :: Query createNotificationTrigger = "create or replace function ?() returns trigger as $$" <> @@ -59,32 +57,30 @@ createNotificationTrigger = "create or replace function ?() returns trigger as $ "drop trigger if exists ? on ?;" <> "create trigger ? after insert on ? for each row execute procedure ?();" -createJobTableInternal :: - Bool -> - -- ^ whether to enable job-results and job-workflow features +createJobTable :: Connection -> TableName -> IO () -createJobTableInternal enableWorkflows conn tname = void $ do +createJobTable conn tname = void $ do let tnameTxt = getTnameTxt tname - let a1 = ( PGS.Identifier $ "idx_" <> tnameTxt <> "_created_at" - , tname - , PGS.Identifier $ "idx_" <> tnameTxt <> "_updated_at" - , tname - , PGS.Identifier $ "idx_" <> tnameTxt <> "_locked_at" - , tname - , PGS.Identifier $ "idx_" <> tnameTxt <> "_locked_by" - , tname - , PGS.Identifier $ "idx_" <> tnameTxt <> "_status" - , tname - , PGS.Identifier $ "idx_" <> tnameTxt <> "_run_at" - , tname - ) - finalFields = if enableWorkflows - then PGS.toRow $ (tname, tname) PGS.:. a1 PGS.:. (tname, PGS.Identifier $ "idx_" <> tnameTxt <> "_parent_job_id") - else PGS.toRow $ (Only tname) PGS.:. a1 - - _ <- PGS.execute conn (createJobTableQueryInternal enableWorkflows) finalFields + args = ( tname + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_created_at" + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_updated_at" + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_locked_at" + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_locked_by" + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_status" + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_run_at" + , tname + , tname + , PGS.Identifier $ "idx_" <> tnameTxt <> "_parent_job_id" + ) + _ <- PGS.execute conn createJobTableQuery args PGS.execute conn createNotificationTrigger ( fnName , pgEventName tname diff --git a/test/Test.hs b/test/Test.hs index ad89768..dc31e87 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -410,7 +410,7 @@ testJobDeletion appPool jobPool = testCase "job immediae deletion" $ do testJobResults appPool jobPool = testCase "job results" $ do withRandomTable jobPool $ \tname -> do - withNamedJobMonitor tname jobPool (\cfg -> cfg{Job.cfgImmediateJobDeletion=(const $ pure False)}) $ \logRef -> do + withNamedJobMonitor tname jobPool (\cfg -> cfg{Job.cfgImmediateJobDeletion=const $ pure False}) $ \logRef -> do Pool.withResource appPool $ \conn -> do Job{jobId} <- Job.createJob conn tname (PayloadSucceed 0 (Just "abcdef")) delaySeconds Job.defaultPollingInterval