You are looking at historical revision 21488 of this page. It may differ significantly from its current revision.
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.
(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"))
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.
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.
The migrationfiles must match the following layout
((UP "statement1" "statement2" "statementx") (DOWN "statement1down" "statement2down" "statementxdown"))
Depending on the direction, the library will either execute the statements in the DOWN or in the UP section of the file. There are two special kinds of statements.
Those can be used to implement migrations that can not be expressed by simple sql-statements
((UP ,(lambda (connection) (do-something-difficult connection)) (DOWN "statement1"))
If a #f is encountered the migration is interpreted as irreversible. An irreversible migration can not be reverted. In such a case, the library stops at this migration and does not attempt to revert earlier migrations.
((UP "statement1") (DOWN #f))
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] filename-pattern
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[procedure] (generate-migration name)
The procedure generates a migration-stub inside migration-directory with the given name. It returns the full name of the migration-file including the version.
Running Migrations[procedure] (migrate #!key (version 'latest) (callback default-callback))
This procedure starts the migration process. It migrates from the currently active version to version. Depending on the active version the migrations run downwards or upwards executing the DOWN and UP portion of the files respectively. After each check-point (a single migration) callback is invoked. If you do not specify this argument the default-handler will just print which migration have been executed and their status.