The hamsql package

[Tags:gpl, library, program, test]

Interpreter for SQL-structure definitions in YAML (YamSql)

[Skip to Readme]


Change log CHANGELOG
Dependencies aeson (==1.0.*), base (>=4.8 && <5.0), bytestring (==0.10.*), containers (==0.5.*), directory (==1.2.*), doctemplates (==0.1.*), file-embed (==0.0.*), filepath (==1.4.*), frontmatter (==0.1.*), groom (==0.1.*), hamsql, network-uri (==2.6.*), optparse-applicative (==0.13.*), postgresql-simple (>=0.4 && <0.6), text (==1.2.*), transformers (==0.5.*), unordered-containers (==0.2.*), yaml (==0.8.*) [details]
License GPL-3
Copyright (c) 2014-2016 Michael Herold et al.
Author Michael Herold <>
Maintainer Michael Herold <>
Category Database
Home page
Bug tracker
Source repository head: git clone
Uploaded Thu Oct 27 17:02:09 UTC 2016 by quabla
Distributions NixOS:
Downloads 83 total (1 in the last 30 days)
1 []
Status Docs available [build log]
Last success reported on 2016-10-29 [all 1 reports]




Maintainer's Corner

For package maintainers and hackage trustees

Readme for hamsql

Readme for hamsql-


An interpreter for SQL structure definitions in YAML (YamSql)

build status Hackage-Deps Hackage

About HamSql

HamSql is a software that parses SQL structures defined in a YAML based language and deploys them on PostgreSQL servers. It allows to maintain PostgreSQL projects in a form more similar to other programming languages.

In contrast to the CREATE OR REPLACE FUNCTION approaches, residual structures are deleted, column properties are deleted without explicit definition of the migration and the ordering imposed by dependencies is resolved automatically.

HamSql is on hackage, which means you can build it with cabal install hamsql. Please note the build requirements and details at the bottom. HamSql binaries for Linux amd64 are available as build artifacts from our build server.

HamSql can be used for

  • Neat SQL development with clearer versioning via "one object one file" principle
  • More flexible development with features like function and table templates
  • On site deployments of SQL structures
  • Off-line computation of upgrade strategies for known status quo
  • Documentation generation of SQL structures and APIs

What is there

  • hamsql install command that deploys the defined structure
  • hamsql upgrade command that updates existing structures
    • Per default no data are at risk during upgrade (no column/table deletion)
    • Overwrite via --permit-data-deletion possible
    • SQL command ordering that avoids dependency conflicts
    • Complicated dependencies are resolved via trial and error
  • hamsql doc creates a documentation of the complete sql structure
  • Code basis tailored for the support of many SQL features
  • Basic documentation of the YamSql Language

Coming soon

  • Support for views and triggers
  • Warn about name conflicts for SQL objects before deployment
  • Output defining YamSql file for each type of error

What is missing

Those are all things on the radar but the exact requirements are unclear or the workforce is missing.

  • Support for several other PostgreSQL features
  • Stable definition of YamSql
  • Support for renaming tables and columns
  • Covering other strategies required for upgrade
  • Unit and integration test as part of YamSql
    • Define scenarios that are loaded into the database
    • Define test and expected result for a function for a scenario
    • Define operations with expected outcome (to tests triggers etc.)

Example Project

The example project below could be deployed via

hamsql install -c postgresql:///dbname

Later changes can be pushed via

hamsql upgrade -c postgresql:///dbname

The default documentation can be written to docs/ via

hamsql doc

You can have a look at the output rendered via Sphinx.

Those are the YamSql files for the project:

# setup.yml
 - math
 - schemas
# schema/math/schema.yml
name: math
description: |
 Some basic math in SQL
# schema/math/function.d/factorial.plsql
name: factorial
description: |
 Factorial function using the
 ``WITH RECURSIVE`` SQL feature.

 Logical definition::

     f(0) = 1
     f(n) = n * f (n - 1)

 - name: p_n
   type: int
returns: int
      SELECT 1 AS f, 0 AS n
      SELECT f * (n + 1), n + 1 FROM t
# schema/math/function.d/
name: erf 
description: |
 Gauss error function

 This function is a wrapper for the
 Python 3 implementation.

 - name: x
   type: float
returns: float

language: plpython3u
import math
return math.erf(x)

Building HamSql on Debian Stretch

To completely build HamSql from source

apt install make ghc cabal-install libpq-dev happy
make install

To avoid compiling all the dependencies you can use the following set of debian packages instead of the above ones

apt install \
 make \
 ghc \
 cabal-install \
 libghc-aeson-dev \
 libghc-file-embed-dev \
 libghc-network-uri-dev \
 libghc-optparse-applicative-dev \
 libghc-pandoc-dev \
 libghc-postgresql-simple-dev \
 libghc-text-dev \
 libghc-unordered-containers-dev \