|
| 1 | +{-# LANGUAGE CPP #-} |
1 | 2 | {-# LANGUAGE OverloadedStrings #-} |
2 | 3 |
|
3 | 4 | module AgentTests.MigrationTests (migrationTests) where |
4 | 5 |
|
5 | 6 | import Control.Monad |
6 | 7 | import Data.Maybe (fromJust) |
7 | 8 | import Data.Word (Word32) |
8 | | -import Database.SQLite.Simple (fromOnly) |
9 | 9 | import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) |
10 | 10 | import Simplex.Messaging.Agent.Store.Migrations (migrationsToRun) |
11 | | -import Simplex.Messaging.Agent.Store.SQLite (closeDBStore, createDBStore) |
12 | | -import qualified Simplex.Messaging.Agent.Store.SQLite.DB as DB |
13 | 11 | import Simplex.Messaging.Agent.Store.Shared |
14 | | -import System.Directory (removeFile) |
15 | 12 | import System.Random (randomIO) |
16 | 13 | import Test.Hspec |
| 14 | +#if defined(dbPostgres) |
| 15 | +import Database.PostgreSQL.Simple (ConnectInfo (..), fromOnly) |
| 16 | +import Simplex.Messaging.Agent.Store.Postgres (closeDBStore, createDBStore, defaultSimplexConnectInfo, dropSchema) |
| 17 | +import qualified Simplex.Messaging.Agent.Store.Postgres.DB as DB |
| 18 | +#else |
| 19 | +import Database.SQLite.Simple (fromOnly) |
| 20 | +import Simplex.Messaging.Agent.Store.SQLite (closeDBStore, createDBStore) |
| 21 | +import qualified Simplex.Messaging.Agent.Store.SQLite.DB as DB |
| 22 | +import System.Directory (removeFile) |
| 23 | +#endif |
17 | 24 |
|
18 | | --- TODO [postgres] run with postgres |
19 | 25 | migrationTests :: Spec |
20 | 26 | migrationTests = do |
21 | 27 | it "should determine migrations to run" testMigrationsToRun |
22 | 28 | describe "run migrations" $ do |
23 | 29 | -- (init migrs, tables) |
24 | 30 | -- (final migrs, confirm modes, final tables or error) |
25 | | - it "up 1-2 tables (yes)" $ |
| 31 | + fit "up 1-2 tables (yes)" $ |
26 | 32 | testMigration |
27 | 33 | ([m1], [t1]) |
28 | 34 | ([m1, m2], [MCYesUp, MCYesUpDown], Right [t1, t2]) |
@@ -98,9 +104,6 @@ migrationTests = do |
98 | 104 | ([m1, m2, m3, m4], [t1, t2, t3, t4]) |
99 | 105 | ([m1, m2, m4], [MCYesUp, MCYesUpDown, MCError], Left . MigrationError $ MTREDifferent (name m4) (name m3)) |
100 | 106 |
|
101 | | -testDB :: FilePath |
102 | | -testDB = "tests/tmp/test_migrations.db" |
103 | | - |
104 | 107 | m1 :: Migration |
105 | 108 | m1 = Migration "20230301-migration1" "create table test1 (id1 integer primary key);" Nothing |
106 | 109 |
|
@@ -180,21 +183,46 @@ testMigration :: |
180 | 183 | IO () |
181 | 184 | testMigration (initMs, initTables) (finalMs, confirmModes, tablesOrError) = forM_ confirmModes $ \confirmMode -> do |
182 | 185 | r <- randomIO :: IO Word32 |
183 | | - let dpPath = testDB <> show r |
184 | | - Right st <- createDBStore dpPath "" False initMs MCError |
| 186 | + Right st <- createStore r initMs MCError |
185 | 187 | st `shouldHaveTables` initTables |
186 | 188 | closeDBStore st |
187 | 189 | case tablesOrError of |
188 | 190 | Right tables -> do |
189 | | - Right st' <- createDBStore dpPath "" False finalMs confirmMode |
| 191 | + Right st' <- createStore r finalMs confirmMode |
190 | 192 | st' `shouldHaveTables` tables |
191 | 193 | closeDBStore st' |
192 | 194 | Left e -> do |
193 | | - Left e' <- createDBStore dpPath "" False finalMs confirmMode |
| 195 | + Left e' <- createStore r finalMs confirmMode |
194 | 196 | e `shouldBe` e' |
195 | | - removeFile dpPath |
| 197 | +#if defined(dbPostgres) |
| 198 | + dropSchema testDBConnectInfo (testSchema r) |
| 199 | +#else |
| 200 | + removeFile (testDB r) |
| 201 | +#endif |
196 | 202 | where |
197 | 203 | shouldHaveTables :: DBStore -> [String] -> IO () |
198 | 204 | st `shouldHaveTables` expected = do |
199 | 205 | tables <- map fromOnly <$> withTransaction st (`DB.query_` "SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY 1;") |
200 | 206 | tables `shouldBe` "migrations" : expected |
| 207 | + |
| 208 | +#if defined(dbPostgres) |
| 209 | +testDBConnectInfo :: ConnectInfo |
| 210 | +testDBConnectInfo = |
| 211 | + defaultSimplexConnectInfo { |
| 212 | + connectUser = "test_user", |
| 213 | + connectDatabase = "test_db" |
| 214 | + } |
| 215 | + |
| 216 | +testSchema :: Word32 -> String |
| 217 | +testSchema randSuffix = "test_migrations_schema" <> show randSuffix |
| 218 | + |
| 219 | +createStore :: Word32 -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError DBStore) |
| 220 | +createStore randSuffix migrations confirmMigrations = |
| 221 | + createDBStore testDBConnectInfo (testSchema randSuffix) migrations confirmMigrations |
| 222 | +#else |
| 223 | +testDB :: Word32 -> FilePath |
| 224 | +testDB randSuffix = "tests/tmp/test_migrations.db" <> show randSuffix |
| 225 | + |
| 226 | +createStore :: Word32 -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError DBStore) |
| 227 | +createStore randSuffix = createDBStore (testDB randSuffix) "" False |
| 228 | +#endif |
0 commit comments