Subject: | exception_action behavior distinction in create vs populate |
Used to set exception_action to catch mysql 'retry transaction' exceptions. This is working with $rs->create, but not $rs->populate. See test in attach
Subject: | populate_test.pl |
#!/usr/bin/perl
use Test::Most;
{
package MyX;
use Exception::Class (
'MyX', {},
'MyX::ConstraintFail', {
isa => 'MyX'
}
);
}
{
package MySchema::MyResult;
use base 'DBIx::Class';
__PACKAGE__->load_components( qw/
Core
/ );
__PACKAGE__->table( 'foo' );
__PACKAGE__->add_columns(
id => {
data_type => 'int',
is_nullable => 0,
},
);
__PACKAGE__->set_primary_key(qw/ id /);
}
{
package MySchema;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_classes( 'MyResult' );
__PACKAGE__->exception_action( sub {
my $e = shift;
if ( ref( $e ) ) {
die $e;
}
if ( $DBI::err && $DBI::err == 19 ) {
MyX::ConstraintFail->throw( "Abort due to constraint violation" );
}
MyX->throw( "Something went wrong with DB: $e" );
} );
}
my $schema = MySchema->connect( 'dbi:SQLite:dbname=:memory:' );
$schema->deploy();
my $rs = $schema->resultset('MyResult');
throws_ok{
$rs->create({ id => 1});
$rs->create({ id => 1});
} 'MyX::ConstraintFail';
$rs->delete;
throws_ok{
$rs->populate([{id => 1},{id => 1}]);
} 'MyX::ConstraintFail';
done_testing;