| 1 | package DBIx::Class::Migration::Population; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use version 0.77; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use File::Spec::Functions 'catdir', 'catfile'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use File::ShareDir::ProjectDistDir 0.3.1 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use MooseX::Types::LoadableClass 'LoadableClass'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Class::Load 'load_class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | has schema_class => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | predicate=>'has_schema_class', | ||||||||||
| 13 | required=>0, | ||||||||||
| 14 | isa => LoadableClass, | ||||||||||
| 15 | coerce=>1); | ||||||||||
| 16 | |||||||||||
| 17 | 1 | has schema_args => (is=>'ro', isa=>'ArrayRef', lazy_build=>1); | |||||||||
| 18 | |||||||||||
| 19 | sub _generate_filename_for_default_db { | ||||||||||
| 20 | 0 | ▶ | my ($schema_class) = @_; | ||||||||
|
Subroutine
_generate_filename_for_default_db: not called CC=1 SCAR=6.9
| |||||||||||
| 21 | 0 | $schema_class =~ s/::/-/g; | |||||||||
| 22 | 0 | return lc($schema_class); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | sub _generate_dsn { | ||||||||||
| 26 | 0 | ▶ | my ($schema_class, $target_dir) = @_; | ||||||||
|
Subroutine
_generate_dsn: not called CC=1 SCAR=6.9
| |||||||||||
| 27 | 0 | my $filename = _generate_filename_for_default_db($schema_class); | |||||||||
| 28 | 0 | 'DBI:SQLite:'. catfile($target_dir, "$filename.db"); | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | sub _build_schema_args { | ||||||||||
| 32 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_schema_args: not called CC=1 SCAR=6.9
| |||||||||||
| 33 | 0 | [ _generate_dsn($self->schema_class, $self->target_dir), '', '' ]; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 1 | has schema => (is=>'ro', lazy_build=>1, predicate=>'has_schema'); | |||||||||
| 37 | |||||||||||
| 38 | sub _build_schema { | ||||||||||
| 39 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_schema: not called CC=1 SCAR=6.9
| |||||||||||
| 40 | 0 | $self->schema_class->connect(@{$self->schema_args}); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | has target_dir_builder_class => ( | |||||||||
| 44 | is => 'ro', | ||||||||||
| 45 | default => 'DBIx::Class::Migration::ShareDirBuilder', | ||||||||||
| 46 | isa => LoadableClass, | ||||||||||
| 47 | coerce=>1); | ||||||||||
| 48 | |||||||||||
| 49 | 1 | has target_dir_builder => ( is => 'ro', lazy_build => 1); | |||||||||
| 50 | |||||||||||
| 51 | sub _infer_schema_class { | ||||||||||
| 52 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_infer_schema_class: called CC=2 SCAR=7.2
| |||||||||||
| 53 | 1 | ▶ | return $self->has_schema_class ? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | $self->schema_class : ref($self->schema); | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | sub _build_target_dir_builder { | ||||||||||
| 58 | 1 | ▶ | my $inferred_schema_class = (my $self = shift) | ||||||||
|
Subroutine
_build_target_dir_builder: called CC=1 SCAR=0.0
| |||||||||||
| 59 | ->_infer_schema_class; | ||||||||||
| 60 | 1 | $self->target_dir_builder_class | |||||||||
| 61 | ->new(schema_class=>$inferred_schema_class); | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 1 | has target_dir => (is=>'ro', isa=>'Str', lazy_build=>1); | |||||||||
| 65 | |||||||||||
| 66 | sub _build_target_dir { | ||||||||||
| 67 | 1 | ▶ | shift->target_dir_builder->build; | ||||||||
|
Subroutine
_build_target_dir: called CC=1 SCAR=0.0
| |||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | has dbic_fixture_class => ( | |||||||||
| 71 | is => 'ro', | ||||||||||
| 72 | default => 'DBIx::Class::Fixtures', | ||||||||||
| 73 | isa => LoadableClass, | ||||||||||
| 74 | coerce=>1); | ||||||||||
| 75 | |||||||||||
| 76 | sub _prepare_fixture_conf_dir { | ||||||||||
| 77 | 0 | ▶ | my ($dir, $version) = @_; | ||||||||
|
Subroutine
_prepare_fixture_conf_dir: not called CC=1 SCAR=6.9
| |||||||||||
| 78 | 0 | my $fixture_conf_dir = catdir($dir, 'fixtures', $version, 'conf'); | |||||||||
| 79 | 0 | return $fixture_conf_dir; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub _prepare_fixture_data_dir { | ||||||||||
| 83 | 0 | ▶ | my ($dir, $version, $set) = @_; | ||||||||
|
Subroutine
_prepare_fixture_data_dir: not called CC=1 SCAR=6.9
| |||||||||||
| 84 | 0 | my $fixture_conf_dir = catdir($dir, 'fixtures', $version, $set); | |||||||||
| 85 | 0 | return $fixture_conf_dir; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub build_dbic_fixtures { | ||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
build_dbic_fixtures: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 90 | 0 | my $dbic_fixtures = $self->dbic_fixture_class; | |||||||||
| 91 | 0 | my $conf_dir = _prepare_fixture_conf_dir($self->target_dir, | |||||||||
| 92 | $self->schema->schema_version); | ||||||||||
| 93 | |||||||||||
| 94 | 0 | $dbic_fixtures->new({ | |||||||||
| 95 | config_dir => $conf_dir}); | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub populate { | ||||||||||
| 99 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
populate: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 100 | 0 | my $version_to_populate = $self->schema->schema_version; | |||||||||
| 101 | |||||||||||
| 102 | 0 | foreach my $set(@_) { | |||||||||
| 103 | 0 | my $target_dir = _prepare_fixture_data_dir($self->target_dir, | |||||||||
| 104 | $version_to_populate, $set); | ||||||||||
| 105 | |||||||||||
| 106 | 0 | $self->build_dbic_fixtures->populate({ | |||||||||
| 107 | no_deploy => 1, | ||||||||||
| 108 | schema => $self->schema, | ||||||||||
| 109 | directory => $target_dir, | ||||||||||
| 110 | }); | ||||||||||
| 111 | |||||||||||
| 112 | 0 | print "Restored set $set to database\n"; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 117 | |||||||||||
| 118 | =head1 NAME | ||||||||||
| 119 | |||||||||||
| 120 | DBIx::Class::Migration::Population - Utility to populate fixture data | ||||||||||
| 121 | |||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||
| 123 | |||||||||||
| 124 | use DBIx::Class::Migration::Population; | ||||||||||
| 125 | use MyApp::Schema; | ||||||||||
| 126 | |||||||||||
| 127 | $schema = MyApp::Schema->connect(...); | ||||||||||
| 128 | (my $population = DBIx::Class::Migration::Population->new( | ||||||||||
| 129 | schema=>$schema))->populate('all_tables'); | ||||||||||
| 130 | |||||||||||
| 131 | =head1 DESCRIPTION | ||||||||||
| 132 | |||||||||||
| 133 | Sometimes you just need to populate data for your scripts, such as during | ||||||||||
| 134 | testing and you don't want to expose a full migrations interface and let | ||||||||||
| 135 | someone accidently wipe your database with one command. This utility is | ||||||||||
| 136 | designed to assist. It is basically a thin wrapper on L<DBIx::Class::Fixtures> | ||||||||||
| 137 | that is just aware of L<DBIx::Class::Migrations> conventions. | ||||||||||
| 138 | |||||||||||
| 139 | You create an instance of this similarly to L<DBIx::Class::Migrations>, except | ||||||||||
| 140 | you can't pass any arguments related to L<DBIx::Class::DeploymentHandler> since | ||||||||||
| 141 | you don't have one :). You can create it from an existing schema, or build it | ||||||||||
| 142 | from a schema_class and schema_args, and optional set a target directory (or | ||||||||||
| 143 | just let it use the default distribution share directory). Afterwards we | ||||||||||
| 144 | expose a C<populate> method that takes a list of fixture set names. | ||||||||||
| 145 | |||||||||||
| 146 | You don't have any control over which version we are trying to populate, we | ||||||||||
| 147 | always use the declared schema Version. We assume you have an existing | ||||||||||
| 148 | deployed database that matches the current schema. | ||||||||||
| 149 | |||||||||||
| 150 | =head1 SEE ALSO | ||||||||||
| 151 | |||||||||||
| 152 | L<DBIx::Class::Migration>, L<DBIx::Class::Manual::Example> | ||||||||||
| 153 | |||||||||||
| 154 | =head1 AUTHOR | ||||||||||
| 155 | |||||||||||
| 156 | See L<DBIx::Class::Migration> for author information | ||||||||||
| 157 | |||||||||||
| 158 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 159 | |||||||||||
| 160 | See L<DBIx::Class::Migration> for copyright and license information | ||||||||||
| 161 | |||||||||||
| 162 | =cut | ||||||||||
| 163 | |||||||||||