| 1 | package DBIx::SchemaChecksum::App::ApplyChanges; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ABSTRACT: Apply changes based on current checksum | ||||||||||||||
| 4 | 4 | our $VERSION = '1.106'; # VERSION | |||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use MooseX::App::Command; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | extends qw(DBIx::SchemaChecksum::App); | |||||||||||||
| 10 | 4 | ▶ | use IO::Prompt::Tiny qw(prompt); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | option '+sqlsnippetdir' => ( required => 1); | |||||||||||||
| 14 | 4 | option 'dry_run' => ( is => 'rw', isa => 'Bool', default => 0, documentation=>'Only list changes, do not apply' ); | |||||||||||||
| 15 | 4 | option 'check' => ( is => 'rw', isa => 'Bool', default => 0, documentation=>'Check whether the database is up to date without applying changes' ); | |||||||||||||
| 16 | 4 | has 'no_prompt' => ( is => 'rw', isa => 'Bool', default => 0, documentation=>'Do not prompt, just use defaults'); | |||||||||||||
| 17 | |||||||||||||||
| 18 | 8 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=8 SCAR=20.8
| |||||||||||||||
| 19 | 8 | my $self = shift; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 8 | my $checksum = $self->checksum; | |||||||||||||
| 22 | 8 | ▶ | if ($self->check) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 6 | ▶ | my $path = $self->_update_path || {}; | ||||||||||||
|
Condition100%2/2
$self->_update_path || {}
MC/DC100%1/1
$self->_update_path || {}
$self->_update_path
Truth table100%2/2
A
A= $self->_update_path
| |||||||||||||||
| 24 | 6 | ▶ | if (exists $path->{$checksum}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 3 | say 'unapplied changes found for current checksum'; | |||||||||||||
| 26 | 3 | exit 1; | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | 3 | for my $entry (values %$path) { | |||||||||||||
| 29 | 4 | ▶ | my $start = $entry->[0] eq 'SAME_CHECKSUM' ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 4 | ▶ | for (my $i = $start + 1; $i < @$entry; $i += 2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 5 | ▶ | if ($entry->[$i] eq $checksum) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 1 | say 'current checksum matches last changes'; | |||||||||||||
| 33 | 1 | return; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | } | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | 2 | say 'no changes match current checksum'; | |||||||||||||
| 38 | 2 | exit 1; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 2 | $self->apply_sql_snippets($checksum); | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | |||||||||||||||
| 45 | 8 | ▶ | sub apply_sql_snippets { | ||||||||||||
|
Subroutine
apply_sql_snippets: called CC=9 SCAR=33.5
| |||||||||||||||
| 46 | 8 | my ($self, $this_checksum ) = @_; | |||||||||||||
| 47 | 8 | my $update_path = $self->_update_path; | |||||||||||||
| 48 | |||||||||||||||
| 49 | my $update = $update_path->{$this_checksum} | ||||||||||||||
| 50 | 8 | ▶ | if ( exists $update_path->{$this_checksum} ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 8 | ▶ | unless ($update) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 0 | foreach my $update_entry (values %{$update_path}) { | |||||||||||||
| 54 | 0 | my $post_checksum_index = 0; | |||||||||||||
| 55 | 0 | ▶ | while (@{$update_entry} > $post_checksum_index) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | ▶ | if ($update_entry->[$post_checksum_index] eq 'SAME_CHECKSUM') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | $post_checksum_index++; | |||||||||||||
| 58 | 0 | next; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | 0 | ▶ | if ($update_entry->[$post_checksum_index+1] eq $this_checksum) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 0 | say "db checksum $this_checksum matching ".$update_entry->[$post_checksum_index]->relative; | |||||||||||||
| 62 | 0 | return; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | 0 | $post_checksum_index += 2; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | } | ||||||||||||||
| 67 | 0 | say "No update found that's based on $this_checksum."; | |||||||||||||
| 68 | 0 | return; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 8 | ▶ | if ( $update->[0] eq 'SAME_CHECKSUM' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 2 | ▶ | return unless $update->[1]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 2 | my ( $file, $expected_post_checksum ) = splice( @$update, 1, 2 ); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 2 | $self->apply_file( $file, $expected_post_checksum ); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | else { | ||||||||||||||
| 78 | 6 | $self->apply_file( @$update ); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 8 | ▶ | sub apply_file { | ||||||||||||
|
Subroutine
apply_file: called CC=15 SCAR=32.7
| |||||||||||||||
| 83 | 8 | my ( $self, $file, $expected_post_checksum ) = @_; | |||||||||||||
| 84 | 8 | my $filename = $file->relative($self->sqlsnippetdir); | |||||||||||||
| 85 | |||||||||||||||
| 86 | 8 | ▶ | my $no_checksum_change = $self->checksum eq $expected_post_checksum ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | |||||||||||||||
| 88 | 8 | my $answer; | |||||||||||||
| 89 | 8 | ▶ | if ($no_checksum_change) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 1 | $answer = prompt("Apply $filename (won't change checksum)? [y/n/s]",'y'); | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | else { | ||||||||||||||
| 93 | 7 | $answer = prompt("Apply $filename? [y/n]",'y'); | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 8 | ▶ | if ($answer eq 'y') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 97 | 8 | ▶ | say "Starting to apply $filename" if $self->verbose; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | 8 | my $content = $file->slurp; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 8 | my $dbh = $self->dbh; | |||||||||||||
| 102 | 8 | $dbh->begin_work; | |||||||||||||
| 103 | |||||||||||||||
| 104 | 8 | my $split_regex = qr/(?!:[\\]);/; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 8 | ▶ | if ($content =~ m/--\s*split-at:\s*(\S+)\n/s) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 0 | say "Splitting $filename commands at >$1<"; | |||||||||||||
| 108 | 0 | $split_regex = qr/$1/; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 8 | $content =~ s/^\s*--.+$//gm; | |||||||||||||
| 112 | 8 | foreach my $command ( split( $split_regex , $content ) ) { | |||||||||||||
| 113 | 15 | $command =~ s/\A\s+//; | |||||||||||||
| 114 | 15 | $command =~ s/\s+\Z//; | |||||||||||||
| 115 | |||||||||||||||
| 116 | 15 | ▶ | next unless $command; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 7 | ▶ | say "Executing SQL statement: $command" if $self->verbose; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 7 | ▶ | my $success = try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 119 | 7 | $dbh->do($command); | |||||||||||||
| 120 | 7 | return 1; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 123 | 0 | $dbh->rollback; | |||||||||||||
| 124 | 0 | ▶ | say "SQL error: $_" unless $dbh->{PrintError}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 125 | 0 | say "ABORTING!"; | |||||||||||||
| 126 | 0 | return undef; | |||||||||||||
| 127 | 7 | }; | |||||||||||||
| 128 | 7 | ▶ | return unless $success; # abort all further changes | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 7 | ▶ | say "Successful!" if $self->verbose; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 8 | ▶ | if ( $self->dry_run ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 0 | $dbh->rollback; | |||||||||||||
| 134 | 0 | say "dry run, so checksums cannot match. We proceed anyway..."; | |||||||||||||
| 135 | 0 | return $self->apply_sql_snippets($expected_post_checksum); | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | # new checksum | ||||||||||||||
| 139 | 8 | $self->reset_checksum; | |||||||||||||
| 140 | 8 | my $post_checksum = $self->checksum; | |||||||||||||
| 141 | |||||||||||||||
| 142 | 8 | ▶ | if ( $post_checksum eq $expected_post_checksum ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 8 | say "post checksum OK"; | |||||||||||||
| 144 | 8 | $dbh->commit; | |||||||||||||
| 145 | 8 | ▶ | if ($self->_update_path->{$post_checksum}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 5 | return $self->apply_sql_snippets($post_checksum); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | else { | ||||||||||||||
| 149 | 3 | say 'No more changes'; | |||||||||||||
| 150 | 3 | return; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | } | ||||||||||||||
| 153 | else { | ||||||||||||||
| 154 | 0 | say "post checksum mismatch!"; | |||||||||||||
| 155 | 0 | say " expected $expected_post_checksum"; | |||||||||||||
| 156 | 0 | say " got $post_checksum"; | |||||||||||||
| 157 | 0 | $dbh->rollback; | |||||||||||||
| 158 | 0 | say "ABORTING!"; | |||||||||||||
| 159 | 0 | return; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | } | ||||||||||||||
| 162 | elsif ($answer eq 's') { | ||||||||||||||
| 163 | 0 | return $self->apply_sql_snippets($expected_post_checksum); | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | else { | ||||||||||||||
| 166 | 0 | say "Not applying $filename, so we stop."; | |||||||||||||
| 167 | 0 | return; | |||||||||||||
| 168 | } | ||||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | 4 | __PACKAGE__->meta->make_immutable(); | |||||||||||||
| 172 | 4 | 1; | |||||||||||||
| 173 | |||||||||||||||
| 174 | __END__ | ||||||||||||||