Skip to content

Commit

Permalink
Import from ameingast/postgresql-simple-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tvh committed Dec 14, 2016
0 parents commit a2abdc8
Show file tree
Hide file tree
Showing 15 changed files with 917 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .ghci
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:set -isrc
:set Wall
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
*.hi
*.o
*.swp
*.prof
dist/
.cabal-sandbox
cabal.sandbox.config
34 changes: 34 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
sudo: required

services:
- postgresql

before_script:
- psql -c 'create database test;' -U postgres

env:
- CABALVER=1.18 GHCVER=7.6.3
- CABALVER=1.18 GHCVER=7.8.4
- CABALVER=1.22 GHCVER=7.10.2
- CABALVER=1.24 GHCVER=8.0.1

before_install:
- travis_retry sudo add-apt-repository -y ppa:hvr/ghc
- travis_retry sudo apt-get update
- travis_retry sudo apt-get install cabal-install-$CABALVER ghc-$GHCVER
- export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH

install:
- cabal --version
- echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
- travis_retry cabal update
- cabal install --only-dependencies --enable-tests --enable-benchmarks

script:
- cabal configure --enable-tests --enable-benchmarks -v2
- cabal build
- cabal test
- cabal check
- cabal sdist
- SRC_TGZ=$(cabal info . | awk '{print $2;exit}').tar.gz &&
(cd dist && cabal install --force-reinstalls "$SRC_TGZ")
28 changes: 28 additions & 0 deletions Changelog.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Changelog

## 0.1.7.0
* Propagate migration and validation result to application exit code

## 0.1.6.0
* Support for GHC 8

## 0.1.5.0
* Bumped dependencies

## 0.1.4.0
* Improved error logging in standalone binary

## 0.1.3.0
* Better transaction handling
* Improved documentation

## 0.1.2.0
* Moved Util module
* Improved documentation

## 0.1.1.0
* Support for schema validations.
* Improved Haskell API

## 0.1.0.0
* Support for file-based and Haskell migrations.
30 changes: 30 additions & 0 deletions License
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2014, Andreas Meingast <[email protected]>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the author nor the names of his contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
163 changes: 163 additions & 0 deletions Readme.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# PostgreSQL Migrations for Haskell

[![Build Status](https://api.travis-ci.org/ameingast/postgresql-simple-migration.png)](https://travis-ci.org/ameingast/postgresql-simple-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.

## Why?
Database migrations should not be hard. They should be under version control
and documented in both your production systems and in your project files.

## What?
This library executes SQL/Haskell migration scripts and keeps track of their
meta information.

Scripts are be executed exactly once and any changes to scripts will cause
a run-time error notifying you of a corrupted database.

The meta information consists of:
* an MD5 checksum of the executed script to make sure already existing
scripts cannot be modified in your production system.
* a time-stamp of the date of execution so you can easily track when a change
happened.

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.

## Compilation and Tests
The program is built with the _cabal_ build system. The following command
builds the library, the standalone binary and the test package.

```bash
cabal configure --enable-tests && cabal build -j
```

To execute the tests, you need a running PostgreSQL server with an empty
database called _test_. Tests are executed through cabal as follows:

```bash
cabal configure --enable-tests && cabal test
```

To build the project in a cabal sandbox, use the following code:

```bash
cabal sandbox init
cabal install -j --only-dependencies --enable-tests --disable-documentation
cabal configure --enable-tests
cabal test
```

To remove the generated cabal sandbox, use:
```bash
cabal sandbox delete
```

## To Do
* Collect executed scripts and check if already executed scripts have been
deleted.
2 changes: 2 additions & 0 deletions Setup.lhs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> import Distribution.Simple
> main = defaultMain
74 changes: 74 additions & 0 deletions postgresql-simple-migration.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: postgresql-simple-migration
version: 0.1.7.0
synopsis: PostgreSQL Schema Migrations
homepage: https://github.com/ameingast/postgresql-simple-migration
Bug-reports: https://github.com/ameingast/postgresql-simple-migration/issues
license: BSD3
license-file: License
author: Andreas Meingast <[email protected]>
maintainer: Andreas Meingast <[email protected]>
copyright: 2014-2016, Andreas Meingast
category: Database
build-type: Simple
cabal-version: >= 1.10
description: A PostgreSQL-simple schema migration utility

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

source-repository head
type: git
location: git://github.com/ameingast/postgresql-simple-migration

Library
exposed-modules: Database.PostgreSQL.Simple.Migration
Database.PostgreSQL.Simple.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,
cryptohash >= 0.11 && < 0.12,
directory >= 1.2 && < 1.3,
postgresql-simple >= 0.4 && < 0.6,
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
ghc-options: -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns
default-extensions: OverloadedStrings, CPP, LambdaCase
default-language: Haskell2010
type: exitcode-stdio-1.0
build-depends: base >= 4.6 && < 5.0,
bytestring >= 0.10 && < 0.11,
postgresql-simple >= 0.4 && < 0.6,
hspec >= 2.2 && < 2.3,
postgresql-simple-migration >= 0.1 && < 0.2
1 change: 1 addition & 0 deletions share/test/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table t3 (c3 varchar);
1 change: 1 addition & 0 deletions share/test/scripts/1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table t2 (c2 varchar);
Loading

0 comments on commit a2abdc8

Please sign in to comment.