| 1 | package Liquibase::Git; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use feature 'say'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Temp qw/tempdir/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use IPC::Cmd qw/run/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ABSTRACT: API and CLI to apply sql scripts from a git repo using Liquibase | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | has username => ( | |||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | required => 1, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | has password => ( | |||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | required => 1, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | has db => ( | |||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | required => 1, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | has hostname => ( | |||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||
| 38 | required => 1, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # something like mydb1 | ||||||||||||||||||||||||||||
| 42 | 1 | has git_changeset_dir => ( | |||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||
| 44 | required => 1, | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | has git_repo => ( | |||||||||||||||||||||||||||
| 48 | is => 'ro', # eg. ssh://git@github.com/foo/myapp.git | ||||||||||||||||||||||||||||
| 49 | required => 1, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | has git_identifier => ( | |||||||||||||||||||||||||||
| 53 | is => 'ro', # something like 'master', 'branchname' or a commit hash | ||||||||||||||||||||||||||||
| 54 | required => 1, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | has temp_dir => ( | ||||||||||||||||||||||||||||
| 58 | # create a temp directory | ||||||||||||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | default => sub { tempdir(CLEANUP => 1); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 1 | ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | has db_type => ( | ||||||||||||||||||||||||||||
| 64 | is => 'ro', | ||||||||||||||||||||||||||||
| 65 | required => 1, | ||||||||||||||||||||||||||||
| 66 | 1 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | die 'Only types available are postgresql and mysql' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] eq "postgresql" or $_[0] eq "mysql"
MC/DC0%0/2
$_[0] eq "postgresql" or $_[0] eq "mysql"
$_[0] eq "postgresql"
$_[0] eq "mysql"
Truth table33%1/3
A or B
A= $_[0] eq "postgresql"B= $_[0] eq "mysql"
| |||||||||||||||||||||||||||||
| 68 | unless $_[0] eq 'postgresql' || $_[0] eq 'mysql'; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 1 | ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # filename relative to the changeset_dir | ||||||||||||||||||||||||||||
| 73 | 1 | has git_changeset_file => ( | |||||||||||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||||||||||
| 75 | required => 1, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | my %default_db_drivers = ( | |||||||||||||||||||||||||||
| 79 | postgresql => { | ||||||||||||||||||||||||||||
| 80 | classpath => '/usr/share/java/postgresql-jdbc.jar', | ||||||||||||||||||||||||||||
| 81 | port => '5432', | ||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||
| 83 | mysql => { | ||||||||||||||||||||||||||||
| 84 | classpath => '/usr/share/java/mysql-connector-java.jar', | ||||||||||||||||||||||||||||
| 85 | port => '3306', | ||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | has classpath => ( | ||||||||||||||||||||||||||||
| 91 | is => 'lazy', | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | return $default_db_drivers{$self->db_type}->{classpath}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 1 | ); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | has port => ( | ||||||||||||||||||||||||||||
| 100 | is => 'lazy', | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return $default_db_drivers{$self->db_type}->{port}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 1 | ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub liquibase_command_stem { | ||||||||||||||||||||||||||
|
Subroutine
liquibase_command_stem: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $command = "CLASSPATH=".$self->classpath. | |||||||||||||||||||||||||||
| 113 | ' /usr/bin/liquibase --changeLogFile='.$self->git_changeset_dir.'/'.$self->git_changeset_file.' '. | ||||||||||||||||||||||||||||
| 114 | '--url="jdbc:'.$self->db_type. | ||||||||||||||||||||||||||||
| 115 | '://'.$self->hostname.':'.$self->port.'/'. | ||||||||||||||||||||||||||||
| 116 | $self->db.'" --username='.$self->username.' --password='.$self->password; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return $command; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # return the buffer (stderr+stdout) only, and die if things go badly | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub run_command { | ||||||||||||||||||||||||||
|
Subroutine
run_command: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 126 | cmd => undef, | ||||||||||||||||||||||||||||
| 127 | @_ | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $buffer; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = run (command => $args{cmd}, buffer => \$buffer); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | STDERR->autoflush(1); | |||||||||||||||||||||||||||
| 135 | 0 | STDOUT->autoflush(1); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $censored_cmd = $args{cmd}; | |||||||||||||||||||||||||||
| 138 | 0 | $censored_cmd =~ s/--password=(\w*)/--password=__CENSORED__/; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | say '================================================='; | |||||||||||||||||||||||||||
| 141 | 0 | say 'COMMAND: '.$censored_cmd; | |||||||||||||||||||||||||||
| 142 | 0 | say '================================================='; | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | say $buffer if $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | unless ($success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | say '================================================='; | |||||||||||||||||||||||||||
| 146 | 0 | say 'FAILED'; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | unless ($success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | say STDERR "ERROR CODE: $error_code"; | |||||||||||||||||||||||||||
| 150 | 0 | say STDERR "ERROR BUF: ".Dumper($stderr_buf); | |||||||||||||||||||||||||||
| 151 | 0 | exit 1; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | return $buffer; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub retrieve_changeset_from_git { | ||||||||||||||||||||||||||
|
Subroutine
retrieve_changeset_from_git: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $cmd_git_clone = "git clone ".$self->git_repo." ".$self->temp_dir; | |||||||||||||||||||||||||||
| 163 | 0 | run_command(cmd => $cmd_git_clone); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | chdir $self->temp_dir; | |||||||||||||||||||||||||||
| 166 | 0 | my $cmd_git_checkout = "git checkout ".$self->git_identifier; | |||||||||||||||||||||||||||
| 167 | 0 | run_command(cmd => $cmd_git_checkout); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my $git_commit_id = run_command(cmd => 'git log --format="%H" -n 1'); | |||||||||||||||||||||||||||
| 170 | 0 | chomp $git_commit_id; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $dt_now = DateTime->now; | |||||||||||||||||||||||||||
| 173 | 0 | my $description = $dt_now.'.'.$self->git_identifier.'.'.$$.".".$git_commit_id; | |||||||||||||||||||||||||||
| 174 | 0 | run_command(cmd => "echo PATCH_RUN_SIGNATURE: ${description}"); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub dryrun { | ||||||||||||||||||||||||||
|
Subroutine
dryrun: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | run_command(cmd => $self->liquibase_command_stem." updateSQL"); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub wetrun { | ||||||||||||||||||||||||||
|
Subroutine
wetrun: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | run_command(cmd => $self->liquibase_command_stem." update"); | |||||||||||||||||||||||||||
| 187 | 0 | run_command(cmd => 'echo PASSED'); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | $self->retrieve_changeset_from_git; | |||||||||||||||||||||||||||
| 194 | 0 | chdir $self->temp_dir; | |||||||||||||||||||||||||||
| 195 | 0 | $self->dryrun; | |||||||||||||||||||||||||||
| 196 | 0 | $self->wetrun; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub updateSQL { | ||||||||||||||||||||||||||
|
Subroutine
updateSQL: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | $self->retrieve_changeset_from_git; | |||||||||||||||||||||||||||
| 203 | 0 | chdir $self->temp_dir; | |||||||||||||||||||||||||||
| 204 | 0 | $self->dryrun; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | 1; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||