{
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;
eval "use DBD::SQLite";
plan $@
? ( skip_all => 'needs DBD::SQLite for testing' )
- : ( tests => 16 );
+ : ( tests => 21 );
}
my $schema = DBICTest->init_schema(no_populate => 1);
});
} );
+
+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 {
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 {
});
} );
-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');
+
+