| 1 | package DBIx::Class::StateMigrations::Migration; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: individual migration for a single version bump | ||
| 7 | |||
| 8 | use Moo; | ||
| 9 | use Types::Standard qw(:all); | ||
| 10 | |||
| 11 | use Scalar::Util 'blessed'; | ||
| 12 | use Path::Class qw( file dir ); | ||
| 13 | require Data::Dump; | ||
| 14 | require Module::Locate; | ||
| 15 | require Module::Runtime; | ||
| 16 | use Class::Inspector; | ||
| 17 | use Clone 'clone'; | ||
| 18 | use Try::Tiny; | ||
| 19 | |||
| 20 | require DBIx::Class::StateMigrations; | ||
| 21 | |||
| 22 | use DBIx::Class::StateMigrations::Migration::Routine::PerlCode; | ||
| 23 | use DBIx::Class::StateMigrations::Migration::Routine::SQL; | ||
| 24 | use DBIx::Class::StateMigrations::Migration::Invalid; | ||
| 25 | |||
| 26 | our $LOADED_MIGRATION_SUBCLASSES = {}; | ||
| 27 | |||
| 28 | sub BUILD { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | my $name = $self->migration_name; | ||
| 32 | |||
| 33 | die "invalid migration_name '$name' - can only contain alpha chars and underscore _" | ||
| 34 | unless($name =~ /^[a-zA-Z0-9\_]+$/); | ||
| 35 | |||
| 36 | die "At least one trigger_SchemaState required" unless ( | ||
| 37 | scalar(@{ $self->trigger_SchemaStates }) > 0 | ||
| 38 | ); | ||
| 39 | |||
| 40 | $self->_validate_fingerprints; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub invalid { 0 } | ||
| 44 | |||
| 45 | has 'migration_name', is => 'ro', lazy => 1, default => sub { | ||
| 46 | my $self = shift; | ||
| 47 | $self->_migration_name_from_classname | ||
| 48 | ? $self->_migration_name_from_classname | ||
| 49 | : join('_', 'sm', map { $_->fingerprint } @{ $self->trigger_SchemaStates }) | ||
| 50 | }, isa => Str; | ||
| 51 | |||
| 52 | has 'trigger_SchemaStates', is => 'ro', required => 1, isa => ArrayRef[ | ||
| 53 | InstanceOf['DBIx::Class::StateMigrations::SchemaState'] | ||
| 54 | ]; | ||
| 55 | |||
| 56 | has 'frozen_trigger_SchemaStates', is => 'ro', lazy => 1, default => sub { | ||
| 57 | my $self = shift; | ||
| 58 | clone( $self->trigger_SchemaStates ) | ||
| 59 | }, isa => ArrayRef[InstanceOf['DBIx::Class::StateMigrations::SchemaState']]; | ||
| 60 | |||
| 61 | has 'DBI_Driver_Name', is => 'ro', required => 1, isa => Str; | ||
| 62 | |||
| 63 | has 'completed_SchemaState', is => 'ro', isa => Maybe[ | ||
| 64 | InstanceOf['DBIx::Class::StateMigrations::SchemaState'] | ||
| 65 | ], default => sub { undef }; | ||
| 66 | |||
| 67 | sub number_routines { scalar(@{ (shift)->Routines }) }; | ||
| 68 | |||
| 69 | has 'Routines', is => 'ro', required => 1, lazy => 1, default => sub { | ||
| 70 | my $self = shift; | ||
| 71 | return [] unless ($self->is_migration_class && $self->directory); | ||
| 72 | my $Dir = dir( $self->directory, 'routines' )->absolute; | ||
| 73 | return [] unless (-d $Dir); | ||
| 74 | |||
| 75 | my @routines = (); | ||
| 76 | |||
| 77 | my @File_list = | ||
| 78 | sort { $a->basename cmp $b->basename } | ||
| 79 | grep { ! $_->is_dir && -f $_ } | ||
| 80 | $Dir->children; | ||
| 81 | |||
| 82 | for my $File (@File_list) { | ||
| 83 | |||
| 84 | if(my $ext = (reverse split(/\./,$File->basename))[0]) { | ||
| 85 | if (lc($ext) eq 'pl') { | ||
| 86 | push @routines, DBIx::Class::StateMigrations::Migration::Routine::PerlCode->new( | ||
| 87 | file_path => $File->absolute->stringify, | ||
| 88 | Migration => $self | ||
| 89 | ); | ||
| 90 | } | ||
| 91 | elsif (lc($ext) eq 'sql') { | ||
| 92 | push @routines, DBIx::Class::StateMigrations::Migration::Routine::SQL->new( | ||
| 93 | file_path => $File->absolute->stringify, | ||
| 94 | Migration => $self | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return \@routines | ||
| 101 | |||
| 102 | }, isa => ArrayRef[InstanceOf['DBIx::Class::StateMigrations::Migration::Routine']]; | ||
| 103 | |||
| 104 | |||
| 105 | sub routines_executed { | ||
| 106 | my ($self, $set) = @_; | ||
| 107 | $self->__routines_executed(1) if ($set && ! $self->__routines_executed); | ||
| 108 | $self->__routines_executed | ||
| 109 | } | ||
| 110 | has '__routines_executed', is => 'rw', init_arg => undef, isa => Bool, default => sub { 0 }; | ||
| 111 | |||
| 112 | sub execute_routines { | ||
| 113 | my $self = shift; | ||
| 114 | my $db = shift; | ||
| 115 | my $callback = shift; | ||
| 116 | |||
| 117 | die "routines already executed!" if ($self->routines_executed); | ||
| 118 | |||
| 119 | die "execute_routines must be supplied connected DBIx::Class::Schema instance argument" | ||
| 120 | unless($db && blessed($db) && $db->isa('DBIx::Class::Schema')); | ||
| 121 | |||
| 122 | die "Optional callback must be a CodeRef" if ($callback && (ref($callback)||'') ne 'CODE'); | ||
| 123 | |||
| 124 | # Empty/no-op Migration: | ||
| 125 | return unless ($self->number_routines > 0); | ||
| 126 | |||
| 127 | for my $Routine (@{ $self->Routines }) { | ||
| 128 | my $ret = $Routine->execute( $db, $self ); | ||
| 129 | $callback->($Routine,$ret) if $callback; | ||
| 130 | } | ||
| 131 | |||
| 132 | $self->routines_executed(1); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub matches_SchemaState { | ||
| 136 | my ($self, $SchemaState) = @_; | ||
| 137 | |||
| 138 | die "check_SchemaState(): Bad argument - not a SchemaState object" unless ( | ||
| 139 | $SchemaState && blessed($SchemaState) | ||
| 140 | && $SchemaState->isa('DBIx::Class::StateMigrations::SchemaState') | ||
| 141 | ); | ||
| 142 | |||
| 143 | for my $SS (@{ $self->trigger_SchemaStates }) { | ||
| 144 | return 1 if ($SS->fingerprint eq $SchemaState->fingerprint); | ||
| 145 | } | ||
| 146 | |||
| 147 | return 0 | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | has 'directory', is => 'ro', lazy => 1, default => sub { | ||
| 152 | my $self = shift; | ||
| 153 | return undef unless ($self->is_migration_class); | ||
| 154 | |||
| 155 | my $pm_path = Module::Locate::locate(blessed $self) or die join('', | ||
| 156 | "Failed to locate pm file path for class '",blessed($self),"'" | ||
| 157 | ); | ||
| 158 | |||
| 159 | file( $pm_path )->parent->absolute->stringify | ||
| 160 | }, isa => Maybe[Str]; | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | has 'is_subclass', is => 'ro', lazy => 1, init_arg => undef, default => sub { | ||
| 165 | my $self = shift; | ||
| 166 | blessed($self) ne 'DBIx::Class::StateMigrations::Migration' | ||
| 167 | }, isa => Bool; | ||
| 168 | |||
| 169 | has 'is_migration_class', is => 'ro', lazy => 1, init_arg => undef, default => sub { | ||
| 170 | my $self = shift; | ||
| 171 | my $name = $self->_migration_name_from_classname; | ||
| 172 | defined $name && $name eq $self->migration_name | ||
| 173 | }, isa => Bool; | ||
| 174 | |||
| 175 | |||
| 176 | has '_migration_name_from_classname', is => 'ro', init_arg => undef, lazy => 1, default => sub { | ||
| 177 | my $self = shift; | ||
| 178 | return undef unless ($self->is_subclass); | ||
| 179 | my $class = blessed($self); | ||
| 180 | my ($junk,$name) = split(/Migration\_/,$class,2); | ||
| 181 | $name | ||
| 182 | }, isa => Maybe[Str]; | ||
| 183 | |||
| 184 | |||
| 185 | sub new_from_migration_dir { | ||
| 186 | my $self = shift; | ||
| 187 | my $dir = shift or die "dir not supplied"; | ||
| 188 | my $match_DBI_Driver_Name = shift; | ||
| 189 | |||
| 190 | my $Dir = dir( $dir )->absolute; | ||
| 191 | -d $Dir or die "'$dir' does not exist or is not a directory"; | ||
| 192 | |||
| 193 | my $pm_file; | ||
| 194 | my $child_count = 0; | ||
| 195 | my $has_routines_dir = 0; | ||
| 196 | for my $File ($Dir->children) { | ||
| 197 | $child_count++; | ||
| 198 | if($File->is_dir) { | ||
| 199 | $has_routines_dir = 1 if ($File->basename eq 'routines'); | ||
| 200 | next; | ||
| 201 | } | ||
| 202 | |||
| 203 | next unless (-f $File); | ||
| 204 | my $ext = (reverse split(/\./,$File->basename))[0]; | ||
| 205 | next unless ($ext && lc($ext) eq 'pm'); | ||
| 206 | |||
| 207 | return $self->_fatal_Invalid("multiple pm files found in directory '$dir'") if ($pm_file); | ||
| 208 | |||
| 209 | $pm_file = $File->absolute; | ||
| 210 | } | ||
| 211 | |||
| 212 | return $self->_Invalid("ignoring empty migration directory '$dir'") unless ($child_count > 0); | ||
| 213 | |||
| 214 | return $self->_Invalid("ignoring migration directory '$dir' with no pm file or routines dir") | ||
| 215 | unless ($pm_file || $has_routines_dir); | ||
| 216 | |||
| 217 | return $self->_fatal_Invalid("No Migration pm file found in directory '$dir'") unless ($pm_file); | ||
| 218 | |||
| 219 | return $self->_fatal_Invalid("Invalid Migration pm file '$pm_file' - must start with 'Migration_'") | ||
| 220 | unless ($pm_file->basename =~ /^Migration_/); | ||
| 221 | |||
| 222 | my $mclass = $pm_file->basename; | ||
| 223 | $mclass =~ s/\.pm$//i; | ||
| 224 | |||
| 225 | unless (Class::Inspector->loaded($mclass)) { | ||
| 226 | |||
| 227 | # TODO: this is now probably redundant: | ||
| 228 | return $self->_fatal_Invalid( | ||
| 229 | "Not loading $pm_file - class named '$mclass' already loaded!" | ||
| 230 | ) if (Module::Locate::locate($mclass)); | ||
| 231 | |||
| 232 | my $caught = undef; | ||
| 233 | try { | ||
| 234 | eval "use lib '$Dir'"; | ||
| 235 | Module::Runtime::require_module($mclass); | ||
| 236 | $LOADED_MIGRATION_SUBCLASSES->{$mclass}++; | ||
| 237 | } | ||
| 238 | catch { | ||
| 239 | $caught = shift; | ||
| 240 | }; | ||
| 241 | $caught and return $self->_fatal_Invalid("Caught exception trying to load Migration class '$mclass' [$caught]"); | ||
| 242 | |||
| 243 | return $self->_fatal_Invalid( | ||
| 244 | "Error loading $pm_file - '$mclass' still not loaded after require" | ||
| 245 | ) unless (Module::Locate::locate($mclass)); | ||
| 246 | } | ||
| 247 | |||
| 248 | my $Migration; | ||
| 249 | my $caught = undef; | ||
| 250 | try { | ||
| 251 | $Migration = $mclass->new; | ||
| 252 | } | ||
| 253 | catch { | ||
| 254 | $caught = shift; | ||
| 255 | }; | ||
| 256 | $caught and return $self->_fatal_Invalid("Caught exception trying to create '$mclass' object instance [$caught]"); | ||
| 257 | |||
| 258 | return $self->_fatal_Invalid( | ||
| 259 | "Created $mclass object instance is not a blessed subclass of 'DBIx::Class::StateMigrations::Migration" | ||
| 260 | ) unless (blessed($Migration) && $Migration->isa('DBIx::Class::StateMigrations::Migration')); | ||
| 261 | |||
| 262 | return $self->_fatal_Invalid( | ||
| 263 | "New $mclass object instance method ->is_migration_class() does not return true" | ||
| 264 | ) unless ($Migration->is_migration_class); | ||
| 265 | |||
| 266 | return $self->_fatal_Invalid( | ||
| 267 | "$mclass is the wrong DBI driver database type ('".$Migration->DBI_Driver_Name . | ||
| 268 | "') - for Migrations on the current connected database driver type ('$match_DBI_Driver_Name')" | ||
| 269 | ) if ($match_DBI_Driver_Name && $match_DBI_Driver_Name ne $Migration->DBI_Driver_Name); | ||
| 270 | |||
| 271 | return $Migration | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | sub as_subclass_pm_code { | ||
| 277 | my $self = shift; | ||
| 278 | |||
| 279 | $self->frozen_trigger_SchemaStates; | ||
| 280 | |||
| 281 | # frozen_trigger_SchemaStates does this also, but we don't want to rely | ||
| 282 | # on when is was loaded/generated (even though it was probably above) | ||
| 283 | my $ss_clone = clone($self->trigger_SchemaStates); | ||
| 284 | |||
| 285 | $_->_clear_DiffState for (@{ $self->trigger_SchemaStates }); | ||
| 286 | |||
| 287 | my @embed_attrs = qw(DBI_Driver_Name completed_SchemaState trigger_SchemaStates); | ||
| 288 | |||
| 289 | my $classname = join('_','Migration',$self->migration_name); | ||
| 290 | |||
| 291 | my $ver = $DBIx::Class::StateMigrations::VERSION or die "unable to load module version number?"; | ||
| 292 | |||
| 293 | my @pm_file_lines = ( | ||
| 294 | 'package ', | ||
| 295 | ' ' . $classname . ';','', | ||
| 296 | "## This file was originally generated by DBIx::Class::StateMigrations v$ver", | ||
| 297 | "## using the class name '$classname'.", | ||
| 298 | "## The part of the class/package name above after 'Migration_' is the", | ||
| 299 | "## the 'name' of the migration and can be changed as long as it is unique", | ||
| 300 | "## and the parent directory is also renamed to match", | ||
| 301 | '', | ||
| 302 | "use DBIx::Class::StateMigrations $ver; # require minimum version",'', | ||
| 303 | 'use strict;', | ||
| 304 | 'use warnings;','', | ||
| 305 | 'use Moo;', | ||
| 306 | 'extends "DBIx::Class::StateMigrations::Migration";', | ||
| 307 | '', | ||
| 308 | $self->__generate_inline_pm_code_lines_for_attrs(@embed_attrs), | ||
| 309 | '1;','','', | ||
| 310 | '#########################################################################################', | ||
| 311 | '## - THE REST OF THE CONTENT IN THIS FILE IS NOT REQUIRED MAY BE REMOVED - ##', | ||
| 312 | '## ##', | ||
| 313 | '## The "frozen_trigger_SchemaStates" contains the full datasets used to generate the ##', | ||
| 314 | '## SchemaState fingerprints and is only useful for historical/forensic purposes. ##', | ||
| 315 | '## ##', | ||
| 316 | '## YOU MAY DELETE THESE COMMENT LINES AND EVERYTHING BELOW ##', | ||
| 317 | '#########################################################################################', | ||
| 318 | '', | ||
| 319 | $self->__generate_inline_pm_code_lines_for_attrs('frozen_trigger_SchemaStates'), | ||
| 320 | '','', | ||
| 321 | '1' | ||
| 322 | ); | ||
| 323 | |||
| 324 | # Put trigger_SchemaStates back the way it was | ||
| 325 | @{ $self->trigger_SchemaStates } = @$ss_clone; | ||
| 326 | |||
| 327 | return join("\n",@pm_file_lines); | ||
| 328 | } | ||
| 329 | |||
| 330 | |||
| 331 | sub __generate_inline_pm_code_lines_for_attrs { | ||
| 332 | my ($self, @attrs) = @_; | ||
| 333 | scalar(@attrs) > 0 or die "no attrs supplied"; | ||
| 334 | |||
| 335 | my @lines = (); | ||
| 336 | |||
| 337 | for my $attr (@attrs) { | ||
| 338 | |||
| 339 | $self->can($attr) or die "No such attr '$attr'"; | ||
| 340 | |||
| 341 | my $val = $self->$attr; | ||
| 342 | |||
| 343 | if(ref($val)) { | ||
| 344 | push @lines, join('',"has '+",$attr,q~', default => sub { ~,$self->_Dump($val),q~ };~),''; | ||
| 345 | } | ||
| 346 | else { | ||
| 347 | $val = defined $val ? "'$val'" : 'undef'; | ||
| 348 | push @lines, join('',"has '+",$attr,q~', default => sub { ~,$val,q~ };~),''; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | @lines | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | sub write_subclass_pm_file { | ||
| 358 | my $self = shift; | ||
| 359 | my $dir = shift or die "dir not supplied"; | ||
| 360 | |||
| 361 | my $Dir = dir( $dir )->absolute; | ||
| 362 | |||
| 363 | -d $Dir or die "'$dir' does not exist or is not a directory"; | ||
| 364 | |||
| 365 | my $pm_file = file( $Dir, 'Migration_' . $self->migration_name . '.pm' ); | ||
| 366 | |||
| 367 | -f $pm_file and die "write_subclass_pm_file(): pm file '$pm_file' already exists"; | ||
| 368 | |||
| 369 | $pm_file->spew( $self->as_subclass_pm_code ); | ||
| 370 | |||
| 371 | |||
| 372 | my $NewMigration = $self->new_from_migration_dir( "$Dir" ); | ||
| 373 | |||
| 374 | die "Loading newly creted migration class failure - not the same migration_name" unless ( | ||
| 375 | $self->migration_name eq $NewMigration->migration_name | ||
| 376 | ); | ||
| 377 | |||
| 378 | return 1; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | sub _validate_fingerprints { | ||
| 384 | my $self = shift; | ||
| 385 | |||
| 386 | $_->validate_fingerprint for (@{ $self->trigger_SchemaStates }); | ||
| 387 | $_->validate_fingerprint for (@{ $self->frozen_trigger_SchemaStates }); | ||
| 388 | |||
| 389 | my %f1 = map { $_->fingerprint => 1 } (@{ $self->trigger_SchemaStates }); | ||
| 390 | my %f2 = map { $_->fingerprint => 1 } (@{ $self->frozen_trigger_SchemaStates }); | ||
| 391 | |||
| 392 | for (keys %f2) { | ||
| 393 | $f1{$_} or die "mismatch with frozen copy of SchemaStates - frozen fingerprint '$_' not found in trigger_SchemaStates"; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | |||
| 398 | sub _Invalid { | ||
| 399 | my $self = shift; | ||
| 400 | my $fatal = $_[1] ? 1 : 0; | ||
| 401 | my $reason = shift or die "Must supply invalid 'reason'"; | ||
| 402 | DBIx::Class::StateMigrations::Migration::Invalid->new( reason => $reason, fatal => $fatal ) | ||
| 403 | } | ||
| 404 | |||
| 405 | sub _fatal_Invalid { (shift)->_Invalid(@_,1) } | ||
| 406 | |||
| 407 | |||
| 408 | sub _Dump { | ||
| 409 | my $self = shift; | ||
| 410 | my $obj = shift; | ||
| 411 | |||
| 412 | require Data::Dump; | ||
| 413 | local $Data::Dump::INDENT = ' '; | ||
| 414 | |||
| 415 | return Data::Dump::dump($obj); | ||
| 416 | } | ||
| 417 | |||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | 1; | ||
| 422 | |||
| 423 | __END__ |