Permalink
Browse files

Implement `begin_work` and `finish_work` in pg.pm.

  • Loading branch information...
1 parent f5c646d commit 5f706665daab10b7f7b3a294cd06eb6cf812bf69 @theory committed Jun 17, 2012
Showing with 34 additions and 0 deletions.
  1. +16 −0 lib/App/Sqitch/Engine/pg.pm
  2. +18 −0 t/pg.t
@@ -229,6 +229,22 @@ sub initialize {
return $self;
}
+sub begin_work {
+ my $self = shift;
+ my $dbh = $self->_dbh;
+
+ # Start transaction and lock steps to allow only one change at a time.
+ $dbh->begin_work;
+ $dbh->do('LOCK TABLE steps IN EXCLUSIVE MODE');
+ return $self;
+}
+
+sub finish_work {
+ my $self = shift;
+ $self->_dbh->commit;
+ return $self;
+}
+
sub run_file {
my ($self, $file) = @_;
$self->_run('--file' => $file);
View
18 t/pg.t
@@ -422,6 +422,24 @@ subtest 'live database' => sub {
'Should find only the second after the first';
is_deeply [$pg->deployed_step_ids_since($step2)], [],
'Should find none after the second';
+
+ ##########################################################################
+ # Test begin_work() and finish_work().
+ can_ok $pg, qw(begin_work finish_work);
+ my $mock_dbh = Test::MockModule->new(ref $pg->_dbh, no_auto => 1);
+ my $txn;
+ $mock_dbh->mock(begin_work => sub { $txn = 1 });
+ $mock_dbh->mock(commit => sub { $txn = 0 });
+ my @do;
+ $mock_dbh->mock(do => sub { shift; @do = @_ });
+ ok $pg->begin_work, 'Begin work';
+ ok $txn, 'Should have started a transaction';
+ is_deeply \@do, [
+ 'LOCK TABLE steps IN EXCLUSIVE MODE',
+ ], 'The steps table should have been locked';
+ ok $pg->finish_work, 'Finish work';
+ ok !$txn, 'Should have committed a transaction';
+ $mock_dbh->unmock_all;
};
done_testing;

0 comments on commit 5f70666

Please sign in to comment.