From: Yuval Kogman Date: Thu, 31 Jul 2008 16:52:32 +0000 (+0000) Subject: log for recreated entries X-Git-Tag: v0.900201~67 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5c9840d813c019331ba1da7c592914f7dadf8778;p=dbsrgits%2FDBIx-Class-Journal.git log for recreated entries --- diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index b95f16a..3187b0c 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -29,10 +29,18 @@ sub insert { my $s_name = $self->result_source->source_name(); my $al = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditLog"); - $al->update_or_create({ - ( map { $_ => $self->get_column($_)} $self->primary_columns ), - created => { changeset_id => $al->result_source->schema->current_changeset }, - }); + + my %id = map { $_ => $self->get_column($_)} $self->primary_columns; + + my $change = { changeset_id => $al->result_source->schema->current_changeset }; + + if ( my $log = $al->find(\%id) ) { + #$log->created($change); # FIXME should this work? + $log->created($al->related_resultset("created")->create($change)); + $log->update; + } else { + $al->create({ %id, created => $change }); + } } return $res; diff --git a/t/01test.t b/t/01test.t index 57da686..33301a5 100644 --- a/t/01test.t +++ b/t/01test.t @@ -10,7 +10,7 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 16 ); + : ( tests => 21 ); } my $schema = DBICTest->init_schema(no_populate => 1); @@ -64,12 +64,35 @@ $schema->txn_do( sub { }); } ); + +my %id = map { $_ => $new_cd->get_column($_) } $new_cd->primary_columns; + $schema->txn_do( sub { $new_cd->delete; -} ); +}); + +{ + my $alentry = $search->find(\%id); + ok($alentry, "got log entry"); + ok(defined($alentry->deleted), 'Deleted set in audit_log'); + cmp_ok( $alentry->deleted->id, ">", $alentry->created->id, "deleted is after created" ); +} + +$new_cd = $schema->txn_do( sub { + $schema->resultset('CD')->create({ + %id, + title => 'lalala', + artist => $artist, + year => 2000, + }); +}); -my $alentry = $search->find({ map { $_ => $new_cd->get_column($_) } $new_cd->primary_columns }); -ok(defined($alentry->deleted), 'Deleted set in audit_log'); +{ + my $alentry = $search->find(\%id); + ok($alentry, "got log entry"); + ok(defined($alentry->deleted), 'Deleted set in audit_log'); + cmp_ok( $alentry->deleted->id, "<", $alentry->created->id, "deleted is before created (recreated)" ); +} $schema->changeset_user(1); $schema->txn_do( sub { @@ -78,12 +101,12 @@ $schema->txn_do( sub { artist => $artist, year => 1999, }); -} ); +}); ok($search->count > 1, 'Created an second entry in the CD audit history'); -my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(5); -is($cset->user_id, 1, 'Set user id for 5th changeset'); +my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(6); +is($cset->user_id, 1, 'Set user id for 6th changeset'); $schema->changeset_session(1); $schema->txn_do( sub { @@ -94,6 +117,8 @@ $schema->txn_do( sub { }); } ); -my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(6); -is($cset2->session_id, 1, 'Set session id for 6th changeset'); +my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(7); +is($cset2->session_id, 1, 'Set session id for 7th changeset'); + +