File Coverage

lib/DBIx/Schema/Changelog/Action/Table.pm
Criterion Covered Total %
statement 92 99 92.9
branch 16 24 66.6
condition n/a
subroutine 14 14 100.0
pod 4 4 100.0
total 126 141 89.3


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.8.0
10              
11             =cut
12              
13             our $VERSION = '0.8.0';
14 5     5   2278 use utf8;
  5         24  
  5         47  
15 5     5   241 use strict;
  5         13  
  5         261  
16 5     5   35 use warnings;
  5         8  
  5         251  
17 5     5   972 use Data::Dumper;
  5         8244  
  5         494  
18 5     5   1200 use Moose;
  5         486690  
  5         50  
19 5     5   41313 use MooseX::Types::Moose qw(HashRef Str);
  5         406208  
  5         75  
20 5     5   35344 use DBIx::Schema::Changelog::Action::Column;
  5         27  
  5         275  
21 5     5   54 use DBIx::Schema::Changelog::Action::Constraint;
  5         10  
  5         153  
22 5     5   26 use Method::Signatures::Simple;
  5         8  
  5         60  
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 => sub { {} },
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             has postfix => ( isa => Str, is => 'rw', default => '' );
111              
112             =back
113 14     14 1 41  
114 14 50       66 =head1 SUBROUTINES/METHODS
115 14         1049  
116 14         654 =over 4
117 14         725  
118 14         709 =item add
119 14 50       64  
120             Create new table.
121 14         42  
122 14         37 =cut
123 14         31  
  14         63  
124 58 100       192 sub add {
125 46         167 my ( $self, $params ) = @_;
126 46         89 return unless $params->{name};
127 46         2496 my $name = $self->prefix() . $params->{name} . $self->postfix();
128             my $actions = $self->driver()->actions;
129 46         1882 my $defaults = $self->driver()->defaults;
130             my $types = $self->driver()->types;
131 46         128 my $debug = ( $params->{debug} ) ? 1 : 0;
132              
133 12 50       647 my @columns = ();
134             my $constraints = [];
135 12         20 foreach my $col ( @{ $params->{columns} } ) {
  12         4745  
136 58         187 unless ( $col->{tpl} ) {
137 58         103 $col->{table} = $params->{name};
138 58         2877 $col->{create_table} = 1;
139             my $const =
140 58         3561 $self->constraint_action()->add( $col, $constraints, $debug );
141             push( @columns,
142             $self->column_action()->add( $col, $const, $debug ) );
143             next;
144 14         89 }
145 14         33 die "Called template $col->{tpl} is not defined"
146 14         72 unless $self->templates()->{ $col->{tpl} };
147 14         35 foreach ( @{ $self->templates()->{ $col->{tpl} } } ) {
148 14         45 $_->{table} = $params->{name};
149 14         507 $_->{create_table} = 1;
150             my $const =
151             $self->constraint_action()->add( $_, $constraints, $debug );
152             push( @columns, $self->column_action()->add( $_, $const, $debug ) );
153             }
154             }
155              
156 14         95 $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             $params->{engine}, $params->{charset}
166             ]
167             );
168 5     5 1 18 $self->_do($sql);
169 5 50       23  
170 5         266 }
171 5         52  
172             =item alter
173 5 100       60  
    50          
    100          
    50          
174 2         4 Decides type of alter table
  2         9  
175 2         10 Run command of alter table
176 2         12  
177             =cut
178 2         122  
179 2         549 sub alter {
180             my ( $self, $params ) = @_;
181             return unless $params->{name};
182             my $actions = $self->driver()->actions;
183 0         0 my $sql =
184             _replace_spare( $actions->{alter_table}, [ $params->{name} ] );
185             if ( defined $params->{addcolumn} ) {
186 1         68 foreach ( @{ $params->{addcolumn} } ) {
187             $_->{table} = $params->{name};
188             my $sql =
189 2 50       12 _replace_spare( $actions->{alter_table}, [ $params->{name} ] );
190 2         574 my $const = $self->constraint_action()->add($_);
191             $self->_do( $sql . $self->column_action()->add( $_, $const ) );
192 2         15 }
193             }
194 0         0 elsif ( defined $params->{altercolumn} ) {
195 0         0 $self->column_action()->alter($params);
196 0         0 }
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 0         0 '). 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             foreach @$constraints;
213             }
214             else {
215 3     3 1 10 die __PACKAGE__
216 3         133 . " Key to alter table not found or implemented.\n Probaply it is misspelled.";
217 3         32 }
218 3         18 }
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             }
232              
233             =back
234 2     2 1 5  
235 2         7 =head1 ADDITIONAL SUBROUTINES/METHODS
236 6         14  
237 6         8 =over 4
  6         17  
238 12 100       32  
239 4 50       211 =item load_templates
240              
241 4         6 load pre defined column templates
  4         198  
242 4         9  
243             =cut
244 8         20  
245             sub load_templates {
246 6         470 my ( $self, $templates ) = @_;
247             foreach (@$templates) {
248             my $tmp = [];
249             foreach my $tpl ( @{ $_->{columns} } ) {
250 5     5   23008 if ( defined $tpl->{tpl} ) {
  5         12  
  5         55  
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