Liquibase-Git

 view release on metacpan or  search on metacpan

lib/Liquibase/Git.pm  view on Meta::CPAN

  my $buffer;

  my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = run (command => $args{cmd}, buffer => \$buffer);

  STDERR->autoflush(1);
  STDOUT->autoflush(1);

  my $censored_cmd = $args{cmd};
  $censored_cmd =~ s/--password=(\w*)/--password=__CENSORED__/;

  say '=================================================';
  say 'COMMAND: '.$censored_cmd;
  say '=================================================';
  say $buffer if $buffer;
  unless ($success) {
    say '=================================================';
    say 'FAILED';
  }
  unless ($success) {
    say STDERR "ERROR CODE: $error_code";
    say STDERR "ERROR BUF: ".Dumper($stderr_buf);
    exit 1;
  }

  return $buffer;
}


sub retrieve_changeset_from_git {
  my $self = shift;


  my $cmd_git_clone = "git clone ".$self->git_repo." ".$self->temp_dir;
  run_command(cmd => $cmd_git_clone);

  chdir $self->temp_dir;
  my $cmd_git_checkout = "git checkout ".$self->git_identifier;
  run_command(cmd => $cmd_git_checkout);

  my $git_commit_id = run_command(cmd => 'git log --format="%H" -n 1');
  chomp $git_commit_id;

  my $dt_now = DateTime->now;
  my $description = $dt_now.'.'.$self->git_identifier.'.'.$$.".".$git_commit_id;
  run_command(cmd => "echo PATCH_RUN_SIGNATURE: ${description}");
}

sub dryrun {
  my $self = shift;

  run_command(cmd => $self->liquibase_command_stem." updateSQL");
}

sub wetrun {
  my $self = shift;

  run_command(cmd => $self->liquibase_command_stem." update");
  run_command(cmd => 'echo PASSED');
}

sub update {
  my $self = shift;

  $self->retrieve_changeset_from_git;
  chdir $self->temp_dir;
  $self->dryrun;
  $self->wetrun;
}

sub updateSQL {
  my $self = shift;

  $self->retrieve_changeset_from_git;
  chdir $self->temp_dir;
  $self->dryrun;
}


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Liquibase::Git - API and CLI to apply sql scripts from a git repo using Liquibase

=head1 VERSION

version 0.0.1

=head1 SYNOPSIS

  use Liquibase::Git;

  my $liquibase = Liquibase::Git->new(
   %params
  );

  $liquibase->apply;

=head1 DESCRIPTION

Install this module on a server with Liquibase installed

Assume you have an app with:

=over

=item * git repo https://github.com/foo/myapp.git containing a liquibase changeset

=item * a database mydb-db1

=item * a database host db1.myapp.com

=back

The following code

  my $liquibase = Liquibase::Git->new(
     username          => 'liquibase',
     password          => 'foobar',
     db                => 'mydb-db1',
     hostname          => 'db1.myapp.com'
     git_repo          => 'https://github.com/foo/myapp.git',
     git_changeset_dir => 'db/db1',
     git_identifier    => 'master',



( run in 0.697 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )