## On delete, update delete_id of AuditLog
+sub delete
+{
+ my ($self, @rest) = @_;
+ $self->next::method(@rest);
+
+ if(!$self->in_storage)
+ {
+ my $s_name = $self->result_source->source_name();
+ my $al = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditLog");
+ my ($pri, $too_many) = map { $self->get_column($_)} $self->primary_columns;
+ if(defined $pri && defined $too_many)
+ {
+ $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self );
+ }
+
+ if($pri)
+ {
+ my $alentry = $al->find({ID => $pri});
+ $self->throw_exception( "No audit_log entry found for ".ref($self) . " item $pri" ) if(!$alentry);
+
+ ## bulk_update doesnt do "create new item on update of rel-accessor with hashref, yet
+ my $change = $self->result_source->schema->_journal_schema->resultset('Change')->create({ changeset_id => $self->result_source->schema->_journal_schema->current_changeset });
+ $alentry->delete_id($change->ID);
+ $alentry->update();
+ }
+ }
+
+}
+
## On update, copy previous row's contents to AuditHistory
+sub update
+{
+ my ($self, $upd, @rest) = @_;
+
+ if($self->in_storage)
+ {
+ my $s_name = $self->result_source->source_name();
+ my $ah = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditHistory");
+
+ my $obj = $self->result_source->resultset->find( $self->ident_condition );
+ $ah->create({
+ $obj->get_columns
+ });
+ }
+
+ $self->next::method($upd, @rest);
+}
+
1;
$data->{change} = {
# ID => \'DEFAULT',
changeset_id => $source->schema->current_changeset,
- %{$data->{created}},
+ %{$data->{change}},
};
$self->next::method($data, @rest);
is_nullable => 1,
is_foreign_key => 1,
});
+__PACKAGE__->set_primary_key('ID');
+
__PACKAGE__->belongs_to('created', 'DBIx::Class::Schema::Journal::DB::Change', 'create_id');
__PACKAGE__->belongs_to('deleted', 'DBIx::Class::Schema::Journal::DB::Change', 'delete_id');
set_date => {
data_type => 'timestamp',
is_nullable => 0,
- default_value => 'NOW()',
+ default_value => 'now()',
},
session_id => {
data_type => 'varchar',
eval "use DBD::SQLite";
plan $@
? ( skip_all => 'needs DBD::SQLite for testing' )
- : ( tests => 6 );
+ : ( tests => 9 );
}
my $schema = DBICTest->init_schema(no_populate => 1);
isa_ok($schema->_journal_schema->source('CDAuditHistory'), 'DBIx::Class::ResultSource', 'CDAuditHistory source exists');
isa_ok($schema->_journal_schema->source('ArtistAuditLog'), 'DBIx::Class::ResultSource', 'ArtistAuditLog source exists');
+my $artist;
my $new_cd = $schema->txn_do( sub {
- my $artist = $schema->resultset('Artist')->create({
+ $artist = $schema->resultset('Artist')->create({
name => 'Fred Bloggs',
});
return $schema->resultset('CD')->create({
my $search = $schema->_journal_schema->resultset('CDAuditLog')->search();
ok($search->count, 'Created an entry in the CD audit log');
+$schema->txn_do( sub {
+ $new_cd->year(2003);
+ $new_cd->update;
+} );
+
+is($new_cd->year, 2003, 'Changed year to 2003');
+my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search();
+ok($cdah->count, 'Created an entry in the CD audit history');
+
+$schema->txn_do( sub {
+ $schema->resultset('CD')->create({
+ title => 'Something',
+ artist => $artist,
+ year => 1999,
+ });
+} );
+
+$schema->txn_do( sub {
+ $new_cd->delete;
+} );
+
+my $alentry = $search->find({ ID => $new_cd->get_column($new_cd->primary_columns) });
+ok(defined($alentry->deleted), 'Deleted set in audit_log');