No description
Find a file
2017-11-15 16:06:50 +07:00
project Add postgresql support, fix tests 2017-11-14 10:56:19 +07:00
src Use ocaml like module system 2017-11-15 15:19:21 +07:00
tests Add documentation, fix some names 2017-11-15 13:34:15 +07:00
.gitignore Add postgresql support, fix tests 2017-11-14 10:56:19 +07:00
.travis.yml Add CI (#1) 2017-11-15 15:59:15 +07:00
config.nims Add postgresql support, fix tests 2017-11-14 10:56:19 +07:00
dbschema.nimble Add documentation, fix some names 2017-11-15 13:34:15 +07:00
LICENSE Initial commit 2017-11-13 13:25:50 +07:00
README.md Fix type in README.md 2017-11-15 16:06:50 +07:00

dbschema

Build Status

Database schema migration library for Nim language.

Usage

First of all, create the migrations:

let migrations0 = asList(
  initMigration(initVersion(1, 0), "Create table t1",
    sql"""
      create table t1(
        id integer primary key,
        name text
      );
    """
  ),
  initMigration(initVersion(1, 1), "Create table t2",
    sql"""
      create table t2(
        id integer primary key,
        name text
      );
    """
  )
)

when defined(debug):
  let migrations = migrations0 ++ asList(
    initMigration(initVersion(1, 0, 1), "Insert records to t1",
      sql"""
        insert into t1 values(1, 'a');
        insert into t1 values(2, 'b');
      """
    ),
    initMigration(initVersion(1, 1, 1), "Insert records to t2",
      sql"""
        insert into t2 values(10, 'A');
        insert into t2 values(20, 'B');
      """
    )
  )
else:
  let migrations = migrations0

And then just migrate database using already opened connection. Also you must provide the full name of the table that will contain the migrations configuration. The configurations schema and the table will be created if needed.

let conn: DbConn = ???
let migrationsConfig = "schema.schema_migtarions"

conn.migrate(migrations, migrationsConfig).run

Also, we can check, if the migration is needed without running it:

if conn.isMigrationsUpToDate(migrations, migrationsConfig).run:
  # DB is up to date
else:
  # DB is out of date

The library can be used in both imperative and functional styles (with nimfp library). Example:

let theProgram = act do:
  conn <- createConnection()

  conn.migrate(migrations, migrationsConfig)
  
  doSomethingWithConnection(conn)
  
  tryM conn.close
  
  yield ()
  
theProgram.run

DB backends.

For now, dbschema supports two backends: sqlite and postgresql. By default, it uses sqlite. For postgresql backend, use the command line switch -d:dbschemaPostgres.