File Coverage

lib/DBIx/Schema/Changelog/Action/Constraint.pm
Criterion Covered Total %
statement 67 67 100.0
branch 21 28 75.0
condition 8 9 88.8
subroutine 14 14 100.0
pod 3 3 100.0
total 113 121 93.3


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