NAME
Doodle::Migration
ABSTRACT
Database Migration Class
SYNOPSIS
# in lib/My/Migration.pm
package My::Migration;
use parent 'Doodle::Migration';
# in lib/My/Migration/Step1.pm
package My::Migration::Step1;
use parent 'Doodle::Migration';
sub up {
my ($self, $doodle) = @_;
my $users = $doodle->table('users');
$users->primary('id');
$users->string('email');
$users->create;
$users->index(columns => ['email'])->unique->create;
return $doodle;
}
sub down {
my ($self, $doodle) = @_;
my $users = $doodle->table('users');
$users->delete;
return $doodle;
}
# in lib/My/Migration/Step2.pm
package My::Migration::Step2;
use parent 'Doodle::Migration';
sub up {
my ($self, $doodle) = @_;
my $users = $doodle->table('users');
$users->string('first_name')->create;
$users->string('last_name')->create;
return $doodle;
}
sub down {
my ($self, $doodle) = @_;
my $users = $doodle->table('users');
$users->string('first_name')->delete;
$users->string('last_name')->delete;
return $doodle;
}
# in script
package main;
my $migrator = My::Migration->new;
my $results = $migrator->migrate('up', 'sqlite', sub {
my ($sql) = @_;
# e.g. $dbi->do($_) for @$sql;
return 1;
});
1;
DESCRIPTION
This package provides a migrator class and migration base class in one package. The migrations
method loads and collects the classes that exists as children of the namespace returned by the namespace
method (which defaults to the current class) and returns the class names as an array-reference.
METHODS
This package implements the following methods.
down
down(Doodle $doodle) : Doodle
The migrate "DOWN" method is invoked automatically by the migrator Doodle::Migrator.
migrate
migrate(Str $updn, Str $grammar, CodeRef $callback) : [Any]
The migrate method collects all processed statements and iterates over the "UP" or "DOWN" SQL statements, passing the set of SQL statements to the supplied callback with each iteration.
- migrate example
-
my $migrate = $self->migrate('up', 'sqlite', sub { my ($sql) = @_; # do something ... return 1; });
migrations
migrations() : [Str]
The migrations method finds and loads child objects under the namespace
and returns an array-reference which contains class names that have subclassed the Doodle::Migration base class.
namespace
namespace() : Str
The namespace method returns the root namespace where all child Doodle::Migration classes can be found.
statements
statements(Str $grammar) : [[[Str],[Str]]]
The statements method loads and processes the migrations using the grammar specified. This method returns a set of migrations, each containing a set of "UP" and "DOWN" sets of SQL statements.
up
up(Doodle $doodle) : Doodle
The migrate "UP" method is invoked automatically by the migrator Doodle::Migrator.