Skip to content

Commit 01c842e

Browse files
committed
example applies migration to real Postgres instance
1 parent 8e849ba commit 01c842e

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module RunMigration where
2+
3+
import Prelude
4+
import Database.PostgreSQL.Simple (connect, close, ConnectInfo(..))
5+
import Pagila.Schema (migrateDB)
6+
7+
-- https://hackage.haskell.org/package/postgresql-simple-0.7.0.0/docs/Database-PostgreSQL-Simple.html#t:ConnectInfo
8+
connInfo :: ConnectInfo
9+
connInfo = ConnectInfo "localhost" (read "5432") "postgres" "foo" "postgres"
10+
11+
main :: IO ()
12+
main = do
13+
putStrLn "Running migration..."
14+
c <- connect connInfo
15+
16+
_ <- migrateDB c
17+
18+
close c

beam-postgres/examples/pagila.cabal

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ library
2222
scientific,
2323
bytestring,
2424
text,
25+
exceptions,
2526
postgresql-simple,
2627
beam-core,
2728
beam-postgres,
@@ -32,22 +33,29 @@ library
3233
executable pagila
3334
import: warnings
3435
main-is: Main.hs
35-
36-
-- Modules included in this executable, other than Main.
37-
-- other-modules:
38-
3936
build-depends: base,
4037
pagila,
4138
text,
4239
bytestring,
4340
beam-core,
4441
beam-postgres,
4542
beam-migrate
46-
47-
-- Directories containing source files.
4843
hs-source-dirs: app
44+
default-language: Haskell2010
4945

50-
-- Base language which the package is written in.
46+
executable pagila-migration
47+
import: warnings
48+
main-is: RunMigration.hs
49+
ghc-options: -main-is RunMigration
50+
build-depends: base,
51+
pagila,
52+
text,
53+
bytestring,
54+
beam-core,
55+
beam-postgres,
56+
beam-migrate,
57+
postgresql-simple
58+
hs-source-dirs: app
5159
default-language: Haskell2010
5260

5361
test-suite pagila-test

beam-postgres/examples/readme.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
# Pagila example
22

3-
`cabal run` to see rendering of Postgres migration.
3+
There are two executables:
4+
5+
1. `cabal run` to see rendering of Postgres migration.
6+
7+
2. **Destructive**: apply migration to Postgres instance
8+
Hard-code your Postgres parameters into `app/RunMigration.hs` `ConnectInfo`.
9+
Then `cabal run pagila-migration`.
10+
11+
This will apply the migration to your database:
12+
```
13+
postgres=# \d
14+
List of relations
15+
Schema | Name | Type | Owner
16+
--------+-------------------------------+----------+----------
17+
public | actor | table | postgres
18+
public | actor_actor_id_seq | sequence | postgres
19+
public | address | table | postgres
20+
public | address_address_id_seq | sequence | postgres
21+
public | beam_migration | table | postgres
22+
public | beam_version | table | postgres
23+
public | category | table | postgres
24+
.
25+
.
26+
.
27+
```
28+
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
module Pagila.Schema
33
( module Pagila.Schema.V0002
4-
, migration, db ) where
4+
, migration, migrateDB, dbSettings ) where
5+
6+
import Database.PostgreSQL.Simple
57

68
import Pagila.Schema.V0002 hiding (migration)
79

@@ -13,11 +15,24 @@ import Control.Arrow ( (>>>) )
1315
import Database.Beam (DatabaseSettings)
1416
import Database.Beam.Migrate.Types ( CheckedDatabaseSettings, MigrationSteps, unCheckDatabase
1517
, evaluateDatabase, migrationStep)
16-
import Database.Beam.Postgres (Postgres)
18+
import Database.Beam.Postgres (Postgres, runBeamPostgresDebug)
19+
import Database.Beam.Migrate.Simple (BringUpToDateHooks, bringUpToDateWithHooks, defaultUpToDateHooks, runIrreversibleHook)
20+
import qualified Database.Beam.Postgres.Migrate as Pg
1721

1822
migration :: MigrationSteps Postgres () (CheckedDatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb)
1923
migration = migrationStep "Initial commit" V0001.migration >>>
2024
migrationStep "Add film actor, inventory, rental table" V0002.migration
2125

22-
db :: DatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb
23-
db = unCheckDatabase (evaluateDatabase migration)
26+
dbSettings :: DatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb
27+
dbSettings = unCheckDatabase (evaluateDatabase migration)
28+
29+
allowDestructive :: (MonadFail m) => BringUpToDateHooks m
30+
allowDestructive =
31+
defaultUpToDateHooks
32+
{ runIrreversibleHook = return True
33+
}
34+
35+
migrateDB :: Connection -> IO (Maybe (CheckedDatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb))
36+
migrateDB conn =
37+
runBeamPostgresDebug putStrLn conn
38+
$ bringUpToDateWithHooks allowDestructive Pg.migrationBackend migration

0 commit comments

Comments
 (0)