diff --git a/beam-postgres/examples/app/RunMigration.hs b/beam-postgres/examples/app/RunMigration.hs new file mode 100644 index 00000000..5c72acd8 --- /dev/null +++ b/beam-postgres/examples/app/RunMigration.hs @@ -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 diff --git a/beam-postgres/examples/pagila.cabal b/beam-postgres/examples/pagila.cabal index 1256b49a..b5f03bdc 100644 --- a/beam-postgres/examples/pagila.cabal +++ b/beam-postgres/examples/pagila.cabal @@ -22,6 +22,7 @@ library scientific, bytestring, text, + exceptions, postgresql-simple, beam-core, beam-postgres, @@ -32,10 +33,6 @@ library executable pagila import: warnings main-is: Main.hs - - -- Modules included in this executable, other than Main. - -- other-modules: - build-depends: base, pagila, text, @@ -43,11 +40,22 @@ executable pagila 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 diff --git a/beam-postgres/examples/readme.md b/beam-postgres/examples/readme.md index b6cd3dfd..5fa5de39 100644 --- a/beam-postgres/examples/readme.md +++ b/beam-postgres/examples/readme.md @@ -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 +. +. +. +``` + diff --git a/beam-postgres/examples/src/Pagila/Schema.hs b/beam-postgres/examples/src/Pagila/Schema.hs index 17c3bc49..ded6e010 100644 --- a/beam-postgres/examples/src/Pagila/Schema.hs +++ b/beam-postgres/examples/src/Pagila/Schema.hs @@ -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) @@ -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