ADD type for database configuration

This commit is contained in:
Nathan Dwarshuis 2022-12-11 18:53:54 -05:00
parent 5d6038292e
commit 68e4ce36ca
4 changed files with 22 additions and 12 deletions

View File

@ -88,7 +88,9 @@ sync = flag' Sync
) )
parse :: Options -> IO () parse :: Options -> IO ()
parse (Options _ Reset) = migrate_ nukeTables parse (Options c Reset) = do
config <- readConfig c
migrate_ (sqlConfig config) nukeTables
parse (Options c DumpAccounts) = runDumpAccounts c parse (Options c DumpAccounts) = runDumpAccounts c
parse (Options c DumpAccountKeys) = runDumpAccountKeys c parse (Options c DumpAccountKeys) = runDumpAccountKeys c
parse (Options c DumpCurrencies) = runDumpCurrencies c parse (Options c DumpCurrencies) = runDumpCurrencies c
@ -141,7 +143,7 @@ runDumpAccountKeys c = do
runSync :: FilePath -> IO () runSync :: FilePath -> IO ()
runSync c = do runSync c = do
config <- readConfig c config <- readConfig c
migrate_ $ do migrate_ (sqlConfig config) $ do
s <- getDBState config s <- getDBState config
flip runReaderT (s $ takeDirectory c) $ do flip runReaderT (s $ takeDirectory c) $ do
insertBudget $ budget config insertBudget $ budget config

View File

@ -66,6 +66,7 @@ typeSubs = firstOrder ++ higherOrder
, toVar (auto :: Decoder SplitNum) , toVar (auto :: Decoder SplitNum)
, toVar (auto :: Decoder MatchDesc) , toVar (auto :: Decoder MatchDesc)
, toVar (auto :: Decoder MatchOther) , toVar (auto :: Decoder MatchOther)
, toVar (auto :: Decoder SqlConfig)
] ]
readYaml :: FromJSON a => FilePath -> IO a readYaml :: FromJSON a => FilePath -> IO a

View File

@ -39,18 +39,20 @@ import Internal.Database.Model
import Internal.Types import Internal.Types
import Internal.Utils import Internal.Utils
migrate_ :: SqlPersistT (ResourceT (NoLoggingT IO)) () -> IO () migrate_ :: SqlConfig -> SqlPersistT (ResourceT (NoLoggingT IO)) () -> IO ()
migrate_ more = runNoLoggingT $ runResourceT migrate_ c more = runNoLoggingT $ runResourceT
$ withSqlConn openConnection (\backend -> $ withSqlConn (openConnection c) (\backend ->
flip runSqlConn backend $ do flip runSqlConn backend $ do
runMigration migrateAll runMigration migrateAll
more more
) )
openConnection :: LogFunc -> IO SqlBackend openConnection :: SqlConfig -> LogFunc -> IO SqlBackend
openConnection logfn = do openConnection c logfn = case c of
conn <- open "/tmp/test.db" Sqlite p -> do
conn <- open p
wrapConnection conn logfn wrapConnection conn logfn
Postgres -> error "postgres not implemented"
nukeTables :: MonadIO m => SqlPersistT m () nukeTables :: MonadIO m => SqlPersistT m ()
nukeTables = do nukeTables = do

View File

@ -34,6 +34,10 @@ import Text.Read
-- | YAML CONFIG -- | YAML CONFIG
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
data SqlConfig = Sqlite T.Text |
Postgres -- TODO
deriving (Generic, FromDhall)
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- | account tree -- | account tree
@ -86,6 +90,7 @@ data Config_ a = Config_
, currencies :: ![Currency] , currencies :: ![Currency]
, statements :: ![Statement] , statements :: ![Statement]
, accounts :: a , accounts :: a
, sqlConfig :: !SqlConfig
} }
deriving (Generic) deriving (Generic)