-
Notifications
You must be signed in to change notification settings - Fork 171
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ | |
-- | Utility functions for common use cases | ||
module Database.Beam.Migrate.Simple | ||
( autoMigrate | ||
, planMigration | ||
, describeMigrationPlan | ||
, executeMigrationPlan | ||
, autoMigrateVerbose | ||
, simpleSchema | ||
, simpleMigration | ||
, runSimpleMigration | ||
|
@@ -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 {}) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
=> [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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this should match |
||
|
||
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 | ||
|
There was a problem hiding this comment.
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 {})
?