| 1 | package DBIx::Class::Migration::RunScript; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use Text::Brew qw(distance); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use Log::Any; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | ▶ | use Carp 'croak'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | ▶ | use DBIx::Class::Migration::Types -all; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 4 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 4 | with 'MooX::Traits'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 4 | our @EXPORT = qw(builder migrate); | |||||||||||||
| 13 | |||||||||||||||
| 14 | sub _log_die { | ||||||||||||||
| 15 | 0 | ▶ | my ($self, $msg) = @_; | ||||||||||||
|
Subroutine
_log_die: not called CC=1 SCAR=6.9
| |||||||||||||||
| 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 | 6 | ▶ | default => sub { Log::Any->get_logger( category => 'DBIx::Class::Migration') }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 4 | ); | |||||||||||||
| 25 | |||||||||||||||
| 26 | 6 | ▶ | sub _trait_namespace { 'DBIx::Class::Migration::RunScript::Trait' } | ||||||||||||
|
Subroutine
_trait_namespace: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 4 | has 'dbh' => (is=>'rw', isa=>Object); | |||||||||||||
| 29 | 4 | has 'version_set' => (is=>'rw', isa=>ArrayRef); | |||||||||||||
| 30 | 4 | has 'runs' => ( | |||||||||||||
| 31 | is=>'ro', | ||||||||||||||
| 32 | isa=>CodeRef, | ||||||||||||||
| 33 | required=>1); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | sub run { | ||||||||||||||
| 36 | 8 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
run: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 37 | 8 | ▶ | eval { $self->runs->(@_); 1 } || | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | $self->handle_errors($@); | ||||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | sub handle_errors { | ||||||||||||||
| 42 | 0 | ▶ | my ($self, $err) = @_; | ||||||||||||
|
Subroutine
handle_errors: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 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 | sub as_coderef { | ||||||||||||||
| 92 | 6 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
as_coderef: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 93 | return sub { | ||||||||||||||
| 94 | 8 | ▶ | my ($schema, $version_set) = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 95 | 8 | $self->dbh($schema->storage->dbh); | |||||||||||||
| 96 | 8 | $self->version_set($version_set); | |||||||||||||
| 97 | 8 | $self->run; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | 6 | } | |||||||||||||
| 100 | |||||||||||||||
| 101 | sub default_plugins { | ||||||||||||||
| 102 | 2 | ▶ | 'SchemaLoader', | ||||||||||||
|
Subroutine
default_plugins: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 103 | 'Populate', | ||||||||||||||
| 104 | 'Dump', | ||||||||||||||
| 105 | 'TargetPath', | ||||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | sub used_plugins { | ||||||||||||||
| 109 | 2 | ▶ | my $path = 'DBIx/Class/Migration/RunScript/Trait/'; | ||||||||||||
|
Subroutine
used_plugins: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 110 | 2 | my $match = "$path(.+).pm"; | |||||||||||||
| 111 | 2 | my $traits_path = $INC{'MooX/Traits/Util.pm'}; | |||||||||||||
| 112 | 5 | return map { m[$match]x } | |||||||||||||
| 113 | 2 | ▶ | grep { $INC{$_} ne $traits_path and m[$path]x } keys %INC; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | sub builder(&) { | ||||||||||||||
| 117 | 5 | ▶ | my ($runs, @plugins) = reverse shift->(); | ||||||||||||
|
Subroutine
builder: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||
| 118 | 5 | my (@traits, %args, %seen); | |||||||||||||
| 119 | 5 | foreach my $plugins (@plugins) { | |||||||||||||
| 120 | 16 | ▶ | if(ref $plugins) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 121 | 0 | %args = (%args, %$plugins); | |||||||||||||
| 122 | } elsif ($seen{$plugins}++) { | ||||||||||||||
| 123 | # skip! | ||||||||||||||
| 124 | } else { | ||||||||||||||
| 125 | 11 | push @traits, $plugins; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 5 | return __PACKAGE__ | |||||||||||||
| 130 | ->new_with_traits(traits=>\@traits, runs=>$runs, %args) | ||||||||||||||
| 131 | ->as_coderef; | ||||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | sub migrate(&) { | ||||||||||||||
| 135 | 2 | ▶ | my $runs = shift; | ||||||||||||
|
Subroutine
migrate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 136 | builder { | ||||||||||||||
| 137 | 2 | ▶ | default_plugins(), | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 138 | used_plugins(), | ||||||||||||||
| 139 | $runs, | ||||||||||||||
| 140 | 2 | }; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 144 | |||||||||||||||
| 145 | =head1 NAME | ||||||||||||||
| 146 | |||||||||||||||
| 147 | DBIx::Class::Migration::RunScript - Control your Perl Migration Run Scripts | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =head1 SYNOPSIS | ||||||||||||||
| 150 | |||||||||||||||
| 151 | Using the C<builder> exported subroutine: | ||||||||||||||
| 152 | |||||||||||||||
| 153 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 154 | |||||||||||||||
| 155 | builder { | ||||||||||||||
| 156 | 'SchemaLoader', | ||||||||||||||
| 157 | sub { | ||||||||||||||
| 158 | shift->schema->resultset('Country') | ||||||||||||||
| 159 | ->populate([ | ||||||||||||||
| 160 | ['code'], | ||||||||||||||
| 161 | ['bel'], | ||||||||||||||
| 162 | ['deu'], | ||||||||||||||
| 163 | ['fra'], | ||||||||||||||
| 164 | ]); | ||||||||||||||
| 165 | }; | ||||||||||||||
| 166 | }; | ||||||||||||||
| 167 | |||||||||||||||
| 168 | Alternatively, use the C<migrate> exported subroutine for standard and external | ||||||||||||||
| 169 | plugins: | ||||||||||||||
| 170 | |||||||||||||||
| 171 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 172 | use DBIx::Class::Migration::RunScript::Trait::AuthenPassphrase; | ||||||||||||||
| 173 | |||||||||||||||
| 174 | migrate { | ||||||||||||||
| 175 | shift->schema | ||||||||||||||
| 176 | ->resultset('Country') | ||||||||||||||
| 177 | ->populate([ | ||||||||||||||
| 178 | ['code'], | ||||||||||||||
| 179 | ['bel'], | ||||||||||||||
| 180 | ['deu'], | ||||||||||||||
| 181 | ['fra'], | ||||||||||||||
| 182 | ]); | ||||||||||||||
| 183 | }; | ||||||||||||||
| 184 | |||||||||||||||
| 185 | =head1 DESCRIPTION | ||||||||||||||
| 186 | |||||||||||||||
| 187 | When using Perl based run files for your migrations, this class lets you | ||||||||||||||
| 188 | manage that and offers a clean method to add in functionality. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | See L<DBIx::Class::Migration::Tutorial> for an extended discussion. | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =head1 ATTRIBUTES | ||||||||||||||
| 193 | |||||||||||||||
| 194 | This class defines the follow attributes | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =head2 version_set | ||||||||||||||
| 197 | |||||||||||||||
| 198 | An arrayref of the from / to version you are attempting to migrate. | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =head2 dbh | ||||||||||||||
| 201 | |||||||||||||||
| 202 | The current database handle to the database you are trying to migrate. | ||||||||||||||
| 203 | |||||||||||||||
| 204 | =head1 EXPORTS | ||||||||||||||
| 205 | |||||||||||||||
| 206 | This class defines the following exports | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =head2 builder | ||||||||||||||
| 209 | |||||||||||||||
| 210 | Allows you to construct a migration script from a subroutine and also lets you | ||||||||||||||
| 211 | specify plugins. | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =head2 migrate | ||||||||||||||
| 214 | |||||||||||||||
| 215 | Run a migration subref with default plugins (SchemaLoader, Populate, TargetDir | ||||||||||||||
| 216 | Dump) and any additional plugins that you've used. For example: | ||||||||||||||
| 217 | |||||||||||||||
| 218 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 219 | |||||||||||||||
| 220 | migrate { | ||||||||||||||
| 221 | my $runscript = shift; | ||||||||||||||
| 222 | } | ||||||||||||||
| 223 | |||||||||||||||
| 224 | In this case C<$runscript> is an instance of L<DBIx::Class::Migration::RunScript> | ||||||||||||||
| 225 | and has the default traits applied (see | ||||||||||||||
| 226 | L<DBIx::Class::Migration::RunScript::Trait::TargetPath>, | ||||||||||||||
| 227 | L<DBIx::Class::Migration::RunScript::Trait::Schema>, | ||||||||||||||
| 228 | L<DBIx::Class::Migration::RunScript::Trait::Populate>, | ||||||||||||||
| 229 | L<DBIx::Class::Migration::RunScript::Trait::Dump> for more). | ||||||||||||||
| 230 | |||||||||||||||
| 231 | Second example: | ||||||||||||||
| 232 | |||||||||||||||
| 233 | use DBIx::Class::Migration::RunScript; | ||||||||||||||
| 234 | use DBIx::Class::Migration::RunScript::Trait::AuthenPassphrase; | ||||||||||||||
| 235 | |||||||||||||||
| 236 | migrate { | ||||||||||||||
| 237 | my $runscript = shift; | ||||||||||||||
| 238 | } | ||||||||||||||
| 239 | |||||||||||||||
| 240 | In this case C<$runscript> is an instance of L<DBIx::Class::Migration::RunScript> | ||||||||||||||
| 241 | with traits applied as above and in addition one more trait, | ||||||||||||||
| 242 | L<DBIx::Class::Migration::RunScript::Trait::AuthenPassphrase> which is available | ||||||||||||||
| 243 | on CPAN (is external because it carries a dependency weight I don't want to | ||||||||||||||
| 244 | impose on people if they don't need it). | ||||||||||||||
| 245 | |||||||||||||||
| 246 | =head1 UTILITY SUBROUTINES | ||||||||||||||
| 247 | |||||||||||||||
| 248 | The follow subroutines are available as package method, and not exported | ||||||||||||||
| 249 | |||||||||||||||
| 250 | =head2 default_plugins | ||||||||||||||
| 251 | |||||||||||||||
| 252 | returns an array of the default plugins. | ||||||||||||||
| 253 | |||||||||||||||
| 254 | =head1 SEE ALSO | ||||||||||||||
| 255 | |||||||||||||||
| 256 | L<DBIx::Class::Migration>. | ||||||||||||||
| 257 | |||||||||||||||
| 258 | =head1 AUTHOR | ||||||||||||||
| 259 | |||||||||||||||
| 260 | See L<DBIx::Class::Migration> for author information | ||||||||||||||
| 261 | |||||||||||||||
| 262 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 263 | |||||||||||||||
| 264 | See L<DBIx::Class::Migration> for copyright and license information | ||||||||||||||
| 265 | |||||||||||||||
| 266 | =cut | ||||||||||||||
| 267 | |||||||||||||||
| 268 | |||||||||||||||
| 269 | |||||||||||||||