X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F71mysql.t;h=2992f95c7d3894c610b64d0dcc3a9af323e4a514;hb=89428546a2960837d82e2d5e6935acdc0eb2f902;hp=66ebfd231152b2dd3a52afa9743c53904a0811f9;hpb=3f5b99fea8b445c7a61057f4210a715ffb8e677e;p=dbsrgits%2FDBIx-Class.git diff --git a/t/71mysql.t b/t/71mysql.t index 66ebfd2..2992f95 100644 --- a/t/71mysql.t +++ b/t/71mysql.t @@ -3,9 +3,12 @@ use warnings; use Test::More; use Test::Exception; + +use DBI::Const::GetInfoType; +use Scalar::Util qw/weaken/; + use lib qw(t/lib); use DBICTest; -use DBI::Const::GetInfoType; use DBIC::SqlMakerTest; my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; @@ -85,6 +88,16 @@ lives_ok { }); } 'Limited FOR UPDATE select works'; +# shared-lock +lives_ok { + $schema->txn_do (sub { + isa_ok ( + $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}), + 'DBICTest::Schema::Artist', + ); + }); +} 'LOCK IN SHARE MODE select works'; + my $test_type_info = { 'artistid' => { 'data_type' => 'INT', @@ -334,13 +347,124 @@ ZEROINSEARCH: { ); } -## If find() is the first query after connect() -## DBI::Storage::sql_maker() will be called before -## _determine_driver() and so the ::SQLHacks class for MySQL -## will not be used +# make sure find hooks determine driver +{ + my $schema = DBICTest::Schema->connect($dsn, $user, $pass); + $schema->resultset("Artist")->find(4); + isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::MySQL'); +} + +# make sure the mysql_auto_reconnect buggery is avoided +{ + local $ENV{MOD_PERL} = 'boogiewoogie'; + my $schema = DBICTest::Schema->connect($dsn, $user, $pass); + ok (! $schema->storage->_get_dbh->{mysql_auto_reconnect}, 'mysql_auto_reconnect unset regardless of ENV' ); + + # Make sure hardcore forking action still works even if mysql_auto_reconnect + # is true (test inspired by ether) + + my $schema_autorecon = DBICTest::Schema->connect($dsn, $user, $pass, { mysql_auto_reconnect => 1 }); + my $orig_dbh = $schema_autorecon->storage->_get_dbh; + weaken $orig_dbh; + + ok ($orig_dbh, 'Got weak $dbh ref'); + ok ($orig_dbh->{mysql_auto_reconnect}, 'mysql_auto_reconnect is properly set if explicitly requested' ); + + my $rs = $schema_autorecon->resultset('Artist'); + + my $pid = fork(); + if (! defined $pid ) { + die "fork() failed: $!" + } + elsif ($pid) { + # sanity check + $schema_autorecon->storage->dbh_do(sub { + is ($_[1], $orig_dbh, 'Storage holds correct $dbh in parent'); + }); + + # kill our $dbh + $schema_autorecon->storage->_dbh(undef); + ok (! defined $orig_dbh, 'Parent $dbh handle is gone'); + } + else { + # wait for parent to kill its $dbh + sleep 1; + + #simulate a subtest to not confuse the parent TAP emission + Test::More->builder->reset; + Test::More->builder->plan('no_plan'); + Test::More->builder->_indent(' ' x 4); + + ok ($orig_dbh, 'Now dead $dbh is still there for the child'); + + # try to do something dbic-esque + $rs->create({ name => "Hardcore Forker $$" }); + + ok (! defined $orig_dbh, 'DBIC operation triggered reconnect - old $dbh is gone'); + + exit 0; + } + + wait; + ok(!$?, 'Child subtests passed'); + + ok ($rs->find({ name => "Hardcore Forker $pid" }), 'Expected row created'); +} + +# Verify that populate in void context uses INSERT INTO foo VALUES (), (), () +# instead of 3 distinct SQL statements as other databases may require. +{ + my $rsrc = $schema->resultset('Artist')->result_source; + my ($rv, $sth, @bind) = $schema->storage->insert_bulk( + $rsrc, + [qw/ artistid name rank charfield /], + [ + [ 100, 'John', 200, 'Smith' ], + [ 101, 'Joan', 201, 'Snith' ], + ], + ); + + is_same_sql_bind( + $sth->{Statement}, + \@bind, + q{INSERT INTO artist ( artistid, name, rank, charfield ) VALUES ( ?, ?, ?, ? ), ( ?, ?, ?, ? )}, + [ + map { [ dummy => $_ ] } ( + 100, 'John', 200, 'Smith', + 101, 'Joan', 201, 'Snith', + ), + ], + ); + + is( $schema->resultset('Artist')->find( 100 )->name, 'John' ); +} + +# Verify that 600 rows ends up with the return SQL having only 40 entries +# because they were inserted 80 rows at a time other than the last group +# of 40 rows. But, all 600 rows are inserted. +{ + my $rsrc = $schema->resultset('Artist')->result_source; + my ($rv, $sth, @bind) = $schema->storage->insert_bulk( + $rsrc, + [qw/ artistid name rank charfield /], + [ + (map { [ 1000 + $_, "X $_", $_ + 1000, "Y $_" ] } 1 .. 600), + ], + ); -my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass); -$schema2->resultset("Artist")->find(4); -isa_ok($schema2->storage->sql_maker, 'DBIx::Class::SQLMaker::MySQL'); + is_same_sql_bind( + $sth->{Statement}, + \@bind, + q{INSERT INTO artist ( artistid, name, rank, charfield ) VALUES } . join(', ', ('( ?, ?, ?, ? )') x 40 ), + [ + map { [ dummy => $_ ] } ( + map { 1000 + $_, "X $_", $_ + 1000, "Y $_" } 561 .. 600 + ), + ], + ); + + is( $schema->resultset('Artist')->find( 1001 )->name, 'X 1' ); + cmp_ok( $schema->resultset('Artist')->search({ artistid => { '>' => 1000 }})->count, '==', 600 ); +} done_testing;