Subject: | Impossible to reliably set an explicit value for a column with an dynamic_default_on_update trigger |
The manner in which DynamicDefault hooks into the update cycle makes it unsuitable for explicitly setting a value for the on_update column.
At first glance there is no user-side workaround aside from performing two separate update() calls.
See below for a simple reproduction:
Subject: | reproaaaaa.txt |
~$ perl -e ' use Time::HiRes;
{
package SomeResult;
use base "DBIx::Class::Core";
__PACKAGE__->load_components("DynamicDefault");
__PACKAGE__->table("foo");
__PACKAGE__->add_columns(
id => { data_type => "integer", is_auto_increment => 1 },
stamp => { data_type => "float", dynamic_default_on_update => "hires_nao" },
data => { data_type => "varchar", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
sub hires_nao { scalar Time::HiRes::time() }
}
{
package SomeSchema;
use base "DBIx::Class::Schema";
__PACKAGE__->register_class( SomeResult => "SomeResult" );
}
my $s = SomeSchema->connect("DBI:SQLite::memory:");
$s->deploy;
$s->storage->debug(1);
my $obj = $s->resultset("SomeResult")->create({ stamp => 4269 });
# the long-hand way does not work
for (1..10) {
$obj->stamp( 4269 );
$obj->data( scalar Time::HiRes::time() );
$obj->update;
}
# neither does the compact way
for (1..10) {
$obj->update({
stamp => 4269,
data => scalar Time::HiRes::time(),
});
}
'
INSERT INTO foo ( stamp) VALUES ( ? ): '4269'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15448689', '1476879890.15457892', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15585089', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15651989', '1476879890.1566081', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15730906', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15793109', '1476879890.15801907', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15871596', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.15933895', '1476879890.15942597', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16013408', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16075492', '1476879890.1608429', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16153908', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16214895', '1476879890.16229391', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16291809', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16360807', '1476879890.1637249', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16435194', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16505694', '1476879890.16516495', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16579008', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16649008', '1476879890.16659808', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16722107', '4269', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16792798', '1476879890.16803598', '1'
UPDATE foo SET data = ?, stamp = ? WHERE ( id = ? ): '1476879890.16865802', '4269', '1'