1:- module(bc_migrate, [
    2    bc_migrate/3 % +Name, +Description, :Goal
    3]).

Handles data migrations for the underlying docstore database. Migrations are run in the order of bc_migrate/3 calls. */

   11:- use_module(library(docstore)).   12:- use_module(library(debug)).   13
   14:- meta_predicate(bc_migrate(+, +, 0)).
 bc_migrate(+Name, +Desc, :Goal) is det
Executes the given migration in transaction. Remembers the migration by it's name. Migration names must be unique.
   22bc_migrate(Name, Description, Goal):-
   23    (   ds_find(migration, name=Name, [_])
   24    ->  true
   25    ;   Transaction = (Goal, bc_remember_migration(Name, Description)),
   26        debug(bc_migrate, 'running migration ~p', [Name]),
   27        ds_transactional(Transaction)).
 remember_migration(+Name, +Description) is det
Records the entry that the given migration has been run.
   33bc_remember_migration(Name, Description):-
   34    ds_insert(migration{ name: Name, description: Description })