| 1 | package DBIx::Class::StateMigrations::Migration::Routine; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: Base class for an individual routine within a migration | ||
| 7 | |||
| 8 | use Moo; | ||
| 9 | use Types::Standard qw(:all); | ||
| 10 | |||
| 11 | use Scalar::Util 'blessed'; | ||
| 12 | |||
| 13 | has 'Migration', is => 'ro', required => 1, isa => InstanceOf['DBIx::Class::StateMigrations::Migration']; | ||
| 14 | |||
| 15 | has 'routine_coderef', ( | ||
| 16 | is => 'ro', lazy => 1, | ||
| 17 | isa => CodeRef, | ||
| 18 | default => sub { (shift)->_get_routine_coderef } | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | sub executed { | ||
| 23 | my ($self, $set) = @_; | ||
| 24 | $self->__executed(1) if ($set && ! $self->__executed); | ||
| 25 | $self->__executed | ||
| 26 | } | ||
| 27 | has '__executed', is => 'rw', init_arg => undef, isa => Bool, default => sub { 0 }; | ||
| 28 | |||
| 29 | |||
| 30 | sub _get_routine_coderef { ... } | ||
| 31 | |||
| 32 | sub BUILD { | ||
| 33 | my $self = shift; | ||
| 34 | $self->CodeRef | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub execute { | ||
| 39 | my ($self, $db) = @_; | ||
| 40 | |||
| 41 | die "already executed!" if ($self->executed); | ||
| 42 | |||
| 43 | die "execute must be supplied connected DBIx::Class::Schema instance argument" | ||
| 44 | unless($db && blessed($db) && $db->isa('DBIx::Class::Schema')); | ||
| 45 | |||
| 46 | die "Supplied schema object is not connected" unless ($db->storage->connected); | ||
| 47 | |||
| 48 | my $ret = $self->routine_coderef->( $db, $self ); | ||
| 49 | $self->executed(1); | ||
| 50 | return $ret; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ |