X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FJournal.pm;h=7002e5eeb46614159b11584ee91c4fb5954573df;hb=27e45f70c410e38f8a9618058ada708a394708b6;hp=1ce29404426deec087474c1d2ab0f131cf315067;hpb=ec16e73a8dc1429581541a031a3bd95e0f9a3009;p=dbsrgits%2FDBIx-Class-Journal.git diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index 1ce2940..7002e5e 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -5,96 +5,74 @@ use base qw/DBIx::Class/; use strict; use warnings; -our $VERSION = '0.01'; +our $VERSION = '0.900001_04'; +$VERSION = eval $VERSION; # no errors in dev versions ## On create/insert, add new entry to AuditLog -# sub new -# { -# my ($class, $attrs, @rest) = @_; +sub _journal_schema { + my $self = shift; + $self->result_source->schema->_journal_schema; +} -# $class->result_source->schema->_journal_schema->current_user(delete $attrs->{user_id}); +sub insert { + my ($self, @args) = @_; -# $class->next::method($attrs, @rest); -# } + return if $self->in_storage; -sub insert -{ - my ($self) = @_; + my $res = $self->next::method(@args); - return if($self->in_storage); - ## create new transaction here? - my $res = $self->next::method(); - 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 ); - } - $pri ||= \'NULL'; #' - $al->create({ - ID => $pri, -# created => { -# changeset => $self->result_source->schema->_journal_schema->current_changeset(), -# }, - }); - } + $self->journal_log_insert; return $res; } +sub journal_log_insert { + my ($self) = @_; + + if ( $self->in_storage ) { + my $j = $self->_journal_schema; + my $change_id = $j->journal_create_change()->id; + $j->journal_update_or_create_log_entry( $self, create_id => $change_id ); + $j->journal_record_in_history( $self, audit_change_id => $change_id ); + } +} + ## 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(); - } +sub delete { + my $self = shift; + $self->next::method(@_); + $self->journal_log_delete(@_); +} + +sub journal_log_delete { + my ($self) = @_; + + unless ($self->in_storage) { + my $j = $self->_journal_schema; + $j->journal_update_or_create_log_entry( $self, delete_id => $j->journal_create_change->id ); } - } ## On update, copy previous row's contents to AuditHistory -sub update -{ - my ($self, $upd, @rest) = @_; +sub update { + my $self = shift; + $self->journal_log_update(@_); + $self->next::method(@_); +} - if($self->in_storage) - { - my $s_name = $self->result_source->source_name(); - my $ah = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditHistory"); +sub journal_log_update { + my $self = shift; - my $obj = $self->result_source->resultset->find( $self->ident_condition ); - $ah->create({ - $obj->get_columns - }); - } + if($self->in_storage) { + my $j = $self->_journal_schema; - $self->next::method($upd, @rest); + my $change = $j->journal_create_change; + my $prev = $self->result_source->resultset->find( $self->ident_condition ); + $j->journal_record_in_history( $prev, audit_change_id => $change ); + } } =head1 NAME @@ -103,21 +81,21 @@ DBIx::Class::Journal - auditing for tables managed by DBIx::Class =head1 SYNOPSIS - package My::Schema; - use base 'DBIx::Class::Schema'; + package My::Schema; + use base 'DBIx::Class::Schema'; - __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Journal/); + __PACKAGE__->load_components(qw/Schema::Journal/); - __PACKAGE__->journal_connection(['dbi:SQLite:t/var/Audit.db']); - __PACKAGE__->journal_user(['My::Schema::User', {'foreign.userid' => 'self.user_id'}]); + __PACKAGE__->journal_connection(['dbi:SQLite:t/var/Audit.db']); + __PACKAGE__->journal_user(['My::Schema::User', {'foreign.userid' => 'self.user_id'}]); - ######## + ####### - $schema->changeset_user($user->id); - my $new_artist = $schema->txn_do( sub { - return = $schema->resultset('Artist')->create({ name => 'Fred' }); - }); + $schema->changeset_user($user->id); + my $new_artist = $schema->txn_do( sub { + return $schema->resultset('Artist')->create({ name => 'Fred' }); + }); =head1 DESCRIPTION @@ -129,7 +107,7 @@ create/update/delete operation an id. The creation and deletion date of each row is stored, as well as the previous contents of any row that gets changed. -All queries which want auditing should be called using +All queries which need auditing must be called using L, which is used to create changesets for each transaction. @@ -142,7 +120,7 @@ change, use C<< $schema->_journal_schema >>. =head2 TABLES -The journal schema contains a number of tables. +The journal schema contains a number of tables. =over @@ -153,10 +131,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 +151,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. @@ -184,23 +162,22 @@ change are added as a new row in this table. =over -=item journal_connection - -=item Arguments: \@connect_info - -=back +=item journal_connection \@connect_info 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. +information to. -=over +Leaving this blank assumes you want to store the audit data into your current +database. The storage object will be shared by the regular schema and the +journalling schema. -=item journal_sources +=item journal_components @components -=item Arguments: \@source_names +If you want to add components to your journal +(L for example) this would be the -=back + +=item journal_sources \@source_names Set a list of source names you would like to audit, if unset, all sources are used. @@ -208,62 +185,43 @@ 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 +=item journal_storage_type $type 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 +=item journal_user \@rel 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 journal_deploy_on_connect $bool -=item changeset_user +If set to a true value will cause C to be called on +C. -=item Arguments: $user_id +Not reccomended, but present for backwards compatibility. -=back +=item changeset_user $user_id Set the user_id for the following changeset(s). This must be an integer. -=over - -=item changeset_session - -=item Arguments: $user_id - -=back +=item changeset_session $session_id Set the session_id for the following changeset(s). This must be an integer. -=over - -=item txn_do - -=iitem Arguments: $code_ref - -=back +=item txn_do $code_ref, @args Overloaded L, this must be used to start a 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. +Currently nested C calls cause a single ChangeSet object to be created. + +=back + =head1 SEE ALSO L - You'll need it to use this.