MyNixOS website logo
Description

Interpreter for SQL-structure definitions in YAML (YamSql)

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
schemas:
 - math
schema_dirs:
 - 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)

parameters:
 - name: p_n
   type: int
returns: int
---
RETURN (
  WITH RECURSIVE t AS (
      SELECT 1 AS f, 0 AS n
    UNION ALL
      SELECT f * (n + 1), n + 1 FROM t
  )
  SELECT f FROM t WHERE n=p_n LIMIT 1
);
---
# schema/math/function.d/erf.py
name: erf 
description: |
 Gauss error function

 This function is a wrapper for the
 Python 3 implementation.

parameters:
 - 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
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 \
 libghc-yaml-dev
Metadata

Version

0.9.0.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows