espial/app/migration/Main.hs

95 lines
2.9 KiB
Haskell
Raw Normal View History

2019-01-31 02:54:47 +00:00
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Main where
import Types
import Model
import ModelCustom
import qualified Database.Persist as P
import qualified Database.Persist.Sqlite as P
import ClassyPrelude
import Lens.Micro
import Options.Generic
data MigrationOpts
2020-03-30 17:06:21 +00:00
= CreateDB { conn :: Text }
2019-01-31 02:54:47 +00:00
| CreateUser { conn :: Text
, userName :: Text
, userPassword :: Text
2020-03-30 17:06:21 +00:00
, privateDefault :: Maybe Bool
, archiveDefault :: Maybe Bool
, privacyLock :: Maybe Bool }
2019-01-31 02:54:47 +00:00
| DeleteUser { conn :: Text
2020-03-30 17:06:21 +00:00
, userName :: Text }
2019-01-31 02:54:47 +00:00
| ImportBookmarks { conn :: Text
, userName :: Text
2020-03-30 17:06:21 +00:00
, bookmarkFile :: FilePath }
2019-03-09 06:21:41 +00:00
| ExportBookmarks { conn :: Text
, userName :: Text
2020-03-30 17:06:21 +00:00
, bookmarkFile :: FilePath }
2019-01-31 02:54:47 +00:00
| ImportNotes { conn :: Text
, userName :: Text
2020-03-30 17:06:21 +00:00
, noteDirectory :: FilePath }
| PrintMigrateDB { conn :: Text }
2019-01-31 02:54:47 +00:00
deriving (Generic, Show)
instance ParseRecord MigrationOpts
main :: IO ()
main = do
args <- getRecord "Migrations"
case args of
2020-03-30 17:06:21 +00:00
PrintMigrateDB {..} ->
2019-01-31 02:54:47 +00:00
P.runSqlite conn dumpMigration
2020-03-30 17:06:21 +00:00
CreateDB {..} -> do
2019-01-31 02:54:47 +00:00
let connInfo = P.mkSqliteConnectionInfo conn
& set P.fkEnabled False
P.runSqliteInfo connInfo runMigrations
2020-03-30 17:06:21 +00:00
CreateUser{..} ->
2019-01-31 02:54:47 +00:00
P.runSqlite conn $ do
2020-03-30 17:06:21 +00:00
hash' <- liftIO (hashPassword userPassword)
2019-01-31 02:54:47 +00:00
void $ P.upsertBy
2020-03-30 17:06:21 +00:00
(UniqueUserName userName)
(User userName hash' Nothing False False False)
2019-01-31 02:54:47 +00:00
[ UserPasswordHash P.=. hash'
2020-03-30 17:06:21 +00:00
, UserApiToken P.=. Nothing
, UserPrivateDefault P.=. fromMaybe False privateDefault
, UserArchiveDefault P.=. fromMaybe False archiveDefault
, UserPrivacyLock P.=. fromMaybe False privacyLock
2019-01-31 02:54:47 +00:00
]
pure () :: DB ()
2020-03-30 17:06:21 +00:00
DeleteUser {..} ->
2019-01-31 02:54:47 +00:00
P.runSqlite conn $ do
2020-03-30 17:06:21 +00:00
muser <- P.getBy (UniqueUserName userName)
2019-01-31 02:54:47 +00:00
case muser of
2020-03-30 17:06:21 +00:00
Nothing -> liftIO (print (userName ++ "not found"))
2019-01-31 02:54:47 +00:00
Just (P.Entity uid _) -> do
P.deleteCascade uid
pure () :: DB ()
2020-03-30 17:06:21 +00:00
ImportBookmarks {..} ->
2019-01-31 02:54:47 +00:00
P.runSqlite conn $ do
2020-03-30 17:06:21 +00:00
muser <- P.getBy (UniqueUserName userName)
2019-01-31 02:54:47 +00:00
case muser of
2020-03-30 17:06:21 +00:00
Just (P.Entity uid _) -> insertFileBookmarks uid bookmarkFile
Nothing -> liftIO (print (userName ++ "not found"))
2019-01-31 02:54:47 +00:00
2020-03-30 17:06:21 +00:00
ExportBookmarks {..} ->
2019-03-09 06:21:41 +00:00
P.runSqlite conn $ do
2020-03-30 17:06:21 +00:00
muser <- P.getBy (UniqueUserName userName)
2019-03-09 06:21:41 +00:00
case muser of
2020-03-30 17:06:21 +00:00
Just (P.Entity uid _) -> exportFileBookmarks uid bookmarkFile
Nothing -> liftIO (print (userName ++ "not found"))
2019-03-09 06:21:41 +00:00
2020-03-30 17:06:21 +00:00
ImportNotes {..} ->
2019-01-31 02:54:47 +00:00
P.runSqlite conn $ do
2020-03-30 17:06:21 +00:00
muser <- P.getBy (UniqueUserName userName)
2019-01-31 02:54:47 +00:00
case muser of
2020-03-30 17:06:21 +00:00
Just (P.Entity uid _) -> insertDirFileNotes uid noteDirectory
Nothing -> liftIO (print (userName ++ "not found"))