The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Doodle::Migrator

ABSTRACT

Database Migrator Class

SYNOPSIS

  # in lib/My/Migrator.pm

  package My::Migrator;

  use parent 'Doodle::Migrator';

  sub namespace {
    return 'My::Migration';
  }

  # in lib/My/Migration/Step1.pm

  package My::Migration::Step1;

  use parent 'Doodle::Migration';

  sub up {
    my ($self, $doodle) = @_;

    # add something ...

    return $doodle;
  }

  sub down {
    my ($self, $doodle) = @_;

    # subtract something ...

    return $doodle;
  }

  # in script

  package main;

  my $migrator = My::Migrator->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 which collects the specified Doodle::Migration classes and processes them.

METHODS

This package implements the following methods.

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() : [Object]

The migrations method find and loads child objects under the namespace and returns a set of Data::Object::Space objects representing classes that have subclassed the Doodle::Migration base class.

migrations example
  my $migrations = $self->migrations();

namespace

  namespace() : Str

The namespace method returns the root namespace where all child Doodle::Migration classes can be found.

namespace example
  my $namespace = $self->namespace();

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.

statements example
  my $statements = $self->statements('sqlite');