| 1 | package Schedule::LongSteps::Storage::AutoDBIx; | ||
| 2 | $Schedule::LongSteps::Storage::AutoDBIx::VERSION = '0.023'; | ||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | extends qw/Schedule::LongSteps::Storage/; | ||
| 5 | |||
| 6 | 1 | ▶ | use Log::Any qw/$log/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Schedule::LongSteps::Storage::DBIxClass; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Schedule::LongSteps::Storage::AutoDBIx::Schema; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use DateTime; | ||
| 12 | |||
| 13 | has 'get_dbh' => ( is => 'ro', isa => 'CodeRef' , required => 1 ); | ||
| 14 | has 'auto_deploy' => ( is => 'ro', isa => 'Bool', default => 1 ); | ||
| 15 | |||
| 16 | has 'dbix_class_storage' => ( is => 'ro', isa => 'Schedule::LongSteps::Storage::DBIxClass' , lazy_build => 1 , handles => [qw/prepare_due_processes create_process find_process/]); | ||
| 17 | has 'schema' => ( is => 'ro', isa => 'Schedule::LongSteps::Storage::AutoDBIx::Schema' , lazy_build => 1); | ||
| 18 | |||
| 19 | sub _build_schema{ | ||
| 20 | my ($self) = @_; | ||
| 21 | my $schema = Schedule::LongSteps::Storage::AutoDBIx::Schema->connect( $self->get_dbh() ); | ||
| 22 | if( $self->auto_deploy() ){ | ||
| 23 | # Attempt selecting something from the resultset. | ||
| 24 | eval{ | ||
| 25 | my $count = $schema->resultset('LongstepProcess')->search(undef, { rows => 1 })->first(); | ||
| 26 | }; | ||
| 27 | if( my $err = $@ ){ | ||
| 28 | $log->warn("Got error: ".$err.". Will try to fix it by deploying the internal schema"); | ||
| 29 | $schema->deploy(); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | return $schema; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _build_dbix_class_storage{ | ||
| 36 | my ($self) = @_; | ||
| 37 | my $storage = Schedule::LongSteps::Storage::DBIxClass->new({ schema => $self->schema(), | ||
| 38 | resultset_name => 'LongstepProcess' | ||
| 39 | }); | ||
| 40 | } | ||
| 41 | |||
| 42 | =head1 NAME | ||
| 43 | |||
| 44 | Schedule::LongSteps::Storage::AutoDBIx - An automatically deployed storage. | ||
| 45 | |||
| 46 | =head1 DEPENDENCIES | ||
| 47 | |||
| 48 | To use this, you will have to add the following dependencies to your dependency manager: | ||
| 49 | |||
| 50 | L<DBIx::Class>, L<SQL::Translator>, L<DBIx::Class::InflateColumn::Serializer>, and one | ||
| 51 | of DateTime::Format::* matching your database. | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | First instantiate a storage with a subroutine returning a valid $dbh (from L<DBI> for instance, | ||
| 56 | or from your own L<DBIx::Class::Schema>)): | ||
| 57 | |||
| 58 | my $storage = Schedule::LongSteps::Storage::AutoDBIx->new({ | ||
| 59 | get_dbh => sub{ return a valid $dbh }, | ||
| 60 | }); | ||
| 61 | |||
| 62 | Note that this will automatically create a table named 'schedule_longsteps_process' | ||
| 63 | in your database. This is not configurable for now. That also means that building such | ||
| 64 | a storage is slow, so try to do it only once in your application. | ||
| 65 | |||
| 66 | Then build and use a L<Schedule::LongSteps> object: | ||
| 67 | |||
| 68 | my $long_steps = Schedule::LongSteps->new({ storage => $storage }); | ||
| 69 | |||
| 70 | ... | ||
| 71 | |||
| 72 | =head1 ATTRIBUTES | ||
| 73 | |||
| 74 | =over | ||
| 75 | |||
| 76 | =item get_dbh | ||
| 77 | |||
| 78 | A subroutine that returns a valid $dbh (from L<DBI>) database connection handle. Required. | ||
| 79 | |||
| 80 | =item auto_deploy | ||
| 81 | |||
| 82 | Set that to false if you dont want this to deploy its built in schema automatically. Defaults to 1. | ||
| 83 | |||
| 84 | =back | ||
| 85 | |||
| 86 | =head2 prepare_due_processes | ||
| 87 | |||
| 88 | See L<Schedule::LongSteps::Storage> | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | =head2 create_process | ||
| 93 | |||
| 94 | See L<Schedule::LongSteps::Storage> | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | =head2 find_process | ||
| 99 | |||
| 100 | See L<Schedule::LongSteps::Storage> | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | =head2 deploy | ||
| 105 | |||
| 106 | Deploys this in the given dbh. Use this only ONCE if 'auto_deploy' is false. | ||
| 107 | |||
| 108 | Usage: | ||
| 109 | |||
| 110 | $this->deploy(); | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub deploy{ | ||
| 115 | my ($self) = @_; | ||
| 116 | return $self->schema()->deploy(); | ||
| 117 | } | ||
| 118 | |||
| 119 | __PACKAGE__->meta->make_immutable(); | ||