Skip to content

Commit

Permalink
example applies migration to real Postgres instance
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbecich committed Dec 25, 2024
1 parent 13065ad commit 9c1c872
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
18 changes: 18 additions & 0 deletions beam-postgres/examples/app/RunMigration.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module RunMigration where

import Prelude
import Database.PostgreSQL.Simple (connect, close, ConnectInfo(..))
import Pagila.Schema (migrateDB)

-- https://hackage.haskell.org/package/postgresql-simple-0.7.0.0/docs/Database-PostgreSQL-Simple.html#t:ConnectInfo
connInfo :: ConnectInfo
connInfo = ConnectInfo "localhost" (read "5432") "postgres" "foo" "postgres"

main :: IO ()
main = do
putStrLn "Running migration..."
c <- connect connInfo

_ <- migrateDB c

close c
22 changes: 15 additions & 7 deletions beam-postgres/examples/pagila.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ library
scientific,
bytestring,
text,
exceptions,
postgresql-simple,
beam-core,
beam-postgres,
Expand All @@ -32,22 +33,29 @@ library
executable pagila
import: warnings
main-is: Main.hs

-- Modules included in this executable, other than Main.
-- other-modules:

build-depends: base,
pagila,
text,
bytestring,
beam-core,
beam-postgres,
beam-migrate

-- Directories containing source files.
hs-source-dirs: app
default-language: Haskell2010

-- Base language which the package is written in.
executable pagila-migration
import: warnings
main-is: RunMigration.hs
ghc-options: -main-is RunMigration
build-depends: base,
pagila,
text,
bytestring,
beam-core,
beam-postgres,
beam-migrate,
postgresql-simple
hs-source-dirs: app
default-language: Haskell2010

test-suite pagila-test
Expand Down
29 changes: 28 additions & 1 deletion beam-postgres/examples/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# Pagila example

`cabal run` to see rendering of Postgres migration.
There are two executables:

### 1. `cabal run`
`cabal run` to see rendering of Postgres migration. This converts the Haskell to SQL statements and prints these to the console.

### 2. Destructive: apply migration to Postgres instance

Hard-code your Postgres connection parameters into `app/RunMigration.hs` `connInfo`.
Then `cabal run pagila-migration`. *This will overwrite your Postgres data*.

The result will be the Pagila database schema in your Postgres instance:
```
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+-------------------------------+----------+----------
public | actor | table | postgres
public | actor_actor_id_seq | sequence | postgres
public | address | table | postgres
public | address_address_id_seq | sequence | postgres
public | beam_migration | table | postgres
public | beam_version | table | postgres
public | category | table | postgres
.
.
.
```

23 changes: 19 additions & 4 deletions beam-postgres/examples/src/Pagila/Schema.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{-# LANGUAGE OverloadedStrings #-}
module Pagila.Schema
( module Pagila.Schema.V0002
, migration, db ) where
, migration, migrateDB, dbSettings ) where

import Database.PostgreSQL.Simple

import Pagila.Schema.V0002 hiding (migration)

Expand All @@ -13,11 +15,24 @@ import Control.Arrow ( (>>>) )
import Database.Beam (DatabaseSettings)
import Database.Beam.Migrate.Types ( CheckedDatabaseSettings, MigrationSteps, unCheckDatabase
, evaluateDatabase, migrationStep)
import Database.Beam.Postgres (Postgres)
import Database.Beam.Postgres (Postgres, runBeamPostgresDebug)
import Database.Beam.Migrate.Simple (BringUpToDateHooks, bringUpToDateWithHooks, defaultUpToDateHooks, runIrreversibleHook)
import qualified Database.Beam.Postgres.Migrate as Pg

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

db :: DatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb
db = unCheckDatabase (evaluateDatabase migration)
dbSettings :: DatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb
dbSettings = unCheckDatabase (evaluateDatabase migration)

allowDestructive :: (MonadFail m) => BringUpToDateHooks m
allowDestructive =
defaultUpToDateHooks
{ runIrreversibleHook = return True
}

migrateDB :: Connection -> IO (Maybe (CheckedDatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb))
migrateDB conn =
runBeamPostgresDebug putStrLn conn
$ bringUpToDateWithHooks allowDestructive Pg.migrationBackend migration

0 comments on commit 9c1c872

Please sign in to comment.