« DBIx/Class/Migration/ShareDirBuilder.pm
↑ Summary
DBIx/Class/Migration/RunScript/Trait/Populate.pm »
| 1 | package DBIx::Class::Migration::RunScript::Trait::Dump; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use File::Spec::Functions 'catdir', 'catfile'; | ||
| 5 | use File::Path 'mkpath'; | ||
| 6 | |||
| 7 | requires 'schema'; | ||
| 8 | |||
| 9 | sub dump { | ||
| 10 | my ($self, @sets) = @_; | ||
| 11 | $ENV{DBIC_MIGRATION_FIXTURES_OBJ}->dump_config_sets({ | ||
| 12 | schema => $self->schema, | ||
| 13 | configs => [map { "$_.json" } @sets], | ||
| 14 | directory_template => sub { | ||
| 15 | my ($fixture, $params, $set) = @_; | ||
| 16 | $set =~s/\.json//; | ||
| 17 | my $fixture_conf_dir = catdir($ENV{DBIC_MIGRATION_FIXTURE_DIR}, $set); | ||
| 18 | mkpath($fixture_conf_dir) | ||
| 19 | unless -d $fixture_conf_dir; | ||
| 20 | return $fixture_conf_dir; | ||
| 21 | }, | ||
| 22 | }); | ||
| 23 | } | ||
| 24 | |||
| 25 | 1; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | DBIx::Class::Migration::RunScript::Trait::Dump - Dump fixtures | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use DBIx::Class::Migration::RunScript; | ||
| 34 | |||
| 35 | builder { | ||
| 36 | 'SchemaLoader', | ||
| 37 | 'Dump', | ||
| 38 | sub { | ||
| 39 | shift->dump('countries'); | ||
| 40 | }; | ||
| 41 | }; | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | This is a L<Moose::Role> that adds a C<dump> method to your run script. This | ||
| 46 | will let you dump fixtures from your runscripts, based on previously defined | ||
| 47 | fixture configurations. | ||
| 48 | |||
| 49 | This might be useful to you if you are building fixtures if they don't already | ||
| 50 | exist (see L<DBIx::Class::Migration::RunScript::Trait::Populate>) and then want | ||
| 51 | to dump them as part of building up your database. For example: | ||
| 52 | |||
| 53 | use DBIx::Class::Migration::RunScript; | ||
| 54 | |||
| 55 | migrate { | ||
| 56 | my $self = shift; | ||
| 57 | if($self->set_has_fixtures('all_tables')) { | ||
| 58 | $self->populate('all_tables'); | ||
| 59 | } else { | ||
| 60 | $self->schema | ||
| 61 | ->resultset('Country') | ||
| 62 | ->populate([ | ||
| 63 | ['code'], | ||
| 64 | ['bel'], | ||
| 65 | ['deu'], | ||
| 66 | ['fra'], | ||
| 67 | ]); | ||
| 68 | |||
| 69 | $self->dump('all_tables'); | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | In the above example if the fixture set exists and has previously been dumped | ||
| 74 | we will populate the database with it. Else, we will create some data manually | ||
| 75 | and then dump it so that next time it is available. | ||
| 76 | |||
| 77 | This trait requires a C<schema> previously defined, such as provided by | ||
| 78 | L<DBIx::Class::Migration::RunScript::Trait::SchemaLoader>. | ||
| 79 | |||
| 80 | This trait is one of the defaults for the exported method C<migrate> in | ||
| 81 | L<DBIx::Class::Migration::RunScript>. | ||
| 82 | |||
| 83 | =head1 methods | ||
| 84 | |||
| 85 | This class defines the follow methods. | ||
| 86 | |||
| 87 | =head2 dump | ||
| 88 | |||
| 89 | Requires $arg || @args | ||
| 90 | |||
| 91 | Given a fixture set (or list of sets), use L<DBIx::Class::Fixtures> to dump | ||
| 92 | them from the current database. | ||
| 93 | |||
| 94 | When naming sets, you skip the '.json' extension. | ||
| 95 | |||
| 96 | =head1 SEE ALSO | ||
| 97 | |||
| 98 | L<DBIx::Class::Migration>, L<DBIx::Class::Migration::RunScript> | ||
| 99 | |||
| 100 | =head1 AUTHOR | ||
| 101 | |||
| 102 | See L<DBIx::Class::Migration> for author information | ||
| 103 | |||
| 104 | =head1 COPYRIGHT & LICENSE | ||
| 105 | |||
| 106 | See L<DBIx::Class::Migration> for copyright and license information | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 |