blib/lib/SQL/Translator/Filter/Globals.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 50.0% 5 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 1 totaltotal 82.3% 42 / 51 CC 14 SCAR 26.0
CC
14
Cov
85%
filter
15.2
BEGIN
1.0
BEGIN
1.0
CRAP 13.4
« blib/lib/SQL/Translator/Diff.pm ↑ Summary blib/lib/SQL/Translator/Utils.pm »
1 package SQL::Translator::Filter::Globals;
2  
3 =head1 NAME
4  
5 SQL::Translator::Filter::Globals - Add global fields and indices to all tables.
6  
7 =head1 SYNOPSIS
8  
9 # e.g. Add timestamp field to all tables.
10 use SQL::Translator;
11  
12 my $sqlt = SQL::Translator->new(
13 from => 'MySQL',
14 to => 'MySQL',
15 filters => [
16 Globals => {
17 fields => [
18 {
19 name => 'modified'
20 data_type => 'TIMESTAMP'
21 }
22 ],
23 indices => [
24 {
25 fields => 'modifed',
26 },
27 ]
28 constraints => [
29 {
30 }
31 ]
32 },
33 ],
34 ) || die "SQLFairy error : ".SQL::Translator->error;
35 my $sql = $sqlt->translate || die "SQLFairy error : ".$sqlt->error;
36  
37 =cut
38  
39 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 our $VERSION = '1.66';
42  
43 1 ▶sub filter {
Subroutine filter: called CC=14 SCAR=27.2
Pod undocumented
44 1 my $schema = shift;
45 1 my %args = @_;
46 1 ▶ my $global_table = $args{global_table} ||= '_GLOBAL_';
Condition50%1/2
$args{"global_table"} ||= "_GLOBAL_"
l!l
01
MC/DC0%0/1
$args{"global_table"} ||= "_GLOBAL_"
$args{"global_table"}
Truth table50%1/2
A
A= $args{"global_table"}
Aresult
11
00
47  
48 1 my (@global_fields, @global_indices, @global_constraints);
49 1 ▶ push @global_fields, @{ $args{fields} } if $args{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"fields"} 1 0 1
50 1 ▶ push @global_indices, @{ $args{indices} } if $args{indices};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"indices"} 1 0 1
51 0 ▶ push @global_constraints, @{ $args{constraints} } if $args{constraints};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"constraints"} 0 1 1
52  
53 # Pull fields and indices off global table and then remove it.
54 1 ▶ if (my $gtbl = $schema->get_table($global_table)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gtbl = $schema->get_table($global_table)) 1 0 1
55  
56 1 foreach ($gtbl->get_fields) {
57  
58 # We don't copy the order attrib so the added fields should get
59 # pushed on the end of each table.
60 1 push @global_fields,
61 {
62 name => $_->name,
63 comments => "" . $_->comments,
64 data_type => $_->data_type,
65 default_value => $_->default_value,
66 size => [ $_->size ],
67 extra => scalar($_->extra),
68 foreign_key_reference => $_->foreign_key_reference,
69 is_auto_increment => $_->is_auto_increment,
70 is_foreign_key => $_->is_foreign_key,
71 is_nullable => $_->is_nullable,
72 is_primary_key => $_->is_primary_key,
73 is_unique => $_->is_unique,
74 is_valid => $_->is_valid,
75 };
76 }
77  
78 1 foreach ($gtbl->get_indices) {
79 1 push @global_indices,
80 {
81 name => $_->name,
82 type => $_->type,
83 fields => [ $_->fields ],
84 options => [ $_->options ],
85 extra => scalar($_->extra),
86 };
87 }
88  
89 1 foreach ($gtbl->get_constraints) {
90 1 push @global_constraints,
91 {
92 name => $_->name,
93 fields => [ $_->fields ],
94 deferrable => $_->deferrable,
95 expression => $_->expression,
96 match_type => $_->match_type,
97 options => [ $_->options ],
98 on_delete => $_->on_delete,
99 on_update => $_->on_update,
100 reference_fields => [ $_->reference_fields ],
101 reference_table => $_->reference_table,
102 table => $_->table,
103 type => $_->type,
104 extra => scalar($_->extra),
105 };
106 }
107  
108 1 $schema->drop_table($gtbl);
109 }
110  
111 # Add globals to tables
112 1 foreach my $tbl ($schema->get_tables) {
113  
114 1 foreach my $new_fld (@global_fields) {
115  
116 # Don't add if field already there
117 2 ▶ next if $tbl->get_field($new_fld->{name});
Branch: 1 branch
BranchTrueFalseTotal
if $tbl->get_field($new_fld->{"name"}) 0 2 2
118 2 $tbl->add_field(%$new_fld);
119 }
120  
121 1 foreach my $new_index (@global_indices) {
122 2 $tbl->add_index(%$new_index);
123 }
124  
125 1 foreach my $new_constraint (@global_constraints) {
126 1 $tbl->add_constraint(%$new_constraint);
127 }
128 }
129 }
130  
131 1 1;
132  
133 __END__
« blib/lib/SQL/Translator/Diff.pm ↑ Summary blib/lib/SQL/Translator/Utils.pm »