2015-10-18 03:00:14 +00:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2015-10-18 03:03:12 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-10-18 02:30:14 +00:00
|
|
|
module Migrate2 where
|
|
|
|
|
|
|
|
import Prelude
|
2015-10-18 23:20:20 +00:00
|
|
|
import Yesod.Core (liftIO)
|
2015-10-18 02:30:14 +00:00
|
|
|
import Database.HDBC
|
|
|
|
import Database.HDBC.PostgreSQL
|
|
|
|
import System.IO
|
|
|
|
import Control.Exception
|
|
|
|
import qualified Data.Text as T
|
2015-10-18 23:36:13 +00:00
|
|
|
import Data.Text.Encoding (decodeUtf8)
|
2015-10-18 02:30:14 +00:00
|
|
|
import qualified Data.ByteString.Char8 as B
|
2015-10-18 23:59:18 +00:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BL
|
2015-10-18 02:30:14 +00:00
|
|
|
import Data.Aeson as A
|
|
|
|
import Data.Time.Clock
|
|
|
|
import Data.Yaml as Y
|
|
|
|
import Data.Time.LocalTime
|
|
|
|
import Database.Bloodhound
|
|
|
|
import Network.HTTP.Client
|
2015-10-18 23:50:37 +00:00
|
|
|
import Network.HTTP.Types.Status
|
2015-10-18 02:30:14 +00:00
|
|
|
|
|
|
|
data ESSettings = ESSettings
|
|
|
|
{ esHost :: T.Text
|
|
|
|
, esShards :: Int
|
|
|
|
, esReplicas :: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
instance A.FromJSON ESSettings where
|
|
|
|
parseJSON = withObject "AppSettings" $ \o -> do
|
|
|
|
esHost <- o .: "searchhost"
|
|
|
|
esShards <- o .: "shards"
|
|
|
|
esReplicas <- o .: "replicas"
|
|
|
|
return ESSettings {..}
|
|
|
|
|
|
|
|
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
|
|
|
|
putStrLn "Enter location of eidolon settings.yml file"
|
|
|
|
settingsFP <- getLine
|
|
|
|
let dbString = "host=" ++ dbHost ++ " port=" ++ dbPort ++ " user=" ++ dbUser ++ " dbname=" ++ dbName ++ " password=" ++ dbPasswd
|
|
|
|
conn <- connectPostgreSQL dbString
|
|
|
|
settingsY <- decodeFile settingsFP :: IO (Maybe ESSettings)
|
|
|
|
elastic <- case settingsY of
|
|
|
|
Just el -> return el
|
|
|
|
Nothing -> error $ "Could not read settings from " ++ settingsFP
|
|
|
|
let server = Server $ esHost elastic
|
|
|
|
let indexSettings = IndexSettings (ShardCount $ esShards elastic) (ReplicaCount $ esReplicas elastic)
|
|
|
|
userStmt <- prepare conn "select * from \"user\""
|
|
|
|
_ <- execute userStmt []
|
|
|
|
userRows <- fetchAllRowsAL userStmt
|
|
|
|
albumStmt <- prepare conn "select * from \"album\""
|
|
|
|
_ <- execute albumStmt []
|
|
|
|
albumRows <- fetchAllRowsAL albumStmt
|
|
|
|
mediumStmt <- prepare conn "select * from \"medium\""
|
|
|
|
_ <- execute mediumStmt []
|
|
|
|
mediumRows <- fetchAllRowsAL mediumStmt
|
|
|
|
commentStmt <- prepare conn "select * from \"comment\""
|
|
|
|
_ <- execute commentStmt []
|
|
|
|
commentRows <- fetchAllRowsAL commentStmt
|
|
|
|
_ <- withBH defaultManagerSettings server $ do
|
|
|
|
_ <- createIndex indexSettings (IndexName "user")
|
|
|
|
_ <- createIndex indexSettings (IndexName "album")
|
|
|
|
_ <- createIndex indexSettings (IndexName "medium")
|
|
|
|
_ <- createIndex indexSettings (IndexName "comment")
|
2015-10-25 21:23:00 +00:00
|
|
|
_ <- mapM (\entry ->
|
2015-10-18 02:30:14 +00:00
|
|
|
case entry of
|
|
|
|
[("id", SqlInteger theId), ("name", SqlByteString name), ("slug", SqlByteString slug), _, _, _, _, _] -> do
|
2015-10-18 23:36:13 +00:00
|
|
|
let u = SUser (decodeUtf8 name) (decodeUtf8 slug)
|
2015-10-18 02:30:14 +00:00
|
|
|
let dId = DocId $ T.pack $ show theId
|
2015-10-18 23:20:20 +00:00
|
|
|
liftIO $ putStrLn $ (show u) ++ "\n"
|
2015-10-18 23:50:37 +00:00
|
|
|
resp <- indexDocument (IndexName "user") (MappingName "user") defaultIndexDocumentSettings u dId
|
|
|
|
case statusCode (responseStatus resp) of
|
2015-10-18 23:59:18 +00:00
|
|
|
200 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
201 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
_ -> liftIO $ putStrLn $ "!!Failure!!: " ++ (BL.unpack $ responseBody resp)
|
2015-10-18 02:30:14 +00:00
|
|
|
bla ->
|
|
|
|
error $ "malformed entry" ++ show bla
|
|
|
|
) userRows
|
2015-10-25 21:23:00 +00:00
|
|
|
_ <- mapM (\entry ->
|
2015-10-18 02:30:14 +00:00
|
|
|
case entry of
|
|
|
|
[("id", SqlInteger theId), ("title", SqlByteString title), _, _, _, _, _] -> do
|
2015-10-18 23:36:13 +00:00
|
|
|
let a = SAlbum (decodeUtf8 title)
|
2015-10-18 02:30:14 +00:00
|
|
|
let dId = DocId $ T.pack $ show theId
|
2015-10-18 23:20:20 +00:00
|
|
|
liftIO $ putStrLn $ (show a) ++ "\n"
|
2015-10-18 23:50:37 +00:00
|
|
|
resp <- indexDocument (IndexName "album") (MappingName "album") defaultIndexDocumentSettings a dId
|
|
|
|
case statusCode (responseStatus resp) of
|
2015-10-18 23:59:18 +00:00
|
|
|
200 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
201 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
_ -> liftIO $ putStrLn $ "!!Failure!!: " ++ (BL.unpack $ responseBody resp)
|
2015-10-18 02:30:14 +00:00
|
|
|
bla ->
|
|
|
|
error $ "malformed entry: " ++ show bla
|
|
|
|
) albumRows
|
2015-10-25 21:23:00 +00:00
|
|
|
_ <- mapM (\entry ->
|
2015-10-18 02:30:14 +00:00
|
|
|
case entry of
|
|
|
|
[("id", SqlInteger theId), ("title", SqlByteString title), _, _, _, ("time", SqlZonedTime time), _, ("description", SqlByteString desc), ("tags", SqlByteString tags), _, _, _, _, _] -> do
|
2015-10-18 23:36:13 +00:00
|
|
|
let m = SMedium (decodeUtf8 title) (zonedTimeToUTC time) (decodeUtf8 desc) (parseTags tags)
|
2015-10-18 02:30:14 +00:00
|
|
|
let dId = DocId $ T.pack $ show theId
|
2015-10-18 23:20:20 +00:00
|
|
|
liftIO $ putStrLn $ (show m) ++ "\n"
|
2015-10-18 23:50:37 +00:00
|
|
|
resp <- indexDocument (IndexName "medium") (MappingName "medium") defaultIndexDocumentSettings m dId
|
|
|
|
case statusCode (responseStatus resp) of
|
2015-10-18 23:59:18 +00:00
|
|
|
200 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
201 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
_ -> liftIO $ putStrLn $ "!!Failure!!: " ++ (BL.unpack $ responseBody resp)
|
2015-10-18 02:30:14 +00:00
|
|
|
bla ->
|
|
|
|
error $ "malformed entry" ++ show bla
|
|
|
|
) mediumRows
|
2015-10-25 21:23:00 +00:00
|
|
|
_ <- mapM (\entry ->
|
2015-10-18 02:30:14 +00:00
|
|
|
case entry of
|
|
|
|
[("id", SqlInteger theId), _, ("author_slug", SqlByteString author), _, _, ("time", SqlZonedTime time), ("content", SqlByteString content)] -> do
|
2015-10-18 23:36:13 +00:00
|
|
|
let c = SComment (decodeUtf8 author) (zonedTimeToUTC time) (decodeUtf8 content)
|
2015-10-18 02:30:14 +00:00
|
|
|
let dId = DocId $ T.pack $ show theId
|
2015-10-18 23:20:20 +00:00
|
|
|
liftIO $ putStrLn $ (show c) ++ "\n"
|
2015-10-19 00:10:31 +00:00
|
|
|
resp <- indexDocument (IndexName "comment") (MappingName "comment") defaultIndexDocumentSettings c dId
|
2015-10-18 23:50:37 +00:00
|
|
|
case statusCode (responseStatus resp) of
|
2015-10-18 23:59:18 +00:00
|
|
|
200 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
201 -> liftIO $ putStrLn $ "OK: " ++ (BL.unpack $ responseBody resp)
|
|
|
|
_ -> liftIO $ putStrLn $ "!!Failure!!: " ++ (BL.unpack $ responseBody resp)
|
2015-10-18 02:30:14 +00:00
|
|
|
bla ->
|
|
|
|
error $ "malformed entry" ++ show bla
|
|
|
|
) commentRows
|
|
|
|
return ()
|
|
|
|
putStrLn "Migration successful!!"
|
|
|
|
|
|
|
|
data SUser = SUser
|
|
|
|
{ suName :: T.Text
|
|
|
|
, suSlug :: T.Text
|
2015-10-18 23:20:20 +00:00
|
|
|
} deriving Show
|
2015-10-18 02:30:14 +00:00
|
|
|
|
|
|
|
instance A.ToJSON SUser where
|
|
|
|
toJSON (SUser n s) = object
|
|
|
|
[ "name" .= n
|
|
|
|
, "slug" .= s
|
|
|
|
]
|
|
|
|
|
|
|
|
data SAlbum = SAlbum
|
2015-10-18 23:20:20 +00:00
|
|
|
{ saName :: T.Text } deriving Show
|
2015-10-18 02:30:14 +00:00
|
|
|
|
|
|
|
instance A.ToJSON SAlbum where
|
|
|
|
toJSON (SAlbum n) = object
|
|
|
|
[ "name" .= n ]
|
|
|
|
|
|
|
|
data SMedium = SMedium
|
|
|
|
{ smName :: T.Text
|
|
|
|
, smTime :: UTCTime
|
|
|
|
, smDesc :: T.Text
|
|
|
|
, smTags :: [T.Text]
|
2015-10-18 23:20:20 +00:00
|
|
|
} deriving Show
|
2015-10-18 02:30:14 +00:00
|
|
|
|
|
|
|
instance A.ToJSON SMedium where
|
|
|
|
toJSON (SMedium n t d g) = object
|
|
|
|
[ "name" .= n
|
|
|
|
, "time" .= t
|
|
|
|
, "description" .= d
|
|
|
|
, "tags" .= g
|
|
|
|
]
|
|
|
|
|
|
|
|
data SComment = SComment
|
|
|
|
{ scAuthor :: T.Text
|
|
|
|
, scTime :: UTCTime
|
|
|
|
, scContent :: T.Text
|
2015-10-18 23:20:20 +00:00
|
|
|
} deriving Show
|
2015-10-18 02:30:14 +00:00
|
|
|
|
|
|
|
instance A.ToJSON SComment where
|
|
|
|
toJSON (SComment a t c) = object
|
|
|
|
[ "author" .= a
|
|
|
|
, "time" .= t
|
|
|
|
, "content" .= c
|
|
|
|
]
|
|
|
|
|
|
|
|
parseTags :: B.ByteString -> [T.Text]
|
|
|
|
parseTags bs = map handle' inner
|
|
|
|
where
|
|
|
|
inner = T.splitOn "," $ T.pack $ B.unpack $ B.init $ B.tail bs
|
2015-10-18 23:24:43 +00:00
|
|
|
handle' = T.dropEnd 1 . T.drop 2
|
2015-10-18 02:30:14 +00:00
|
|
|
|
|
|
|
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
|