From: Jess Robinson Date: Thu, 10 May 2007 13:46:31 +0000 (+0000) Subject: Added update & delete support and more tests X-Git-Tag: v0.900201~103 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1e996809d704dca1a0998228cb2710bea1edbe7c;p=dbsrgits%2FDBIx-Class-Journal.git Added update & delete support and more tests --- diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index 623cfe0..4d362a9 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -42,8 +42,55 @@ sub insert ## 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; diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm b/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm index f1d65c6..1be108f 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm @@ -18,7 +18,7 @@ sub new $data->{change} = { # ID => \'DEFAULT', changeset_id => $source->schema->current_changeset, - %{$data->{created}}, + %{$data->{change}}, }; $self->next::method($data, @rest); diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm b/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm index 879c47d..b55a35a 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm @@ -19,6 +19,8 @@ __PACKAGE__->add_columns( 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'); diff --git a/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm index a5c3928..2f59b41 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm @@ -20,7 +20,7 @@ __PACKAGE__->add_columns( set_date => { data_type => 'timestamp', is_nullable => 0, - default_value => 'NOW()', + default_value => 'now()', }, session_id => { data_type => 'varchar', diff --git a/t/01test.t b/t/01test.t index 3ab112b..c8d997e 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 => 6 ); + : ( tests => 9 ); } my $schema = DBICTest->init_schema(no_populate => 1); @@ -20,8 +20,9 @@ isa_ok($schema->_journal_schema, 'DBIx::Class::Schema::Journal::DB', 'Actually h 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({ @@ -35,4 +36,27 @@ isa_ok($new_cd, 'DBIx::Class::Journal', 'Created CD object'); 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');