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:
Joey Hess 2018-10-30 13:47:38 -04:00
parent f00b329e0c
commit 3963c5fcf5
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
4 changed files with 19 additions and 16 deletions

View file

@ -1,6 +1,6 @@
{- Persistent sqlite database handles. {- 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. - Licensed under the GNU GPL version 3 or higher.
-} -}

View file

@ -1,6 +1,6 @@
{- Persistent sqlite database initialization {- 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. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -10,18 +10,17 @@ module Database.Init where
import Annex.Common import Annex.Common
import Annex.Perms import Annex.Perms
import Utility.FileMode import Utility.FileMode
import Database.Handle
import Database.Persist.Sqlite import Database.Persist.Sqlite
import qualified Database.Sqlite as Sqlite import qualified Database.Sqlite as Sqlite
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T import qualified Data.Text as T
import Lens.Micro
{- Ensures that the database is freshly initialized. Deletes any {- Ensures that the database is freshly initialized. Deletes any
- existing database. Pass the migration action for the database. - 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 - The permissions of the database are set based on the
- core.sharedRepository setting. Setting these permissions on the main db - core.sharedRepository setting. Setting these permissions on the main db
- file causes Sqlite to always use the same permissions for additional - file causes Sqlite to always use the same permissions for additional
@ -34,9 +33,7 @@ initDb db migration = do
let tmpdb = tmpdbdir </> "db" let tmpdb = tmpdbdir </> "db"
liftIO $ do liftIO $ do
createDirectoryIfMissing True tmpdbdir createDirectoryIfMissing True tmpdbdir
let tdb = T.pack tmpdb runSqliteInfo (mkConnInfo tmpdb) migration
enableWAL tdb
runSqlite tdb migration
setAnnexDirPerm tmpdbdir setAnnexDirPerm tmpdbdir
-- Work around sqlite bug that prevents it from honoring -- Work around sqlite bug that prevents it from honoring
-- less restrictive umasks. -- less restrictive umasks.
@ -46,10 +43,14 @@ initDb db migration = do
void $ tryIO $ removeDirectoryRecursive dbdir void $ tryIO $ removeDirectoryRecursive dbdir
rename tmpdbdir dbdir rename tmpdbdir dbdir
enableWAL :: T.Text -> IO () {- Make sure that the database uses WAL mode, to prevent readers
enableWAL db = do - from blocking writers, and prevent a writer from blocking readers.
conn <- Sqlite.open db -
stmt <- Sqlite.prepare conn (T.pack "PRAGMA journal_mode=WAL;") - This is the default in persistent-sqlite currently, but force it on just
void $ Sqlite.step stmt - in case.
void $ Sqlite.finalize stmt -
Sqlite.close conn - 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
View file

@ -50,6 +50,7 @@ Build-Depends:
libghc-persistent-template-dev, libghc-persistent-template-dev,
libghc-persistent-sqlite-dev, libghc-persistent-sqlite-dev,
libghc-esqueleto-dev, libghc-esqueleto-dev,
libghc-microlens-dev,
libghc-securemem-dev, libghc-securemem-dev,
libghc-byteable-dev, libghc-byteable-dev,
libghc-stm-chans-dev, libghc-stm-chans-dev,

View file

@ -336,9 +336,10 @@ Executable git-annex
time, time,
old-locale, old-locale,
esqueleto, esqueleto,
persistent-sqlite, persistent-sqlite (>= 2.1.3),
persistent, persistent,
persistent-template, persistent-template,
microlens,
aeson, aeson,
vector, vector,
tagsoup, tagsoup,