File Coverage

lib/DBIx/Schema/Changelog/Action/Constraint.pm
Criterion Covered Total %
statement 64 64 100.0
branch 21 28 75.0
condition 8 9 88.8
subroutine 13 13 100.0
pod 3 3 100.0
total 109 117 93.1


line stmt bran cond sub pod time code
1             package DBIx::Schema::Changelog::Action::Constraint;
2              
3             =head1 NAME
4              
5             DBIx::Schema::Changelog::Action::Constraint - Action handler for constraint
6              
7             =head1 VERSION
8              
9             Version 0.7.2
10              
11             =cut
12              
13             our $VERSION = '0.7.2';
14              
15 7     7   1219 use strict;
  7         15  
  7         257  
16 7     7   32 use warnings;
  7         11  
  7         178  
17 7     7   29 use Data::Dumper;
  7         9  
  7         335  
18 7     7   28 use Moose;
  7         8  
  7         41  
19 7     7   31225 use Method::Signatures::Simple;
  7         6201  
  7         89  
20 7     7   5191 use DBIx::Schema::Changelog::Action::Default;
  7         22  
  7         382  
21              
22             with 'DBIx::Schema::Changelog::Action';
23              
24             has default => (
25             is => 'rw',
26             lazy => 1,
27             does => 'DBIx::Schema::Changelog::Action',
28             default => method {
29             DBIx::Schema::Changelog::Action::Default->new(
30             driver => $self->driver(),
31             dbh => $self->dbh()
32             )
33             },
34             );
35              
36             =head1 SUBROUTINES/METHODS
37              
38             =head2 add
39 111     111 1 965  
40 111 50       216 =cut
41              
42 111         3810 sub add {
43 111 100 100     380 my ( $self, $col, $constr_ref, $debug ) = @_;
44 1         3 print __PACKAGE__, ' (', __LINE__, ') ', $/, Dumper( $col, $constr_ref )
45 1         2 if ($debug);
46             my $consts = $self->driver()->constraints;
47 110 100       319 if ( defined $col->{primarykey} && ref $col->{primarykey} eq 'ARRAY' ) {
48 1         5 push( @$constr_ref, $self->_primary($col) );
49 1         3 return;
50             }
51             if ( defined $col->{unique} ) {
52 109 100       195 push( @$constr_ref, $self->_unique($col) );
53 109 100 100     449 return;
54             }
55              
56 109 50 66     260 my $must_nn = ( defined $col->{primarykey} ) ? 1 : 0;
57 109 100       301 my $isnt_nn =
58             ( !defined $col->{default} && !defined $col->{foreign} ) ? 1 : 0;
59 109 100       278  
60 109 100       182 die "No default value set for $col->{name}" if ( $must_nn && $isnt_nn );
61             push( @$constr_ref, $self->_foreign($col) ) if ( defined $col->{foreign} );
62 109         4468  
63 109         409 my $not_null = ( $col->{notnull} ) ? $consts->{not_null} : '';
64             my $primarykey =
65             ( defined $col->{primarykey} ) ? $consts->{primary_key} : '';
66             my $default = $self->default()->add( $col, $debug );
67             return qq~$not_null $primarykey $default~;
68             }
69              
70             =head2 alter
71 1     1 1 421  
72             =cut
73              
74             sub alter {
75             my ( $self, $table_name, $col, $constr_ref ) = @_;
76              
77             #$self->table_action()->add($_) if (uc $constraint->{type} eq 'NOT_NULL' );
78             #$self->table_action()->drop($_) if (uc $constraint_->{type} eq 'UNIQUE' );
79             #$self->table_action()->alter($_) if (uc $constraint_->{type} eq 'PRIMARY' );
80             #$self->index_action()->add($_) if (uc $constraint_->{type} eq 'FOREIGN' );
81             #$self->index_action()->alter($_) if (uc $constraint_->{type} eq 'CHECK' );
82             #$self->index_action()->drop($_) if (uc $constraint_->{type} eq 'DEFAULT' );
83             }
84              
85             =head2 drop
86 1     1 1 4  
87             =cut
88              
89             sub drop {
90             my ( $self, $table_name, $col, $constraints ) = @_;
91              
92             #$self->table_action()->add($_) if (uc $constraint->{type} eq 'NOT_NULL' );
93             #$self->table_action()->drop($_) if (uc $constraint_->{type} eq 'UNIQUE' );
94             #$self->table_action()->alter($_) if (uc $constraint_->{type} eq 'PRIMARY' );
95             #$self->index_action()->add($_) if (uc $constraint_->{type} eq 'FOREIGN' );
96             #$self->index_action()->alter($_) if (uc $constraint_->{type} eq 'CHECK' );
97             #$self->index_action()->drop($_) if (uc $constraint_->{type} eq 'DEFAULT' );
98             }
99              
100             =head1 AUXILIARY SUBROUTINES/METHODS
101              
102             =head2 _foreign
103              
104             Private sub to handle foreign keys constraints
105 20     20   40  
106 20         827 =cut
107 20 50       67  
108 20         45 sub _foreign {
109 20         49 my ( $self, $col, $constr_ref ) = @_;
110 20         31 my $actions = $self->driver()->actions;
111 20         37 die "Foreign key is not supported!", $/ unless $actions->{foreign_key};
112             my $table = '' . $col->{table};
113 20         41 my $ref_table = $col->{foreign}->{reftable};
114 20         26 my $name = $col->{name};
115 20         33 my $refcolumn = $col->{foreign}->{refcolumn};
116              
117 20         173 $table =~ s/"//g;
118             $ref_table =~ s/"//g;
119             $name =~ s/"//g;
120              
121             return _replace_spare(
122             $actions->{foreign_key},
123             [
124             $col->{name}, $ref_table,
125             $col->{foreign}->{refcolumn},
126             "fkey_$table" . "_$refcolumn" . "_$name"
127             ]
128             );
129             }
130              
131             =head2 _unique
132              
133             Private sub to handle unique constraints
134 1     1   2  
135 1         23 =cut
136 1 50       3  
137 1 50       5 sub _unique {
138 1         1 my ( $self, $col, $constr_ref ) = @_;
139 1 50       4 my $actions = $self->driver()->actions;
140 1         4 return unless $actions->{unique};
141 1         2 my $table = ( defined $col->{table} ) ? $col->{table} . '' : '';
142             $table =~ s/"//g;
143             my $name = ( defined $col->{name} ) ? $col->{name} : time();
144             return _replace_spare( $actions->{unique},
145             [ qq~unique_$name~, join( ',', @{ $col->{unique} } ) ] );
146             }
147              
148             =head2 _primary
149              
150             Private sub to to handle primary key with more than one column
151 1     1   2  
152 1         29 =cut
153 1 50       4  
154 1         6 sub _primary {
155 1         3 my ( $self, $col, $constr_ref ) = @_;
156             my $actions = $self->driver()->actions;
157             my $name = ( defined $col->{name} ) ? $col->{name} : time() . '_gen';
158 7     7   6126 return _replace_spare( $actions->{primary},
  7         15  
  7         40  
159             [ qq~pkay_multi_$name~, join( ',', @{ $col->{primarykey} } ) ] );
160             }
161              
162             no Moose;
163             __PACKAGE__->meta->make_immutable;
164              
165             1;
166              
167             __END__
168              
169             =head1 AUTHOR
170              
171             Mario Zieschang, C<< <mario.zieschang at combase.de> >>
172              
173             =head1 LICENSE AND COPYRIGHT
174              
175             Copyright 2015 Mario Zieschang.
176              
177             This program is free software; you can redistribute it and/or modify it
178             under the terms of the the Artistic License (2.0). You may obtain a
179             copy of the full license at:
180              
181             L<http://www.perlfoundation.org/artistic_license_2_0>
182              
183             Any use, modification, and distribution of the Standard or Modified
184             Versions is governed by this Artistic License. By using, modifying or
185             distributing the Package, you accept this license. Do not use, modify,
186             or distribute the Package, if you do not accept this license.
187              
188             If your Modified Version has been derived from a Modified Version made
189             by someone other than you, you are nevertheless required to ensure that
190             your Modified Version complies with the requirements of this license.
191              
192             This license does not grant you the right to use any trademark, service
193             mark, trade name, or logo of the Copyright Holder.
194              
195             This license includes the non-exclusive, worldwide, free-of-charge
196             patent license to make, have made, use, offer to sell, sell, import and
197             otherwise transfer the Package with respect to any patent claims
198             licensable by the Copyright Holder that are necessarily infringed by the
199             Package. If you institute patent litigation (including a cross-claim or
200             counterclaim) against any party alleging that the Package constitutes
201             direct or contributory patent infringement, then this Artistic License
202             to you shall terminate on the date that such litigation is filed.
203              
204             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
205             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
206             THE IMPLIED WARRANTIES OF MERCHANT ABILITY, FITNESS FOR A PARTICULAR
207             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
208             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
209             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
210             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
211             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212              
213             =cut