| 1 | package OpusVL::AppKit::RolesFor::Schema::DataInitialisation; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | sub deploy_with_data | ||
| 6 | { | ||
| 7 | my $self = shift; | ||
| 8 | $self->deploy; | ||
| 9 | $self->initdb; | ||
| 10 | return $self; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub initdb | ||
| 14 | { | ||
| 15 | my $self = shift; | ||
| 16 | for my $resultset ($self->sources) | ||
| 17 | { | ||
| 18 | my $rs = $self->resultset($resultset); | ||
| 19 | $rs->initdb if $rs->can('initdb'); | ||
| 20 | $rs->initdb_populate if $rs->can('initdb_populate'); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | sub clear_dataset | ||
| 25 | { | ||
| 26 | my $self = shift; | ||
| 27 | for my $resultset ($self->sources) | ||
| 28 | { | ||
| 29 | my $rs = $self->resultset($resultset); | ||
| 30 | $rs->clear_dataset if $rs->can('clear_dataset'); | ||
| 31 | } | ||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; | ||
| 36 | |||
| 37 | __END__ | ||
| 38 | |||
| 39 | =pod | ||
| 40 | |||
| 41 | =encoding UTF-8 | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | OpusVL::AppKit::RolesFor::Schema::DataInitialisation | ||
| 46 | |||
| 47 | =head1 VERSION | ||
| 48 | |||
| 49 | version 1 | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | This role allows you to extend your DBIx::Class::Schema with a deploy_with_data | ||
| 54 | method that can be used instead of deploy to deploy and populate your database | ||
| 55 | with some initial data. This is typically used to put a basic user into an auth | ||
| 56 | database and setting up any standard system parameters that are required. | ||
| 57 | |||
| 58 | package My::Schema; | ||
| 59 | use Moose; | ||
| 60 | use namespace::autoclean; | ||
| 61 | extends 'DBIx::Class::Schema'; | ||
| 62 | with 'OpusVL::AppKit::RolesFor::Schema::DataInitialisation'; | ||
| 63 | __PACKAGE__->load_namespaces; | ||
| 64 | __PACKAGE__->meta->make_immutable; | ||
| 65 | 1; | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | OpusVL::AppKit::RolesFor::Schema::DataInitialisation - Deploy with data | ||
| 70 | |||
| 71 | =head1 METHODS | ||
| 72 | |||
| 73 | =head2 deploy_with_data | ||
| 74 | |||
| 75 | This method does a deploy and then checks your resultsets for initdb methods to call. | ||
| 76 | If they are found they are called. This allows you to populate your newly deployed | ||
| 77 | database with initial data. | ||
| 78 | |||
| 79 | =head2 clear_dataset | ||
| 80 | |||
| 81 | A method to call clear_dataset on all the resultsets that support the method. The | ||
| 82 | idea is to allow a method for clearing a dataset of all transient data while | ||
| 83 | retaining all essential configuration. This will vary by project and so is down | ||
| 84 | to how you implement it. | ||
| 85 | |||
| 86 | =head1 LICENSE AND COPYRIGHT | ||
| 87 | |||
| 88 | Copyright 2012 OpusVL. | ||
| 89 | |||
| 90 | This software is licensed according to the "IP Assignment Schedule" provided with the development project. | ||
| 91 | |||
| 92 | =head1 AUTHOR | ||
| 93 | |||
| 94 | Colin Newell <colin@opusvl.com> | ||
| 95 | |||
| 96 | =head1 COPYRIGHT AND LICENSE | ||
| 97 | |||
| 98 | This software is copyright (c) 2016 by OpusVL. | ||
| 99 | |||
| 100 | This is free software; you can redistribute it and/or modify it under | ||
| 101 | the same terms as the Perl 5 programming language system itself. | ||
| 102 | |||
| 103 | =cut |