2017-02-13 21:30:28 +00:00
|
|
|
{- Persistent sqlite database initialization
|
|
|
|
-
|
2020-03-05 18:56:47 +00:00
|
|
|
- Copyright 2015-2020 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
|
|
|
-}
|
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2017-02-13 21:30:28 +00:00
|
|
|
module Database.Init where
|
|
|
|
|
|
|
|
import Annex.Common
|
2022-08-11 20:57:44 +00:00
|
|
|
import qualified Annex
|
2017-02-13 21:30:28 +00:00
|
|
|
import Annex.Perms
|
|
|
|
import Utility.FileMode
|
2020-10-28 21:25:59 +00:00
|
|
|
import Utility.Directory.Create
|
2020-11-05 22:45:37 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2017-02-13 21:30:28 +00:00
|
|
|
|
|
|
|
import Database.Persist.Sqlite
|
2018-10-30 17:47:38 +00:00
|
|
|
import Lens.Micro
|
2020-11-05 22:45:37 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified System.FilePath.ByteString as P
|
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
|
|
|
|
-}
|
2020-11-05 22:45:37 +00:00
|
|
|
initDb :: P.RawFilePath -> SqlPersistM () -> Annex ()
|
2017-02-13 21:30:28 +00:00
|
|
|
initDb db migration = do
|
2020-11-05 22:45:37 +00:00
|
|
|
let dbdir = P.takeDirectory db
|
|
|
|
let tmpdbdir = dbdir <> ".tmp"
|
|
|
|
let tmpdb = tmpdbdir P.</> "db"
|
|
|
|
let tdb = T.pack (fromRawFilePath tmpdb)
|
2022-08-11 20:57:44 +00:00
|
|
|
gc <- Annex.getGitConfig
|
|
|
|
top <- case annexDbDir gc of
|
|
|
|
Just topdbdir -> pure $ parentDir $ topdbdir
|
|
|
|
Nothing -> parentDir <$> fromRepo gitAnnexDir
|
2017-02-13 21:30:28 +00:00
|
|
|
liftIO $ do
|
2022-08-12 16:45:46 +00:00
|
|
|
createDirectoryUnder [top] tmpdbdir
|
2018-11-09 17:09:02 +00:00
|
|
|
runSqliteInfo (enableWAL tdb) migration
|
2017-02-13 21:30:28 +00:00
|
|
|
setAnnexDirPerm tmpdbdir
|
|
|
|
-- Work around sqlite bug that prevents it from honoring
|
|
|
|
-- less restrictive umasks.
|
2020-11-05 22:45:37 +00:00
|
|
|
liftIO $ R.setFileMode tmpdb =<< defaultFileMode
|
2017-02-13 21:30:28 +00:00
|
|
|
setAnnexFilePerm tmpdb
|
|
|
|
liftIO $ do
|
2020-11-05 22:45:37 +00:00
|
|
|
void $ tryIO $ removeDirectoryRecursive (fromRawFilePath dbdir)
|
2022-07-12 18:53:32 +00:00
|
|
|
R.rename tmpdbdir dbdir
|
2017-02-13 21:30:28 +00:00
|
|
|
|
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
|
|
|
enableWAL :: T.Text -> SqliteConnectionInfo
|
|
|
|
enableWAL db = over walEnabled (const True) $
|
|
|
|
mkSqliteConnectionInfo db
|