Skip to content

Commit

Permalink
Port to Hasql
Browse files Browse the repository at this point in the history
  • Loading branch information
tvh committed Dec 14, 2016
1 parent a2abdc8 commit 8430a50
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 715 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
dist/
.cabal-sandbox
cabal.sandbox.config
stack.yaml
.stack-work
28 changes: 0 additions & 28 deletions Changelog.markdown

This file was deleted.

111 changes: 6 additions & 105 deletions Readme.markdown
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
# PostgreSQL Migrations for Haskell

[![Build Status](https://api.travis-ci.org/ameingast/postgresql-simple-migration.png)](https://travis-ci.org/ameingast/postgresql-simple-migration)
[![Build Status](https://api.travis-ci.org/tvh/hasql-migration.png)](https://travis-ci.org/tvh/hasql-migration)

Welcome to postgresql-simple-migrations, a tool for helping you with
PostgreSQL schema migrations.

This project is an open-source database migration tool. It favors simplicity
over configuration.

It is implemented in Haskell and uses the (excellent) postgresql-simple
library to communicate with PostgreSQL.

It comes in two flavors: a library that features an easy to use Haskell
API and as a standalone application.

Database migrations can be written in SQL (in this case PostgreSQL-sql)
or in Haskell.
Welcome to hasql-migrations, a tool for helping you with
PostgreSQL schema migrations. This is a port of
[postgresql-simple-migration](https://github.com/ameingast/postgresql-simple-migration)
for use with hasql.

## Why?
Database migrations should not be hard. They should be under version control
Expand All @@ -38,96 +28,7 @@ This library also supports migration validation so you can ensure (some)
correctness before your application logic kicks in.

## How?
This utility can be used in two ways: embedded in your Haskell program or as
a standalone binary.

### Standalone
The standalone program supports file-based migrations. To execute all SQL-files
in a directory $BASE\_DIR, execute the following command to initialize the database
in a first step.

```bash
CON="host=$host dbname=$db user=$user password=$pw"
./dist/build/migrate/migrate init $CON
./dist/build/migrate/migrate migrate $CON $BASE_DIR
```

To validate already executed scripts, execute the following:
```bash
CON="host=$host dbname=$db user=$user password=$pw"
./dist/build/migrate/migrate init $CON
./dist/build/migrate/migrate validate $CON $BASE_DIR
```

For more information about the PostgreSQL connection string, see:
[libpq-connect](http://www.postgresql.org/docs/9.3/static/libpq-connect.html).

### Library
The library supports more actions than the standalone program.

Initializing the database:

```haskell
main :: IO ()
main = do
let url = "host=$host dbname=$db user=$user password=$pw"
con <- connectPostgreSQL (BS8.pack url)
withTransaction con $ runMigration $
MigrationContext MigrationInitialization True con
```

For file-based migrations, the following snippet can be used:

```haskell
main :: IO ()
main = do
let url = "host=$host dbname=$db user=$user password=$pw"
let dir = "."
con <- connectPostgreSQL (BS8.pack url)
withTransaction con $ runMigration $
MigrationContext (MigrationDirectory dir) True con
```

To run Haskell-based migrations, use this:

```haskell
main :: IO ()
main = do
let url = "host=$host dbname=$db user=$user password=$pw"
let name = "my script"
let script = "create table users (email varchar not null)";
con <- connectPostgreSQL (BS8.pack url)
withTransaction con $ runMigration $
MigrationContext (MigrationScript name script) True con
```

Validations wrap _MigrationCommands_. This means that you can re-use all
MigrationCommands to perform a read-only validation of your migrations.

To perform a validation on a directory-based migration, you can use the
following code:

```haskell
main :: IO ()
main = do
let url = "host=$host dbname=$db user=$user password=$pw"
con <- connectPostgreSQL (BS8.pack url)
withTransaction con $ runMigration $ MigrationContext
(MigrationValidation (MigrationDirectory dir)) True con
```

Database migrations should always be performed in a transactional context.

The standalone binary takes care of proper transaction handling automatically.

The library does not make any assumptions about the current transactional state
of the system. This means that the caller of the library has to take care of
opening/closing/rolling-back transactions. This way you can execute multiple
migration-commands or validations in sequence while still staying in the
transaction you opened.

The tests make use of this. After executing all migration-tests, the
transaction is rolled back.
TODO

## Compilation and Tests
The program is built with the _cabal_ build system. The following command
Expand Down
47 changes: 16 additions & 31 deletions postgresql-simple-migration.cabal → hasql-migration.cabal
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: postgresql-simple-migration
version: 0.1.7.0
name: hasql-migration
version: 0.1.0
synopsis: PostgreSQL Schema Migrations
homepage: https://github.com/ameingast/postgresql-simple-migration
Bug-reports: https://github.com/ameingast/postgresql-simple-migration/issues
homepage: https://github.com/tvh/hasql-migration
Bug-reports: https://github.com/tvh/hasql-migration/issues
license: BSD3
license-file: License
author: Andreas Meingast <[email protected]>
maintainer: Andreas Meingast <[email protected]>
maintainer: Timo von Holtz <[email protected]>
copyright: 2014-2016, Andreas Meingast
category: Database
build-type: Simple
Expand All @@ -17,12 +17,6 @@ extra-source-files: License
Readme.markdown
Changelog.markdown

src/*.hs
src/Database/PostgreSQL/Simple/*.hs

test/*.hs
test/Database/PostgreSQL/Simple/*.hs

share/test/*.sql
share/test/scripts/*.sql

Expand All @@ -31,35 +25,24 @@ source-repository head
location: git://github.com/ameingast/postgresql-simple-migration

Library
exposed-modules: Database.PostgreSQL.Simple.Migration
Database.PostgreSQL.Simple.Util
exposed-modules: Hasql.Migration
Hasql.Migration.Util
hs-source-dirs: src
ghc-options: -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns
default-extensions: OverloadedStrings, CPP, LambdaCase
default-language: Haskell2010
build-depends: base >= 4.6 && < 5.0,
base64-bytestring >= 1.0 && < 1.1,
bytestring >= 0.10 && < 0.11,
contravariant,
cryptohash >= 0.11 && < 0.12,
data-default-class,
directory >= 1.2 && < 1.3,
postgresql-simple >= 0.4 && < 0.6,
hasql >= 0.19 && < 0.20,
hasql-transaction >= 0.4 && < 0.5,
text,
time >= 1.4 && < 1.7

Executable migrate
main-is: Main.hs
hs-source-dirs: src
ghc-options: -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns
default-extensions: OverloadedStrings, CPP, LambdaCase
default-language: Haskell2010
build-depends: base >= 4.6 && < 5.0,
base64-bytestring >= 1.0 && < 1.1,
bytestring >= 0.10 && < 0.11,
cryptohash >= 0.11 && < 0.12,
directory >= 1.2 && < 1.3,
postgresql-simple >= 0.4 && < 0.6,
time >= 1.4 && < 1.7,
text >= 1.2 && < 1.3

test-suite tests
main-is: Main.hs
hs-source-dirs: test
Expand All @@ -69,6 +52,8 @@ test-suite tests
type: exitcode-stdio-1.0
build-depends: base >= 4.6 && < 5.0,
bytestring >= 0.10 && < 0.11,
postgresql-simple >= 0.4 && < 0.6,
hasql >= 0.19 && < 0.20,
hasql-migration >= 0.1 && < 0.2,
hasql-transaction >= 0.4 && < 0.5,
hspec >= 2.2 && < 2.3,
postgresql-simple-migration >= 0.1 && < 0.2
transformers
Loading

0 comments on commit 8430a50

Please sign in to comment.