[Bast-commits] r3276 - in trunk/DBIx-Class-Journal: lib/DBIx/Class lib/DBIx/Class/Schema/Journal/DB t

castaway at dev.catalyst.perl.org castaway at dev.catalyst.perl.org
Thu May 10 14:46:31 GMT 2007


Author: castaway
Date: 2007-05-10 14:46:31 +0100 (Thu, 10 May 2007)
New Revision: 3276

Modified:
   trunk/DBIx-Class-Journal/lib/DBIx/Class/Journal.pm
   trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm
   trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm
   trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm
   trunk/DBIx-Class-Journal/t/01test.t
Log:
Added update & delete support and more tests


Modified: trunk/DBIx-Class-Journal/lib/DBIx/Class/Journal.pm
===================================================================
--- trunk/DBIx-Class-Journal/lib/DBIx/Class/Journal.pm	2007-05-10 00:18:35 UTC (rev 3275)
+++ trunk/DBIx-Class-Journal/lib/DBIx/Class/Journal.pm	2007-05-10 13:46:31 UTC (rev 3276)
@@ -42,8 +42,55 @@
 
 ## 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;

Modified: trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm
===================================================================
--- trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm	2007-05-10 00:18:35 UTC (rev 3275)
+++ trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm	2007-05-10 13:46:31 UTC (rev 3276)
@@ -18,7 +18,7 @@
     $data->{change} = { 
 #        ID => \'DEFAULT',
         changeset_id => $source->schema->current_changeset,
-        %{$data->{created}}, 
+        %{$data->{change}}, 
     };
 
     $self->next::method($data, @rest);

Modified: trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm
===================================================================
--- trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm	2007-05-10 00:18:35 UTC (rev 3275)
+++ trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm	2007-05-10 13:46:31 UTC (rev 3276)
@@ -19,6 +19,8 @@
                                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');
 

Modified: trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm
===================================================================
--- trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm	2007-05-10 00:18:35 UTC (rev 3275)
+++ trunk/DBIx-Class-Journal/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm	2007-05-10 13:46:31 UTC (rev 3276)
@@ -20,7 +20,7 @@
                          set_date => {
                              data_type => 'timestamp',
                              is_nullable => 0,
-                             default_value => 'NOW()',
+                             default_value => 'now()',
                          },
                          session_id => {
                              data_type => 'varchar',

Modified: trunk/DBIx-Class-Journal/t/01test.t
===================================================================
--- trunk/DBIx-Class-Journal/t/01test.t	2007-05-10 00:18:35 UTC (rev 3275)
+++ trunk/DBIx-Class-Journal/t/01test.t	2007-05-10 13:46:31 UTC (rev 3276)
@@ -10,7 +10,7 @@
     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->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 @@
 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');
+




More information about the Bast-commits mailing list