From dcb0c1425c0533e531c9bdaaffcc6f7691b16643 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 24 Jan 2022 11:56:34 -0500 Subject: [PATCH] RunCommandLogs: set a UUID automatically --- src/lib/Hydra/Schema/Result/RunCommandLogs.pm | 21 +++++++++++++++++++ t/Hydra/Schema/Result/RunCommandLogs.t | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/src/lib/Hydra/Schema/Result/RunCommandLogs.pm b/src/lib/Hydra/Schema/Result/RunCommandLogs.pm index 0bc144d5..54ab7a2f 100644 --- a/src/lib/Hydra/Schema/Result/RunCommandLogs.pm +++ b/src/lib/Hydra/Schema/Result/RunCommandLogs.pm @@ -174,9 +174,30 @@ __PACKAGE__->belongs_to( # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZVpYU6k3d/k/nitjpdgf/A use POSIX qw(WEXITSTATUS WIFEXITED WIFSIGNALED WTERMSIG); +use UUID4::Tiny qw(create_uuid_string); use Digest::SHA1 qw(sha1_hex); use Hydra::Model::DB; + +=head2 new + +Initialize a new row object. + +Sets the UUID automatically unless a UUID is specified in the attributes. + +=cut +sub new { + my ($class, $attrs) = @_; + + if (!defined $attrs->{uuid}) { + $attrs->{uuid} = create_uuid_string(); + } + + my $new = $class->next::method($attrs); + + return $new; +} + =head2 started Update the row with the current timestamp as the start time. diff --git a/t/Hydra/Schema/Result/RunCommandLogs.t b/t/Hydra/Schema/Result/RunCommandLogs.t index f93f92b6..80589549 100644 --- a/t/Hydra/Schema/Result/RunCommandLogs.t +++ b/t/Hydra/Schema/Result/RunCommandLogs.t @@ -20,6 +20,12 @@ sub new_run_log { }); } +subtest "A new record has a UUID" => sub { + my $runlog = new_run_log(); + is(length($runlog->uuid), 36, "The UUID attribute is sufficiently UUID-like."); + like($runlog->uuid, qr/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/, "The UUID attribute is sufficiently UUID-like."); +}; + subtest "Not yet started" => sub { my $runlog = new_run_log();