Skip to content

Commit 3cf9f98

Browse files
authored
Update haddocks
1 parent e216d0a commit 3cf9f98

File tree

10 files changed

+120
-40
lines changed

10 files changed

+120
-40
lines changed

beam-core/Database/Beam.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- | Top-level Beam module. This module re-exports all the symbols
33
-- necessary for most common user operations.
44
--
5-
-- The most interesting modules are 'Database.Beam.Schema' and 'Database.Beam.Query'.
5+
-- The most interesting modules are "Database.Beam.Schema" and "Database.Beam.Query".
66
--
77
-- This is mainly reference documentation. Most users will want to consult the
88
-- [manual](https://tathougies.github.io/beam).

beam-core/Database/Beam/Backend/SQL.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Control.Monad.IO.Class
2222
--
2323
-- Provided here is a low-level interface. Most often, you'll only need the
2424
-- 'withDatabase' and 'withDatabaseDebug' function. The 'run*' functions are
25-
-- wrapped by the appropriate functions in 'Database.Beam.Query'.
25+
-- wrapped by the appropriate functions in "Database.Beam.Query".
2626
--
2727
-- This interface is very high-level and isn't meant to expose the full power
2828
-- of the underlying database. Namely, it only supports simple data retrieval

beam-core/Database/Beam/Backend/SQL/AST.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE UndecidableInstances #-}
33
-- | This module implements an AST type for SQL92. It allows us to realize
4-
-- the call structure of the builders defined in 'Database.Beam.Backend.SQL92'
4+
-- the call structure of the builders defined in "Database.Beam.Backend.SQL.SQL92"
55
module Database.Beam.Backend.SQL.AST where
66

77
import Prelude hiding (Ordering)

beam-migrate/Database/Beam/Migrate.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
--
3535
-- As one final point, @beam-migrate@ can generate schemas in any beam-supported
3636
-- SQL DDL syntax. The @beam-migrate@ module provides a DDL syntax for Haskell
37-
-- in 'Database.Beam.Haskell.Syntax'. This allows @beam-migrate@ to translate
37+
-- in "Database.Beam.Haskell.Syntax". This allows @beam-migrate@ to translate
3838
-- predicate sets into the corresponding Haskell schema and the corresponding
3939
-- Haskell migration script. This reflection allows tool based off of
4040
-- @beam-migrate@ to support schema generation from an existing database.
4141
--
4242
-- For more information on checked databases, see the
43-
-- 'Database.Beam.Migrate.Checks' module.
43+
-- "Database.Beam.Migrate.Checks" module.
4444
--
4545
-- == Migrations
4646
--
@@ -58,12 +58,12 @@
5858
-- script in a given backend syntax, or generate an appropriate
5959
-- 'DatabaseSettings' object for use with the rest of the beam ecosystem.
6060
--
61-
-- For more information in migrations see 'Database.Beam.Migrate.Types'
61+
-- For more information in migrations see "Database.Beam.Migrate.Types"
6262
--
6363
-- == Syntax
6464
--
6565
-- For low-level access to the underlying SQL syntax builders, see
66-
-- 'Database.Beam.Migrate.Syntax'
66+
-- "Database.Beam.Migrate.SQL.SQL92"
6767
--
6868
-- == Advanced features
6969
--

beam-migrate/Database/Beam/Migrate/Backend.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
-- deserialize them from JSON. Finally, if your backend has custom
2020
-- 'DatabasePredicate's you will have to provide appropriate 'ActionProvider's
2121
-- to discover potential actions for your backend. See the documentation for
22-
-- 'Database.Beam.Migrate.Actions' for more information.
22+
-- "Database.Beam.Migrate.Actions" for more information.
2323
--
2424
-- Tools may be interested in the 'SomeBeamMigrationBackend' data type which
2525
-- provides a monomorphic type to wrap the polymorphic 'BeamMigrationBackend'
2626
-- type. Currently, @beam-migrate-cli@ uses this type to get the underlying
2727
-- 'BeamMigrationBackend' via the @hint@ package.
2828
--
29-
-- For an example migrate backend, see 'Database.Beam.Sqlite.Migrates'
29+
-- For an example migrate backend, see "Database.Beam.Sqlite.Migrate"
3030
module Database.Beam.Migrate.Backend
3131
( BeamMigrationBackend(..)
3232
, DdlError

beam-migrate/Database/Beam/Migrate/Simple.hs

+23-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ import qualified Data.Text as T
3939
data BringUpToDateHooks m
4040
= BringUpToDateHooks
4141
{ runIrreversibleHook :: m Bool
42+
-- ^ Called before we're about to run an irreversible migration step. Return
43+
-- 'True' to run the step, or 'False' to abort immediately.
4244
, startStepHook :: Int -> T.Text -> m ()
45+
-- ^ Called at the beginning of each step with the step index and description
4346
, endStepHook :: Int -> T.Text -> m ()
47+
-- ^ Called at the end of each step with the step index and description
4448
, runCommandHook :: Int -> String -> m ()
49+
-- ^ Called before a command is about to run. The first argument is the step
50+
-- index and the second is a string representing the command about to be run.
4551

4652
, queryFailedHook :: m ()
4753
-- ^ Called when a query fails
@@ -56,6 +62,8 @@ data BringUpToDateHooks m
5662
-- the number of entries passed the given migrations the database has.
5763
}
5864

65+
-- | Default set of 'BringUpToDateHooks'. Refuses to run irreversible
66+
-- migrations, and fails in case of error, using 'fail'.
5967
defaultUpToDateHooks :: Monad m => BringUpToDateHooks m
6068
defaultUpToDateHooks =
6169
BringUpToDateHooks
@@ -76,16 +84,24 @@ defaultUpToDateHooks =
7684
fail ("The database is ahead of the known schema by " ++ show aheadBy ++ " migration(s)")
7785
}
7886

79-
-- | Check for the beam-migrate log. If it exists, use it and the supplied
80-
-- migrations to bring the database up-to-date. Otherwise, create the log and
81-
-- run all migrations.
87+
-- | Equivalent to calling 'bringUpToDateWithHooks' with 'defaultUpToDateHooks'.
88+
--
89+
-- Tries to bring the database up to date, using the database log and the given
90+
-- 'MigrationSteps'. Fails if the migration is irreversible, or an error occurs.
8291
bringUpToDate :: Database be db
8392
=> BeamMigrationBackend cmd be hdl m
8493
-> MigrationSteps cmd () (CheckedDatabaseSettings be db)
8594
-> m (Maybe (CheckedDatabaseSettings be db))
8695
bringUpToDate be@BeamMigrationBackend {} =
8796
bringUpToDateWithHooks defaultUpToDateHooks be
8897

98+
-- | Check for the beam-migrate log. If it exists, use it and the supplied
99+
-- migrations to bring the database up-to-date. Otherwise, create the log and
100+
-- run all migrations.
101+
--
102+
-- Accepts a set of hooks that can be used to customize behavior. See the
103+
-- documentation for 'BringUpToDateHooks' for more information. Calling this
104+
-- with 'defaultUpToDateHooks' is the same as using 'bringUpToDate'.
89105
bringUpToDateWithHooks :: forall db cmd be hdl m
90106
. Database be db
91107
=> BringUpToDateHooks m
@@ -178,6 +194,10 @@ createSchema BeamMigrationBackend { backendActionProvider = actions } db =
178194
Just cmds ->
179195
mapM_ runNoReturn cmds
180196

197+
-- | Given a 'BeamMigrationBackend', attempt to automatically bring the current
198+
-- database up-to-date with the given 'CheckedDatabaseSettings'. Fails (via
199+
-- 'fail') if this involves an irreversible migration (one that may result in
200+
-- data loss).
181201
autoMigrate :: Database be db
182202
=> BeamMigrationBackend cmd be hdl m
183203
-> CheckedDatabaseSettings be db

beam-migrate/Database/Beam/Migrate/Types.hs

+35-21
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ module Database.Beam.Migrate.Types
4040
, MigrationCommand(..), MigrationDataLoss(..)
4141

4242
, runMigrationSteps, runMigrationSilenced
43-
, runMigrationVerbose, executeMigration
44-
, eraseMigrationType, migrationStep, upDown
45-
, migrationDataLoss
43+
, executeMigration, eraseMigrationType, migrationStep
44+
, upDown, migrationDataLoss
4645

4746
, migrateScript, evaluateDatabase, stepNames ) where
4847

49-
import Database.Beam
50-
import Database.Beam.Backend
51-
5248
import Database.Beam.Migrate.Types.CheckedEntities
5349
import Database.Beam.Migrate.Types.Predicates
5450

@@ -61,19 +57,27 @@ import Data.Text (Text)
6157

6258
-- * Migration types
6359

60+
-- | Represents a particular step in a migration
6461
data MigrationStep syntax next where
6562
MigrationStep :: Text -> Migration syntax a -> (a -> next) -> MigrationStep syntax next
6663
deriving instance Functor (MigrationStep syntax)
64+
65+
-- | A series of 'MigrationStep's that take a database from the schema in @from@
66+
-- to the one in @to@. Use the 'migrationStep' function and the arrow interface
67+
-- to sequence 'MigrationSteps'.
6768
newtype MigrationSteps syntax from to = MigrationSteps (Kleisli (F (MigrationStep syntax)) from to)
6869
deriving (Category, Arrow)
6970

71+
-- | Free monadic function for 'Migration's
7072
data MigrationF syntax next where
7173
MigrationRunCommand
7274
:: { _migrationUpCommand :: syntax {-^ What to execute when applying the migration -}
7375
, _migrationDownCommand :: Maybe syntax {-^ What to execute when unapplying the migration -}
7476
, _migrationNext :: next }
7577
-> MigrationF syntax next
7678
deriving instance Functor (MigrationF syntax)
79+
80+
-- | A sequence of potentially reversible schema update commands
7781
type Migration syntax = F (MigrationF syntax)
7882

7983
-- | Information on whether a 'MigrationCommand' loses data. You can
@@ -101,10 +105,14 @@ data MigrationCommand cmd
101105
-- ^ Information on whether the migration loses data
102106
} deriving Show
103107

108+
-- | Run the migration steps between the given indices, using a custom execution function.
104109
runMigrationSteps :: Monad m
105-
=> Int -> Maybe Int
106-
-> MigrationSteps syntax () a
110+
=> Int -- ^ Zero-based index of the first step to run
111+
-> Maybe Int -- ^ Index of the last step to run, or 'Nothing' to run every step
112+
-> MigrationSteps syntax () a -- ^ The set of steps to run
107113
-> (forall a'. Int -> Text -> Migration syntax a' -> m a')
114+
-- ^ Callback for each step. Called with the step index, the
115+
-- step description and the migration.
108116
-> m a
109117
runMigrationSteps firstIdx lastIdx (MigrationSteps steps) runMigration =
110118
runF (runKleisli steps ()) finish step 0
@@ -114,33 +122,37 @@ runMigrationSteps firstIdx lastIdx (MigrationSteps steps) runMigration =
114122
then runMigration i nm doStep >>= \x -> next x (i + 1)
115123
else next (runMigrationSilenced doStep) (i + 1)
116124

125+
-- | Get the result of a migration, without running any steps
117126
runMigrationSilenced :: Migration syntax a -> a
118127
runMigrationSilenced m = runF m id step
119128
where
120129
step (MigrationRunCommand _ _ next) = next
121130

122-
runMigrationVerbose :: MonadBeam syntax be hdl m => (syntax -> String)
123-
-> Migration syntax a -> m a
124-
runMigrationVerbose renderMigrationSyntax steps =
125-
runF steps finish step
126-
where finish = pure
127-
step (MigrationRunCommand up _ next) =
128-
do liftIO (putStrLn (renderMigrationSyntax up))
129-
runNoReturn up
130-
next
131-
131+
-- | Remove the explicit source and destination schemas from a 'MigrationSteps' object
132132
eraseMigrationType :: a -> MigrationSteps syntax a a' -> MigrationSteps syntax () ()
133133
eraseMigrationType a (MigrationSteps steps) = MigrationSteps (arr (const a) >>> steps >>> arr (const ()))
134134

135+
-- | Create a 'MigrationSteps' from the given description and migration function.
135136
migrationStep :: Text -> (a -> Migration syntax a') -> MigrationSteps syntax a a'
136137
migrationStep stepName migration =
137138
MigrationSteps (Kleisli (\a -> liftF (MigrationStep stepName (migration a) id)))
138139

140+
-- | Given a command in the forward direction, and an optional one in the
141+
-- reverse direction, construct a 'Migration' that performs the given
142+
-- command. Multiple commands can be sequenced monadically.
139143
upDown :: syntax -> Maybe syntax -> Migration syntax ()
140144
upDown up down = liftF (MigrationRunCommand up down ())
141145

142-
migrateScript :: forall syntax m a.
143-
Monoid m => (Text -> m) -> (syntax -> m) -> MigrationSteps syntax () a -> m
146+
-- | Given functions to render a migration step description and the underlying
147+
-- syntax, create a script for the given 'MigrationSteps'.
148+
migrateScript :: forall syntax m a. Monoid m
149+
=> (Text -> m)
150+
-- ^ Called at the beginning of each 'MigrationStep' with the step description
151+
-> (syntax -> m)
152+
-- ^ Called for each command in the migration step
153+
-> MigrationSteps syntax () a
154+
-- ^ The set of steps to run
155+
-> m
144156
migrateScript renderMigrationHeader renderMigrationSyntax (MigrationSteps steps) =
145157
runF (runKleisli steps ()) (\_ x -> x)
146158
(\(MigrationStep header migration next) x ->
@@ -168,16 +180,18 @@ migrationDataLoss go = runF go (\_ -> MigrationKeepsData)
168180
Nothing -> MigrationLosesData
169181
_ -> next)
170182

183+
-- | Run a 'MigrationSteps' without executing any of the commands against a
184+
-- database.
171185
evaluateDatabase :: forall syntax a. MigrationSteps syntax () a -> a
172186
evaluateDatabase (MigrationSteps f) = runF (runKleisli f ()) id (\(MigrationStep _ migration next) -> next (runMigration migration))
173187
where
174188
runMigration :: forall a'. Migration syntax a' -> a'
175189
runMigration migration = runF migration id (\(MigrationRunCommand _ _ next) -> next)
176190

191+
-- | Collect the names of all steps in hte given 'MigrationSteps'
177192
stepNames :: forall syntax a. MigrationSteps syntax () a -> [Text]
178193
stepNames (MigrationSteps f) = runF (runKleisli f ()) (\_ x -> x) (\(MigrationStep nm migration next) x -> next (runMigration migration) (x ++ [nm])) []
179194
where
180195
runMigration :: forall a'. Migration syntax a' -> a'
181196
runMigration migration = runF migration id (\(MigrationRunCommand _ _ next) -> next)
182197

183-
-- * Checked database entities

beam-postgres/Database/Beam/Postgres/Full.hs

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ instance Monoid (PgLockedTables s) where
6464
mempty = PgLockedTables []
6565
mappend (PgLockedTables a) (PgLockedTables b) = PgLockedTables (a <> b)
6666

67+
-- | Combines the result of a query along with a set of locked tables. Used as a
68+
-- return value for the 'lockingFor_' function.
6769
data PgWithLocking s a = PgWithLocking (PgLockedTables s) a
6870
instance ProjectibleWithPredicate c syntax a => ProjectibleWithPredicate c syntax (PgWithLocking s a) where
6971
project' p mutateM (PgWithLocking tbls a) =
@@ -73,7 +75,7 @@ instance ProjectibleWithPredicate c syntax a => ProjectibleWithPredicate c synta
7375
lockAll_ :: a -> PgWithLocking s a
7476
lockAll_ = PgWithLocking mempty
7577

76-
-- | Return and lock the given tables. Typically used as an infix operator. See
78+
-- | Return and lock the given tables. Typically used as an infix operator. See the
7779
-- <http://tathougies.github.io/beam/user-guide/backends/beam-postgres/ the user guide> for usage
7880
-- examples
7981
withLocks_ :: a -> PgLockedTables s -> PgWithLocking s a
@@ -89,8 +91,8 @@ locked_ (DatabaseEntity (DatabaseTable tblNm tblSettings)) = do
8991
pure (PgLockedTables [nm], joined)
9092

9193
-- | Lock some tables during the execution of a query. This is rather complicated, and there are
92-
-- several usage examples in <http://tathougies.github.io/beam/user-guide/backends/beam-postgres/
93-
-- the user guide>
94+
-- several usage examples in
95+
-- <http://tathougies.github.io/beam/user-guide/backends/beam-postgres/ the user guide>
9496
--
9597
-- The Postgres locking clause is rather complex, and beam currently does not check several
9698
-- pre-conditions. It is assumed you kinda know what you're doing.

beam-postgres/Database/Beam/Postgres/PgSpecific.hs

+34-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module Database.Beam.Postgres.PgSpecific
4949
, pgSumMoney_, pgAvgMoney_
5050

5151
-- ** Set-valued functions
52+
-- $set-valued-funs
5253
, PgSetOf, pgUnnest
5354
, pgUnnestArray, pgUnnestArrayWithOrdinality
5455

@@ -431,15 +432,20 @@ instance ToJSON a => HasSqlValueSyntax PgValueSyntax (PgJSONB a) where
431432
PgValueSyntax $
432433
emit "'" <> escapeString (BL.toStrict (encode a)) <> emit "'::jsonb"
433434

435+
-- | Key-value pair, used as output of 'pgJsonEachText' and 'pgJsonEach'
434436
data PgJSONEach valType f
435-
= PgJSONEach { pgJsonEachKey :: C f T.Text, pgJsonEachValue :: C f valType }
436-
deriving Generic
437+
= PgJSONEach
438+
{ pgJsonEachKey :: C f T.Text
439+
, pgJsonEachValue :: C f valType
440+
} deriving Generic
437441
instance Beamable (PgJSONEach valType)
438442

443+
-- | Output row of 'pgJsonKeys'
439444
data PgJSONKey f = PgJSONKey { pgJsonKey :: C f T.Text }
440445
deriving Generic
441446
instance Beamable PgJSONKey
442447

448+
-- | Output row of 'pgJsonArrayElements' and 'pgJsonArrayElementsText'
443449
data PgJSONElement a f = PgJSONElement { pgJsonElement :: C f a }
444450
deriving Generic
445451
instance Beamable (PgJSONElement a)
@@ -451,8 +457,6 @@ instance Beamable (PgJSONElement a)
451457
-- section on
452458
-- <https://www.postgresql.org/docs/current/static/functions-json.html JSON>.
453459
--
454-
-- Note that @beam-postgres@ does not yet support @setof@ so some functions are
455-
-- missing. PRs to implement this functionality are welcome!
456460
class IsPgJSON (json :: * -> *) where
457461
-- | The @json_each@ or @jsonb_each@ function. Values returned as @json@ or
458462
-- @jsonb@ respectively. Use 'pgUnnest' to join against the result
@@ -985,3 +989,29 @@ instance HasDefaultSqlDataTypeConstraints PgColumnSchemaSyntax (V.Vector a)
985989
-- For more information on Psotgres json support see the postgres
986990
-- <https://www.postgresql.org/docs/current/static/functions-json.html manual>.
987991

992+
993+
-- $set-valued-funs
994+
--
995+
-- Postgres supports functions that returns /sets/. We can join directly against
996+
-- these sets or arrays. @beam-postgres@ supports this feature via the
997+
-- 'pgUnnest' and 'pgUnnestArray' functions.
998+
--
999+
-- Any function that returns a set can be typed as an expression returning
1000+
-- 'PgSetOf'. This polymorphic type takes one argument, which is a 'Beamable'
1001+
-- type that represents the shape of the data in the rows. For example, the
1002+
-- @json_each@ function returns a key and a value, so the corresponding
1003+
-- @beam-postgres@ function ('pgJsonEach') returns a value of type 'PgSetOf
1004+
-- (PgJSONEach Value)', which represents a set containing 'PgJSONEach'
1005+
-- rows. 'PgJSONEach' is a table with a column for keys ('pgJsonEachKey') and
1006+
-- one for values ('pgJsonEachValue').
1007+
--
1008+
-- Any 'PgSetOf' value can be introduced into the 'Q' monad using the 'pgUnnest'
1009+
-- function.
1010+
--
1011+
-- Postgres arrays (represented by the 'V.Vector' type) can also be joined
1012+
-- against using the 'pgUnnestArray' function. This directly corresponds to the
1013+
-- SQL @UNNEST@ keyword. Unlike sets, arrays have a sense of order. The
1014+
-- 'pgUnnestArrayWithOrdinality' function allows you to join against the
1015+
-- elements of an array along with its index. This corresponds to the
1016+
-- @UNNEST .. WITH ORDINALITY@ clause.
1017+

0 commit comments

Comments
 (0)