StateMigrations.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 203 CC 8 SCAR 24.1
CC
8
Cov
0%
create_dump_blank_Migration
42.0
has_matched_migration
6.0
_validate_Migrations
6.0
CRAP 11.1
1 package DBIx::Class::StateMigrations;
2  
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: Schema migrations via checksums instead of versions
7  
8 our $VERSION = '0.001';
9  
10 use Moo;
11 use Types::Standard qw(:all);
12 use Scalar::Util 'blessed';
13 use Try::Tiny;
14  
15 use Path::Class qw/file dir/;
16 use DBIx::Class::Schema::Diff 1.11;
17  
18 use DBIx::Class::Schema::Diff::State;
19 use DBIx::Class::StateMigrations::SchemaState;
20 use DBIx::Class::StateMigrations::Migration;
21  
22 sub BUILD {
23 my $self = shift;
24 try{$self->connected_schema->storage->connected} or die join('',
25 'Supplied connected_schema "', $self->connected_schema, '" is not connected'
26 );
27 }
28  
29 sub load_migrations {
30 my $self = shift;
31 $self->_validate_Migrations
32 }
33  
34 sub load_current_state {
35 my $self = shift;
36 $self->current_SchemaState
37 }
38  
39 sub has_matched_migration {
40 my $self = shift;
41 $self->matched_Migration ? 1 : 0
42 }
43  
44 sub num_migrations { scalar(@{ (shift)->Migrations }) }
45 sub all_migrations { @{ (shift)->Migrations } }
46  
47  
48 sub execute_matched_Migration_routines {
49 my $self = shift;
50 my $callback = shift;
51
52 my $Migration = $self->matched_Migration or die join('',
53 'execute_matched_Migration(): no migration matched'
54 );
55
56 $Migration->execute_routines( $self->connected_schema, $callback )
57 }
58  
59  
60  
61  
62 has 'migrations_dir', is => 'ro', default => sub { undef };
63 has 'connected_schema', is => 'ro', required => 1, isa => InstanceOf['DBIx::Class::Schema'];
64  
65 has 'DBI_Driver_Name', is => 'ro', init_arg => undef, lazy => 1, default => sub {
66 my $self = shift;
67 $self->connected_schema->storage->dbh->{Driver}{Name}
68 }, isa => Str;
69  
70 has 'schema_class', is => 'ro', lazy => 1, init_arg => 1, default => sub {
71 my $self = shift;
72 blessed $self->connected_schema ? blessed $self->connected_schema : $self->connected_schema
73 };
74  
75 has 'loader_options', is => 'ro', default => sub {{
76 naming => { ALL => 'v7'},
77 use_namespaces => 1,
78 use_moose => 0,
79 debug => 0,
80 qualify_objects => 1
81 }}, isa => HashRef;
82  
83  
84 has 'diff_filters', is => 'ro', default => sub {[
85 filter_out => 'isa'
86 ]}, isa => ArrayRef;
87  
88  
89 has 'Migrations', is => 'ro', lazy => 1, default => sub {
90 my $self = shift;
91 if(my $dir = $self->migrations_dir) {
92 my $Dir = dir( $dir )->absolute;
93 -d $Dir or die "migrations dir '$dir' not found or is not a directory";
94
95 my @migrations = ();
96
97 for my $m_dir ($Dir->children) {
98 next unless $m_dir->is_dir;
99 my $Migration = DBIx::Class::StateMigrations::Migration
100 ->new_from_migration_dir($m_dir->absolute->stringify);
101 push @migrations, $Migration if ($Migration);
102 }
103 return \@migrations;
104 }
105 else {
106 return []
107 }
108 }, isa => ArrayRef[InstanceOf['DBIx::Class::StateMigrations::Migration']];
109  
110 sub _validate_Migrations {
111 my $self = shift;
112
113 my @wrong_driver =
114 grep { $self->DBI_Driver_Name ne $_->DBI_Driver_Name }
115 $self->all_migrations;
116
117 my $number_wrong = scalar(@wrong_driver);
118
119 die join('',
120 $number_wrong, ' of the loaded Migrations are for the wrong DBI driver ',
121 '(',$self->DBI_Driver_Name,'). Bad migrations are: ',
122 join(', ',map{ $_->migration_name . ' (driver: ' . $_->DBI_Driver_Name . ')'} @wrong_driver)
123 ) if ($number_wrong > 0);
124
125 return 1;
126 }
127  
128  
129 sub matched_and_executed {
130 my $self = shift;
131 $self->matched_Migration and $self->matched_Migration->routines_executed
132 }
133  
134  
135 has 'matched_Migration', is => 'ro', lazy => 1, default => sub {
136 my $self = shift;
137
138 $self->_validate_Migrations;
139
140 my @matches = ();
141
142 for my $Migration (@{ $self->Migrations }) {
143 push @matches, $Migration if (
144 $Migration->matches_SchemaState( $self->current_SchemaState )
145 );
146 }
147
148 my $match_count = scalar(@matches);
149
150 die join('',
151 "ERROR: $match_count loaded Migrations ",
152 "(",join(',',map { $_->migration_name } @matches),") ",
153 "matched the current_SchemaState. This is a bug - only one Migration should match"
154 ) if ($match_count > 1);
155
156 return $match_count == 1 ? $matches[0] : undef
157  
158 }, isa => Maybe[InstanceOf['DBIx::Class::StateMigrations::Migration']];
159  
160  
161 has 'connect_info_args', is => 'ro', lazy => 1, default => sub {
162 my $self = shift;
163 $self->connected_schema->storage->connect_info
164 }, isa => Ref;
165  
166 has 'loaded_schema_class', is => 'ro', lazy => 1, default => sub {
167 my $self = shift;
168
169 my $schema_class = $self->schema_class || 'ScannedSchmeaForMigration';
170
171 my $ref_class = join('_',$schema_class,'RefSchema',String::Random->new->randregex('[a-z0-9A-Z]{5}'));
172
173 DBIx::Class::Schema::Loader::make_schema_at(
174 $ref_class => $self->loader_options, $self->connect_info_args
175 ) or die "Loading schema failed";
176  
177 $ref_class
178 }, isa => Str;
179  
180  
181  
182 has 'current_SchemaState', is => 'ro', lazy => 1, default => sub {
183 my $self = shift;
184
185 my $State = DBIx::Class::Schema::Diff->state(
186 schema => $self->loaded_schema_class
187 );
188
189 DBIx::Class::StateMigrations::SchemaState->new(
190 DiffState => $State,
191 diff_filters => $self->diff_filters
192 )
193 }, isa => InstanceOf['DBIx::Class::StateMigrations::SchemaState'];
194  
195  
196 sub create_dump_blank_Migration {
197 my $self = shift;
198
199 die join('',
200 "create_dump_blank_Migration(): ERROR! An existing Migration already ",
201 "matched for the current SchemaState. This option is only avaialable when ",
202 "there are no matches to make it easy to start a new Migration"
203 ) if ($self->matched_Migration);
204
205 die join('',
206 "create_dump_blank_Migration(): Only available when using 'migrations_dir'"
207 ) unless ($self->migrations_dir);
208
209 my $Dir = dir( $self->migrations_dir )->absolute;
210
211
212 die join('',
213 "create_dump_blank_Migration(): 'migrations_dir' is set to '$Dir' but does not exist. ",
214 "Please run: mkdir -p $Dir and try again"
215 ) unless (-e $Dir);
216
217 die join('',
218 "create_dump_blank_Migration(): 'migrations_dir' '$Dir' exists but is not a directory"
219 ) unless (-d $Dir);
220
221 my $new_name = 'auto_' . $self->current_SchemaState->fingerprint;
222
223 my $BlankMigration = DBIx::Class::StateMigrations::Migration->new(
224 migration_name => $new_name,
225 trigger_SchemaStates => [$self->current_SchemaState],
226 DBI_Driver_Name => $self->DBI_Driver_Name
227 );
228
229 my $new_dir = dir( $Dir, $new_name )->absolute;
230
231 die join('',
232 "create_dump_blank_Migration(): New dir '$new_dir' already exists!"
233 ) if (-e $new_dir);
234
235 $new_dir->mkpath(1);
236
237 $BlankMigration->write_subclass_pm_file( "$new_dir" );
238
239 dir( $new_dir, 'routines' )->mkpath(1);
240 }
241  
242  
243  
244  
245 1;
246  
247 __END__