blib/script/narada-mysqldump

filter: Statementstmt 40.0% 91 / 227 Branchbran 7.4% 4 / 54 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 72.0% 31 / 43 Podpod n/a totaltotal 35.9% 126 / 351 CC 39 SCAR 30.0
CC
39
Cov
32%
init_globals
61.1
del_dumps_except
56.1
get_key
42.0
CRAP 20.1
« blib/script/narada-setup-mysql ↑ Summary lib/Narada.pm »
1 #!/usr/bin/env perl
2 102 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 102 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 100 our $VERSION = 'v2.3.8';
8  
9 102 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 102 ▶use lib "$FindBin::Bin/../lib/perl5";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 102 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 102 ▶use Narada::Lock qw( exclusive_lock unlock_new unlock );;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 102 ▶use Narada::Config qw( get_config get_config_line get_db_config );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 102 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 102 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 102 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 102 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 102 ▶use Fcntl qw(F_SETFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 102 ▶use constant IS_NARADA => Narada::detect() eq 'narada';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 102 ▶use constant CONFIG_DIR => IS_NARADA ? 'mysql' : 'db';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 102 ▶use constant SQL_DIR => IS_NARADA ? 'var/mysql' : 'var/sql';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 102 ▶use constant SCHEME => SQL_DIR.'/db.scheme.sql';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 102 ▶use constant REQUIRED_TABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 102 ▶use constant OPTIONAL_TABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 102 ▶use constant TIMELOCAL_MONTH=> 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 102 ▶use constant STAT_MTIME => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 102 ▶use constant DESC_FIELD => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 102 ▶use constant DESC_TYPE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 102 ▶use constant DESC_NULL => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 102 ▶use constant DESC_KEY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 102 ▶use constant DESC_DEFAULT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 102 ▶use constant DESC_EXTRA => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # GLOBALS: $::dbh, $::mycnf, $::MYSQLDUMP
36  
37  
38 100 ▶main(@ARGV) if !caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 100 0 100
39  
40  
41 0 ▶sub err { die "narada-mysqldump: @_\n" }
Subroutine err: not called CC=1 SCAR=6.9
42  
43 100 ▶sub main {
Subroutine main: called CC=3 SCAR=19.5
44 100 ▶ die "Usage: narada-mysqldump\n" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 100 100
45  
46 100 ▶ init_globals() or return;
Branch: 1 branch
BranchTrueFalseTotal
init_globals() or return 0 100 100
47  
48 0 exclusive_lock();
49 0 unlock_new();
50  
51 0 my ($full, $incremental, $ignore) = list_tables();
52 0 my $unchanged = detect_unchanged($full);
53  
54 0 del_dumps_except($incremental, $unchanged);
55 0 dump_scheme_except($ignore);
56 0 dump_full($full, $unchanged);
57 0 dump_incremental($incremental);
58  
59 # To correctly detect_unchanged() we should guarantee next table's
60 # Update_time after executing narada-mysqldump will be at least 1
61 # second later than previous (which kept in dump file's mtime).
62 0 sleep 1;
63  
64 0 unlock();
65  
66 0 return;
67 }
68  
69 100 ▶sub init_globals {
Subroutine init_globals: called CC=8 SCAR=41.1
70 100 ▶ my $db = get_db_config() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $db = get_db_config() or return 0 100 100
71 $::dbh = DBI->connect($db->{dsn}, $db->{login}, $db->{pass},
72 0 ▶ {RaiseError=>1}) or err DBI->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$main::dbh = "DBI"->connect($db->{"dsn"}, $db->{"login"}, $db->{"pass"}, {"RaiseError", 1}) or "DBI"->err->errstr 0 0 0
73 0 $::mycnf = tempfile(DIR=>'tmp');
74 0 print {$::mycnf} "[client]\n";
75 0 print {$::mycnf} "user = $db->{login}\n";
76 0 ▶ print {$::mycnf} "password = $db->{pass}\n" if length $db->{pass}; ## no critic (ProhibitPostfixControls)
Branch: 1 branch
BranchTrueFalseTotal
if length $db->{"pass"} 0 0 0
77 0 ▶ print {$::mycnf} "host = $db->{host}\n" if length $db->{host}; ## no critic (ProhibitPostfixControls)
Branch: 1 branch
BranchTrueFalseTotal
if length $db->{"host"} 0 0 0
78 0 ▶ print {$::mycnf} "port = $db->{port}\n" if length $db->{port}; ## no critic (ProhibitPostfixControls)
Branch: 1 branch
BranchTrueFalseTotal
if length $db->{"port"} 0 0 0
79 0 $::mycnf->flush;
80 0 ▶ sysseek $::mycnf, 0, 0 or err "sysseek: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysseek $main::mycnf, 0, 0 or err("sysseek: $!") 0 0 0
81 0 ▶ fcntl $::mycnf, F_SETFD, 0 or err "fcntl: $!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $main::mycnf, 2, 0 or err("fcntl: $!") 0 0 0
82 0 my $fd = fileno $::mycnf;
83 0 $::MYSQLDUMP = "mysqldump --defaults-file=/proc/self/fd/\Q$fd\E \Q$db->{db}\E";
84 0 return 1;
85 }
86  
87 0 ▶sub list_tables {
Subroutine list_tables: not called CC=3 SCAR=24.8
88 0 my $incremental = load_db_conf('dump/incremental', REQUIRED_TABLE);
89 0 my $empty = load_db_conf('dump/empty', REQUIRED_TABLE);
90 0 my $ignore = load_db_conf('dump/ignore', OPTIONAL_TABLE);
91 0 my %other = map {$_=>1} @{$incremental}, @{$empty}, @{$ignore};
92 0 my @full;
93 0 for my $table (@{ $::dbh->selectcol_arrayref('SHOW TABLES') }) {
94 0 ▶ next if $other{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $other{$table} 0 0 0
95 0 push @full, $table;
96 }
97 0 return ([sort @full], $incremental, $ignore);
98 }
99  
100 0 ▶sub load_db_conf {
Subroutine load_db_conf: not called CC=4 SCAR=30.0
101 0 my ($conf, $required) = @_;
102 0 my @conf = eval { split /\s*\n/xms, get_config(CONFIG_DIR."/$conf") };
103 0 my @tables;
104 0 for my $table (@conf) {
105 0 local ($::dbh->{RaiseError}, $::dbh->{PrintError});
106 0 my $desc = $::dbh->selectall_arrayref('DESC '.$table);
107 0 ▶ if ($desc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($desc) { } 0 0 0
elsif ($required == 0) { } 0 0 0
108 0 push @tables, $table;
109 }
110 elsif ($required == REQUIRED_TABLE) {
111 0 err "Table $table listed in ".CONFIG_DIR."/$conf does not exists\n";
112 }
113 }
114 0 return [sort @tables];
115 }
116  
117 0 ▶sub detect_unchanged {
Subroutine detect_unchanged: not called CC=4 SCAR=30.0
118 0 my ($full) = @_;
119 0 my @unchanged;
120 0 for my $table (@{$full}) {
121 0 my $file = SQL_DIR."/$table.sql";
122 0 ▶ if (-f $file && mtime($file) == get_table_status($table, 'Update_time')) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and mtime($file) == get_table_status($table, "Update_time")) 0 0 0
Condition0%0/3
-f $file and mtime($file) == get_table_status($table, "Update_time")
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and mtime($file) == get_table_status($table, "Update_time")
-f $file mtime($file) == get_table_status($table, "Update_time")
Truth table0%0/3
A and B
A= -f $fileB= mtime($file) == get_table_status($table, "Update_time")
ABresult
0X0
100
111
123 0 push @unchanged, $table;
124 }
125 }
126 0 return \@unchanged;
127 }
128  
129 0 ▶sub del_dumps_except {
Subroutine del_dumps_except: not called CC=8 SCAR=40.3
130 0 my ($incremental, $unchanged) = @_;
131 0 my %incremental = map {$_=>1} @{$incremental};
132 0 my %unchanged = map {$_=>1} @{$unchanged};
133 0 ▶ for my $file (glob SQL_DIR.'/*.sql') {
Subroutine BEGIN: called CC=1 SCAR=0.2
134 0 ▶ if ($file =~ m{\A\Q${\SQL_DIR}\E/([^.]*)[.]sql\z}xms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ m[\A\Q${\'var/mysql';}\E/([^.]*)[.]sql\z]msx) { } 0 0 0
elsif ($file =~ m[\A\Q${\'var/mysql';}\E/([^.]*)[.]\d+-(\d+)[.]sql\z]msx) { } 0 0 0
135 0 my $table = $1;
136 0 ▶ next if $unchanged{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $unchanged{$table} 0 0 0
137 }
138 0 elsif ($file =~ m{\A\Q${\SQL_DIR}\E/([^.]*)[.]\d+-(\d+)[.]sql\z}xms) {
139 0 my $table = $1;
140 0 ▶ next if $incremental{$table}
Branch: 1 branch
BranchTrueFalseTotal
if $incremental{$table} and mtime($file) >= get_table_status($table, "Create_time") 0 0 0
Condition0%0/3
$incremental{$table} and mtime($file) >= get_table_status($table, "Create_time")
!ll&&!rl&&r
000
MC/DC0%0/2
$incremental{$table} and mtime($file) >= get_table_status($table, "Create_time")
$incremental{$table} mtime($file) >= get_table_status($table, "Create_time")
Truth table0%0/3
A and B
A= $incremental{$table}B= mtime($file) >= get_table_status($table, "Create_time")
ABresult
0X0
100
111
141 && mtime($file) >= get_table_status($table, 'Create_time');
142 }
143 0 ▶ unlink $file or err "unlink($file): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or err("unlink($file): $!\n") 0 0 0
144 }
145 0 return;
146 }
147  
148 0 ▶sub dump_scheme_except {
Subroutine dump_scheme_except: not called CC=1 SCAR=6.9
149 0 my ($ignore) = @_;
150 0 my $db = get_config_line(CONFIG_DIR.'/db');
151 0 my $tables = join q{ }, map {"--ignore-table=\Q$db\E.\Q$_\E"} @{$ignore};
152 0 mysqldump("--opt -d $tables", SCHEME, time);
153 0 return;
154 }
155  
156 0 ▶sub dump_full {
Subroutine dump_full: not called CC=3 SCAR=24.8
157 0 my ($full, $unchanged) = @_;
158 0 my %unchanged = map {$_=>1} @{$unchanged};
159 0 for my $table (@{$full}) {
160 0 ▶ next if $unchanged{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $unchanged{$table} 0 0 0
161 0 my $file = SQL_DIR."/$table.sql";
162 0 my $t = get_table_status($table, 'Update_time');
163 0 mysqldump("--opt -t \Q$table\E", $file, $t);
164 }
165 0 return;
166 }
167  
168 0 ▶sub dump_incremental {
Subroutine dump_incremental: not called CC=3 SCAR=24.8
169 0 my ($incremental) = @_;
170 0 for my $table (@{$incremental}) {
171 0 my $key = get_key($table);
172 0 my $prev = max(0, map {m/-(\d+)[.]sql\z/xms} glob SQL_DIR."/\Q$table\E.*.sql");
173 0 my $next = get_table_status($table, 'Auto_increment');
174 0 ▶ if ($prev < $next-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev < $next - 1) 0 0 0
175 0 my $from = $prev+1;
176 0 my $to = $next-1;
177 0 my $file = SQL_DIR."/$table.$from-$to.sql";
178 0 my $where= "$key>=$from AND $key<=$to";
179 0 my $t = get_table_status($table, 'Update_time');
180 0 mysqldump("--opt -t -w \Q$where\E \Q$table\E", $file, $t);
181 }
182 }
183 0 return;
184 }
185  
186 ###
187  
188 0 ▶sub get_table_status {
Subroutine get_table_status: not called CC=3 SCAR=24.8
189 0 my ($table, $field) = @_;
190 my $val = $::dbh->selectrow_hashref('SHOW TABLE STATUS LIKE ?',
191 0 undef, $table)->{$field};
192 0 ▶ if ($field =~ /_time\z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /_time\z/msx) 0 0 0
193 0 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
194 0 $val = 0;
195 }
196 else {
197 0 my @datetime = reverse split /\D+/xms, $val;
198 0 $datetime[TIMELOCAL_MONTH]--;
199 0 $val = timelocal(@datetime);
200 }
201 }
202 0 return $val;
203 }
204  
205 0 ▶sub get_key {
Subroutine get_key: not called CC=6 SCAR=37.4
206 0 my ($table) = @_;
207 0 my $desc = $::dbh->selectall_arrayref('DESC '.$table);
208 err "First field in table $table must be: INT AUTO_INCREMENT PRIMARY KEY\n"
209 0 if !(@{ $desc }
210 && $desc->[0][DESC_TYPE]=~/\A\w*int\b/xms
211 && $desc->[0][DESC_KEY] eq 'PRI'
212 && $desc->[0][DESC_EXTRA] eq 'auto_increment'
213 0 ▶ && 1 == grep {$_->[DESC_KEY] eq 'PRI'} @{$desc});
Branch: 1 branch
BranchTrueFalseTotal
unless @{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment" and 1 == grep({$_->[3] eq "PRI";} @{$desc;}) 0 0 0
Condition0%0/12
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment" and 1 == grep({$_->[3] eq "PRI";} @{$desc;})
!ll&&!rl&&r
000
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment"
!ll&&!rl&&r
000
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI"
!ll&&!rl&&r
000
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx
!ll&&!rl&&r
000
MC/DC0%0/5
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment" and 1 == grep({$_->[3] eq "PRI";} @{$desc;})
@{$desc;} $desc->[0][1] =~ /\A\w*int\b/msx $desc->[0][3] eq "PRI" $desc->[0][5] eq "auto_increment" 1 == grep({$_->[3] eq "PRI";} @{$desc;})
Truth table0%0/6
A and B and C and D and E
A= @{$desc;}B= $desc->[0][1] =~ /\A\w*int\b/msxC= $desc->[0][3] eq "PRI"D= $desc->[0][5] eq "auto_increment"E= 1 == grep({$_->[3] eq "PRI";} @{$desc;})
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
214 0 return $desc->[0][DESC_FIELD];
215 }
216  
217 0 ▶sub mtime {
Subroutine mtime: not called CC=1 SCAR=6.9
218 0 my ($file) = @_;
219 0 return (stat $file)[STAT_MTIME];
220 }
221  
222 0 ▶sub mysqldump {
Subroutine mysqldump: not called CC=4 SCAR=30.0
223 0 my ($opt, $file, $t) = @_;
224 0 ▶ system("$::MYSQLDUMP $opt > \Q$file\E.tmp")
Branch: 1 branch
BranchTrueFalseTotal
system("$main::MYSQLDUMP $opt > \Q$file\E.tmp") == 0 or err("system($main::MYSQLDUMP $opt > $file.tmp): $?") 0 0 0
225 == 0 or err "system($::MYSQLDUMP $opt > $file.tmp): $?";
226 0 ▶ rename "$file.tmp", $file or err "rename($file.tmp, $file): $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$file.tmp", $file or err("rename($file.tmp, $file): $!") 0 0 0
227 0 ▶ utime $t, $t, $file or err "utime($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
utime $t, $t, $file or err("utime($file): $!") 0 0 0
228 0 return;
229 }
230  
231  
232 1; # Magic true value required at end of module
233 __END__
« blib/script/narada-setup-mysql ↑ Summary lib/Narada.pm »