Skip to content

Commit

Permalink
Add support for triggers and associated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsynacek committed Apr 12, 2022
1 parent d26ebb3 commit 3a44e59
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# hpqtypes-extras-1.15.0.0 (2022-??-??)
* Add support for triggers and trigger functions.

# hpqtypes-extras-1.14.2.0 (2022-??-??)
* Add support for GHC 9.2.
* Drop support for GHC < 8.8.
Expand Down
4 changes: 3 additions & 1 deletion hpqtypes-extras.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: hpqtypes-extras
version: 1.14.2.0
version: 1.15.0.0
synopsis: Extra utilities for hpqtypes library
description: The following extras for hpqtypes library:
.
Expand Down Expand Up @@ -68,6 +68,7 @@ library
, Database.PostgreSQL.PQTypes.Model.Migration
, Database.PostgreSQL.PQTypes.Model.PrimaryKey
, Database.PostgreSQL.PQTypes.Model.Table
, Database.PostgreSQL.PQTypes.Model.Trigger
, Database.PostgreSQL.PQTypes.SQL.Builder
, Database.PostgreSQL.PQTypes.Versions

Expand Down Expand Up @@ -111,6 +112,7 @@ test-suite hpqtypes-extras-tests
ghc-options: -Wall

build-depends: base
, containers
, exceptions
, hpqtypes
, hpqtypes-extras
Expand Down
19 changes: 19 additions & 0 deletions src/Database/PostgreSQL/PQTypes/Checks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,15 @@ checkDBStructure options tables = fmap mconcat . forM tables $ \(table, version)
indexes <- fetchMany fetchTableIndex
runQuery_ $ sqlGetForeignKeys table
fkeys <- fetchMany fetchForeignKey
triggers <- getDBTriggers
return $ mconcat [
checkColumns 1 tblColumns desc
, checkPrimaryKey tblPrimaryKey pk
, checkChecks tblChecks checks
, checkIndexes tblIndexes indexes
, checkForeignKeys tblForeignKeys fkeys
, checkTriggers tblTriggers $
filter (\Trigger{..} -> triggerTable == tblName) triggers
]
where
fetchTableColumn
Expand Down Expand Up @@ -541,6 +544,9 @@ checkDBStructure options tables = fmap mconcat . forM tables $ \(table, version)
, checkNames (fkName tblName) fkeys
]

checkTriggers :: [Trigger] -> [Trigger] -> ValidationResult
checkTriggers = checkEquality "TRIGGERs"

-- | Checks whether database is consistent, performing migrations if
-- necessary. Requires all table names to be in lower case.
--
Expand Down Expand Up @@ -607,6 +613,11 @@ checkDBConsistency options domains tablesWithVersions migrations = do
expectedMigrationVersions
= reverse $ take (length presentMigrationVersions) $
reverse [0 .. tblVersion table - 1]
-- -- TODO: File a separate issue about this with a reproducer!
-- = if null presentMigrationVersions
-- then []
-- else [0 .. tblVersion table - 1]

checkMigrationsListValidity table presentMigrationVersions
expectedMigrationVersions

Expand Down Expand Up @@ -814,6 +825,14 @@ checkDBConsistency options domains tablesWithVersions migrations = do
runSQL_ "COMMIT"
runQuery_ (sqlDropIndexConcurrently tname idx) `finally` begin
updateTableVersion

CreateTriggerMigration trigger@Trigger{..} -> do
logInfo_ $ " Creating function" <+> (unRawSQL $ tfName triggerFunction)
runQuery_ $ sqlCreateTriggerFunction triggerFunction
logInfo_ $ " Creating trigger" <+> (unRawSQL $ triggerMakeName triggerName triggerTable)
runQuery_ $ sqlCreateTrigger trigger
updateTableVersion

where
logMigration = do
logInfo_ $ arrListTable mgrTableName
Expand Down
10 changes: 9 additions & 1 deletion src/Database/PostgreSQL/PQTypes/Migrate.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Database.PostgreSQL.PQTypes.Migrate (
createDomain,
createTable,
createTableConstraints
createTableConstraints,
createTableTriggers
) where

import Control.Monad
Expand All @@ -28,6 +29,8 @@ createTable withConstraints table@Table{..} = do
forM_ tblIndexes $ runQuery_ . sqlCreateIndexMaybeDowntime tblName
-- Add all the other constraints if applicable.
when withConstraints $ createTableConstraints table
-- Create triggers.
createTableTriggers table
-- Register the table along with its version.
runQuery_ . sqlInsert "table_versions" $ do
sqlSet "name" (tblNameText table)
Expand All @@ -42,3 +45,8 @@ createTableConstraints Table{..} = when (not $ null addConstraints) $ do
, map sqlAddValidCheckMaybeDowntime tblChecks
, map (sqlAddValidFKMaybeDowntime tblName) tblForeignKeys
]

createTableTriggers :: MonadDB m => Table -> m ()
createTableTriggers Table{..} = forM_ tblTriggers $ \t -> do
runQuery_ . sqlCreateTriggerFunction $ triggerFunction t
runQuery_ $ sqlCreateTrigger t
2 changes: 2 additions & 0 deletions src/Database/PostgreSQL/PQTypes/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Database.PostgreSQL.PQTypes.Model (
, module Database.PostgreSQL.PQTypes.Model.Migration
, module Database.PostgreSQL.PQTypes.Model.PrimaryKey
, module Database.PostgreSQL.PQTypes.Model.Table
, module Database.PostgreSQL.PQTypes.Model.Trigger
) where

import Database.PostgreSQL.PQTypes.Model.Check
Expand All @@ -21,3 +22,4 @@ import Database.PostgreSQL.PQTypes.Model.Index
import Database.PostgreSQL.PQTypes.Model.Migration
import Database.PostgreSQL.PQTypes.Model.PrimaryKey
import Database.PostgreSQL.PQTypes.Model.Table
import Database.PostgreSQL.PQTypes.Model.Trigger
6 changes: 6 additions & 0 deletions src/Database/PostgreSQL/PQTypes/Model/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Data.Int

import Database.PostgreSQL.PQTypes.Model.Index
import Database.PostgreSQL.PQTypes.Model.Table
import Database.PostgreSQL.PQTypes.Model.Trigger
import Database.PostgreSQL.PQTypes.SQL.Raw

-- | Migration action to run, either an arbitrary 'MonadDB' action, or
Expand All @@ -57,6 +58,9 @@ data MigrationAction m =
(RawSQL ()) -- ^ Table name
TableIndex -- ^ Index

-- | Migration for creating a trigger.
| CreateTriggerMigration Trigger

-- | Migration object.
data Migration m =
Migration {
Expand All @@ -78,6 +82,7 @@ isStandardMigration Migration{..} =
DropTableMigration{} -> False
CreateIndexConcurrentlyMigration{} -> False
DropIndexConcurrentlyMigration{} -> False
CreateTriggerMigration{} -> False

isDropTableMigration :: Migration m -> Bool
isDropTableMigration Migration{..} =
Expand All @@ -86,3 +91,4 @@ isDropTableMigration Migration{..} =
DropTableMigration{} -> True
CreateIndexConcurrentlyMigration{} -> False
DropIndexConcurrentlyMigration{} -> False
CreateTriggerMigration{} -> False
3 changes: 3 additions & 0 deletions src/Database/PostgreSQL/PQTypes/Model/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Database.PostgreSQL.PQTypes.Model.ColumnType
import Database.PostgreSQL.PQTypes.Model.ForeignKey
import Database.PostgreSQL.PQTypes.Model.Index
import Database.PostgreSQL.PQTypes.Model.PrimaryKey
import Database.PostgreSQL.PQTypes.Model.Trigger

data TableColumn = TableColumn {
colName :: RawSQL ()
Expand Down Expand Up @@ -69,6 +70,7 @@ data Table =
, tblChecks :: [Check]
, tblForeignKeys :: [ForeignKey]
, tblIndexes :: [TableIndex]
, tblTriggers :: [Trigger]
, tblInitialSetup :: Maybe TableInitialSetup
}

Expand All @@ -86,6 +88,7 @@ tblTable = Table {
, tblChecks = []
, tblForeignKeys = []
, tblIndexes = []
, tblTriggers = []
, tblInitialSetup = Nothing
}

Expand Down
Loading

0 comments on commit 3a44e59

Please sign in to comment.