Skip to content

Commit 5ecb5c9

Browse files
committed
Add convenience accessors
1 parent 17ea69c commit 5ecb5c9

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

beam-core/Database/Beam/Schema/Tables.hs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Database.Beam.Schema.Tables
1919
, DatabaseEntityDescriptor(..)
2020
, DatabaseEntity(..), TableEntity, ViewEntity, DomainTypeEntity
2121
, dbEntityDescriptor
22+
, dbName, dbSchema, dbTableFields
2223
, DatabaseModification, EntityModification(..)
2324
, FieldModification(..)
2425
, dbModification, tableModification, withDbModification
@@ -96,7 +97,6 @@ import GHC.TypeLits
9697
import GHC.Types
9798

9899
import Lens.Micro hiding (to)
99-
import qualified Lens.Micro as Lens
100100

101101
-- | Allows introspection into database types.
102102
--
@@ -311,7 +311,7 @@ class RenamableWithRule (FieldRenamer (DatabaseEntityDescriptor be entityType))
311311
type DatabaseEntityRegularRequirements be entityType :: Constraint
312312

313313
dbEntityName :: Lens' (DatabaseEntityDescriptor be entityType) Text
314-
dbEntitySchema :: Traversal' (DatabaseEntityDescriptor be entityType) (Maybe Text)
314+
dbEntitySchema :: Lens' (DatabaseEntityDescriptor be entityType) (Maybe Text)
315315

316316
dbEntityAuto :: DatabaseEntityDefaultRequirements be entityType =>
317317
Text -> DatabaseEntityDescriptor be entityType
@@ -387,7 +387,7 @@ instance IsDatabaseEntity be (DomainTypeEntity ty) where
387387
type DatabaseEntityRegularRequirements be (DomainTypeEntity ty) = ()
388388

389389
dbEntityName f (DatabaseDomainType s t) = DatabaseDomainType s <$> f t
390-
dbEntitySchema f (DatabaseDomainType s t) = DatabaseDomainType <$> f s <*> pure t
390+
dbEntitySchema f (DatabaseDomainType s t) = (\s' -> DatabaseDomainType s' t) <$> f s
391391
dbEntityAuto = DatabaseDomainType Nothing
392392

393393
-- | Represents a meta-description of a particular entityType. Mostly, a wrapper
@@ -398,8 +398,21 @@ data DatabaseEntity be (db :: (Type -> Type) -> Type) entityType where
398398
IsDatabaseEntity be entityType =>
399399
DatabaseEntityDescriptor be entityType -> DatabaseEntity be db entityType
400400

401-
dbEntityDescriptor :: SimpleGetter (DatabaseEntity be db entityType) (DatabaseEntityDescriptor be entityType)
402-
dbEntityDescriptor = Lens.to (\(DatabaseEntity e) -> e)
401+
dbEntityDescriptor :: Lens' (DatabaseEntity be db entityType) (DatabaseEntityDescriptor be entityType)
402+
dbEntityDescriptor f (DatabaseEntity d) = DatabaseEntity <$> f d
403+
404+
dbName :: IsDatabaseEntity be entityType => Lens' (DatabaseEntity be db entityType) Text
405+
dbName = dbEntityDescriptor . dbEntityName
406+
407+
dbSchema :: IsDatabaseEntity be entityType => Lens' (DatabaseEntity be db entityType) (Maybe Text)
408+
dbSchema = dbEntityDescriptor . dbEntitySchema
409+
410+
dbTableFields :: Lens' (DatabaseEntity be db (TableEntity table)) (TableSettings table)
411+
dbTableFields = dbEntityDescriptor . (\f DatabaseTable { dbTableSchema = sch
412+
, dbTableOrigName = nm
413+
, dbTableCurrentName = curNm
414+
, dbTableSettings = s } ->
415+
DatabaseTable sch nm curNm <$> f s)
403416

404417
-- | When parameterized by this entity tag, a database type will hold
405418
-- meta-information on the Haskell mappings of database entities. Under the

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Database.Beam.Migrate.Backend
3434
, sql92HsPredicateConverters
3535
, hasColumnConverter
3636
, trivialHsConverter, hsPredicateConverter
37+
, withExtraPredicateParsers
3738

3839
-- * For tooling authors
3940
, SomeBeamMigrationBackend(..), SomeCheckedDatabaseSettings(..) )
@@ -93,6 +94,9 @@ data BeamMigrationBackend be m where
9394
, backendConnect :: String -> IO (BeamMigrateConnection be m)
9495
} -> BeamMigrationBackend be m
9596

97+
withExtraPredicateParsers :: BeamMigrationBackend be m -> BeamDeserializers be -> BeamMigrationBackend be m
98+
withExtraPredicateParsers be ds = be { backendPredicateParsers = backendPredicateParsers be <> ds }
99+
96100
data BeamMigrateConnection be m where
97101
BeamMigrateConnection
98102
:: { backendRun :: forall a. m a -> IO (Either DdlError a)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module Database.Beam.Migrate.Types
1313
, CheckedDatabaseEntity(..)
1414

1515
, unCheckDatabase, collectChecks
16-
, renameCheckedEntity
16+
, unCheckedDbLens
17+
, renameCheckedEntity, checkedDbDescriptor
1718

1819
-- ** Modifyinging checked entities
1920
--

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ data CheckedDatabaseEntity be (db :: (Type -> Type) -> Type) entityType where
6363
-> [ SomeDatabasePredicate ]
6464
-> CheckedDatabaseEntity be db entityType
6565

66+
checkedDbDescriptor :: Lens' (CheckedDatabaseEntity be db entityType)
67+
(CheckedDatabaseEntityDescriptor be entityType)
68+
checkedDbDescriptor fn (CheckedDatabaseEntity f ps) =
69+
(\f' -> CheckedDatabaseEntity f' ps) <$> fn f
70+
6671
-- | The type of a checked database descriptor. Conceptually, this is just a
6772
-- 'DatabaseSettings' with a set of predicates. Use 'unCheckDatabase' to get the
6873
-- regular 'DatabaseSettings' object and 'collectChecks' to access the
@@ -78,6 +83,10 @@ renameCheckedEntity renamer =
7883
unCheckDatabase :: forall be db. Database be db => CheckedDatabaseSettings be db -> DatabaseSettings be db
7984
unCheckDatabase db = runIdentity $ zipTables (Proxy @be) (\(CheckedDatabaseEntity x _) _ -> pure $ DatabaseEntity (unCheck x)) db db
8085

86+
unCheckedDbLens :: forall be db. Database be db => Lens' (CheckedDatabaseSettings be db) (DatabaseSettings be db)
87+
unCheckedDbLens f db =
88+
(\db' -> runIdentity (zipTables (Proxy @be) (\(CheckedDatabaseEntity d cks) d' -> pure (CheckedDatabaseEntity (d & unChecked .~ (d' ^. dbEntityDescriptor)) cks)) db db')) <$> f (unCheckDatabase db)
89+
8190
-- | A @beam-migrate@ database schema is defined completely by the set of
8291
-- predicates that apply to it. This function allows you to access this
8392
-- definition for a 'CheckedDatabaseSettings' object.

0 commit comments

Comments
 (0)