on dev help sign in
pq stuff

simpify migration code manipulateParams_

commit994dad32372ac4fd4b1642a89e425f4f9d7c5ab1 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateSun 14 Apr 2019, 01:23 (8 years ago) parentff64af3
2 files changed+23−22
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+16−22 squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+7
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+16−22
@@ -247,9 +247,9 @@ pureMigration migration = Migration
247247
248248instance Migratory (Terminally PQ IO) where
249249
250− migrateUp migration = unsafePQ $
250+ migrateUp migration = unsafePQ . transactionally_ $ do
251251 define createMigrations
252− & pqThen (transactionally_ (upMigrations migration))
252+ upMigrations migration
253253
254254 where
255255
@@ -258,30 +258,27 @@ instance Migratory (Terminally PQ IO) where
258258 -> PQ MigrationsSchemas MigrationsSchemas IO ()
259259 upMigrations = \case
260260 Done -> return ()
261− step :>> steps -> upMigration step & pqThen (upMigrations steps)
261+ step :>> steps -> upMigration step >> upMigrations steps
262262
263263 upMigration
264264 :: Migration (Terminally PQ IO) schemas0 schemas1
265265 -> PQ MigrationsSchemas MigrationsSchemas IO ()
266− upMigration step =
267− queryExecuted step
268− & pqBind ( \ executed -> unless (executed == 1)
269− $ unsafePQ (runTerminally (up step))
270− & pqThen (manipulateParams insertMigration (Only (name step)))
271− -- insert execution record
272− & pqBind okResult )
266+ upMigration step = do
267+ executed <- queryExecuted step
268+ unless (executed == 1) $ do
269+ unsafePQ . runTerminally $ up step
270+ manipulateParams_ insertMigration (Only (name step))
273271
274272 queryExecuted
275273 :: Migration (Terminally PQ IO) schemas0 schemas1
276274 -> PQ MigrationsSchemas MigrationsSchemas IO Row
277275 queryExecuted step = do
278276 result <- runQueryParams selectMigration (Only (name step))
279− okResult result
280277 ntuples result
281278
282− migrateDown migrations = unsafePQ $
279+ migrateDown migrations = unsafePQ . transactionally_ $ do
283280 define createMigrations
284− & pqThen (transactionally_ (downMigrations migrations))
281+ downMigrations migrations
285282
286283 where
287284
@@ -290,25 +287,22 @@ instance Migratory (Terminally PQ IO) where
290287 -> PQ MigrationsSchemas MigrationsSchemas IO ()
291288 downMigrations = \case
292289 Done -> return ()
293− step :>> steps -> downMigrations steps & pqThen (downMigration step)
290+ step :>> steps -> downMigrations steps >> downMigration step
294291
295292 downMigration
296293 :: Migration (Terminally PQ IO) schemas0 schemas1
297294 -> PQ MigrationsSchemas MigrationsSchemas IO ()
298− downMigration step =
299− queryExecuted step
300− & pqBind ( \ executed -> unless (executed == 0)
301− $ unsafePQ (runTerminally (down step))
302− & pqThen (manipulateParams deleteMigration (Only (name step)))
303− -- delete execution record
304− & pqBind okResult )
295+ downMigration step = do
296+ executed <- queryExecuted step
297+ unless (executed == 0) $ do
298+ unsafePQ . runTerminally $ down step
299+ manipulateParams_ deleteMigration (Only (name step))
305300
306301 queryExecuted
307302 :: Migration (Terminally PQ IO) schemas0 schemas1
308303 -> PQ MigrationsSchemas MigrationsSchemas IO Row
309304 queryExecuted step = do
310305 result <- runQueryParams selectMigration (Only (name step))
311− okResult result
312306 ntuples result
313307
314308unsafePQ :: (Functor m) => PQ db0 db1 m x -> PQ db0' db1' m x
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+7
@@ -323,6 +323,13 @@ class Monad pq => MonadPQ schemas pq | pq -> schemas where
323323 manipulateParams manipulation params = lift $
324324 manipulateParams manipulation params
325325
326+ manipulateParams_
327+ :: ToParams x params
328+ => Manipulation '[] schemas params '[]
329+ -- ^ `insertInto`, `update` or `deleteFrom`
330+ -> x -> pq ()
331+ manipulateParams_ q x = void $ manipulateParams q x
332+
326333 manipulate :: Manipulation '[] schemas '[] ys -> pq (K LibPQ.Result ys)
327334 manipulate statement = manipulateParams statement ()
328335