| 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 | |||
| 18 | use DBIx::Class::StateMigrations::Migration::Routine::PerlCode; | ||
| 19 | use DBIx::Class::StateMigrations::Migration::Routine::SQL; | ||
| 20 | |||
| 21 | sub BUILD { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | my $name = $self->migration_name; | ||
| 25 | |||
| 26 | die "invalid migration_name '$name' - can only contain alpha chars and underscore _" | ||
| 27 | unless($name =~ /^[a-zA-Z0-9\_]+$/); | ||
| 28 | |||
| 29 | die "At least one trigger_SchemaState required" unless ( | ||
| 30 | scalar(@{ $self->trigger_SchemaStates }) > 0 | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | has 'migration_name', is => 'ro', lazy => 1, default => sub { | ||
| 36 | my $self = shift; | ||
| 37 | $self->_migration_name_from_classname | ||
| 38 | ? $self->_migration_name_from_classname | ||
| 39 | : join('_', 'sm', map { $_->fingerprint } @{ $self->trigger_SchemaStates }) | ||
| 40 | }, isa => Str; | ||
| 41 | |||
| 42 | has 'trigger_SchemaStates', is => 'ro', required => 1, isa => ArrayRef[ | ||
| 43 | InstanceOf['DBIx::Class::StateMigrations::SchemaState'] | ||
| 44 | ]; | ||
| 45 | |||
| 46 | has 'DBI_Driver_Name', is => 'ro', required => 1, isa => Str; | ||
| 47 | |||
| 48 | has 'completed_SchemaState', is => 'ro', isa => Maybe[ | ||
| 49 | InstanceOf['DBIx::Class::StateMigrations::SchemaState'] | ||
| 50 | ], default => sub { undef }; | ||
| 51 | |||
| 52 | sub number_routines { scalar(@{ (shift)->Routines }) }; | ||
| 53 | |||
| 54 | has 'Routines', is => 'ro', required => 1, lazy => 1, default => sub { | ||
| 55 | my $self = shift; | ||
| 56 | return undef unless ($self->is_migration_class && $self->directory); | ||
| 57 | my $Dir = dir( $self->directory, 'routines' )->absolute; | ||
| 58 | return undef unless (-d $Dir); | ||
| 59 | |||
| 60 | my @routines = (); | ||
| 61 | |||
| 62 | my @File_list = | ||
| 63 | sort { $a->basename cmp $b->basename } | ||
| 64 | grep { ! $_->is_dir && -f $_ } | ||
| 65 | $Dir->children; | ||
| 66 | |||
| 67 | for my $File (@File_list) { | ||
| 68 | |||
| 69 | if(my $ext = (reverse split(/\./,$File->basename))[0]) { | ||
| 70 | if (lc($ext) eq 'pl') { | ||
| 71 | push @routines, DBIx::Class::StateMigrations::Migration::Routine::PerlCode->new( | ||
| 72 | file_path => $File->absolute->stringify, | ||
| 73 | Migration => $self | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | elsif (lc($ext) eq 'sql') { | ||
| 77 | push @routines, DBIx::Class::StateMigrations::Migration::Routine::SQL->new( | ||
| 78 | file_path => $File->absolute->stringify, | ||
| 79 | Migration => $self | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | return \@routines | ||
| 86 | |||
| 87 | }, isa => ArrayRef[InstanceOf['DBIx::Class::StateMigrations::Migration::Routine']]; | ||
| 88 | |||
| 89 | |||
| 90 | sub routines_executed { | ||
| 91 | my ($self, $set) = @_; | ||
| 92 | $self->__routines_executed(1) if ($set && ! $self->__routines_executed); | ||
| 93 | $self->__routines_executed | ||
| 94 | } | ||
| 95 | has '__routines_executed', is => 'rw', init_arg => undef, isa => Bool, default => sub { 0 }; | ||
| 96 | |||
| 97 | sub execute_routines { | ||
| 98 | my $self = shift; | ||
| 99 | my $db = shift; | ||
| 100 | my $callback = shift; | ||
| 101 | |||
| 102 | die "routines already executed!" if ($self->routines_executed); | ||
| 103 | |||
| 104 | die "execute_routines must be supplied connected DBIx::Class::Schema instance argument" | ||
| 105 | unless($db && blessed($db) && $db->isa('DBIx::Class::Schema')); | ||
| 106 | |||
| 107 | die "Optional callback must be a CodeRef" if ($callback && (ref($callback)||'') ne 'CODE'); | ||
| 108 | |||
| 109 | # Empty/no-op Migration: | ||
| 110 | return unless ($self->number_routines > 0); | ||
| 111 | |||
| 112 | for my $Routine (@{ $self->Routines }) { | ||
| 113 | my $ret = $Routine->execute( $db, $self ); | ||
| 114 | $callback->($Routine,$ret) if $callback; | ||
| 115 | } | ||
| 116 | |||
| 117 | $self->routines_executed(1); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub matches_SchemaState { | ||
| 121 | my ($self, $SchemaState) = @_; | ||
| 122 | |||
| 123 | die "check_SchemaState(): Bad argument - not a SchemaState object" unless ( | ||
| 124 | $SchemaState && blessed($SchemaState) | ||
| 125 | && $SchemaState->isa('DBIx::Class::StateMigrations::SchemaState') | ||
| 126 | ); | ||
| 127 | |||
| 128 | for my $SS (@{ $self->trigger_SchemaStates }) { | ||
| 129 | return 1 if ($SS->fingerprint eq $SchemaState->fingerprint); | ||
| 130 | } | ||
| 131 | |||
| 132 | return 0 | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | has 'directory', is => 'ro', lazy => 1, default => sub { | ||
| 137 | my $self = shift; | ||
| 138 | return undef unless ($self->is_migration_class); | ||
| 139 | |||
| 140 | my $pm_path = Module::Locate::locate(blessed $self) or die join('', | ||
| 141 | "Failed to locate pm file path for class '",blessed($self),"'" | ||
| 142 | ); | ||
| 143 | |||
| 144 | file( $pm_path )->parent->absolute->stringify | ||
| 145 | }, isa => Maybe[Str]; | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | has 'is_subclass', is => 'ro', lazy => 1, init_arg => undef, default => sub { | ||
| 150 | my $self = shift; | ||
| 151 | blessed($self) ne 'DBIx::Class::StateMigrations::Migration' | ||
| 152 | }, isa => Bool; | ||
| 153 | |||
| 154 | has 'is_migration_class', is => 'ro', lazy => 1, init_arg => undef, default => sub { | ||
| 155 | my $self = shift; | ||
| 156 | my $name = $self->_migration_name_from_classname; | ||
| 157 | defined $name && $name eq $self->migration_name | ||
| 158 | }, isa => Bool; | ||
| 159 | |||
| 160 | |||
| 161 | has '_migration_name_from_classname', is => 'ro', init_arg => undef, lazy => 1, default => sub { | ||
| 162 | my $self = shift; | ||
| 163 | return undef unless ($self->is_subclass); | ||
| 164 | my $class = blessed($self); | ||
| 165 | my ($junk,$name) = split(/Migration\_/,$class,2); | ||
| 166 | $name | ||
| 167 | }, isa => Maybe[Str]; | ||
| 168 | |||
| 169 | |||
| 170 | sub new_from_migration_dir { | ||
| 171 | my $self = shift; | ||
| 172 | my $dir = shift or die "dir not supplied"; | ||
| 173 | |||
| 174 | my $Dir = dir( $dir )->absolute; | ||
| 175 | -d $Dir or die "'$dir' does not exist or is not a directory"; | ||
| 176 | |||
| 177 | my $pm_file; | ||
| 178 | for my $File ($Dir->children) { | ||
| 179 | next if $File->is_dir; | ||
| 180 | next unless (-f $File); | ||
| 181 | my $ext = (reverse split(/\./,$File->basename))[0]; | ||
| 182 | next unless ($ext && lc($ext) eq 'pm'); | ||
| 183 | |||
| 184 | die "Error - multiple pm files found in directory '$dir'" if ($pm_file); | ||
| 185 | |||
| 186 | $pm_file = $File->absolute; | ||
| 187 | } | ||
| 188 | |||
| 189 | die "No Migration pm file found in directory '$dir'" unless ($pm_file); | ||
| 190 | die "Invalid Migration pm file '$pm_file' - must start with 'Migration_'" | ||
| 191 | unless ($pm_file->basename =~ /^Migration_/); | ||
| 192 | |||
| 193 | my $mclass = $pm_file->basename; | ||
| 194 | $mclass =~ s/\.pm$//i; | ||
| 195 | |||
| 196 | unless (Class::Inspector->loaded($mclass)) { | ||
| 197 | |||
| 198 | # TODO: this is now probably redundant: | ||
| 199 | die "Not loading $pm_file - class named '$mclass' already loaded!" if (Module::Locate::locate($mclass)); | ||
| 200 | |||
| 201 | eval "use lib '$Dir'"; | ||
| 202 | Module::Runtime::require_module($mclass); | ||
| 203 | |||
| 204 | die "Error loading $pm_file - '$mclass' still not loaded after require" unless (Module::Locate::locate($mclass)); | ||
| 205 | } | ||
| 206 | |||
| 207 | my $Migration = $mclass->new; | ||
| 208 | |||
| 209 | die "Error loading new $mclass object instance - not a valid Migration class" unless ( | ||
| 210 | blessed($Migration) && $Migration->isa('DBIx::Class::StateMigrations::Migration') | ||
| 211 | ); | ||
| 212 | |||
| 213 | die "New $mclass object instance returns false for ->is_migration_class" unless ( | ||
| 214 | $Migration->is_migration_class | ||
| 215 | ); | ||
| 216 | |||
| 217 | return $Migration | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | sub as_subclass_pm_code { | ||
| 223 | my $self = shift; | ||
| 224 | |||
| 225 | my @embed_attrs = qw(DBI_Driver_Name completed_SchemaState trigger_SchemaStates); | ||
| 226 | |||
| 227 | my $classname = join('_','Migration',$self->migration_name); | ||
| 228 | |||
| 229 | my @pm_file_lines = ( | ||
| 230 | 'package ', | ||
| 231 | ' ' . $classname . ';','', | ||
| 232 | 'use strict;', | ||
| 233 | 'use warnings;','', | ||
| 234 | 'use Moo;', | ||
| 235 | 'extends "DBIx::Class::StateMigrations::Migration";', | ||
| 236 | '', | ||
| 237 | $self->__generate_inline_pm_code_lines_for_attrs(@embed_attrs), | ||
| 238 | '1','' | ||
| 239 | ); | ||
| 240 | |||
| 241 | return join("\n",@pm_file_lines); | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub __generate_inline_pm_code_lines_for_attrs { | ||
| 246 | my ($self, @attrs) = @_; | ||
| 247 | scalar(@attrs) > 0 or die "no attrs supplied"; | ||
| 248 | |||
| 249 | my @lines = (); | ||
| 250 | |||
| 251 | for my $attr (@attrs) { | ||
| 252 | |||
| 253 | $self->can($attr) or die "No such attr '$attr'"; | ||
| 254 | |||
| 255 | my $val = $self->$attr; | ||
| 256 | |||
| 257 | if(ref($val)) { | ||
| 258 | push @lines, join('',"has '+",$attr,q~', default => sub { ~,$self->_Dump($val),q~ };~),''; | ||
| 259 | } | ||
| 260 | else { | ||
| 261 | $val = defined $val ? "'$val'" : 'undef'; | ||
| 262 | push @lines, join('',"has '+",$attr,q~', default => sub { ~,$val,q~ };~),''; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | @lines | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | sub write_subclass_pm_file { | ||
| 272 | my $self = shift; | ||
| 273 | my $dir = shift or die "dir not supplied"; | ||
| 274 | |||
| 275 | my $Dir = dir( $dir )->absolute; | ||
| 276 | |||
| 277 | -d $Dir or die "'$dir' does not exist or is not a directory"; | ||
| 278 | |||
| 279 | my $pm_file = file( $Dir, 'Migration_' . $self->migration_name . '.pm' ); | ||
| 280 | |||
| 281 | -f $pm_file and die "write_subclass_pm_file(): pm file '$pm_file' already exists"; | ||
| 282 | |||
| 283 | $pm_file->spew( $self->as_subclass_pm_code ); | ||
| 284 | |||
| 285 | |||
| 286 | my $NewMigration = $self->new_from_migration_dir( "$Dir" ); | ||
| 287 | |||
| 288 | die "Loading newly creted migration class failure - not the same migration_name" unless ( | ||
| 289 | $self->migration_name eq $NewMigration->migration_name | ||
| 290 | ); | ||
| 291 | |||
| 292 | return 1; | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | sub _Dump { | ||
| 297 | my $self = shift; | ||
| 298 | my $obj = shift; | ||
| 299 | |||
| 300 | require Data::Dump; | ||
| 301 | local $Data::Dump::INDENT = ' '; | ||
| 302 | |||
| 303 | return Data::Dump::dump($obj); | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | 1; | ||
| 309 | |||
| 310 | __END__ |