NAME

Migraine - DB schema MIGRAtor that takes headache out of the game

SYNOPSIS

use Migraine;
$migrator = Migraine->new($dsn);
$migrator = Migraine->new($dsn, user => 'dbuser', password => 's3kr3t');
$migrator = Migraine->new($dsn, migrations_dir => 'migrations');
$migrator->migrate;                 # Latest version
$migrator->migrate(version => $v);  # Custom version
$migrator->migrate(before_migrate => sub {
                                        my ($version, $path) = @_;
                                        # ...
                                     });
$migrator->migrate(after_migrate => sub {
                                        my ($version, $path) = @_
                                        # ...
                                    });

DESCRIPTION

DB schema migrator (migraine) implementation module. See migraine for more details.

To be able to know the current version of the DB schema in a given database, the Migraine module maintains certain meta information in it, in special tables called migraine_*. These tables shouldn't be deleted or modified in any way.

METHODS

dbh

Returns a DB handler.

migraine_metadata_present

Returns if the migraine metadata is already present in the target database (needed for migraine to work).

migraine_metadata_usable

Returns if the migraine metadata is already present in the target database, and that it's the correct format/version.

migraine_metadata_version

Returns the migraine metadata version for the target database.

create_migraine_metadata

Creates the migraine metadata in the target metadata, if it's not already there.

upgrade_migraine_metadata

Upgrades the migraine metadata if it's an older version. If it doesn't exist at all, it's created. It dies if the metadata has a future version.

latest_version

Returns the latest migration version in the specified migrations dir.

current_version

This method is deprecated. Now, thinking in terms of the version of the database is obsolete: now a database has a list of migrations applied to it. It returns the highest id of the applied migrations. It returns 0 if the migraine metadata is not in place.

migration_applied($id)

Returns if the given migration is already applied.

migrate(%options)

Migrates the database. The valid keys for the %options hash are:

version

Specifies the version to migrate to (instead of just using the latest one)

before_migrate, after_migrate

Subroutines to be executed before and after each migration. When they are called, they are passed two parameters: the version of the migration to be/just executed, and the path for the migration file to be/just executed.

no_act

Specifies that no migrations should be actually executed in the DB. The hooks will be executed, though.

skip_missing_migrations

Specifies that if a migration doesn't exist (or there is more than one migration with the same id or something similar) it will just be skipped instead of producing an error and stopping execution.

apply_migration($id, %user_opts)

Applies the given migration ($id). Supports the following options: before_migrate, after_migrate, no_act and skip_missing_migrations. See the migrate documentation for details.

applied_migrations

Returns the list of migration ids that have been already applied.

applied_migration_ranges

Returns a list of strings representing version ranges of applied migrations. For example, if the applied migration list is (1,2,3,8,10,11), it will return ("1-3","8","10-11").

VERSION

0.54

LICENSE AND COPYRIGHT

This code is offered under the Open Source BSD license.

Copyright (c) 2009, Opera Software. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of Opera Software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

DISCLAIMER OF WARRANTY

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.