File Coverage

lib/DBIx/Schema/Changelog/Action/Table.pm
Criterion Covered Total %
statement 89 96 92.7
branch 16 24 66.6
condition n/a
subroutine 13 13 100.0
pod 4 4 100.0
total 122 137 89.0


line stmt bran cond sub pod time code
1             package DBIx::Schema::Changelog::Action::Table;
2              
3             =head1 NAME
4              
5             DBIx::Schema::Changelog::Action::Table - Action handler for tables
6              
7             =head1 VERSION
8              
9             Version 0.7.1
10              
11             =cut
12              
13             our $VERSION = '0.7.1';
14              
15 5     5   768 use strict;
  5         8  
  5         196  
16 5     5   23 use warnings;
  5         8  
  5         141  
17 5     5   622 use Data::Dumper;
  5         4914  
  5         318  
18 5     5   530 use Moose;
  5         357079  
  5         34  
19 5     5   22309 use MooseX::Types::Moose qw(HashRef Str);
  5         236665  
  5         57  
20 5     5   22713 use DBIx::Schema::Changelog::Action::Column;
  5         15  
  5         189  
21 5     5   36 use DBIx::Schema::Changelog::Action::Constraint;
  5         5  
  5         78  
22 5     5   19 use Method::Signatures::Simple;
  5         8  
  5         57  
23              
24             with 'DBIx::Schema::Changelog::Action';
25              
26             =head1 ATTRIBUTES
27              
28             =over 4
29              
30             =item templates
31              
32             Stored parsed templates from main changelog file.
33              
34             =cut
35              
36             has templates => (
37             is => 'ro',
38             isa => 'HashRef',
39             default => method { {} },
40             );
41              
42             =item constraint_action
43              
44             DBIx::Schema::Changelog::Action::Constraint object.
45              
46             =cut
47              
48             has constraint_action => (
49             is => 'ro',
50             lazy => 1,
51             does => 'DBIx::Schema::Changelog::Action',
52             default => method {
53             DBIx::Schema::Changelog::Action::Constraint->new(
54             driver => $self->driver(),
55             dbh => $self->dbh()
56             )
57             },
58             );
59              
60             =item column_action
61              
62             DBIx::Schema::Changelog::Action::Column object.
63              
64             =cut
65              
66             has column_action => (
67             is => 'ro',
68             lazy => 1,
69             does => 'DBIx::Schema::Changelog::Action',
70             default => method {
71             DBIx::Schema::Changelog::Action::Column->new(
72             driver => $self->driver(),
73             dbh => $self->dbh(),
74             ),
75             },
76             );
77              
78             =item column_action
79              
80             DBIx::Schema::Changelog::Action::Index object to add indices to the table.
81              
82             =cut
83              
84             has index_action => (
85             is => 'ro',
86             lazy => 1,
87             does => 'DBIx::Schema::Changelog::Action',
88             default => method {
89             DBIx::Schema::Changelog::Action::Index->new(
90             driver => $self->driver(),
91             dbh => $self->dbh(),
92             ),
93             },
94             );
95              
96             =item prefix
97              
98             Configurable prefix for table names.
99              
100             =cut
101              
102             has prefix => ( isa => Str, is => 'rw', default => '' );
103              
104             =item postfix
105              
106             Configurable postfix for table names.
107              
108             =cut
109              
110 13     13 1 34 has postfix => ( isa => Str, is => 'rw', default => '' );
111 13 50       54  
112 13         562 =back
113 13         527  
114 13         487 =head1 SUBROUTINES/METHODS
115 13         465  
116 13 50       97 =over 4
117              
118 13         28 =item add
119 13         27  
120 13         25 Create new table.
  13         51  
121 53 100       146  
122 42         122 =cut
123 42         66  
124 42         1820 sub add {
125             my ( $self, $params ) = @_;
126 42         1737 return unless $params->{name};
127             my $name = $self->prefix() . $params->{name} . $self->postfix();
128 42         100 my $actions = $self->driver()->actions;
129             my $defaults = $self->driver()->defaults;
130 11 50       492 my $types = $self->driver()->types;
131             my $debug = ( $params->{debug} ) ? 1 : 0;
132 11         24  
  11         389  
133 54         137 my @columns = ();
134 54         74 my $constraints = [];
135 54         2264 foreach my $col ( @{ $params->{columns} } ) {
136             unless ( $col->{tpl} ) {
137 54         2165 $col->{table} = $params->{name};
138             $col->{create_table} = 1;
139             my $const =
140             $self->constraint_action()->add( $col, $constraints, $debug );
141 13         59 push( @columns,
142 13         23 $self->column_action()->add( $col, $const, $debug ) );
143 13         43 next;
144 13         22 }
145 13         33 die "Called template $col->{tpl} is not defined"
146 13         149 unless $self->templates()->{ $col->{tpl} };
147             foreach ( @{ $self->templates()->{ $col->{tpl} } } ) {
148             $_->{table} = $params->{name};
149             $_->{create_table} = 1;
150             my $const =
151             $self->constraint_action()->add( $_, $constraints, $debug );
152             push( @columns, $self->column_action()->add( $_, $const, $debug ) );
153 13         76 }
154             }
155              
156             $self->index_action()->add( $_, $constraints, $debug )
157             foreach @{ $params->{indices} };
158             $self->constraint_action()->add( $_, $constraints, $debug )
159             foreach @{ $params->{constraints} };
160             push( @columns, @$constraints );
161             my $sql = _replace_spare(
162             $actions->{create_table},
163             [
164             $name, join( ",\n\t", @columns ),
165 5     5 1 18 $params->{engine}, $params->{charset}
166 5 50       27 ]
167 5         449 );
168 5         50 $self->_do($sql);
169              
170 5 100       46 }
    50          
    100          
    50          
171 2         4  
  2         11  
172 2         11 =item alter
173 2         15  
174             Decides type of alter table
175 2         141 Run command of alter table
176 2         313  
177             =cut
178              
179             sub alter {
180 0         0 my ( $self, $params ) = @_;
181             return unless $params->{name};
182             my $actions = $self->driver()->actions;
183 1         59 my $sql =
184             _replace_spare( $actions->{alter_table}, [ $params->{name} ] );
185             if ( defined $params->{addcolumn} ) {
186 2 50       12 foreach ( @{ $params->{addcolumn} } ) {
187 2         588 $_->{table} = $params->{name};
188             my $sql =
189 2         10 _replace_spare( $actions->{alter_table}, [ $params->{name} ] );
190             my $const = $self->constraint_action()->add($_);
191 0         0 $self->_do( $sql . $self->column_action()->add( $_, $const ) );
192 0         0 }
193 0         0 }
194 0         0 elsif ( defined $params->{altercolumn} ) {
195             $self->column_action()->alter($params);
196             }
197 0         0 elsif ( defined $params->{dropcolumn} ) {
198             $self->column_action()->drop($params);
199             }
200 0         0 elsif ( defined $params->{addconstraint} ) {
201             unless ( $actions->{add_constraint} ) {
202             print STDERR __PACKAGE__, ' (', __LINE__,
203             '). Add constraint is not supported!', $/;
204             return;
205             }
206             my $constraints = [];
207             my $fk = $params->{addconstraint};
208             $fk->{table} = $params->{name};
209             $self->constraint_action()->add( $fk, $constraints );
210             $self->_do(
211             $sql . ' ' . _replace_spare( $actions->{add_constraint}, [$_] ) )
212 3     3 1 8 foreach @$constraints;
213 3         141 }
214 3         30 else {
215 3         18 die __PACKAGE__
216             . " Key to alter table not found or implemented.\n Probaply it is misspelled.";
217             }
218             }
219              
220             =item drop
221              
222             Drop defined table.
223              
224             =cut
225              
226             sub drop {
227             my ( $self, $params ) = @_;
228             my $actions = $self->driver()->actions();
229             my $sql = _replace_spare( $actions->{drop_table}, [ $params->{name} ] );
230             $self->_do($sql);
231 2     2 1 5 }
232 2         8  
233 6         8 =back
234 6         9  
  6         12  
235 12 100       26 =head1 ADDITIONAL SUBROUTINES/METHODS
236 4 50       117  
237             =over 4
238 4         2  
  4         114  
239 4         7 =item load_templates
240              
241 8         12 load pre defined column templates
242              
243 6         198 =cut
244              
245             sub load_templates {
246             my ( $self, $templates ) = @_;
247 5     5   10655 foreach (@$templates) {
  5         11  
  5         31  
248             my $tmp = [];
249             foreach my $tpl ( @{ $_->{columns} } ) {
250             if ( defined $tpl->{tpl} ) {
251             die "Called template: '$tpl->{tpl}' is not defined"
252             unless $self->templates()->{ $tpl->{tpl} };
253             push( @$tmp, @{ $self->templates()->{ $tpl->{tpl} } } );
254             next;
255             }
256             push( @$tmp, $tpl );
257             }
258             $self->templates()->{ $_->{name} } = $tmp;
259             }
260             }
261              
262             no Moose;
263             __PACKAGE__->meta->make_immutable;
264              
265             1;
266              
267             __END__
268              
269             =back
270              
271             =head1 AUTHOR
272              
273             Mario Zieschang, C<< <mario.zieschang at combase.de> >>
274              
275             =head1 LICENSE AND COPYRIGHT
276              
277             Copyright 2015 Mario Zieschang.
278              
279             This program is free software; you can redistribute it and/or modify it
280             under the terms of the the Artistic License (2.0). You may obtain a
281             copy of the full license at:
282              
283             L<http://www.perlfoundation.org/artistic_license_2_0>
284              
285             Any use, modification, and distribution of the Standard or Modified
286             Versions is governed by this Artistic License. By using, modifying or
287             distributing the Package, you accept this license. Do not use, modify,
288             or distribute the Package, if you do not accept this license.
289              
290             If your Modified Version has been derived from a Modified Version made
291             by someone other than you, you are nevertheless required to ensure that
292             your Modified Version complies with the requirements of this license.
293              
294             This license does not grant you the right to use any trademark, service
295             mark, trade name, or logo of the Copyright Holder.
296              
297             This license includes the non-exclusive, worldwide, free-of-charge
298             patent license to make, have made, use, offer to sell, sell, import and
299             otherwise transfer the Package with respect to any patent claims
300             licensable by the Copyright Holder that are necessarily infringed by the
301             Package. If you institute patent litigation (including a cross-claim or
302             counterclaim) against any party alleging that the Package constitutes
303             direct or contributory patent infringement, then this Artistic License
304             to you shall terminate on the date that such litigation is filed.
305              
306             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
307             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
308             THE IMPLIED WARRANTIES OF MERCHANT ABILITY, FITNESS FOR A PARTICULAR
309             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
310             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
311             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
312             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
313             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
314              
315             =cut