yammat/Migration/0.0.0-0.0.1/Migration.hs

50 lines
1.4 KiB
Haskell
Raw Normal View History

2015-09-17 13:27:53 +00:00
import Database.HDBC
import Database.HDBC.PostgreSQL
import System.IO
import Control.Exception
import Data.ByteString.Base64
import qualified Crypto.Hash.SHA3 as SHA3
main :: IO ()
main = do
putStrLn "Enter database host"
dbHost <- getLine
putStrLn "Enter database port"
dbPort <- getLine
putStrLn "Enter database user"
dbUser <- getLine
putStrLn "Enter database name"
dbName <- getLine
putStrLn "Enter database password"
dbPasswd <- getPasswd
let dbString = "host=" ++ dbHost ++ " port=" ++ dbPort ++ " user=" ++ dbUser ++ " dbname=" ++ dbName ++ " password=" ++ dbPasswd
conn <- connectPostgreSQL dbString
2015-09-17 14:06:50 +00:00
stmt1 <- prepare conn "select * from avatar"
2015-09-17 13:27:53 +00:00
_ <- execute stmt1 []
rows <- fetchAllRowsAL stmt1
tups <- return $ map (\entry ->
case entry of
[("id", theId), ("ident", _), ("data", SqlByteString theData), ("hash", _)] ->
2015-09-17 14:15:46 +00:00
[SqlByteString $ encode $ SHA3.hash 32 theData, theId]
2015-09-17 13:27:53 +00:00
_ ->
error "malformed entry"
) rows
stmt2 <- prepare conn "update avatar set hash = ? where id = ?"
executeMany stmt2 tups
commit conn
disconnect conn
putStrLn "Migration successfull!!"
getPasswd :: IO String
getPasswd = do
putStr "Password: "
hFlush stdout
pass <- withEcho False getLine
putChar '\n'
return pass
withEcho :: Bool -> IO a -> IO a
withEcho echo action = do
old <- hGetEcho stdin
bracket_ (hSetEcho stdin echo) (hSetEcho stdin old) action