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.2
10              
11             =cut
12              
13             our $VERSION = '0.7.2';
14              
15 5     5   542 use strict;
  5         6  
  5         183  
16 5     5   19 use warnings;
  5         8  
  5         144  
17 5     5   531 use Data::Dumper;
  5         3969  
  5         291  
18 5     5   572 use Moose;
  5         293679  
  5         36  
19 5     5   22408 use MooseX::Types::Moose qw(HashRef Str);
  5         225319  
  5         1295  
20 5     5   27431 use DBIx::Schema::Changelog::Action::Column;
  5         14  
  5         176  
21 5     5   33 use DBIx::Schema::Changelog::Action::Constraint;
  5         8  
  5         81  
22 5     5   19 use Method::Signatures::Simple;
  5         7  
  5         45  
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 14     14 1 32 has postfix => ( isa => Str, is => 'rw', default => '' );
111 14 50       55  
112 14         569 =back
113 14         588  
114 14         448 =head1 SUBROUTINES/METHODS
115 14         455  
116 14 50       55 =over 4
117              
118 14         21 =item add
119 14         30  
120 14         25 Create new table.
  14         100  
121 58 100       151  
122 46         100 =cut
123 46         67  
124 46         1677 sub add {
125             my ( $self, $params ) = @_;
126 46         1548 return unless $params->{name};
127             my $name = $self->prefix() . $params->{name} . $self->postfix();
128 46         96 my $actions = $self->driver()->actions;
129             my $defaults = $self->driver()->defaults;
130 12 50       487 my $types = $self->driver()->types;
131             my $debug = ( $params->{debug} ) ? 1 : 0;
132 12         17  
  12         426  
133 58         132 my @columns = ();
134 58         81 my $constraints = [];
135 58         2452 foreach my $col ( @{ $params->{columns} } ) {
136             unless ( $col->{tpl} ) {
137 58         2258 $col->{table} = $params->{name};
138             $col->{create_table} = 1;
139             my $const =
140             $self->constraint_action()->add( $col, $constraints, $debug );
141 14         96 push( @columns,
142 14         27 $self->column_action()->add( $col, $const, $debug ) );
143 14         78 next;
144 14         22 }
145 14         38 die "Called template $col->{tpl} is not defined"
146 14         165 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 14         80 }
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 9 $params->{engine}, $params->{charset}
166 5 50       19 ]
167 5         191 );
168 5         41 $self->_do($sql);
169              
170 5 100       34 }
    50          
    100          
    50          
171 2         6  
  2         7  
172 2         8 =item alter
173 2         10  
174             Decides type of alter table
175 2         84 Run command of alter table
176 2         73  
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         29 my $sql =
184             _replace_spare( $actions->{alter_table}, [ $params->{name} ] );
185             if ( defined $params->{addcolumn} ) {
186 2 50       8 foreach ( @{ $params->{addcolumn} } ) {
187 2         374 $_->{table} = $params->{name};
188             my $sql =
189 2         9 _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 9 foreach @$constraints;
213 3         124 }
214 3         24 else {
215 3         12 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 3 }
232 2         6  
233 6         9 =back
234 6         7  
  6         12  
235 12 100       24 =head1 ADDITIONAL SUBROUTINES/METHODS
236 4 50       140  
237             =over 4
238 4         7  
  4         137  
239 4         7 =item load_templates
240              
241 8         15 load pre defined column templates
242              
243 6         208 =cut
244              
245             sub load_templates {
246             my ( $self, $templates ) = @_;
247 5     5   9441 foreach (@$templates) {
  5         9  
  5         36  
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