module Database.Beam.Migrate.Simple
( simpleSchema, simpleMigration
, runSimpleMigration, backendMigrationScript
, module Database.Beam.Migrate.Actions
, module Database.Beam.Migrate.Types ) where
import Database.Beam
import Database.Beam.Backend.SQL
import Database.Beam.Migrate.Backend
import Database.Beam.Migrate.Types
import Database.Beam.Migrate.Actions
import qualified Data.Text as T
simpleSchema :: Database db
=> ActionProvider cmd
-> CheckedDatabaseSettings be db
-> Maybe [cmd]
simpleSchema provider settings =
let allChecks = collectChecks settings
solver = heuristicSolver provider [] allChecks
in case finalSolution solver of
Solved cmds -> Just cmds
Candidates {} -> Nothing
simpleMigration :: ( MonadBeam cmd be handle m
, Database db )
=> BeamMigrationBackend be cmd handle
-> handle
-> CheckedDatabaseSettings be db
-> IO (Maybe [cmd])
simpleMigration BeamMigrationBackend { backendGetDbConstraints = getCs
, backendActionProvider = action } hdl db = do
pre <- withDatabase hdl getCs
let post = collectChecks db
solver = heuristicSolver action pre post
case finalSolution solver of
Solved cmds -> pure (Just cmds)
Candidates {} -> pure Nothing
runSimpleMigration :: MonadBeam cmd be hdl m
=> hdl -> [cmd] -> IO ()
runSimpleMigration hdl =
withDatabase hdl . mapM_ runNoReturn
backendMigrationScript :: (cmd -> String)
-> Migration cmd a
-> String
backendMigrationScript render mig =
migrateScript ((++"\n") . T.unpack) ((++"\n") . render) (migrationStep "Migration Script" (\() -> mig))