1212{-# OPTIONS_GHC -Wno-unused-top-binds #-}
1313
1414-- | Work with SQLite database used for caches.
15- module Stack.PersistentTH
16- ( initCacheStorage
17- , withCacheStorage
15+ module Stack.Storage
16+ ( initStorage
17+ , withStorage
1818 , loadConfigCache
1919 , saveConfigCache
2020 , deactiveConfigCache
@@ -23,24 +23,21 @@ module Stack.PersistentTH
2323 ) where
2424
2525import qualified Data.ByteString as S
26- import Data.Pool (Pool , destroyAllResources )
2726import qualified Data.Set as Set
28- import Database.Persist.Sql (SqlBackend , runMigrationSilent , runSqlPool )
27+ import Database.Persist.Sql (SqlBackend )
2928import Database.Persist.Sqlite
3029import Database.Persist.TH
31- import Database.Sqlite ( SqliteException )
30+ import qualified Pantry.SQLite as SQLite
3231import Path
33- import Path.IO (ensureDir )
34- import qualified RIO.Text as T
3532import Stack.Prelude hiding (MigrationFailure )
3633import Stack.Types.Build
3734import Stack.Types.Cache
38- import Stack.Types.Config (HasConfig , configCachePool , configL )
35+ import Stack.Types.Config (HasConfig , configStorage , configL )
3936import Stack.Types.GhcPkgId
4037
4138share [ mkPersist sqlSettings
4239 , mkDeleteCascade sqlSettings
43- , mkMigrate " migrateAllCache "
40+ , mkMigrate " migrateAll "
4441 ]
4542 [persistLowerCase |
4643ConfigCacheParent sql="config_cache"
@@ -88,58 +85,22 @@ PrecompiledCacheExe
8885 deriving Show
8986|]
9087
91- data PersistentException =
92- MigrationFailure ! (Path Abs File )
93- ! SqliteException
94- deriving (Typeable )
88+ -- | Initialize the database.
89+ initStorage
90+ :: HasLogFunc env
91+ => Path Abs File -- ^ storage file
92+ -> (SQLite. Storage -> RIO env a )
93+ -> RIO env a
94+ initStorage =
95+ SQLite. initStorage " Stack" migrateAll
9596
96- instance Exception PersistentException
97-
98- instance Show PersistentException where
99- show = T. unpack . utf8BuilderToText . display
100-
101- instance Display PersistentException where
102- display (MigrationFailure fp ex) =
103- " Encountered error while migrating cache database:" <> " \n " <>
104- displayShow ex <>
105- " \n Please report this on https://github.com/commercialhaskell/stack/issues" <>
106- " \n As a workaround you may delete the database in " <>
107- fromString (toFilePath fp) <>
108- " triggering its recreation."
109-
110- -- | Initialize the cache database.
111- initCacheStorage ::
112- HasLogFunc env
113- => Path Abs File -- ^ storage file
114- -> (Pool SqlBackend -> RIO env a )
115- -> RIO env a
116- initCacheStorage fp inner = do
117- ensureDir $ parent fp
118- bracket
119- (createSqlitePoolFromInfo (sqinfo False ) 1 )
120- (liftIO . destroyAllResources) $ \ pool -> do
121- migrates <-
122- wrapMigrationFailure $
123- runSqlPool (runMigrationSilent migrateAllCache) pool
124- forM_ migrates $ \ mig ->
125- logDebug $ " Migration executed: " <> display mig
126- bracket
127- (createSqlitePoolFromInfo (sqinfo True ) 1 )
128- (liftIO . destroyAllResources) $ \ pool -> inner pool
129- where
130- wrapMigrationFailure = handle (throwIO . MigrationFailure fp)
131- sqinfo fk =
132- set extraPragmas [" PRAGMA busy_timeout=2000;" ] $
133- set fkEnabled fk $ mkSqliteConnectionInfo (fromString $ toFilePath fp)
134-
135- -- | Run an action in a cache database transaction.
136- withCacheStorage ::
97+ -- | Run an action in a database transaction
98+ withStorage ::
13799 (HasConfig env , HasLogFunc env )
138100 => ReaderT SqlBackend (RIO env ) a
139101 -> RIO env a
140- withCacheStorage action = do
141- pool <- view $ configL . to configCachePool
142- runSqlPool action pool
102+ withStorage inner =
103+ SQLite. withStorage inner =<< view (configL . to configStorage)
143104
144105-- | Internal helper to read the 'ConfigCache'
145106readConfigCache ::
@@ -173,7 +134,7 @@ loadConfigCache ::
173134 => ConfigCacheKey
174135 -> RIO env (Maybe ConfigCache )
175136loadConfigCache key =
176- withCacheStorage $ do
137+ withStorage $ do
177138 mparent <- getBy (UniqueConfigCacheParent key)
178139 case mparent of
179140 Nothing -> return Nothing
@@ -189,7 +150,7 @@ saveConfigCache ::
189150 -> ConfigCache
190151 -> RIO env ()
191152saveConfigCache key new =
192- withCacheStorage $ do
153+ withStorage $ do
193154 mparent <- getBy (UniqueConfigCacheParent key)
194155 (parentId, mold) <-
195156 case mparent of
@@ -239,9 +200,12 @@ saveConfigCache key new =
239200 (configCacheComponents new)
240201
241202-- | Mark 'ConfigCache' as inactive in the database.
203+ -- We use a flag instead of deleting the records since, in most cases, the same
204+ -- cache will be written again within in a few seconds (after
205+ -- `cabal configure`), so this avoids unnecessary database churn.
242206deactiveConfigCache :: HasConfig env => ConfigCacheKey -> RIO env ()
243207deactiveConfigCache key =
244- withCacheStorage $
208+ withStorage $
245209 updateWhere
246210 [ConfigCacheParentKey ==. key]
247211 [ConfigCacheParentActive =. False ]
@@ -270,7 +234,7 @@ loadPrecompiledCache ::
270234 => PrecompiledCacheKey
271235 -> RIO env (Maybe (PrecompiledCache Rel ))
272236loadPrecompiledCache key =
273- withCacheStorage $ fmap snd <$> readPrecompiledCache key
237+ withStorage $ fmap snd <$> readPrecompiledCache key
274238
275239-- | Insert or update 'PrecompiledCache' to the database.
276240savePrecompiledCache ::
@@ -279,7 +243,7 @@ savePrecompiledCache ::
279243 -> PrecompiledCache Rel
280244 -> RIO env ()
281245savePrecompiledCache key new =
282- withCacheStorage $ do
246+ withStorage $ do
283247 mIdOld <- readPrecompiledCache key
284248 (parentId, mold) <-
285249 case mIdOld of
0 commit comments