@@ -17,12 +17,12 @@ where
1717import Control.Monad (forM_ , when )
1818import Data.List.NonEmpty (NonEmpty )
1919import qualified Data.Map.Strict as M
20+ import Data.Maybe (fromMaybe )
2021import Data.Text (Text )
2122import Data.Text.Encoding (decodeLatin1 )
2223import Data.Time.Clock (getCurrentTime )
2324import Database.SQLite.Simple (Only (.. ), Query (.. ))
2425import qualified Database.SQLite.Simple as SQL
25- import Database.SQLite.Simple.QQ (sql )
2626import qualified Database.SQLite3 as SQLite3
2727import Simplex.Messaging.Agent.Protocol (extraSMPServerHosts )
2828import qualified Simplex.Messaging.Agent.Store.DB as DB
@@ -32,25 +32,29 @@ import Simplex.Messaging.Agent.Store.Shared
3232import Simplex.Messaging.Encoding.String
3333import Simplex.Messaging.Transport.Client (TransportHost )
3434
35- getCurrentMigrations :: DB. Connection -> IO [Migration ]
36- getCurrentMigrations DB. Connection {DB. conn} = map toMigration <$> SQL. query_ conn " SELECT name, down FROM migrations ORDER BY name ASC;"
35+ getCurrentMigrations :: Maybe Query -> DB. Connection -> IO [Migration ]
36+ getCurrentMigrations migrationsTable DB. Connection {DB. conn} =
37+ map toMigration
38+ <$> SQL. query_ conn (" SELECT name, down FROM " <> table <> " ORDER BY name ASC;" )
3739 where
40+ table = fromMaybe " migrations" migrationsTable
3841 toMigration (name, down) = Migration {name, up = " " , down}
3942
40- run :: DBStore -> Bool -> MigrationsToRun -> IO ()
41- run st vacuum = \ case
43+ run :: DBStore -> Maybe Query -> Bool -> MigrationsToRun -> IO ()
44+ run st migrationsTable vacuum = \ case
4245 MTRUp [] -> pure ()
4346 MTRUp ms -> do
4447 mapM_ runUp ms
4548 when vacuum $ withConnection' st (`execSQL` " VACUUM;" )
4649 MTRDown ms -> mapM_ runDown $ reverse ms
4750 MTRNone -> pure ()
4851 where
52+ table = fromMaybe " migrations" migrationsTable
4953 runUp Migration {name, up, down} = withTransaction' st $ \ db -> do
5054 when (name == " m20220811_onion_hosts" ) $ updateServers db
5155 insert db >> execSQL db up'
5256 where
53- insert db = SQL. execute db " INSERT INTO migrations (name, down, ts) VALUES (?,?,?)" . (name,down,) =<< getCurrentTime
57+ insert db = SQL. execute db ( " INSERT INTO " <> table <> " (name, down, ts) VALUES (?,?,?)" ) . (name,down,) =<< getCurrentTime
5458 up'
5559 | dbNew st && name == " m20230110_users" = fromQuery new_m20230110_users
5660 | otherwise = up
@@ -59,24 +63,19 @@ run st vacuum = \case
5963 in SQL. execute db " UPDATE servers SET host = ? WHERE host = ?" (hs, decodeLatin1 $ strEncode h)
6064 runDown DownMigration {downName, downQuery} = withTransaction' st $ \ db -> do
6165 execSQL db downQuery
62- SQL. execute db " DELETE FROM migrations WHERE name = ?" (Only downName)
66+ SQL. execute db ( " DELETE FROM " <> table <> " WHERE name = ?" ) (Only downName)
6367 execSQL db = SQLite3. exec $ SQL. connectionHandle db
6468
65- initialize :: DBStore -> IO ()
66- initialize st = withTransaction' st $ \ db -> do
67- cs :: [Text ] <- map fromOnly <$> SQL. query_ db " SELECT name FROM pragma_table_info('migrations ')"
69+ initialize :: DBStore -> Maybe Query -> IO ()
70+ initialize st migrationsTable = withTransaction' st $ \ db -> do
71+ cs :: [Text ] <- map fromOnly <$> SQL. query_ db ( " SELECT name FROM pragma_table_info('" <> table <> " ')" )
6872 case cs of
6973 [] -> createMigrations db
70- _ -> when (" down" `notElem` cs) $ SQL. execute_ db " ALTER TABLE migrations ADD COLUMN down TEXT"
74+ _ -> when (" down" `notElem` cs) $ SQL. execute_ db $ " ALTER TABLE " <> table <> " ADD COLUMN down TEXT"
7175 where
76+ table = fromMaybe " migrations" migrationsTable
7277 createMigrations db =
73- SQL. execute_
74- db
75- [sql |
76- CREATE TABLE IF NOT EXISTS migrations (
77- name TEXT NOT NULL,
78- ts TEXT NOT NULL,
79- down TEXT,
80- PRIMARY KEY (name)
81- );
82- |]
78+ SQL. execute_ db $
79+ " CREATE TABLE IF NOT EXISTS "
80+ <> table
81+ <> " (name TEXT NOT NULL PRIMARY KEY, ts TEXT NOT NULL, down TEXT)"
0 commit comments