better approach to enabling WAL mode
The old approach opened the database an extra time to enable WAL mode, but more recent persistent-sqlite has a better API to enable it.
This commit is contained in:
parent
f00b329e0c
commit
3963c5fcf5
4 changed files with 19 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
|||
{- Persistent sqlite database handles.
|
||||
-
|
||||
- Copyright 2015 Joey Hess <id@joeyh.name>
|
||||
- Copyright 2015-2018 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- Licensed under the GNU GPL version 3 or higher.
|
||||
-}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{- Persistent sqlite database initialization
|
||||
-
|
||||
- Copyright 2015-2017 Joey Hess <id@joeyh.name>
|
||||
- Copyright 2015-2018 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- Licensed under the GNU GPL version 3 or higher.
|
||||
-}
|
||||
|
@ -10,18 +10,17 @@ module Database.Init where
|
|||
import Annex.Common
|
||||
import Annex.Perms
|
||||
import Utility.FileMode
|
||||
import Database.Handle
|
||||
|
||||
import Database.Persist.Sqlite
|
||||
import qualified Database.Sqlite as Sqlite
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import qualified Data.Text as T
|
||||
import Lens.Micro
|
||||
|
||||
{- Ensures that the database is freshly initialized. Deletes any
|
||||
- existing database. Pass the migration action for the database.
|
||||
-
|
||||
- The database is initialized using WAL mode, to prevent readers
|
||||
- from blocking writers, and prevent a writer from blocking readers.
|
||||
-
|
||||
- The permissions of the database are set based on the
|
||||
- core.sharedRepository setting. Setting these permissions on the main db
|
||||
- file causes Sqlite to always use the same permissions for additional
|
||||
|
@ -34,9 +33,7 @@ initDb db migration = do
|
|||
let tmpdb = tmpdbdir </> "db"
|
||||
liftIO $ do
|
||||
createDirectoryIfMissing True tmpdbdir
|
||||
let tdb = T.pack tmpdb
|
||||
enableWAL tdb
|
||||
runSqlite tdb migration
|
||||
runSqliteInfo (mkConnInfo tmpdb) migration
|
||||
setAnnexDirPerm tmpdbdir
|
||||
-- Work around sqlite bug that prevents it from honoring
|
||||
-- less restrictive umasks.
|
||||
|
@ -46,10 +43,14 @@ initDb db migration = do
|
|||
void $ tryIO $ removeDirectoryRecursive dbdir
|
||||
rename tmpdbdir dbdir
|
||||
|
||||
enableWAL :: T.Text -> IO ()
|
||||
enableWAL db = do
|
||||
conn <- Sqlite.open db
|
||||
stmt <- Sqlite.prepare conn (T.pack "PRAGMA journal_mode=WAL;")
|
||||
void $ Sqlite.step stmt
|
||||
void $ Sqlite.finalize stmt
|
||||
Sqlite.close conn
|
||||
{- Make sure that the database uses WAL mode, to prevent readers
|
||||
- from blocking writers, and prevent a writer from blocking readers.
|
||||
-
|
||||
- This is the default in persistent-sqlite currently, but force it on just
|
||||
- in case.
|
||||
-
|
||||
- Note that once WAL mode is enabled, it will persist whenever the
|
||||
- database is opened. -}
|
||||
mkConnInfo :: FilePath -> SqliteConnectionInfo
|
||||
mkConnInfo db = over walEnabled (const True) $
|
||||
mkSqliteConnectionInfo (T.pack db)
|
||||
|
|
1
debian/control
vendored
1
debian/control
vendored
|
@ -50,6 +50,7 @@ Build-Depends:
|
|||
libghc-persistent-template-dev,
|
||||
libghc-persistent-sqlite-dev,
|
||||
libghc-esqueleto-dev,
|
||||
libghc-microlens-dev,
|
||||
libghc-securemem-dev,
|
||||
libghc-byteable-dev,
|
||||
libghc-stm-chans-dev,
|
||||
|
|
|
@ -336,9 +336,10 @@ Executable git-annex
|
|||
time,
|
||||
old-locale,
|
||||
esqueleto,
|
||||
persistent-sqlite,
|
||||
persistent-sqlite (>= 2.1.3),
|
||||
persistent,
|
||||
persistent-template,
|
||||
microlens,
|
||||
aeson,
|
||||
vector,
|
||||
tagsoup,
|
||||
|
|
Loading…
Reference in a new issue