blib/lib/Strehler/Element/Role/Maintainer.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 68 CC 2 SCAR 11.5
CC
2
Cov
0%
deploy_entity_on_db
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 package Strehler::Element::Role::Maintainer;
2 $Strehler::Element::Role::Maintainer::VERSION = '1.6.8';
3 use strict;
4 use Moo::Role;
5 use SQL::Translator;
6 use SQL::Translator::Parser::DBIx::Class;
7 use Module::Load;
8  
9 requires 'install';
10  
11 sub deploy_entity_on_db
12 {
13 my $self = shift;
14 my $dbh = shift;
15 my $entities = shift;
16  
17 my $schema = Strehler::Schema->connect(sub {return $dbh });
18 my $producer = $dbh->{Driver}->{Name};
19 $producer = "MySQL" if $producer eq 'mysql';
20  
21 my $trans1 = SQL::Translator->new (
22 parser => 'SQL::Translator::Parser::DBIx::Class',
23 parser_args => {
24 dbic_schema => $schema,
25 no_comments => 1,
26 },
27 producer => $producer
28 ) or print SQL::Translator->error;
29
30 my @already_done_queries = $trans1->translate();
31  
32 foreach my $table (@{$entities})
33 {
34 print "Working on $table...\n";
35 my $added = $table;
36 load $added;
37 my $table_name = $table;
38 $table_name =~ s/^.*:://;
39 $schema->register_class($table_name, $added);
40  
41 my $new_trans = SQL::Translator->new (
42 parser => 'SQL::Translator::Parser::DBIx::Class',
43 parser_args => {
44 dbic_schema => $schema,
45 no_comments => 1,
46 },
47 producer => $producer
48 ) or print SQL::Translator->error;
49 my %already_done = map {$_ => 1} @already_done_queries;
50 my @todo = grep {not $already_done{$_}} $new_trans->translate();
51 foreach my $q (@todo)
52 {
53 $dbh->do($q) or print $dbh->errstr . "\n";
54 }
55 @already_done_queries = $new_trans->translate();
56 }
57  
58 return;
59 }
60  
61 =encoding utf8
62  
63 =head1 NAME
64  
65 Strehler::Element::Role::Maintainer - Maintainer role
66  
67 =head1 DESCRIPTION
68  
69 This role is used only during entity installation. It requires an install method to be implemented. Install method will be called by strehler script when used with the command initentity.
70  
71 This role was introduced to collect helpers and introspective methods useful for installation.
72  
73 =head1 INSTALL METHOD
74  
75 strehler initentity My::Entity::Maintained
76  
77 Using strehler script this way the system call the install method implemented in the entity passing as parameters the database handler defined using Dancer2 configuration and public directory. This two elements can be used to customize the enviroment as for entity requests.
78  
79 =head1 HELPERS
80  
81 =head2 DEPLOY_ENTITY_ON_DB
82  
83 arguments: $dbh, $schema_classes
84  
85 $class->deploy_entity_on_db($dbh, ['My::Entity::Schema', 'My::Entity::Multilang::Schema']);
86  
87 You can use this method to add new tables to site database. Define needed tables through DBIx::Schema classes, that call the method with the database handler and an array containing class names. Tables will be added to the database.
88  
89 =head3 CAVEATS
90  
91 Defining classes array, order matters! If you have foreign keys ensure that the table that will be linke is already created. For example, always place main table first and multilang element second.
92  
93 Writing this methos was a great pain and I had to hack a little many things. I tested it on MySQL and SQLite, but I don't know how it can behave in more exotic environments. Try it and tell me...
94  
95 =cut
96  
97 1;
98  
99  
100  
101