on dev help sign in
Revert "one way migrations"

This reverts commit 383e27799aa66e2ae9db995920710b5dbcfd37ca.

commit045aae6d118a4dbed6a4af0ce4d240115cb2eb07 authorEitan Chatav <eitan@EEM109LMBP-1.local> dateThu 10 Oct 2019, 13:37 (7 years ago) parent383e277
1 file changed+29−53
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+29−53
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+29−53
@@ -38,9 +38,10 @@ Now we can define some `Migration`s to make our tables.
3838
3939>>> :{
4040let
41− makeUsers :: Migration (Iso Definition) (Public '[]) '["public" ::: '["users" ::: 'Table UsersTable]]
42− makeUsers = Migration "make users table" Iso
43− { up = createTable #users
41+ makeUsers :: Migration Definition (Public '[]) '["public" ::: '["users" ::: 'Table UsersTable]]
42+ makeUsers = Migration
43+ { name = "make users table"
44+ , up = createTable #users
4445 ( serial `as` #id :*
4546 notNullable text `as` #name )
4647 ( primaryKey #id `as` #pk_users )
@@ -50,10 +51,11 @@ let
5051
5152>>> :{
5253let
53− makeEmails :: Migration (Iso Definition) '["public" ::: '["users" ::: 'Table UsersTable]]
54+ makeEmails :: Migration Definition '["public" ::: '["users" ::: 'Table UsersTable]]
5455 '["public" ::: '["users" ::: 'Table UsersTable, "emails" ::: 'Table EmailsTable]]
55− makeEmails = Migration "make emails table" Iso
56− { up = createTable #emails
56+ makeEmails = Migration
57+ { name = "make emails table"
58+ , up = createTable #emails
5759 ( serial `as` #id :*
5860 notNullable int `as` #user_id :*
5961 nullable text `as` #email )
@@ -83,9 +85,9 @@ withConnection "host=localhost port=5432 dbname=exampledb" $
8385Migrate
8486Rollback
8587
86−We can also create a simple executable using `mainMigrateIso`.
88+We can also create a simple executable using `defaultMain`.
8789
88−>>> let main = mainMigrateIso "host=localhost port=5432 dbname=exampledb" migrations
90+>>> let main = defaultMain "host=localhost port=5432 dbname=exampledb" migrations
8991
9092>>> withArgs [] main
9193Invalid command: "". Use:
@@ -140,12 +142,10 @@ module Squeal.PostgreSQL.Migration
140142 Migration (..)
141143 , Migratory (..)
142144 , Terminally (..)
143− , Iso (..)
144145 , terminally
145146 , pureMigration
146− , pureMigrationIso
147147 , MigrationsTable
148− , mainMigrateIso
148+ , defaultMain
149149 ) where
150150
151151import Control.Category
@@ -187,7 +187,8 @@ import Squeal.PostgreSQL.Transaction
187187data Migration p schemas0 schemas1 = Migration
188188 { name :: Text -- ^ The `name` of a `Migration`.
189189 -- Each `name` in a `Migration` should be unique.
190− , instruction :: p schemas0 schemas1 -- ^ The instruction of a `Migration`.
190+ , up :: p schemas0 schemas1 -- ^ The `up` instruction of a `Migration`.
191+ , down :: p schemas1 schemas0 -- ^ The `down` instruction of a `Migration`.
191192 } deriving (GHC.Generic)
192193
193194{- |
@@ -197,10 +198,6 @@ SQL `Definition`s and the category of impure `Terminally` `PQ` `IO` actions.
197198-}
198199class Category p => Migratory p where
199200
200− migrate
201− :: Path (Migration p) schemas0 schemas1
202− -> PQ schemas0 schemas1 IO ()
203−
204201 {- |
205202 Run a `Path` of `Migration`s.
206203 Create the `MigrationsTable` as @public.schema_migrations@ if it does not already exist.
@@ -208,7 +205,7 @@ class Category p => Migratory p where
208205 if not, `up` the `Migration` and insert its `name` in the `MigrationsTable`.
209206 -}
210207 migrateUp
211− :: Path (Migration (Iso p)) schemas0 schemas1
208+ :: Path (Migration p) schemas0 schemas1
212209 -> PQ schemas0 schemas1 IO ()
213210
214211 {- |
@@ -218,13 +215,12 @@ class Category p => Migratory p where
218215 if so, `down` the `Migration` and delete its `name` in the `MigrationsTable`.
219216 -}
220217 migrateDown
221− :: Path (Migration (Iso p)) schemas0 schemas1
218+ :: Path (Migration p) schemas0 schemas1
222219 -> PQ schemas1 schemas0 IO ()
223220
224221instance Migratory Definition where
225− migrate = migrate . cmap pureMigration
226− migrateUp = migrateUp . cmap pureMigrationIso
227− migrateDown = migrateDown . cmap pureMigrationIso
222+ migrateUp = migrateUp . cmap pureMigration
223+ migrateDown = migrateDown . cmap pureMigration
228224
229225{- | `Terminally` turns an indexed monad transformer and the monad it transforms
230226into a category by restricting the return type to @()@ and permuting the type variables.
@@ -236,11 +232,6 @@ newtype Terminally trans monad x0 x1 = Terminally
236232 { runTerminally :: trans x0 x1 monad () }
237233 deriving GHC.Generic
238234
239−data Iso c x y = Iso
240− { up :: c x y
241− , down :: c y x
242− } deriving GHC.Generic
243−
244235instance
245236 ( IndexedMonadTransPQ trans
246237 , Monad monad
@@ -265,26 +256,13 @@ pureMigration
265256 -> Migration (Terminally PQ IO) schemas0 schemas1
266257pureMigration migration = Migration
267258 { name = name migration
268− , instruction = terminally . define $ instruction migration
269− }
270−
271−-- | A `pureMigrationIso` turns a reversible `Migration`
272−-- involving only pure SQL
273−-- `Definition`s into a `Migration` that may be combined with arbitrary `IO`.
274−pureMigrationIso
275− :: Migration (Iso Definition) schemas0 schemas1
276− -> Migration (Iso (Terminally PQ IO)) schemas0 schemas1
277−pureMigrationIso migration = Migration
278− { name = name migration
279− , instruction = Iso
280− { up = terminally . define . up $ instruction migration
281− , down = terminally . define . down $ instruction migration
282− }
259+ , up = terminally . define $ up migration
260+ , down = terminally . define $ down migration
283261 }
284262
285263instance Migratory (Terminally PQ IO) where
286264
287− migrate migration = unsafePQ . transactionally_ $ do
265+ migrateUp migration = unsafePQ . transactionally_ $ do
288266 define createMigrations
289267 upMigrations migration
290268
@@ -303,7 +281,7 @@ instance Migratory (Terminally PQ IO) where
303281 upMigration step = do
304282 executed <- queryExecuted step
305283 unless (executed == 1) $ do
306− unsafePQ . runTerminally $ instruction step
284+ unsafePQ . runTerminally $ up step
307285 manipulateParams_ insertMigration (Only (name step))
308286
309287 queryExecuted
@@ -313,8 +291,6 @@ instance Migratory (Terminally PQ IO) where
313291 result <- runQueryParams selectMigration (Only (name step))
314292 ntuples result
315293
316− migrateUp = migrate . cmap (\(Migration n iso) -> Migration n (up iso))
317−
318294 migrateDown migrations = unsafePQ . transactionally_ $ do
319295 define createMigrations
320296 downMigrations migrations
@@ -322,23 +298,23 @@ instance Migratory (Terminally PQ IO) where
322298 where
323299
324300 downMigrations
325− :: Path (Migration (Iso (Terminally PQ IO))) schemas0 schemas1
301+ :: Path (Migration (Terminally PQ IO)) schemas0 schemas1
326302 -> PQ MigrationsSchemas MigrationsSchemas IO ()
327303 downMigrations = \case
328304 Done -> return ()
329305 step :>> steps -> downMigrations steps >> downMigration step
330306
331307 downMigration
332− :: Migration (Iso (Terminally PQ IO)) schemas0 schemas1
308+ :: Migration (Terminally PQ IO) schemas0 schemas1
333309 -> PQ MigrationsSchemas MigrationsSchemas IO ()
334310 downMigration step = do
335311 executed <- queryExecuted step
336312 unless (executed == 0) $ do
337− unsafePQ . runTerminally . down $ instruction step
313+ unsafePQ . runTerminally $ down step
338314 manipulateParams_ deleteMigration (Only (name step))
339315
340316 queryExecuted
341− :: Migration (Iso (Terminally PQ IO)) schemas0 schemas1
317+ :: Migration (Terminally PQ IO) schemas0 schemas1
342318 -> PQ MigrationsSchemas MigrationsSchemas IO Row
343319 queryExecuted step = do
344320 result <- runQueryParams selectMigration (Only (name step))
@@ -403,16 +379,16 @@ data MigrateCommand
403379 | MigrateUp
404380 | MigrateDown deriving (GHC.Generic, Show)
405381
406−{- | `mainMigrateIso` creates a simple executable
382+{- | `defaultMain` creates a simple executable
407383from a connection string and a list of `Migration`s. -}
408−mainMigrateIso
384+defaultMain
409385 :: Migratory p
410386 => ByteString
411387 -- ^ connection string
412− -> Path (Migration (Iso p)) db0 db1
388+ -> Path (Migration p) db0 db1
413389 -- ^ migrations
414390 -> IO ()
415−mainMigrateIso connectTo migrations = do
391+defaultMain connectTo migrations = do
416392 command <- readCommandFromArgs
417393 maybe (pure ()) performCommand command
418394