2017-02-13 21:30:28 +00:00
|
|
|
{- Persistent sqlite database initialization
|
|
|
|
-
|
2018-10-30 17:47:38 +00:00
|
|
|
- Copyright 2015-2018 Joey Hess <id@joeyh.name>
|
2017-02-13 21:30:28 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2017-02-13 21:30:28 +00:00
|
|
|
-}
|
|
|
|
|
2018-11-09 17:09:02 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2017-02-13 21:30:28 +00:00
|
|
|
module Database.Init where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Annex.Perms
|
|
|
|
import Utility.FileMode
|
|
|
|
|
|
|
|
import Database.Persist.Sqlite
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import qualified Data.Text as T
|
2018-11-09 17:09:02 +00:00
|
|
|
#if MIN_VERSION_persistent_sqlite(2,6,2)
|
2018-10-30 17:47:38 +00:00
|
|
|
import Lens.Micro
|
2018-11-09 17:09:02 +00:00
|
|
|
#else
|
|
|
|
import qualified Database.Sqlite as Sqlite
|
|
|
|
#endif
|
2017-02-13 21:30:28 +00:00
|
|
|
|
|
|
|
{- Ensures that the database is freshly initialized. Deletes any
|
|
|
|
- existing database. Pass the migration action for the database.
|
|
|
|
-
|
|
|
|
- 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
|
|
|
|
- files it writes later on
|
|
|
|
-}
|
|
|
|
initDb :: FilePath -> SqlPersistM () -> Annex ()
|
|
|
|
initDb db migration = do
|
|
|
|
let dbdir = takeDirectory db
|
|
|
|
let tmpdbdir = dbdir ++ ".tmp"
|
|
|
|
let tmpdb = tmpdbdir </> "db"
|
2018-11-09 17:09:02 +00:00
|
|
|
let tdb = T.pack tmpdb
|
2017-02-13 21:30:28 +00:00
|
|
|
liftIO $ do
|
|
|
|
createDirectoryIfMissing True tmpdbdir
|
2018-11-09 17:09:02 +00:00
|
|
|
#if MIN_VERSION_persistent_sqlite(2,6,2)
|
|
|
|
runSqliteInfo (enableWAL tdb) migration
|
|
|
|
#else
|
|
|
|
enableWAL tdb
|
|
|
|
runSqlite tdb migration
|
|
|
|
#endif
|
2017-02-13 21:30:28 +00:00
|
|
|
setAnnexDirPerm tmpdbdir
|
|
|
|
-- Work around sqlite bug that prevents it from honoring
|
|
|
|
-- less restrictive umasks.
|
|
|
|
liftIO $ setFileMode tmpdb =<< defaultFileMode
|
|
|
|
setAnnexFilePerm tmpdb
|
|
|
|
liftIO $ do
|
|
|
|
void $ tryIO $ removeDirectoryRecursive dbdir
|
|
|
|
rename tmpdbdir dbdir
|
|
|
|
|
2018-10-30 17:47:38 +00:00
|
|
|
{- Make sure that the database uses WAL mode, to prevent readers
|
|
|
|
- from blocking writers, and prevent a writer from blocking readers.
|
|
|
|
-
|
2018-11-09 17:09:02 +00:00
|
|
|
- This is the default in recent persistent-sqlite versions, but
|
|
|
|
- force it on just in case.
|
2018-10-30 17:47:38 +00:00
|
|
|
-
|
|
|
|
- Note that once WAL mode is enabled, it will persist whenever the
|
|
|
|
- database is opened. -}
|
2018-11-09 17:09:02 +00:00
|
|
|
#if MIN_VERSION_persistent_sqlite(2,6,2)
|
|
|
|
enableWAL :: T.Text -> SqliteConnectionInfo
|
|
|
|
enableWAL db = over walEnabled (const True) $
|
|
|
|
mkSqliteConnectionInfo db
|
|
|
|
#else
|
|
|
|
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
|
|
|
|
#endif
|