README.md

# strata

A library for managing schema migrations.

## Usage

Here is an example using the strata_sqlite3 driver along with the esqlite3 library.

Make your migration modules like this:

    -module(strata_create_todos).
    -behaviour(strata_migration).

    -export([up/0, down/0]).

    up() ->
        [<<"CREATE TABLE IF NOT EXISTS todos ">>,
         <<"  id INTEGER PRIMARY KEY AUTOINCREMENT, ">>,
         <<"  body TEXT NOT NULL ">>,
         <<")">>].

    down() ->
        <<"DROP TABLE IF EXISTS todos">>.

Once we have made our migration modules we can setup our strata configuration.
A configuration consists of a list of migrations, a driver, and a connection.

The migration list is in the format [{Module, Timestamp}]. The ascending order
of timestamps determines the order that the migrations will be run in.
While it is best practice to keep these migrations sorted, strata will sort them
before executing commands regardless.

Exodus provides a convenient function to get a timestamp:

    strata:timestamp().

The driver implements the strata_driver behavior for a given database.
You can use an existing driver, or make your own. It's just a module that implements a handful of functions.

Here is a list of drivers for common relational databases:

- [strata_sqlite3](https://git.sr.ht/~fancycade/strata_sqlite3)
- [strata_postgres](https://git.sr.ht/~fancycade/strata_postgres)
- [strata_mysql](https://git.sr.ht/~fancycade/strata_mysql)

That Connection is the last part. Some db libraries like pgo configure the connection statically so the connection is optional.

    {ok, Conn} = esqlite3:open("test.db").

    Migrations = [{strata_create_todos, 1234},
                  {strata_create_tags, 12345}].

    Config = strata:config(Migrations, strata_sqlite3, Conn).

To run the migrations simply use the run function with the config.

    strata:run(Config).

Now you can list all the migrations that have been run:

    strata:list(Config).

If you want to revert the last migration use revert:

    strata:revert(Config).

If you want to test that your latest migration's up and down methods are correct you can do this:

    strata:redo(Config).

This will run the down, and then the up.

## Build

    rebar3 compile

## Test

    rebar3 eunit
    rebar3 ct