-- |
-- Module: Clear
-- Copyright: (c) 2015 Mark Fine
-- License: MIT
-- Maintainer: Mark Fine <mark.fine@gmail.com>
-- Stability: experimental
-- Portability: portable
--
-- Tool for clearing PostgreSQL migrations.
import BasicPrelude
import Data.Text ( pack )
import Database.PostgreSQL.Schema
import Options.Applicative
import Shelly
import System.Environment
import System.IO hiding ( putStr, getLine )
data Args = Args
{ aUrl :: Maybe String
} deriving ( Eq, Read, Show )
args :: ParserInfo Args
args =
info ( helper <*> args' )
( fullDesc
<> header "schema-apply: Apply Schema to PostgreSQL Database"
<> progDesc "Apply Schema" ) where
args' = Args
<$> optional ( strOption
( long "url"
<> metavar "URL"
<> help "Database URL" ) )
prompt :: String -> IO Bool
prompt url = do
putStr $ "Really drop " <> pack url <> "? [y]: "
hFlush stdout
(== "y") <$> getLine
exec :: String -> IO ()
exec url = do
y <- prompt url
when y $ shelly $
clear schema (pack url) where
schema = "schema_evolution_manager"
main :: IO ()
main =
execParser args >>= call where
call Args{..} = do
url <- lookupEnv "DATABASE_URL"
maybe (err "No Database URL") exec (aUrl <|> url) where
err = hPutStrLn stderr