Skip to content

Commit 1dc6498

Browse files
committedNov 26, 2022
Allow overriding database with BABEL_DATABASE env var.
1 parent f2c0672 commit 1dc6498

File tree

7 files changed

+47
-46
lines changed

7 files changed

+47
-46
lines changed
 

‎app/Main.hs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
2-
{-# LANGUAGE TemplateHaskell #-}
2+
{-# LANGUAGE TemplateHaskell #-}
33
module Main (main) where
44

5-
import Application
6-
import Import
7-
import Import.Main
8-
import RIO.Process
9-
import Options.Applicative.Simple
5+
import Application
6+
import Import
7+
import Import.Main
8+
import Options.Applicative.Simple
109
import qualified Paths_babel_cards
11-
import System.Environment.XDG.BaseDir
10+
import RIO.Process
1211

1312
main :: IO ()
1413
main = do
@@ -25,19 +24,18 @@ main = do
2524

2625
lo <- logOptionsHandle stderr (optionsVerbose options)
2726
pc <- mkDefaultProcessContext
28-
settings <- loadSettings
27+
dbSettings <- loadSettings
2928
config <- loadConfig
3029
rngTV <- newTFGen >>= newTVarIO
31-
dataDir <- getUserDataDir "babel-cards"
32-
connPool <- createConnPool dataDir settings
30+
connPool <- createConnPool dbSettings
3331
withLogFunc lo $ \lf ->
3432
let babel = Babel
3533
{ bLogFunc = lf
3634
, bProcessContext = pc
3735
, bOptions = options
3836
, bRNG = rngTV
3937
, bConfig = config
40-
, bEmbeddedSettings = settings
38+
, bDatabaseSettings = dbSettings
4139
, bConnPool = connPool
4240
}
4341
in runRIO babel bootstrapBabel

‎babel-cards.cabal

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.33.0.
3+
-- This file has been generated from package.yaml by hpack version 0.35.0.
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 9fde3af3248564b38705619462ce38aca0340fe72457770b3ee367564ef4e1c4
7+
-- hash: 8a344dcc26aef96c7290cf75ca42ad8bcfb97dbaef4eed426e446c9ea3d8be76
88

99
name: babel-cards
1010
version: 0.1.0
@@ -78,6 +78,7 @@ library
7878
, tf-random
7979
, transformers
8080
, vty
81+
, xdg-basedir
8182
, yaml
8283
default-language: Haskell2010
8384

@@ -93,5 +94,4 @@ executable babel-cards
9394
, base >=4.11 && <10
9495
, optparse-simple
9596
, rio >=0.1.12.0
96-
, xdg-basedir
9797
default-language: Haskell2010

‎package.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ library:
7272
- tf-random
7373
- transformers
7474
- vty
75+
- xdg-basedir
7576
- yaml
7677

7778
executables:
@@ -81,7 +82,6 @@ executables:
8182
dependencies:
8283
- babel-cards
8384
- optparse-simple
84-
- xdg-basedir
8585
ghc-options:
8686
- -threaded
8787
- -rtsopts

‎src/Application.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
module Application (bootstrapBabel) where
33

44
import Application.Database
5-
import Database.Persist.Sql (runMigration)
5+
import Database.Persist.Sql (runMigration)
66
-- import Model
7+
import Application.TUI (lifecycle)
78
import RIO
89
import Types
9-
import Application.TUI (lifecycle)
1010

1111
bootstrapBabel :: RIO Babel ()
1212
bootstrapBabel = do

‎src/Import/Main.hs

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@ import Database.Persist.Sql (ConnectionPool)
55
import Database.Persist.Sqlite (SqliteConf (..), createSqlitePool,
66
createSqlitePoolFromInfo,
77
sqlConnectionStr)
8-
98
import Import
109
import Settings as Import.Main (loadSettings)
1110
import System.Directory (createDirectoryIfMissing)
1211
import System.Environment
1312
import System.FilePath.Posix
1413
import System.Random.TF.Init as Import.Main (newTFGen)
1514
import Util.String (mapText)
15+
import System.Environment.XDG.BaseDir (getUserDataDir)
1616

17-
createConnPool :: FilePath -> BabelEmbeddedSettings -> IO ConnectionPool
18-
createConnPool dataDir' settings = do
19-
dataDir <- ensureDataDirExists dataDir'
20-
runNoLoggingT $ case besDatabase settings of
21-
SqliteConf connStr poolSize ->
22-
createSqlitePool (mapText (dataDir </>) connStr) poolSize
23-
SqliteConfInfo confInfo poolSize -> createSqlitePoolFromInfo
24-
(over sqlConnectionStr (mapText (dataDir </>)) confInfo)
25-
poolSize
17+
createConnPool :: SqliteConf -> IO ConnectionPool
18+
createConnPool settings = do
19+
ensureDataDirExists
20+
runNoLoggingT $ case settings of
21+
-- SqliteConf connStr poolSize ->
22+
-- createSqlitePool (mapText (dataDir </>) connStr) poolSize
23+
info@(SqliteConfInfo connInfo poolSize) -> createSqlitePoolFromInfo connInfo poolSize
2624

27-
ensureDataDirExists :: FilePath -> IO FilePath
28-
ensureDataDirExists dataDir =
29-
createDirectoryIfMissing True dataDir
30-
>> return dataDir
25+
ensureDataDirExists :: IO ()
26+
ensureDataDirExists =
27+
getUserDataDir "babel-cards" >>= createDirectoryIfMissing True
3128

3229
loadConfig :: IO BabelConfig
3330
loadConfig = do

‎src/Settings.hs

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
{-# LANGUAGE TemplateHaskell #-}
22
module Settings where
33

4-
import Data.ByteString
5-
import Data.FileEmbed
6-
import Data.Yaml
7-
import Types
4+
import Data.Maybe (fromMaybe)
5+
import qualified Data.Text as Text (pack)
6+
import Database.Persist.Sqlite (SqliteConf (..), fkEnabled,
7+
mkSqliteConnectionInfo,
8+
walEnabled)
9+
import Lens.Micro
10+
import System.Environment (lookupEnv)
11+
import System.Environment.XDG.BaseDir (getUserDataDir)
12+
import System.FilePath.Posix
813

9-
loadSettings :: IO BabelEmbeddedSettings
10-
loadSettings = decodeThrow embeddedSettings
11-
12-
embeddedSettings :: ByteString
13-
embeddedSettings = $(embedFile "config/settings.yaml")
14+
loadSettings :: IO SqliteConf
15+
loadSettings = do
16+
dbOverride <- lookupEnv "BABEL_DATABASE"
17+
userDataDir <- getUserDataDir "babel-cards"
18+
let dbPath = fromMaybe
19+
(userDataDir </> "babel-cards.sqlite")
20+
dbOverride
21+
connString = mkSqliteConnectionInfo (Text.pack dbPath)
22+
& walEnabled .~ True
23+
& fkEnabled .~ True
24+
return $ SqliteConfInfo connString 1

‎src/Types.hs

+1-6
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,14 @@ data Babel = Babel
2828
, bConnPool :: !ConnectionPool
2929
, bRNG :: !(TVar TFGen)
3030
, bConfig :: !BabelConfig
31-
, bEmbeddedSettings :: !BabelEmbeddedSettings
31+
, bDatabaseSettings :: !SqliteConf
3232
}
3333

3434
data BabelConfig = BabelConfig
3535
{ bcMaxInterval :: !NominalDiffTime
3636
, bcMinInterval :: !NominalDiffTime
3737
}
3838

39-
data BabelEmbeddedSettings = BabelEmbeddedSettings
40-
{ besDatabase :: !SqliteConf
41-
} deriving Generic
42-
43-
$(deriveFromJSON defaultOptions { fieldLabelModifier = decapitalize . drop 3 } 'BabelEmbeddedSettings)
4439
$(deriveJSON defaultOptions { fieldLabelModifier = decapitalize . drop 2} 'BabelConfig)
4540

4641
instance HasLogFunc Babel where

0 commit comments

Comments
 (0)
Please sign in to comment.