Skip to content

Commit a10ad4c

Browse files
committed
migration tests pass
1 parent 7ea4d69 commit a10ad4c

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ dropSchema connectInfo schema = do
131131

132132
dropDatabaseAndUser :: ConnectInfo -> IO ()
133133
dropDatabaseAndUser ConnectInfo {connectUser = user, connectDatabase = dbName} = do
134+
-- TODO [postgres] terminate all connections to the database
135+
-- ALTER DATABASE your_database_name WITH ALLOW_CONNECTIONS false;
136+
-- SELECT pg_terminate_backend(pg_stat_activity.pid)
137+
-- FROM pg_stat_activity
138+
-- WHERE datname = <db_name>
139+
-- AND pid <> pg_backend_pid();
134140
bracket (PSQL.connect defaultConnectInfo {connectUser = "postgres", connectDatabase = "postgres"}) PSQL.close $
135141
\db -> do
136142
void $ PSQL.execute_ db (fromString $ "DROP USER " <> user)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module Simplex.Messaging.Agent.Store.Postgres.Migrations
1212
)
1313
where
1414

15-
import Control.Concurrent.MVar (takeMVar)
1615
import Control.Monad (void)
1716
import Data.List (sortOn)
1817
import Data.Text (Text)
@@ -27,6 +26,7 @@ import Database.PostgreSQL.Simple.SqlQQ (sql)
2726
import Simplex.Messaging.Agent.Store.Postgres.Common
2827
import Simplex.Messaging.Agent.Store.Postgres.Migrations.M20241210_initial
2928
import Simplex.Messaging.Agent.Store.Shared
29+
import UnliftIO.MVar
3030

3131
schemaMigrations :: [(String, Text, Maybe Text)]
3232
schemaMigrations =
@@ -68,9 +68,9 @@ run st = \case
6868
runDown DownMigration {downName, downQuery} = withTransaction' st $ \db -> do
6969
execSQL db downQuery
7070
void $ PSQL.execute db "DELETE FROM migrations WHERE name = ?" (Only downName)
71-
execSQL db query = do
72-
pqConn <- takeMVar (connectionHandle db)
73-
void $ LibPQ.exec pqConn (TE.encodeUtf8 query)
71+
execSQL db query =
72+
withMVar (connectionHandle db) $ \pqConn ->
73+
void $ LibPQ.exec pqConn (TE.encodeUtf8 query)
7474

7575
getCurrent :: PSQL.Connection -> IO [Migration]
7676
getCurrent db = map toMigration <$> PSQL.query_ db "SELECT name, down FROM migrations ORDER BY name ASC;"

tests/AgentTests/MigrationTests.hs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ migrationTests = do
2828
describe "run migrations" $ do
2929
-- (init migrs, tables)
3030
-- (final migrs, confirm modes, final tables or error)
31-
fit "up 1-2 tables (yes)" $
31+
it "up 1-2 tables (yes)" $
3232
testMigration
3333
([m1], [t1])
3434
([m1, m2], [MCYesUp, MCYesUpDown], Right [t1, t2])
@@ -183,9 +183,7 @@ testMigration ::
183183
IO ()
184184
testMigration (initMs, initTables) (finalMs, confirmModes, tablesOrError) = forM_ confirmModes $ \confirmMode -> do
185185
r <- randomIO :: IO Word32
186-
print 0
187186
Right st <- createStore r initMs MCError
188-
print 1
189187
st `shouldHaveTables` initTables
190188
closeDBStore st
191189
case tablesOrError of
@@ -196,16 +194,7 @@ testMigration (initMs, initTables) (finalMs, confirmModes, tablesOrError) = forM
196194
Left e -> do
197195
Left e' <- createStore r finalMs confirmMode
198196
e `shouldBe` e'
199-
#if defined(dbPostgres)
200-
dropSchema testDBConnectInfo (testSchema r)
201-
#else
202-
removeFile (testDB r)
203-
#endif
204-
where
205-
shouldHaveTables :: DBStore -> [String] -> IO ()
206-
st `shouldHaveTables` expected = do
207-
tables <- map fromOnly <$> withTransaction st (`DB.query_` "SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY 1;")
208-
tables `shouldBe` "migrations" : expected
197+
cleanup r
209198

210199
#if defined(dbPostgres)
211200
-- TODO [postgres] move to shared module
@@ -222,10 +211,26 @@ testSchema randSuffix = "test_migrations_schema" <> show randSuffix
222211
createStore :: Word32 -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError DBStore)
223212
createStore randSuffix migrations confirmMigrations =
224213
createDBStore testDBConnectInfo (testSchema randSuffix) migrations confirmMigrations
214+
215+
cleanup :: Word32 -> IO ()
216+
cleanup randSuffix = dropSchema testDBConnectInfo (testSchema randSuffix)
217+
218+
shouldHaveTables :: DBStore -> [String] -> IO ()
219+
st `shouldHaveTables` expected = do
220+
tables <- map fromOnly <$> withTransaction st (`DB.query_` "SELECT table_name FROM information_schema.tables WHERE table_schema = current_schema() AND table_type = 'BASE TABLE' ORDER BY 1")
221+
tables `shouldBe` "migrations" : expected
225222
#else
226223
testDB :: Word32 -> FilePath
227224
testDB randSuffix = "tests/tmp/test_migrations.db" <> show randSuffix
228225

229226
createStore :: Word32 -> [Migration] -> MigrationConfirmation -> IO (Either MigrationError DBStore)
230227
createStore randSuffix = createDBStore (testDB randSuffix) "" False
228+
229+
cleanup :: Word32 -> IO ()
230+
cleanup randSuffix = removeFile (testDB randSuffix)
231+
232+
shouldHaveTables :: DBStore -> [String] -> IO ()
233+
st `shouldHaveTables` expected = do
234+
tables <- map fromOnly <$> withTransaction st (`DB.query_` "SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY 1")
235+
tables `shouldBe` "migrations" : expected
231236
#endif

0 commit comments

Comments
 (0)