X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FJournal.pm;h=5fc9e69e1dae7f929b2dd9603762d7d0df41acf7;hb=90dae731ce80fb789656927041b8d07bacfae4d5;hp=8c3a57d6d932a98a03f9fe1286614141efa091f7;hpb=fd45c47654aec53a2d8351106f67bc5265441515;p=dbsrgits%2FDBIx-Class-Journal.git diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index 8c3a57d..5fc9e69 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -5,7 +5,7 @@ use base qw/DBIx::Class/; use strict; use warnings; -our $VERSION = '0.02'; +our $VERSION = '0.02_01'; ## On create/insert, add new entry to AuditLog @@ -29,17 +29,9 @@ sub insert { 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 ); - } - $pri ||= \'NULL'; #' - $al->create({ - ID => $pri, -# created => { -# changeset => $self->result_source->schema->_journal_schema->current_changeset(), -# }, + $al->update_or_create({ + ( map { $_ => $self->get_column($_)} $self->primary_columns ), + created => { changeset_id => $al->result_source->schema->current_changeset }, }); } @@ -57,22 +49,12 @@ sub delete { 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('ChangeLog')->create({ changeset_id => $self->result_source->schema->_journal_schema->current_changeset }); - $alentry->delete_id($change->id); - $alentry->update(); - } + my $alentry = $al->find_or_create({ map { $_ => $self->get_column($_)} $self->primary_columns }); + + ## bulk_update doesnt do "create new item on update of rel-accessor with hashref, yet + my $change = $self->result_source->schema->_journal_schema->resultset('ChangeLog')->create({ changeset_id => $self->result_source->schema->_journal_schema->current_changeset }); + $alentry->delete_id($change->id); + $alentry->update(); } } @@ -90,8 +72,9 @@ sub update my $obj = $self->result_source->resultset->find( $self->ident_condition ); $ah->create({ - $obj->get_columns - }); + $obj->get_columns, + change => { changeset_id => $ah->result_source->schema->current_changeset }, + }); } $self->next::method($upd, @rest); @@ -153,10 +136,10 @@ session_id, and a set_date which defaults to the current datetime. A ChangeSet has_many Changes. -=item Change +=item ChangeLog Each change/operation done in the transaction is recorded as a row in -the Change table. It contains an auto-incrementing ID, the +the ChangeLog table. It contains an auto-incrementing ID, the changeset_id and an order column for the ordering of each change in the changeset. @@ -173,7 +156,7 @@ created or deleted this row. For every table in the original database to be audited, an AuditHistory table is created. Each row has a change_id field -containing the ID of the Change row. The other fields correspond to +containing the ID of the ChangeLog row. The other fields correspond to all the fields from the original table. Each time a column value in the original table is changed, the entire row contents before the change are added as a new row in this table. @@ -188,74 +171,50 @@ change are added as a new row in this table. =item Arguments: \@connect_info -=back - Set the connection information for the database to save your audit information to. Leaving this blank assumes you want to store the audit data into your current database. -=over - =item journal_sources =item Arguments: \@source_names -=back - Set a list of source names you would like to audit, if unset, all sources are used. NOTE: Currently only sources with a single-column PK are supported, so use this method if you have sources with multi-column PKs. -=over - =item journal_storage_type =item Arguments: $storage_type -=back - Enter the special storage type of your journal schema if needed. See L for more information on storage types. -=over - =item journal_user =item Arguments: \@relation_args -=back - The user_id column in the L will be linked to your user id with a belongs_to relation, if this is set with the appropriate arguments. -=over - =item changeset_user =item Arguments: $user_id -=back - Set the user_id for the following changeset(s). This must be an integer. -=over - =item changeset_session =item Arguments: $user_id -=back - Set the session_id for the following changeset(s). This must be an integer. -=over - =item txn_do -=iitem Arguments: $code_ref +=item Arguments: $code_ref =back @@ -264,6 +223,8 @@ new changeset to cover a group of changes. Each subsequent change to an audited table will use the changeset_id created in the most recent txn_do call. +=back + =head1 SEE ALSO L - You'll need it to use this.