Skip to content

Commit c7a07d2

Browse files
committed
agent: option to enable/disable vacuum after SQLite migration
1 parent 3cf9dac commit c7a07d2

File tree

11 files changed

+43
-41
lines changed

11 files changed

+43
-41
lines changed

src/Simplex/Messaging/Agent/Env/SQLite.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ newSMPAgentEnv config store = do
281281
createAgentStore :: ConnectInfo -> String -> MigrationConfirmation -> IO (Either MigrationError DBStore)
282282
createAgentStore = createStore
283283
#else
284-
createAgentStore :: FilePath -> ScrubbedBytes -> Bool -> MigrationConfirmation -> IO (Either MigrationError DBStore)
284+
createAgentStore :: FilePath -> ScrubbedBytes -> Bool -> MigrationConfirmation -> Bool -> IO (Either MigrationError DBStore)
285285
createAgentStore = createStore
286286
#endif
287287

src/Simplex/Messaging/Agent/Store.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ import Simplex.Messaging.Protocol
5656
import qualified Simplex.Messaging.Protocol as SMP
5757
#if defined(dbPostgres)
5858
import Database.PostgreSQL.Simple (ConnectInfo (..))
59-
import qualified Simplex.Messaging.Agent.Store.Postgres as StoreFunctions
59+
import qualified Simplex.Messaging.Agent.Store.Postgres as Store
6060
#else
6161
import Data.ByteArray (ScrubbedBytes)
62-
import qualified Simplex.Messaging.Agent.Store.SQLite as StoreFunctions
62+
import qualified Simplex.Messaging.Agent.Store.SQLite as Store
6363
#endif
6464

6565
#if defined(dbPostgres)
6666
createStore :: ConnectInfo -> String -> MigrationConfirmation -> IO (Either MigrationError DBStore)
67-
createStore connectInfo schema = StoreFunctions.createDBStore connectInfo schema Migrations.app
67+
createStore connectInfo schema = Store.createDBStore connectInfo schema Migrations.app
6868
#else
69-
createStore :: FilePath -> ScrubbedBytes -> Bool -> MigrationConfirmation -> IO (Either MigrationError DBStore)
70-
createStore dbFilePath dbKey keepKey = StoreFunctions.createDBStore dbFilePath dbKey keepKey Migrations.app
69+
createStore :: FilePath -> ScrubbedBytes -> Bool -> MigrationConfirmation -> Bool -> IO (Either MigrationError DBStore)
70+
createStore dbFilePath dbKey keepKey = Store.createDBStore dbFilePath dbKey keepKey Migrations.app
7171
#endif
7272

7373
closeStore :: DBStore -> IO ()
74-
closeStore = StoreFunctions.closeDBStore
74+
closeStore = Store.closeDBStore
7575

7676
execSQL :: DB.Connection -> Text -> IO [Text]
77-
execSQL = StoreFunctions.execSQL
77+
execSQL = Store.execSQL
7878

7979
-- * Queue types
8080

src/Simplex/Messaging/Agent/Store/Migrations.hs

+12-12
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,41 @@ migrationsToRun (a : as) (d : ds)
4848
| name a == name d = migrationsToRun as ds
4949
| otherwise = Left $ MTREDifferent (name a) (name d)
5050

51-
migrateSchema :: DBStore -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError ())
52-
migrateSchema st migrations confirmMigrations = do
51+
migrateSchema :: DBStore -> [Migration] -> MigrationConfirmation -> Bool -> IO (Either MigrationError ())
52+
migrateSchema st migrations confirmMigrations vacuum = do
5353
Migrations.initialize st
5454
get st migrations >>= \case
5555
Left e -> do
5656
when (confirmMigrations == MCConsole) $ confirmOrExit ("Database state error: " <> mtrErrorDescription e)
5757
pure . Left $ MigrationError e
5858
Right MTRNone -> pure $ Right ()
5959
Right ms@(MTRUp ums)
60-
| dbNew st -> Migrations.run st ms $> Right ()
60+
| dbNew st -> Migrations.run st vacuum ms $> Right ()
6161
| otherwise -> case confirmMigrations of
62-
MCYesUp -> runWithBackup st ms
63-
MCYesUpDown -> runWithBackup st ms
64-
MCConsole -> confirm err >> runWithBackup st ms
62+
MCYesUp -> runWithBackup st vacuum ms
63+
MCYesUpDown -> runWithBackup st vacuum ms
64+
MCConsole -> confirm err >> runWithBackup st vacuum ms
6565
MCError -> pure $ Left err
6666
where
6767
err = MEUpgrade $ map upMigration ums -- "The app has a newer version than the database.\nConfirm to back up and upgrade using these migrations: " <> intercalate ", " (map name ums)
6868
Right ms@(MTRDown dms) -> case confirmMigrations of
69-
MCYesUpDown -> runWithBackup st ms
70-
MCConsole -> confirm err >> runWithBackup st ms
69+
MCYesUpDown -> runWithBackup st vacuum ms
70+
MCConsole -> confirm err >> runWithBackup st vacuum ms
7171
MCYesUp -> pure $ Left err
7272
MCError -> pure $ Left err
7373
where
7474
err = MEDowngrade $ map downName dms
7575
where
7676
confirm err = confirmOrExit $ migrationErrorDescription err
7777

78-
runWithBackup :: DBStore -> MigrationsToRun -> IO (Either a ())
78+
runWithBackup :: DBStore -> Bool -> MigrationsToRun -> IO (Either a ())
7979
#if defined(dbPostgres)
80-
runWithBackup st ms = Migrations.run st ms $> Right ()
80+
runWithBackup st vacuum ms = Migrations.run st vacuum ms $> Right ()
8181
#else
82-
runWithBackup st ms = do
82+
runWithBackup st vacuum ms = do
8383
let f = dbFilePath st
8484
copyFile f (f <> ".bak")
85-
Migrations.run st ms
85+
Migrations.run st vacuum ms
8686
pure $ Right ()
8787
#endif
8888

src/Simplex/Messaging/Agent/Store/Postgres.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ createDBStore :: ConnectInfo -> String -> [Migration] -> MigrationConfirmation -
4646
createDBStore connectInfo schema migrations confirmMigrations = do
4747
createDBAndUserIfNotExists connectInfo
4848
st <- connectPostgresStore connectInfo schema
49-
r <- migrateSchema st migrations confirmMigrations `onException` closeDBStore st
49+
r <- migrateSchema st migrations confirmMigrations True `onException` closeDBStore st
5050
case r of
5151
Right () -> pure $ Right st
5252
Left e -> closeDBStore st $> Left e

src/Simplex/Messaging/Agent/Store/Postgres/Migrations.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ initialize st = withTransaction' st $ \db ->
5353
)
5454
|]
5555

56-
run :: DBStore -> MigrationsToRun -> IO ()
57-
run st = \case
56+
run :: DBStore -> Bool -> MigrationsToRun -> IO ()
57+
run st _vacuum = \case
5858
MTRUp [] -> pure ()
5959
MTRUp ms -> mapM_ runUp ms
6060
MTRDown ms -> mapM_ runDown $ reverse ms

src/Simplex/Messaging/Agent/Store/SQLite.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ import UnliftIO.STM
6565

6666
-- * SQLite Store implementation
6767

68-
createDBStore :: FilePath -> ScrubbedBytes -> Bool -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError DBStore)
69-
createDBStore dbFilePath dbKey keepKey migrations confirmMigrations = do
68+
createDBStore :: FilePath -> ScrubbedBytes -> Bool -> [Migration] -> MigrationConfirmation -> Bool -> IO (Either MigrationError DBStore)
69+
createDBStore dbFilePath dbKey keepKey migrations confirmMigrations vacuum = do
7070
let dbDir = takeDirectory dbFilePath
7171
createDirectoryIfMissing True dbDir
7272
st <- connectSQLiteStore dbFilePath dbKey keepKey
73-
r <- migrateSchema st migrations confirmMigrations `onException` closeDBStore st
73+
r <- migrateSchema st migrations confirmMigrations vacuum `onException` closeDBStore st
7474
case r of
7575
Right () -> pure $ Right st
7676
Left e -> closeDBStore st $> Left e

src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs

+5-3
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ getCurrent DB.Connection {DB.conn} = map toMigration <$> SQL.query_ conn "SELECT
122122
where
123123
toMigration (name, down) = Migration {name, up = "", down}
124124

125-
run :: DBStore -> MigrationsToRun -> IO ()
126-
run st = \case
125+
run :: DBStore -> Bool -> MigrationsToRun -> IO ()
126+
run st vacuum = \case
127127
MTRUp [] -> pure ()
128-
MTRUp ms -> mapM_ runUp ms >> withConnection' st (`execSQL` "VACUUM;")
128+
MTRUp ms -> do
129+
mapM_ runUp ms
130+
when vacuum $ withConnection' st (`execSQL` "VACUUM;")
129131
MTRDown ms -> mapM_ runDown $ reverse ms
130132
MTRNone -> pure ()
131133
where

tests/AgentTests/FunctionalAPITests.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3113,7 +3113,7 @@ insertUser :: DBStore -> IO ()
31133113
insertUser st = withTransaction st (`DB.execute_` "INSERT INTO users DEFAULT VALUES")
31143114
#else
31153115
createStore :: String -> IO (Either MigrationError DBStore)
3116-
createStore dbPath = createAgentStore dbPath "" False MCError
3116+
createStore dbPath = createAgentStore dbPath "" False MCError True
31173117

31183118
insertUser :: DBStore -> IO ()
31193119
insertUser st = withTransaction st (`DB.execute_` "INSERT INTO users (user_id) VALUES (1)")

tests/AgentTests/MigrationTests.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ testDB :: Word32 -> FilePath
218218
testDB randSuffix = "tests/tmp/test_migrations.db" <> show randSuffix
219219

220220
createStore :: Word32 -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError DBStore)
221-
createStore randSuffix = createDBStore (testDB randSuffix) "" False
221+
createStore randSuffix migrations migrationConf = createDBStore (testDB randSuffix) "" False migrations migrationConf True
222222

223223
cleanup :: Word32 -> IO ()
224224
cleanup randSuffix = removeFile (testDB randSuffix)

tests/AgentTests/SQLiteTests.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ createEncryptedStore key keepKey = do
8181
-- Randomize DB file name to avoid SQLite IO errors supposedly caused by asynchronous
8282
-- IO operations on multiple similarly named files; error seems to be environment specific
8383
r <- randomIO :: IO Word32
84-
Right st <- createDBStore (testDB <> show r) key keepKey Migrations.app MCError
84+
Right st <- createDBStore (testDB <> show r) key keepKey Migrations.app MCError True
8585
withTransaction' st (`SQL.execute_` "INSERT INTO users (user_id) VALUES (1);")
8686
pure st
8787

tests/AgentTests/SchemaDump.hs

+9-9
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ testVerifySchemaDump :: IO ()
4949
testVerifySchemaDump = do
5050
savedSchema <- ifM (doesFileExist appSchema) (readFile appSchema) (pure "")
5151
savedSchema `deepseq` pure ()
52-
void $ createDBStore testDB "" False Migrations.app MCConsole
52+
void $ createDBStore testDB "" False Migrations.app MCConsole True
5353
getSchema testDB appSchema `shouldReturn` savedSchema
5454
removeFile testDB
5555

5656
testVerifyLintFKeyIndexes :: IO ()
5757
testVerifyLintFKeyIndexes = do
5858
savedLint <- ifM (doesFileExist appLint) (readFile appLint) (pure "")
5959
savedLint `deepseq` pure ()
60-
void $ createDBStore testDB "" False Migrations.app MCConsole
60+
void $ createDBStore testDB "" False Migrations.app MCConsole True
6161
getLintFKeyIndexes testDB "tests/tmp/agent_lint.sql" `shouldReturn` savedLint
6262
removeFile testDB
6363

@@ -70,7 +70,7 @@ withTmpFiles =
7070
testSchemaMigrations :: IO ()
7171
testSchemaMigrations = do
7272
let noDownMigrations = dropWhileEnd (\Migration {down} -> isJust down) Migrations.app
73-
Right st <- createDBStore testDB "" False noDownMigrations MCError
73+
Right st <- createDBStore testDB "" False noDownMigrations MCError True
7474
mapM_ (testDownMigration st) $ drop (length noDownMigrations) Migrations.app
7575
closeDBStore st
7676
removeFile testDB
@@ -80,32 +80,32 @@ testSchemaMigrations = do
8080
putStrLn $ "down migration " <> name m
8181
let downMigr = fromJust $ toDownMigration m
8282
schema <- getSchema testDB testSchema
83-
Migrations.run st $ MTRUp [m]
83+
Migrations.run st True $ MTRUp [m]
8484
schema' <- getSchema testDB testSchema
8585
schema' `shouldNotBe` schema
86-
Migrations.run st $ MTRDown [downMigr]
86+
Migrations.run st True $ MTRDown [downMigr]
8787
unless (name m `elem` skipComparisonForDownMigrations) $ do
8888
schema'' <- getSchema testDB testSchema
8989
schema'' `shouldBe` schema
90-
Migrations.run st $ MTRUp [m]
90+
Migrations.run st True $ MTRUp [m]
9191
schema''' <- getSchema testDB testSchema
9292
schema''' `shouldBe` schema'
9393

9494
testUsersMigrationNew :: IO ()
9595
testUsersMigrationNew = do
96-
Right st <- createDBStore testDB "" False Migrations.app MCError
96+
Right st <- createDBStore testDB "" False Migrations.app MCError True
9797
withTransaction' st (`SQL.query_` "SELECT user_id FROM users;")
9898
`shouldReturn` ([] :: [Only Int])
9999
closeDBStore st
100100

101101
testUsersMigrationOld :: IO ()
102102
testUsersMigrationOld = do
103103
let beforeUsers = takeWhile (("m20230110_users" /=) . name) Migrations.app
104-
Right st <- createDBStore testDB "" False beforeUsers MCError
104+
Right st <- createDBStore testDB "" False beforeUsers MCError True
105105
withTransaction' st (`SQL.query_` "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'users';")
106106
`shouldReturn` ([] :: [Only String])
107107
closeDBStore st
108-
Right st' <- createDBStore testDB "" False Migrations.app MCYesUp
108+
Right st' <- createDBStore testDB "" False Migrations.app MCYesUp True
109109
withTransaction' st' (`SQL.query_` "SELECT user_id FROM users;")
110110
`shouldReturn` ([Only (1 :: Int)])
111111
closeDBStore st'

0 commit comments

Comments
 (0)