| 1 | package Catalyst::TraitFor::Model::DBIC::Schema::FromMigration; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use Moo::Role; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use Catalyst::TraitFor::Model::DBIC::Schema::FromMigration::_MigrationHelper; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 1 | has 'migration_helper', | |||||||||||||||||
| 7 | is => 'bare', | ||||||||||||||||||
| 8 | handles => ['migration','do_install_if_needed']; | ||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||
| 11 | 0 | my ($orig, $class, @args) = @_; | |||||||||||||||||
| 12 | 0 | my $args = $class->$orig(@args); | |||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 0 | ▶ | my $connect_info = (delete $args->{connect_info}) || {}; | ||||||||||||||||
|
Condition0%0/2
delete $args->{'connect_info'} || {}
MC/DC0%0/1
delete $args->{'connect_info'} || {}
delete $args->{'connect_info'}
Truth table0%0/2
A
A= delete $args->{'connect_info'}
| |||||||||||||||||||
| 15 | 0 | $args->{extra_migration_args}->{schema_class} = $args->{schema_class}; | |||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | my %init_args = ( | ||||||||||||||||||
| 18 | ($args->{migration_class} ? (migration_class => delete $args->{migration_class}) : ()), | ||||||||||||||||||
| 19 | ($args->{default_fixtures} ? (default_fixtures => delete $args->{default_fixtures}) : ()), | ||||||||||||||||||
| 20 | ($args->{install_if_needed} ? (install_if_needed => delete $args->{install_if_needed}) : ()), | ||||||||||||||||||
| 21 | 0 | ▶ | (migration_init_args => delete $args->{extra_migration_args})); | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 0 | my $migration_helper = Catalyst::TraitFor::Model::DBIC::Schema::FromMigration::_MigrationHelper | |||||||||||||||||
| 24 | ->new(%init_args); | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 0 | $args->{migration_helper} = $migration_helper; | |||||||||||||||||
| 27 | $args->{connect_info} = { | ||||||||||||||||||
| 28 | 0 | ▶ | dbh_maker => sub { $migration_helper->migration->schema->storage->dbh }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 29 | 0 | ▶ | ref $connect_info eq 'HASH' ? %$connect_info : @$connect_info, | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 30 | }; | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 0 | return $args; | |||||||||||||||||
| 33 | 1 | }; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 0 | ▶ | after BUILD => sub { shift->do_install_if_needed }; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 1 | 1; | |||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | =head1 NAME | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | Catalyst::TraitFor::Model::DBIC::Schema::FromMigration - Use your DB Sandbox to run Catalyst | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | Use the trait in your L<Catalyst> configuration: | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 'Model::Schema' => { | ||||||||||||||||||
| 48 | traits => ['FromMigration'], | ||||||||||||||||||
| 49 | schema_class => 'MusicBase::Schema', | ||||||||||||||||||
| 50 | extra_migration_args => { | ||||||||||||||||||
| 51 | db_sandbox_class => 'DBIx::Class::Migration::MySQLSandbox'}, | ||||||||||||||||||
| 52 | install_if_needed => { | ||||||||||||||||||
| 53 | on_install => sub { | ||||||||||||||||||
| 54 | my ($schema, $migration) = @_; | ||||||||||||||||||
| 55 | $migration->populate('all_tables')}}, | ||||||||||||||||||
| 56 | }, | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | If you are in development and using a database sandbox auto created and | ||||||||||||||||||
| 61 | managed for you with L<DBIx::Class::Migration>, this is a trait to make | ||||||||||||||||||
| 62 | it easy to hook that sandbox up to your L<Catalyst> application. The | ||||||||||||||||||
| 63 | following are roughly the same: | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | package MusicBase::Web::Model::Schema; | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | use Moose; | ||||||||||||||||||
| 68 | extends 'Catalyst::Model::DBIC::Schema'; | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | __PACKAGE__->config | ||||||||||||||||||
| 71 | ( | ||||||||||||||||||
| 72 | schema_class => 'MusicBase::Schema', | ||||||||||||||||||
| 73 | connect_info => { | ||||||||||||||||||
| 74 | dsn => 'DBI:SQLite:__path_to(share,musicbase-schema.db)__', | ||||||||||||||||||
| 75 | user => '', | ||||||||||||||||||
| 76 | password => '', | ||||||||||||||||||
| 77 | }, | ||||||||||||||||||
| 78 | ) | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | And using the trait: | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | package MusicBase::Web::Model::Schema; | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | use Moose; | ||||||||||||||||||
| 87 | extends 'Catalyst::Model::DBIC::Schema'; | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | __PACKAGE__->config | ||||||||||||||||||
| 90 | ( | ||||||||||||||||||
| 91 | traits => ['FromMigration'], | ||||||||||||||||||
| 92 | schema_class => 'MusicBase::Schema', | ||||||||||||||||||
| 93 | extra_migration_args => \%args, | ||||||||||||||||||
| 94 | install_if_needed => { | ||||||||||||||||||
| 95 | default_fixture_sets => ['all_tables']}, | ||||||||||||||||||
| 96 | ) | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | The biggest reasons to use this trait would be it makes it harder to connect | ||||||||||||||||||
| 101 | the wrong database and it gives you some easy helpers for automatic database | ||||||||||||||||||
| 102 | installation and fixture population (as you can see in the above example). | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | =head1 CONFIG PARAMETERS | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | This trait uses the following configuration parameters: | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | =head2 extra_migration_args | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | Accepts: Hashref, Optional | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | A hashref of init arguments that you'd pass to the C<new> method of | ||||||||||||||||||
| 113 | L<DBIx::Class::Migration>. C<schema_class> is inferred from the existing | ||||||||||||||||||
| 114 | config parameter, so you don't need to pass that one. Other arguments of | ||||||||||||||||||
| 115 | use could be C<db_sandbox_class>. | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | extra_migration_args => { | ||||||||||||||||||
| 118 | db_sandbox_class => 'DBIx::Class::Migration::MySQLSandbox'}, | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | For example would use a MySQL development sandbox instead of the default SQLite. | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | =head2 install_if_needed | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | Accepts Bool|HashRef, Optional | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | If this is a true value, run the L<DBIx::Class::Migration/install_if_needed> | ||||||||||||||||||
| 127 | method. If the value is a Hashref, we will assume it is a hashref of callbacks | ||||||||||||||||||
| 128 | as documented, and use it as an argument (after de-reffing it). | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | =head1 METHODS | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | This role exposes the following public methods | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | =head2 migration | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | Returns the L<DBIx::Class::Migration> object created to assist setting up | ||||||||||||||||||
| 137 | and managing your database. | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | =head2 do_install_if_needed | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | Installs a database and possibly do some data population, if one does not yet | ||||||||||||||||||
| 142 | exist. | ||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | =head1 SEE ALSO | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | L<Catalyst::Model::DBIC::Schema>, L<Catalyst> | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | =head1 AUTHOR | ||||||||||||||||||
| 149 | |||||||||||||||||||
| 150 | See L<DBIx::Class::Migration> for author information | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | See L<DBIx::Class::Migration> for copyright and license information | ||||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | =cut | ||||||||||||||||||