| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Perl module for applying incremental database deltas/migrations to a database | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package DBIx::Delta; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use FindBin qw($Bin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use IO::Dir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | $VERSION = '1.0.0'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # abstract connect() - should be overridden with a sub returning a valid $dbh | ||||||||||||||||||||||||||||
| 20 | sub connect | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 0 | die "connect() is an abstract method that must be provided by a subclass"; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Return a string for the environment if you want to distinguish prod/test etc. | ||||||||||||||||||||||||||||
| 26 | sub environment | ||||||||||||||||||||||||||||
| 27 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
environment: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 7 | return ''; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub _die | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_die: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 0 | $self->{dbh}->disconnect; | |||||||||||||||||||||||||||
| 35 | 0 | die join ' ', @_; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # For subclassing to localise statements e.g. mysql grants in dev might need to | ||||||||||||||||||||||||||||
| 39 | # use different IP addresses in production | ||||||||||||||||||||||||||||
| 40 | # e.g. s/^\s* (grant\b.*?) localhost /${1}192.168.0.1/x | ||||||||||||||||||||||||||||
| 41 | sub filter_statement | ||||||||||||||||||||||||||||
| 42 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
filter_statement: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 7 | shift; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Parse arguments | ||||||||||||||||||||||||||||
| 48 | sub parse_args | ||||||||||||||||||||||||||||
| 49 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_args: called CC=13 SCAR=32.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 7 | ▶ | @ARGV = @_ if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | my %opts = (); | |||||||||||||||||||||||||||
| 54 | 7 | getopts('?bdfhnqsu',\%opts); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | ▶ | if ($opts{'?'} || $opts{h}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"?"} or $opts{"h"}
MC/DC0%0/2
$opts{"?"} or $opts{"h"}
$opts{"?"}
$opts{"h"}
Truth table33%1/3
A or B
A= $opts{"?"}B= $opts{"h"}
| |||||||||||||||||||||||||||||
| 57 | 0 | print "usage: " . basename($0) . " [-qbd] [-n] [-f] [-s|-u] [<delta> ...]\n"; | |||||||||||||||||||||||||||
| 58 | 0 | exit 1; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 7 | ▶ | $self->{brief} = $opts{b} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'b'} || ''
MC/DC0%0/1
$opts{'b'} || ''
$opts{'b'}
Truth table50%1/2
A
A= $opts{'b'}
| |||||||||||||||||||||||||||||
| 62 | 7 | ▶ | $self->{debug} = $opts{d} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'d'} || ''
MC/DC0%0/1
$opts{'d'} || ''
$opts{'d'}
Truth table50%1/2
A
A= $opts{'d'}
| |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | $self->{force} = $opts{f} && @ARGV ? $opts{f} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"f"} and @ARGV
MC/DC0%0/2
$opts{"f"} and @ARGV
$opts{"f"}
@ARGV
Truth table33%1/3
A and B
A= $opts{"f"}B= @ARGV
| |||||||||||||||||||||||||||||
| 64 | 7 | ▶ | $self->{noop} = $opts{n} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'n'} || ''
MC/DC0%0/1
$opts{'n'} || ''
$opts{'n'}
Truth table50%1/2
A
A= $opts{'n'}
| |||||||||||||||||||||||||||||
| 65 | 7 | ▶ | $self->{quiet} = $opts{q} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'q'} || ''
MC/DC0%0/1
$opts{'q'} || ''
$opts{'q'}
Truth table50%1/2
A
A= $opts{'q'}
| |||||||||||||||||||||||||||||
| 66 | 7 | ▶ | $self->{statements} = $opts{s} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'s'} || ''
MC/DC100%1/1
$opts{'s'} || ''
$opts{'s'}
Truth table100%2/2
A
A= $opts{'s'}
| |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | $self->{update} = $opts{u} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'u'} || ''
MC/DC100%1/1
$opts{'u'} || ''
$opts{'u'}
Truth table100%2/2
A
A= $opts{'u'}
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | if ($self->{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | printf "+ brief: %s\n", $self->{brief}; | |||||||||||||||||||||||||||
| 71 | 0 | printf "+ debug: %s\n", $self->{debug}; | |||||||||||||||||||||||||||
| 72 | 0 | printf "+ force: %s\n", $self->{force}; | |||||||||||||||||||||||||||
| 73 | 0 | printf "+ noop: %s\n", $self->{noop}; | |||||||||||||||||||||||||||
| 74 | 0 | printf "+ quiet: %s\n", $self->{quiet}; | |||||||||||||||||||||||||||
| 75 | 0 | printf "+ statements: %s\n", $self->{statements}; | |||||||||||||||||||||||||||
| 76 | 0 | printf "+ update: %s\n", $self->{update}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 7 | return @ARGV; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Return a hashref containing the set of deltas we've already applied | ||||||||||||||||||||||||||||
| 83 | sub load_applied_deltas | ||||||||||||||||||||||||||||
| 84 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
load_applied_deltas: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 7 | my $env = $self->environment; | |||||||||||||||||||||||||||
| 88 | 7 | ▶ | $env = "_$env" if $env; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 7 | my $applied_dir = "$Bin/applied$env"; | |||||||||||||||||||||||||||
| 91 | 7 | ▶ | unless (-d $applied_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 2 | warn "No 'applied' directory found - creating\n"; | |||||||||||||||||||||||||||
| 93 | 2 | ▶ | make_path($applied_dir) or die "Directory create failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | return {}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 5 | ▶ | die "Cannot write to directory '$applied_dir' - aborting\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | unless -w $applied_dir; | ||||||||||||||||||||||||||||
| 98 | 5 | $self->{applied_dir} = $applied_dir; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 5 | my $loaded = {}; | |||||||||||||||||||||||||||
| 101 | 5 | ▶ | my $d = IO::Dir->new($applied_dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | or die "Cannot read from applied directory '$applied_dir': $!\n"; | ||||||||||||||||||||||||||||
| 103 | 5 | ▶ | while (defined (my $applied = $d->read)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 12 | ▶ | next if $applied =~ m/^\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | $applied = basename $applied; | |||||||||||||||||||||||||||
| 106 | 2 | $applied =~ s/\.sql$//; | |||||||||||||||||||||||||||
| 107 | 2 | $loaded->{$applied} = 1; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 5 | ▶ | print "+ applied deltas: " . join(',', keys %$loaded) . "\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 5 | return $loaded; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Find an array of outstanding delta filenames | ||||||||||||||||||||||||||||
| 116 | sub find_deltas | ||||||||||||||||||||||||||||
| 117 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
find_deltas: called CC=8 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 7 | my @delta = @_; | |||||||||||||||||||||||||||
| 120 | 3 | ▶ | @delta = <*.sql> unless @delta; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 7 | ▶ | unless (@delta) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | print ("No '*.sql' deltas found - exiting.\n"); | |||||||||||||||||||||||||||
| 124 | 0 | exit 0; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 7 | ▶ | print "+ candidate deltas: " . join(',', @delta) . "\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 7 | my @outstanding = (); | |||||||||||||||||||||||||||
| 129 | 7 | for my $d (@delta) { | |||||||||||||||||||||||||||
| 130 | 11 | my $name = $d; | |||||||||||||||||||||||||||
| 131 | 11 | $name =~ s/\.sql$//; | |||||||||||||||||||||||||||
| 132 | 11 | ▶ | if ($self->{force} || ! exists $self->{applied}->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"force"} or not exists $self->{"applied"}{$name}
MC/DC50%1/2
$self->{"force"} or not exists $self->{"applied"}{$name}
$self->{"force"}
not exists $self->{"applied"}{$name}
Truth table66%2/3
A or not(B)
A= $self->{"force"}B= not exists $self->{"applied"}{$name}
| |||||||||||||||||||||||||||||
| 133 | 9 | push @outstanding, $d; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 7 | ▶ | print "+ oustanding deltas: " . join(',', @outstanding) . "\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 7 | return @outstanding; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Apply the given deltas to the database | ||||||||||||||||||||||||||||
| 142 | sub apply_deltas | ||||||||||||||||||||||||||||
| 143 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
apply_deltas: called CC=26 SCAR=35.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Connect to db | ||||||||||||||||||||||||||||
| 147 | 4 | ▶ | my $dbh = $self->{dbh} = $self->connect | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | or die "Database connect failed\n"; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 4 | my @st = (); | |||||||||||||||||||||||||||
| 151 | 4 | for my $d (@_) { | |||||||||||||||||||||||||||
| 152 | # Load delta | ||||||||||||||||||||||||||||
| 153 | 4 | ▶ | my $fh = IO::File->new($d, 'r') or die("cannot open delta '$d': $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 4 | my $delta; | |||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | 4 | local $/ = undef; | |||||||||||||||||||||||||||
| 157 | 4 | $delta = <$fh>; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Apply miscellaneous cleanups | ||||||||||||||||||||||||||||
| 161 | 4 | $delta =~ s/^--[^\n]*\n/\n/mg; | |||||||||||||||||||||||||||
| 162 | 4 | $delta =~ s/^\s*\n+//; | |||||||||||||||||||||||||||
| 163 | 4 | $delta =~ s/\n\s*\n+/\n/g; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Escape semicolons inside single-quoted strings | ||||||||||||||||||||||||||||
| 166 | 4 | my @bits = split /(?<!\\)'/, $delta; | |||||||||||||||||||||||||||
| 167 | 4 | ▶ | printf "+ delta split into %d bits\n", scalar(@bits) if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 4 | ▶ | for (my $i = 1; $i <= $#bits; $i += 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 12 | ▶ | print "+ checking string '$bits[$i]' for semi-colons\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 12 | $bits[$i] =~ s/(?<!\\);/\\;/g; | |||||||||||||||||||||||||||
| 171 | 12 | ▶ | print "+ munged string: '$bits[$i]'\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 4 | $delta = join("'", @bits); | |||||||||||||||||||||||||||
| 174 | # do {} while $delta =~ s/\G([^']*'[^']*)(?<!\\);([^']*')/$1\\;$2/gsm; | ||||||||||||||||||||||||||||
| 175 | # Split each file into a set of statements on (non-escaped) semicolons | ||||||||||||||||||||||||||||
| 176 | 4 | my @stmt = split /(?<!\\);/, $delta; | |||||||||||||||||||||||||||
| 177 | # Skip everything after the last semicolon | ||||||||||||||||||||||||||||
| 178 | 4 | ▶ | pop @stmt if @stmt > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 4 | ▶ | $self->{stmt}->{$d} = \@stmt if $self->{test_mode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | printf "+ [%s] %d statement(s) found:\n---\n%s\n---\n", | ||||||||||||||||||||||||||||
| 181 | 4 | ▶ | $d, scalar(@stmt), join("\n---", @stmt) if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Execute the statements | ||||||||||||||||||||||||||||
| 184 | 4 | ▶ | for (my $i = 0; $i <= $#stmt; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 16 | ▶ | print "+ executing stmt $i ... " if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | # Unescape semicolons escaped above | ||||||||||||||||||||||||||||
| 187 | 16 | $stmt[$i] =~ s/\\;/;/g; | |||||||||||||||||||||||||||
| 188 | # Trim leading whitespace | ||||||||||||||||||||||||||||
| 189 | 16 | $stmt[$i] =~ s/^\s+//; | |||||||||||||||||||||||||||
| 190 | 16 | my $st = $self->filter_statement( $stmt[$i] ); | |||||||||||||||||||||||||||
| 191 | 16 | ▶ | push @st, $st if $self->{statements}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 16 | ▶ | if ($self->{noop}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 193 | 0 | print "\n\n[NOOP]\n$st\n\n"; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | elsif ($self->{update}) { | ||||||||||||||||||||||||||||
| 196 | 7 | ▶ | $dbh->do($st) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | or $self->_die("[$d] update failed: " . $dbh->errstr . "\ndoing: $st\n"); | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 16 | ▶ | print "+ done\n" if $self->{debug} && ! $self->{noop}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"debug"} and not $self->{"noop"}
MC/DC0%0/2
$self->{"debug"} and not $self->{"noop"}
$self->{"debug"}
not $self->{"noop"}
Truth table33%1/3
A and not(B)
A= $self->{"debug"}B= not $self->{"noop"}
| |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Record that the delta has been applied | ||||||||||||||||||||||||||||
| 203 | 4 | ▶ | if ($self->{update} && ! $self->{noop}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"update"} and not $self->{"noop"}
MC/DC50%1/2
$self->{"update"} and not $self->{"noop"}
$self->{"update"}
not $self->{"noop"}
Truth table66%2/3
A and not(B)
A= $self->{"update"}B= not $self->{"noop"}
| |||||||||||||||||||||||||||||
| 204 | 2 | ▶ | print "+ creating applied entry ... " if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 2 | my $filename = $self->{applied_dir} . '/' . $d; | |||||||||||||||||||||||||||
| 206 | 2 | $filename =~ s/\.sql$//; | |||||||||||||||||||||||||||
| 207 | 2 | ▶ | IO::File->new($filename, 'w') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | or die "Create of applied file '$filename' failed: $!"; | ||||||||||||||||||||||||||||
| 209 | 2 | ▶ | print "done\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 4 | ▶ | print "All done.\n" unless $self->{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 4 | ▶ | return $self->{statements} ? @st : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # Main method | ||||||||||||||||||||||||||||
| 219 | sub run | ||||||||||||||||||||||||||||
| 220 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=13 SCAR=34.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 221 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 222 | 7 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # Parse arguments | ||||||||||||||||||||||||||||
| 225 | 7 | my @args = @_; | |||||||||||||||||||||||||||
| 226 | 7 | ▶ | unless (@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 0 | @args = @ARGV; | |||||||||||||||||||||||||||
| 228 | 0 | @ARGV = (); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 7 | @args = $self->parse_args(@args); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Load applied deltas | ||||||||||||||||||||||||||||
| 233 | 7 | $self->{applied} = $self->load_applied_deltas; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # Find outstanding deltas | ||||||||||||||||||||||||||||
| 236 | 7 | my @outstanding = $self->find_deltas(@args); | |||||||||||||||||||||||||||
| 237 | 7 | ▶ | if (! @outstanding) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | if (@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | print "$_ already applied.\n" for @args; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | else { | ||||||||||||||||||||||||||||
| 242 | 0 | print "No outstanding deltas found.\n"; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | 0 | return 0; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 7 | my @return = @outstanding; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 7 | ▶ | unless ($self->{quiet}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | ▶ | print $self->{update} ? "Applying deltas:\n" : "Outstanding deltas:\n" unless $self->{brief}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 251 | 0 | foreach (@outstanding) { | |||||||||||||||||||||||||||
| 252 | 0 | ▶ | print $self->{brief} ? '' : ' '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | print "$_\n"; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | @return = $self->apply_deltas(@outstanding) | ||||||||||||||||||||||||||||
| 258 | 7 | ▶ | if $self->{update} || $self->{statements}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"update"} or $self->{"statements"}
MC/DC100%2/2
$self->{"update"} or $self->{"statements"}
$self->{"update"}
$self->{"statements"}
Truth table100%3/3
A or B
A= $self->{"update"}B= $self->{"statements"}
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 7 | ▶ | return wantarray ? ( scalar(@return), \@return ) : scalar (@return); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 3 | 1; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | __END__ | ||||||||||||||||||||||||||||