Doodle::Migration
Database Migration Class
# in lib/Migration.pm package Migration; use parent 'Doodle::Migration'; # in lib/My/Migration/Step1.pm package Migration::Step1; use parent 'Doodle::Migration'; no warnings 'redefine'; 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 Migration::Step2; use parent 'Doodle::Migration'; no warnings 'redefine'; 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; } # elsewhere package main; my $self = Migration->new; my $results = $self->migrate('up', 'sqlite', sub { my ($sql) = @_; # e.g. $dbi->do($_) for @$sql; return 1; }); 1;
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.
migrations
namespace
This package uses type constraints from:
Doodle::Library
This package implements the following methods:
down(Doodle $doodle) : Doodle
The migrate "DOWN" method is invoked automatically by the migrator Doodle::Migrator.
# given: synopsis my $doodle = Doodle->new; $doodle = $self->down($doodle);
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.
# given: synopsis my $migrate = $self->migrate('up', 'sqlite', sub { my ($sql) = @_; # do something ... return 1; });
migrations() : ArrayRef[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.
# given: synopsis my $doodle = Doodle->new; my $migrations = $self->migrations;
namespace() : Str
The namespace method returns the root namespace where all child Doodle::Migration classes can be found.
# given: synopsis my $namespace = $self->namespace;
statements(Str $grammar) : ArrayRef[Tuple[ArrayRef[Str], ArrayRef[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.
# given: synopsis my $statements = $self->statements('sqlite');
up(Doodle $doodle) : Doodle
The migrate "UP" method is invoked automatically by the migrator Doodle::Migrator.
# given: synopsis my $doodle = Doodle->new; $doodle = $self->up($doodle);
Al Newkirk, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2011-2019, Al Newkirk, et al.
This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated in the "license file".
Wiki
Project
Initiatives
Milestones
Contributing
Issues
To install Doodle, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Doodle
CPAN shell
perl -MCPAN -e shell install Doodle
For more information on module installation, please visit the detailed CPAN module installation guide.