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