1 {-# LANGUAGE TemplateHaskell, DataKinds #-}
    2 module Databrary.Model.Release
    3   ( module Databrary.Model.Release.Types
    4   , changeRelease
    5   ) where
    6 
    7 import Control.Monad (guard)
    8 import Database.PostgreSQL.Typed.Query
    9 import Database.PostgreSQL.Typed.Types
   10 import qualified Data.ByteString
   11 import Data.ByteString (ByteString)
   12 import qualified Data.String
   13 
   14 import Databrary.Service.DB
   15 import Databrary.Model.SQL
   16 import Databrary.Model.Audit
   17 import Databrary.Model.Slot.Types
   18 import Databrary.Model.Container.Types
   19 import Databrary.Model.Release.Types
   20 -- import Databrary.Model.Release.SQL
   21 
   22 -- useTDB
   23 
   24 mapQuery :: ByteString -> ([PGValue] -> a) -> PGSimpleQuery a
   25 mapQuery qry mkResult =
   26   fmap mkResult (rawPGSimpleQuery qry)
   27 
   28 changeRelease :: MonadAudit c m => Slot -> Maybe Release -> m Bool
   29 changeRelease s Nothing = do
   30   ident <- getAuditIdentity
   31   let _tenv_a649Y = unknownPGTypeEnv
   32   dbExecute1 -- .(deleteRelease 'ident 's)
   33     (mapQuery
   34                     (Data.ByteString.concat
   35                        [Data.String.fromString
   36                           "WITH audit_row AS (DELETE FROM slot_release WHERE container = ",
   37                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   38                           _tenv_a649Y
   39                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   40                              Database.PostgreSQL.Typed.Types.PGTypeName "integer")
   41                           (containerId $ containerRow $ slotContainer s),
   42                         Data.String.fromString " AND segment <@ ",
   43                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   44                           _tenv_a649Y
   45                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   46                              Database.PostgreSQL.Typed.Types.PGTypeName "segment")
   47                           (slotSegment s),
   48                         Data.String.fromString
   49                           " RETURNING *) INSERT INTO audit.slot_release SELECT CURRENT_TIMESTAMP, ",
   50                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   51                           _tenv_a649Y
   52                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   53                              Database.PostgreSQL.Typed.Types.PGTypeName "integer")
   54                           (auditWho ident),
   55                         Data.String.fromString ", ",
   56                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   57                           _tenv_a649Y
   58                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   59                              Database.PostgreSQL.Typed.Types.PGTypeName "inet")
   60                           (auditIp ident),
   61                         Data.String.fromString
   62                           ", 'remove'::audit.action, * FROM audit_row"])
   63             (\[] -> ()))
   64       -- (containerId $ containerRow $ slotContainer s)
   65       -- (slotSegment s)
   66       -- (auditWho ident)
   67       -- (auditIp ident)
   68 
   69 changeRelease s (Just c) = do
   70   ident <- getAuditIdentity
   71   let _tenv_a64aA = unknownPGTypeEnv
   72       _tenv_a64bO = unknownPGTypeEnv
   73   either (const False) ((0 <) . fst) <$> tryUpdateOrInsert (guard . isExclusionViolation)
   74     -- .(updateRelease 'ident 's 'c)
   75     (mapQuery
   76                     (Data.ByteString.concat
   77                        [Data.String.fromString
   78                           "WITH audit_row AS (UPDATE slot_release SET release=",
   79                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   80                           _tenv_a64aA
   81                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   82                              Database.PostgreSQL.Typed.Types.PGTypeName "release")
   83                           c,
   84                         Data.String.fromString " WHERE container=",
   85                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   86                           _tenv_a64aA
   87                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   88                              Database.PostgreSQL.Typed.Types.PGTypeName "integer")
   89                           (containerId $ containerRow $ slotContainer s),
   90                         Data.String.fromString " AND segment=",
   91                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   92                           _tenv_a64aA
   93                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
   94                              Database.PostgreSQL.Typed.Types.PGTypeName "segment")
   95                           (slotSegment s),
   96                         Data.String.fromString
   97                           " RETURNING *) INSERT INTO audit.slot_release SELECT CURRENT_TIMESTAMP, ",
   98                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
   99                           _tenv_a64aA
  100                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  101                              Database.PostgreSQL.Typed.Types.PGTypeName "integer")
  102                           (auditWho ident),
  103                         Data.String.fromString ", ",
  104                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
  105                           _tenv_a64aA
  106                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  107                              Database.PostgreSQL.Typed.Types.PGTypeName "inet")
  108                           (auditIp ident),
  109                         Data.String.fromString
  110                           ", 'change'::audit.action, * FROM audit_row"])
  111             (\[] -> ()))
  112     -- .(insertRelease 'ident 's 'c)
  113     (mapQuery
  114                    (Data.ByteString.concat
  115                        [Data.String.fromString
  116                           "WITH audit_row AS (INSERT INTO slot_release (container,segment,release) VALUES (",
  117                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
  118                           _tenv_a64bO
  119                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  120                              Database.PostgreSQL.Typed.Types.PGTypeName "integer")
  121                           (containerId $ containerRow $ slotContainer s),
  122                         Data.String.fromString ",",
  123                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
  124                           _tenv_a64bO
  125                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  126                              Database.PostgreSQL.Typed.Types.PGTypeName "segment")
  127                           (slotSegment s),
  128                         Data.String.fromString ",",
  129                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
  130                           _tenv_a64bO
  131                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  132                              Database.PostgreSQL.Typed.Types.PGTypeName "release")
  133                           c,
  134                         Data.String.fromString
  135                           ") RETURNING *) INSERT INTO audit.slot_release SELECT CURRENT_TIMESTAMP, ",
  136                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
  137                           _tenv_a64bO
  138                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  139                              Database.PostgreSQL.Typed.Types.PGTypeName "integer")
  140                           (auditWho ident),
  141                         Data.String.fromString ", ",
  142                         Database.PostgreSQL.Typed.Types.pgEscapeParameter
  143                           _tenv_a64bO
  144                           (Database.PostgreSQL.Typed.Types.PGTypeProxy ::
  145                              Database.PostgreSQL.Typed.Types.PGTypeName "inet")
  146                           (auditIp ident),
  147                         Data.String.fromString ", 'add'::audit.action, * FROM audit_row"])
  148             (\[] -> ()))
  149 
  150