| 1 | package DBIx::Class::Migration::RunScript; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use Text::Brew qw(distance); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | ▶ | use Log::Any; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 5 | ▶ | use Carp 'croak'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 5 | ▶ | use DBIx::Class::Migration::Types -all; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 5 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 5 | with 'MooX::Traits'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 5 | our @EXPORT = qw(builder migrate); | |||||||||||||
| 13 | |||||||||||||||
| 14 | 0 | ▶ | sub _log_die { | ||||||||||||
|
Subroutine
_log_die: not called CC=1 SCAR=6.9
| |||||||||||||||
| 15 | 0 | my ($self, $msg) = @_; | |||||||||||||
| 16 | 0 | $self->log->error($msg); | |||||||||||||
| 17 | 0 | croak $msg; | |||||||||||||
| 18 | } | ||||||||||||||
| 19 | |||||||||||||||
| 20 | has log => ( | ||||||||||||||
| 21 | is => 'ro', | ||||||||||||||
| 22 | isa => InstanceOf['Log::Any::Proxy'], | ||||||||||||||
| 23 | 7 | ▶ | default => sub { Log::Any->get_logger( category => 'DBIx::Class::Migration') }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 5 | ); | |||||||||||||
| 25 | |||||||||||||||
| 26 | 7 | ▶ | sub _trait_namespace { 'DBIx::Class::Migration::RunScript::Trait' } | ||||||||||||
|
Subroutine
_trait_namespace: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 5 | has 'dbh' => (is=>'rw', isa=>Object); | |||||||||||||
| 29 | 5 | has 'version_set' => (is=>'rw', isa=>ArrayRef); | |||||||||||||
| 30 | 5 | has 'runs' => ( | |||||||||||||
| 31 | is=>'ro', | ||||||||||||||
| 32 | isa=>CodeRef, | ||||||||||||||
| 33 | required=>1); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 9 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 36 | 9 | my ($self) = @_; | |||||||||||||
| 37 | 9 | ▶ | eval { $self->runs->(@_); 1 } || | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | $self->handle_errors($@); | ||||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | sub handle_errors { | ||||||||||||
|
Subroutine
handle_errors: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 42 | 0 | my ($self, $err) = @_; | |||||||||||||
| 43 | 0 | ▶ | if($err =~m/Can't find source for (.+?) at/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 44 | my @presentsources = map { | ||||||||||||||
| 45 | 0 | ▶ | (distance($_, $1))[0] < 3 ? "$_ <== Possible Match\n" : "$_\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | } $self->schema->sources; | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 0 | $self->_log_die(<<"ERR"); | |||||||||||||
| 49 | $err | ||||||||||||||
| 50 | You are probably seeing this error because the DBIC source in your migration | ||||||||||||||
| 51 | script called "$1" doesn't match a source defined in the schema that | ||||||||||||||
| 52 | ::SchemaLoader has inferred from your existing database. You may be confused | ||||||||||||||
| 53 | since that source might exist in your hand coded Schema files. Since your | ||||||||||||||
| 54 | migration script doesn't use your hand coded Schema (it can't since we cannot | ||||||||||||||
| 55 | be sure it is in sync with your database state) but instead uses SchemaLoader | ||||||||||||||
| 56 | to autogenerate a schema, it uses the default SchemaLoader rules for creating | ||||||||||||||
| 57 | source names from the database tables it finds. | ||||||||||||||
| 58 | |||||||||||||||
| 59 | To help you debug this issue, here's a list of the actual sources that the | ||||||||||||||
| 60 | schema available to your migration knows about: | ||||||||||||||
| 61 | |||||||||||||||
| 62 | @presentsources | ||||||||||||||
| 63 | ERR | ||||||||||||||
| 64 | } elsif($err =~m/No such column (.+?) on DBIx.+?Result::(.+?) at/) { | ||||||||||||||
| 65 | my @presentsources = map { | ||||||||||||||
| 66 | 0 | ▶ | (distance($_, $1))[0] < 3 ? "$_ <== Possible Match\n" : "$_\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | } ($self->schema->resultset($2)->result_source->columns, | ||||||||||||||
| 68 | $self->schema->resultset($2)->result_source->relationships); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | $self->_log_die(<<"ERR"); | |||||||||||||
| 71 | $err | ||||||||||||||
| 72 | You are probably seeing this error because the DBIC resultset $2 does | ||||||||||||||
| 73 | not have a column or method called $1 defined in the schema which | ||||||||||||||
| 74 | ::SchemaLoader has inferred from your existing database. You may be confused | ||||||||||||||
| 75 | since that method might exist in your hand coded Schema files. Since your | ||||||||||||||
| 76 | migration script doesn't use your hand coded Schema (it can't since we cannot | ||||||||||||||
| 77 | be sure it is in sync with your database state) but instead uses SchemaLoader | ||||||||||||||
| 78 | to autogenerate a schema, it uses the default SchemaLoader rules for creating | ||||||||||||||
| 79 | column and method names. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | To help you debug this issue, here's a list of the actual columns and methods | ||||||||||||||
| 82 | that $2 has available: | ||||||||||||||
| 83 | |||||||||||||||
| 84 | @presentsources | ||||||||||||||
| 85 | ERR | ||||||||||||||
| 86 | } else { | ||||||||||||||
| 87 | 0 | $self->_log_die($err); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 7 | ▶ | sub as_coderef { | ||||||||||||
|
Subroutine
as_coderef: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 92 | 7 | my $self = shift; | |||||||||||||
| 93 | 9 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 94 | 9 | my ($schema, $version_set) = @_; | |||||||||||||
| 95 | 9 | $self->dbh($schema->storage->dbh); | |||||||||||||
| 96 | 9 | $self->version_set($version_set); | |||||||||||||
| 97 | 9 | $self->run; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | 7 | } | |||||||||||||
| 100 | |||||||||||||||
| 101 | 3 | ▶ | sub default_plugins { | ||||||||||||
|
Subroutine
default_plugins: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 102 | 3 | 'SchemaLoader', | |||||||||||||
| 103 | 'Populate', | ||||||||||||||
| 104 | 'Dump', | ||||||||||||||
| 105 | 'TargetPath', | ||||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 3 | ▶ | sub used_plugins { | ||||||||||||
|
Subroutine
used_plugins: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 109 | #allow the user to specify plugins for the migration by just | ||||||||||||||
| 110 | #use-ing them | ||||||||||||||
| 111 | #We find them by looking for packages loaded in %INC that have | ||||||||||||||
| 112 | #the key matching the namespace defined by $path | ||||||||||||||
| 113 | 3 | my $path = 'DBIx/Class/Migration/RunScript/Trait/'; | |||||||||||||
| 114 | 3 | my $match = "$path(.+).pm"; | |||||||||||||
| 115 | |||||||||||||||
| 116 | #however, there's one danger: if there's already been a | ||||||||||||||
| 117 | #migration object created, which will have MANY matches for | ||||||||||||||
| 118 | #$path in it, along with __AND and other things that | ||||||||||||||
| 119 | #Moox::Traits::Util names a package with dynamic roles, we | ||||||||||||||
| 120 | #shouldn't try to include that. Packages made that way get the | ||||||||||||||
| 121 | #same %INC value as MooX::Traits::Util, so let's save that. | ||||||||||||||
| 122 | 3 | my $traits_path = $INC{'MooX/Traits/Util.pm'}; | |||||||||||||
| 123 | |||||||||||||||
| 124 | #so we get the list of packages loaded that match $path | ||||||||||||||
| 125 | 3 | my @traits = grep { m[$path]x } keys %INC; | |||||||||||||
| 126 | #filter out any that were made via MooX::Traits::Util, if it was | ||||||||||||||
| 127 | #loaded at all, yet | ||||||||||||||
| 128 | 3 | ▶ | @traits = grep { $INC{$_} ne $traits_path } @traits if $traits_path; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | #and return the last part of the path, the name of the plugin | ||||||||||||||
| 130 | 3 | return map { m[$match]x } @traits; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 6 | ▶ | sub builder(&) { | ||||||||||||
|
Subroutine
builder: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||
| 134 | 6 | my ($runs, @plugins) = reverse shift->(); | |||||||||||||
| 135 | 6 | my (@traits, %args, %seen); | |||||||||||||
| 136 | 6 | foreach my $plugins (@plugins) { | |||||||||||||
| 137 | 21 | ▶ | if(ref $plugins) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 138 | 0 | %args = (%args, %$plugins); | |||||||||||||
| 139 | } elsif ($seen{$plugins}++) { | ||||||||||||||
| 140 | # skip! | ||||||||||||||
| 141 | } else { | ||||||||||||||
| 142 | 15 | push @traits, $plugins; | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 6 | return __PACKAGE__ | |||||||||||||
| 147 | ->new_with_traits(traits=>\@traits, runs=>$runs, %args) | ||||||||||||||
| 148 | ->as_coderef; | ||||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 3 | ▶ | sub migrate(&) { | ||||||||||||
|
Subroutine
migrate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 152 | 3 | my $runs = shift; | |||||||||||||
| 153 | 3 | ▶ | builder { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 154 | 3 | default_plugins(), | |||||||||||||
| 155 | used_plugins(), | ||||||||||||||
| 156 | $runs, | ||||||||||||||
| 157 | 3 | }; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 161 | |||||||||||||||
| 162 | =head1 NAME | ||||||||||||||
| 163 | |||||||||||||||
| 164 | DBIx::Class::Migration::RunScript - Control your Perl Migration Run Scripts | ||||||||||||||
| 165 | |||||||||||||||
| 166 | =head1 SYNOPSIS | ||||||||||||||
| 167 | |||||||||||||||
| 168 | Using the C<builder> exported subroutine: | ||||||||||||||
| 169 | |||||||||||||||
| 170 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 171 | |||||||||||||||
| 172 | builder { | ||||||||||||||
| 173 | 'SchemaLoader', | ||||||||||||||
| 174 | sub { | ||||||||||||||
| 175 | shift->schema->resultset('Country') | ||||||||||||||
| 176 | ->populate([ | ||||||||||||||
| 177 | ['code'], | ||||||||||||||
| 178 | ['bel'], | ||||||||||||||
| 179 | ['deu'], | ||||||||||||||
| 180 | ['fra'], | ||||||||||||||
| 181 | ]); | ||||||||||||||
| 182 | }; | ||||||||||||||
| 183 | }; | ||||||||||||||
| 184 | |||||||||||||||
| 185 | Alternatively, use the C<migrate> exported subroutine for standard and external | ||||||||||||||
| 186 | plugins: | ||||||||||||||
| 187 | |||||||||||||||
| 188 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 189 | use DBIx::Class::Migration::RunScript::Trait::AuthenPassphrase; | ||||||||||||||
| 190 | |||||||||||||||
| 191 | migrate { | ||||||||||||||
| 192 | shift->schema | ||||||||||||||
| 193 | ->resultset('Country') | ||||||||||||||
| 194 | ->populate([ | ||||||||||||||
| 195 | ['code'], | ||||||||||||||
| 196 | ['bel'], | ||||||||||||||
| 197 | ['deu'], | ||||||||||||||
| 198 | ['fra'], | ||||||||||||||
| 199 | ]); | ||||||||||||||
| 200 | }; | ||||||||||||||
| 201 | |||||||||||||||
| 202 | =head1 DESCRIPTION | ||||||||||||||
| 203 | |||||||||||||||
| 204 | When using Perl based run files for your migrations, this class lets you | ||||||||||||||
| 205 | manage that and offers a clean method to add in functionality. | ||||||||||||||
| 206 | |||||||||||||||
| 207 | See L<DBIx::Class::Migration::Tutorial> for an extended discussion. | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =head1 ATTRIBUTES | ||||||||||||||
| 210 | |||||||||||||||
| 211 | This class defines the follow attributes | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =head2 version_set | ||||||||||||||
| 214 | |||||||||||||||
| 215 | An arrayref of the from / to version you are attempting to migrate. | ||||||||||||||
| 216 | |||||||||||||||
| 217 | =head2 dbh | ||||||||||||||
| 218 | |||||||||||||||
| 219 | The current database handle to the database you are trying to migrate. | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =head1 EXPORTS | ||||||||||||||
| 222 | |||||||||||||||
| 223 | This class defines the following exports | ||||||||||||||
| 224 | |||||||||||||||
| 225 | =head2 builder | ||||||||||||||
| 226 | |||||||||||||||
| 227 | Allows you to construct a migration script from a subroutine and also lets you | ||||||||||||||
| 228 | specify plugins. | ||||||||||||||
| 229 | |||||||||||||||
| 230 | =head2 migrate | ||||||||||||||
| 231 | |||||||||||||||
| 232 | Run a migration subref with default plugins (SchemaLoader, Populate, TargetDir | ||||||||||||||
| 233 | Dump) and any additional plugins that you've used. For example: | ||||||||||||||
| 234 | |||||||||||||||
| 235 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 236 | |||||||||||||||
| 237 | migrate { | ||||||||||||||
| 238 | my $runscript = shift; | ||||||||||||||
| 239 | } | ||||||||||||||
| 240 | |||||||||||||||
| 241 | In this case C<$runscript> is an instance of L<DBIx::Class::Migration::RunScript> | ||||||||||||||
| 242 | and has the default traits applied (see | ||||||||||||||
| 243 | L<DBIx::Class::Migration::RunScript::Trait::TargetPath>, | ||||||||||||||
| 244 | L<DBIx::Class::Migration::RunScript::Trait::Schema>, | ||||||||||||||
| 245 | L<DBIx::Class::Migration::RunScript::Trait::Populate>, | ||||||||||||||
| 246 | L<DBIx::Class::Migration::RunScript::Trait::Dump> for more). | ||||||||||||||
| 247 | |||||||||||||||
| 248 | Second example: | ||||||||||||||
| 249 | |||||||||||||||
| 250 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 251 | use DBIx::Class::Migration::RunScript::Trait::AuthenPassphrase; | ||||||||||||||
| 252 | |||||||||||||||
| 253 | migrate { | ||||||||||||||
| 254 | my $runscript = shift; | ||||||||||||||
| 255 | } | ||||||||||||||
| 256 | |||||||||||||||
| 257 | In this case C<$runscript> is an instance of L<DBIx::Class::Migration::RunScript> | ||||||||||||||
| 258 | with traits applied as above and in addition one more trait, | ||||||||||||||
| 259 | L<DBIx::Class::Migration::RunScript::Trait::AuthenPassphrase> which is available | ||||||||||||||
| 260 | on CPAN (is external because it carries a dependency weight I don't want to | ||||||||||||||
| 261 | impose on people if they don't need it). | ||||||||||||||
| 262 | |||||||||||||||
| 263 | =head1 UTILITY SUBROUTINES | ||||||||||||||
| 264 | |||||||||||||||
| 265 | The follow subroutines are available as package method, and not exported | ||||||||||||||
| 266 | |||||||||||||||
| 267 | =head2 default_plugins | ||||||||||||||
| 268 | |||||||||||||||
| 269 | returns an array of the default plugins. | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =head1 SEE ALSO | ||||||||||||||
| 272 | |||||||||||||||
| 273 | L<DBIx::Class::Migration>. | ||||||||||||||
| 274 | |||||||||||||||
| 275 | =head1 AUTHOR | ||||||||||||||
| 276 | |||||||||||||||
| 277 | See L<DBIx::Class::Migration> for author information | ||||||||||||||
| 278 | |||||||||||||||
| 279 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 280 | |||||||||||||||
| 281 | See L<DBIx::Class::Migration> for copyright and license information | ||||||||||||||
| 282 | |||||||||||||||
| 283 | =cut | ||||||||||||||
| 284 | |||||||||||||||
| 285 | |||||||||||||||
| 286 | |||||||||||||||