Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Database.Beam.Migrate.Simple #421

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions beam-migrate/Database/Beam/Migrate/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
-- | Utility functions for common use cases
module Database.Beam.Migrate.Simple
( autoMigrate
, planMigration
, describeMigrationPlan
, executeMigrationPlan
, autoMigrateVerbose
, simpleSchema
, simpleMigration
, runSimpleMigration
Expand Down Expand Up @@ -211,18 +215,54 @@ autoMigrate :: (Database be db, Fail.MonadFail m)
=> BeamMigrationBackend be m
-> CheckedDatabaseSettings be db
-> m ()
autoMigrate BeamMigrationBackend { backendActionProvider = actions
, backendGetDbConstraints = getCs }
autoMigrate b@(BeamMigrationBackend {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs @(BeamMigrationBackend {})?

db =
do cmds <- planMigration b db
executeMigrationPlan b cmds

planMigration :: (Database be db, Fail.MonadFail m)
=> BeamMigrationBackend be m
-> CheckedDatabaseSettings be db
-> m [MigrationCommand be]
planMigration BeamMigrationBackend { backendActionProvider = actions
, backendGetDbConstraints = getCs }
db =
do actual <- getCs
let expected = collectChecks db
case finalSolution (heuristicSolver actions actual expected) of
Candidates {} -> Fail.fail "autoMigrate: Could not determine migration"
Solved cmds ->
-- Check if any of the commands are irreversible
case foldMap migrationCommandDataLossPossible cmds of
MigrationKeepsData -> mapM_ (runNoReturn . migrationCommand) cmds
_ -> Fail.fail "autoMigrate: Not performing automatic migration due to data loss"
Candidates {} -> Fail.fail "planMigration: Could not determine migration"
Solved cmds -> return cmds

describeMigrationPlan :: Sql92DisplaySyntax (BeamSqlBackendSyntax be)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type signature and implementation of this are both pretty specific to the use in autoMigrateVerbose. I think we should not export it.

=> [MigrationCommand be]
-> [String]
describeMigrationPlan = map $ \cmd -> safety cmd <> displaySyntax (migrationCommand cmd)
where
safety cmd = case migrationCommandDataLossPossible cmd of
MigrationLosesData -> "<UNSAFE> "
MigrationKeepsData -> " "

executeMigrationPlan :: Fail.MonadFail m
=> BeamMigrationBackend be m
-> [MigrationCommand be]
-> m ()
executeMigrationPlan (BeamMigrationBackend {}) cmds =
-- Check if any of the commands are irreversible
case foldMap migrationCommandDataLossPossible cmds of
MigrationKeepsData -> mapM_ (runNoReturn . migrationCommand) cmds
_ -> Fail.fail "executeMigrationPlan: Not performing automatic migration due to data loss"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this should match MigrationKeepsData explicitly.


autoMigrateVerbose :: ( Database be db, Sql92DisplaySyntax (BeamSqlBackendSyntax be), MonadIO m
, Fail.MonadFail m
)
=> BeamMigrationBackend be m
-> CheckedDatabaseSettings be db
-> m ()
autoMigrateVerbose b@(BeamMigrationBackend {})
db =
do cmds <- planMigration b db
liftIO $ putStr $ unlines $ describeMigrationPlan cmds
executeMigrationPlan b cmds

-- | Given a migration backend, a handle to a database, and a checked database,
-- attempt to find a schema. This should always return 'Just', unless the
Expand Down