StateMigrations.pm untested

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 255 CC 14 SCAR 27.7
CC
14
Cov
0%
_validate_Migrations
42.0
create_dump_blank_Migration
42.0
DEMOLISH
12.0
CRAP 16.0
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.002';
9  
10 use Moo;
11 use Types::Standard qw(:all);
12 use Scalar::Util 'blessed';
13 use Try::Tiny;
14 use Class::Unload;
15 use Class::Inspector;
16  
17 use Path::Class qw/file dir/;
18 use DBIx::Class::Schema::Diff 1.11;
19  
20 use DBIx::Class::Schema::Diff::State;
21 use DBIx::Class::StateMigrations::SchemaState;
22 use DBIx::Class::StateMigrations::Migration;
23  
24 sub BUILD {
25 my $self = shift;
26 try{$self->connected_schema->storage->connected} or die join('',
27 'Supplied connected_schema "', $self->connected_schema, '" is not connected'
28 );
29 }
30  
31 sub load_migrations {
32 my $self = shift;
33 $self->_validate_Migrations
34 }
35  
36 sub load_current_state {
37 my $self = shift;
38 $self->current_SchemaState
39 }
40  
41 sub has_matched_migration {
42 my $self = shift;
43 $self->matched_Migration ? 1 : 0
44 }
45  
46 sub num_migrations { scalar(@{ (shift)->Migrations }) }
47 sub all_migrations { @{ (shift)->Migrations } }
48  
49  
50 sub execute_matched_Migration_routines {
51 my $self = shift;
52 my $callback = shift;
53
54 my $Migration = $self->matched_Migration or die join('',
55 'execute_matched_Migration(): no migration matched'
56 );
57
58 $Migration->execute_routines( $self->connected_schema, $callback )
59 }
60  
61 has '__loaded_vagrant_classes', is => 'ro', isa => HashRef, default => sub {{}};
62  
63  
64 has 'migrations_dir', is => 'ro', default => sub { undef };
65 has 'connected_schema', is => 'ro', required => 1, isa => InstanceOf['DBIx::Class::Schema'];
66  
67 has 'DBI_Driver_Name', is => 'ro', init_arg => undef, lazy => 1, default => sub {
68 my $self = shift;
69 $self->connected_schema->storage->dbh->{Driver}{Name}
70 }, isa => Str;
71  
72 has 'schema_class', is => 'ro', lazy => 1, init_arg => 1, default => sub {
73 my $self = shift;
74 blessed $self->connected_schema ? blessed $self->connected_schema : $self->connected_schema
75 };
76  
77 has 'loader_options', is => 'ro', default => sub {{
78 naming => { ALL => 'v7'},
79 use_namespaces => 1,
80 use_moose => 0,
81 debug => 0,
82 qualify_objects => 1
83 }}, isa => HashRef;
84  
85  
86 has 'diff_filters', is => 'ro', default => sub {[
87 filter_out => 'isa'
88 ]}, isa => ArrayRef;
89  
90  
91 has 'Migrations', is => 'ro', lazy => 1, default => sub {
92 my $self = shift;
93 if(my $dir = $self->migrations_dir) {
94 my $Dir = dir( $dir )->absolute;
95 -d $Dir or die "migrations dir '$dir' not found or is not a directory";
96
97 my @migrations = ();
98
99 local $DBIx::Class::StateMigrations::Migration::LOADED_MIGRATION_SUBCLASSES = $self->__loaded_vagrant_classes;
100
101 for my $m_dir ($Dir->children) {
102 next unless $m_dir->is_dir;
103 my $Migration = DBIx::Class::StateMigrations::Migration
104 ->new_from_migration_dir($m_dir->absolute->stringify,$self->DBI_Driver_Name);
105 push @migrations, $Migration if ($Migration);
106 }
107 return \@migrations;
108 }
109 else {
110 return []
111 }
112 }, isa => ArrayRef[InstanceOf[
113 'DBIx::Class::StateMigrations::Migration',
114 'DBIx::Class::StateMigrations::Migration::Invalid'
115 ]];
116  
117 sub _validate_Migrations {
118 my $self = shift;
119
120 my (@good,@warns,@dies);
121 my @all = $self->all_migrations;
122  
123 for (@all) {
124 ! $_->invalid ? push(@good, $_) :
125 ! $_->fatal ? push(@warns,$_) :
126 push(@dies, $_)
127 }
128
129 my %num = (
130 all => scalar(@all),
131 good => scalar(@good),
132 warns => scalar(@warns),
133 dies => scalar(@dies)
134 );
135
136 die join("\n",'',
137 " *** Attempted to load $num{all} Migrations:",
138 " - $num{good} loaded.",
139 " - $num{warns} not loaded but ignored" . ($num{warns} ? ':'.join("\n",'',
140 map { " * ".$_->reason } @warns) : ()),
141 " - $num{dies} not loaded due to fatal errors:",
142 ( map { " * ".$_->reason } @dies ),'','',''
143 ) if ($num{dies});
144
145 if ($num{warns}) {
146 warn join("\n",
147 " *** Ignoring $num{warns} of the $num{all} total identified Migrations:",
148 (map { " - ".$_->reason } @warns),'',
149 );
150 @{$self->Migrations} = @good;
151 }
152
153 return 1;
154 }
155  
156  
157 sub matched_and_executed {
158 my $self = shift;
159 $self->matched_Migration and $self->matched_Migration->routines_executed
160 }
161  
162  
163 has 'matched_Migration', is => 'ro', lazy => 1, default => sub {
164 my $self = shift;
165
166 $self->_validate_Migrations;
167
168 my @matches = ();
169
170 for my $Migration (@{ $self->Migrations }) {
171 push @matches, $Migration if (
172 $Migration->matches_SchemaState( $self->current_SchemaState )
173 );
174 }
175
176 my $match_count = scalar(@matches);
177
178 die join('',
179 "ERROR: $match_count loaded Migrations ",
180 "(",join(',',map { $_->migration_name } @matches),") ",
181 "matched the current_SchemaState. This is a bug - only one Migration should match"
182 ) if ($match_count > 1);
183
184 return $match_count == 1 ? $matches[0] : undef
185  
186 }, isa => Maybe[InstanceOf['DBIx::Class::StateMigrations::Migration']];
187  
188  
189 has 'connect_info_args', is => 'ro', lazy => 1, default => sub {
190 my $self = shift;
191 $self->connected_schema->storage->connect_info
192 }, isa => Ref;
193  
194 has 'loaded_schema_class', is => 'ro', lazy => 1, default => sub {
195 my $self = shift;
196
197 my $schema_class = $self->schema_class || 'ScannedSchmeaForMigration';
198
199 my $ref_class = join('_',$schema_class,'RefSchema',String::Random->new->randregex('[a-z0-9A-Z]{5}'));
200
201 DBIx::Class::Schema::Loader::make_schema_at(
202 $ref_class => $self->loader_options, $self->connect_info_args
203 ) or die "Loading schema failed";
204
205 $self->__loaded_vagrant_classes->{$ref_class}++;
206  
207 $ref_class
208 }, isa => Str;
209  
210  
211  
212 has 'current_SchemaState', is => 'ro', lazy => 1, default => sub {
213 my $self = shift;
214
215 my $State = DBIx::Class::Schema::Diff->state(
216 schema => $self->loaded_schema_class
217 );
218
219 DBIx::Class::StateMigrations::SchemaState->new(
220 DiffState => $State,
221 diff_filters => $self->diff_filters
222 )
223 }, isa => InstanceOf['DBIx::Class::StateMigrations::SchemaState'];
224  
225  
226 sub create_dump_blank_Migration {
227 my $self = shift;
228
229 die join('',
230 "create_dump_blank_Migration(): ERROR! An existing Migration already ",
231 "matched for the current SchemaState. This option is only avaialable when ",
232 "there are no matches to make it easy to start a new Migration"
233 ) if ($self->matched_Migration);
234
235 die join('',
236 "create_dump_blank_Migration(): Only available when using 'migrations_dir'"
237 ) unless ($self->migrations_dir);
238
239 my $Dir = dir( $self->migrations_dir )->absolute;
240
241
242 die join('',
243 "create_dump_blank_Migration(): 'migrations_dir' is set to '$Dir' but does not exist. ",
244 "Please run: mkdir -p $Dir and try again"
245 ) unless (-e $Dir);
246
247 die join('',
248 "create_dump_blank_Migration(): 'migrations_dir' '$Dir' exists but is not a directory"
249 ) unless (-d $Dir);
250
251 my $new_name = 'auto_' . $self->current_SchemaState->fingerprint;
252
253 my $BlankMigration = DBIx::Class::StateMigrations::Migration->new(
254 migration_name => $new_name,
255 trigger_SchemaStates => [$self->current_SchemaState],
256 DBI_Driver_Name => $self->DBI_Driver_Name
257 );
258
259 my $new_dir = dir( $Dir, $new_name )->absolute;
260
261 die join('',
262 "create_dump_blank_Migration(): New dir '$new_dir' already exists!"
263 ) if (-e $new_dir);
264
265 $new_dir->mkpath(1);
266
267 $BlankMigration->write_subclass_pm_file( "$new_dir" );
268
269 my $rDir = dir( $new_dir, 'routines' );
270
271 $rDir->mkpath(1);
272
273 my $README = q~## This file was auto-generated by ~ . __PACKAGE__ . " v$VERSION ##\n" . q~
274 Put the "routines" in this directory (routines/) for this migration. Routines are simple
275 SQL (*.sql) or Perl (*.pl) scripts which will be executed in sort-order. SQL scripts are
276 simple, raw SQL files to run on the db, while Perl scripts should eval to return a CodeRef
277 which will be called with the first argument containing the connected DBIx::Class::Schema
278 object instance. Files which do not end with *.sql or *.pl are ignored.
279  
280 For more information, see the online documentation for StateMigrations:
281  
282 * https://metacpan.org/pod/DBIx::Class::StateMigrations
283
284 ~;
285
286 file( $rDir, 'README.txt' )->spew( $README );
287 }
288  
289  
290  
291 sub DEMOLISH {
292 my ($self, $gd) = @_;
293
294 # "global destruction" meaning the program is terminating and in that case we
295 # don't need to do any of this, and worse, it could cause problems (thanks mst)
296 return if $gd;
297  
298 # Clean up after ourselves. We have to handle additional things beyond normal
299 # object instances which clean themselves up automatically because we have
300 # created/loaded new classes and packages dynamically during the course of
301 # our normal function of operation, and these have no validity beyond our
302 # scope, and some of them could be quite large, so we want to get rid of them
303
304 # Throughout our lifecycle we've been tracking all the dynamically generated
305 # packages/classes in this hashref:
306 my @classes = keys %{ $self->__loaded_vagrant_classes };
307
308 # This is very agressive but I can't think of any downsides - blow away
309 # the entire object leaveing it as a blessed empty HashRef. This is so
310 # all the objects, such as Migration class object instances, will get
311 # garbage collected now, so we can then safely and cleanly unload the
312 # actual classes and not have to worry about any instances existing
313 %$self = ();
314
315 # and now unload the dynamic/migration classes by hand as our final act:
316
317 for (@classes) {
318 if (Class::Inspector->loaded( $_ )) {
319 #warn " >>> Vagrant class $_ is loaded\n";
320 Class::Unload->unload( $_ )
321 #? warn " >> unloaded $_ \n"
322 #: warn " >> FAILED to unload $_ \n";
323 }
324 else {
325 #warn " >>> Vagrant class $_ is not loaded\n";
326 }
327 }
328 }
329  
330  
331 1;
332  
333 __END__