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