You are looking at historical revision 21466 of this page. It may differ significantly from its current revision.

nomads

Introduction

This extension provides a simple framework for database migrations similar to those used in web frameworks like Ruby on Rails. It allows the user to keep different versions of database-schemas and manage the up- and down migration in a convenient manner.

The library aims to be simple to use, yet powerful enough to do more sophisticated migrations.

Examples

(use nomads nomads-sql-de-lite)

(migration-directory "./migrations")
(database-credentials "test.db")
(migrate)

Where a migration file inside the migration-directory may look something like this.

;; 1-create-tests.scm
((UP
   "CREATE TABLE tests (name string)")
 (DOWN
  "DROP TABLE tests"))

Authors

David Krentzlin

License

 Copyright (c) 2010 David Krentzlin 

 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

Documentation

The library does currently ship with a bind to sql-de-lite. This means that you can use it to manage your sqlite3 migrations. The postgresql-binding is a work in progress and will follow soon.

Configuration

There are some parameters that you might want to tweak to use migrations. There are also some parameters that allow you to tweak the behaviour of the library even more. See the versioning section for details. Most of the time though you'll only have to set two parameters for the library to function.

[parameter] database-credentials

Depending on the database-binding you use, this holds the connection-specification that will be passed to the binding's connect-procedure. See the documentation for sql-de-lite and postgresql for details.

[parameter] migration-directory

As the name suggests this parameter specifies where migration files can be found.

Versioning and filename format

The library allows you to implement your own custom versioning scheme. By default it uses a simple sequential scheme, that simple prefixes every migration with a numeric value. You're free to implement your own versioning scheme, for example to use timestamped versioning.

[parameter] file-pattern
[parameter] filename-partitioner
[parameter] filename-joiner
[parameter] versioner
[parameter] version?
[parameter] version-less?
[parameter] version-equal?
[parameter] version->string

The following example set's up a versioning scheme that uses timestamps

(use nomads nomads-sql-de-lite numbers)

(versioner (lambda (max-version)
            (inexact->exact (current-seconds))))

(filename-partitioner      
 (lambda (filename)
   (let ((parts (string-split filename "-")))
     (cond
      ((null? parts) (cons #f ""))
      ((string->number (car parts))
       => (lambda (num)
            (cons (number->string (inexact->exact num)) (string-join (cdr parts) "-"))))
      (else (cons #f filename))))))

(filename-joiner           
 (lambda (version file)
    (sprintf "~A-~A" version file)))

 ;;we need to bind it to the number's version of those
(version? number?)

(define (->number what)
  (inexact->exact (if (string? what) (string->number what) what)))

(version-less?
 (lambda (l r)
   (< (->number l) (->number r))))

(version-equal?
 (lambda (l r)
   (equal? (->number l) (->number r))))

Generating Migrations

Running Migrations

[procedure] (migrate #!key (version 'latest) (callback default-callback))