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