DBIx-Class-PgLog

 view release on metacpan or  search on metacpan

lib/DBIx/Class/PgLog.pm  view on Meta::CPAN

    use base qw/DBIx::Class::Core/;

    __PACKAGE__->load_components(qw/PgLog/);

If you want to use methods created by L<DBIx::Class::Relationship::Base>, like "add_to_$rel" or "set_$rel",
if you are planing to use L<DBIx::Class::ResultSet/delete> or L<DBIx::Class::ResultSet/update> or if you use
modules which make use of these methods (like L<HTML::FormHandler> or L<DBIx::Class::ResultSet::RecursiveUpdate>,
load the PgLog-component in your ResultSet classes:

    package My::Schema::ResultSet::Table;

    use base 'DBIx::Class::ResultSet';

    __PACKAGE__->load_components('ResultSet::PgLog');

    1;

In your application wrap any insert/update/delete in a transaction to have pg log activated:

* Mandatorily Pass an extra hashref to the txn_do method to indicate a UserId and optional Description for the transaction:

    $my_schema->txn_do(
        sub {
            $my_row->update({ ... });
        },
        {
            UserId => 'User_id',
            Description => 'description of transaction' # optional
        }
    );

=head1 DBIx::Class OVERRIDDEN METHODS
 
=head2 insert
 
=head2 update
 
=head2 delete

=cut

# local $DBIx::Class::AuditLog::enabled = 0;
# can be set to temporarily disable audit logging
our $enabled = 1;

sub insert {
    my $self = shift;

    return $self->next::method(@_) if !$enabled || $self->in_storage;

    my $result = $self->next::method(@_);

	my $action = "INSERT";

	my %column_data = $result->get_columns;
	$self->_store_changes( $action, $result, {}, \%column_data );

    return $result;
}

sub update {
    my $self = shift;

    return $self->next::method(@_) if !$enabled;

    my $stored_row      = $self->get_from_storage;
    my %new_data        = $self->get_columns;
    my @changed_columns = keys %{ $_[0] || {} };

    my $result = $self->next::method(@_);

    return unless $stored_row; # update on deleted row - nothing to log

    my %old_data = $stored_row->get_columns;

    if (@changed_columns) {
        @new_data{@changed_columns} = map $self->get_column($_),
            @changed_columns;
    }

    if ( keys %new_data ) {
		my $action = "UPDATE";
		$self->_store_changes( $action, $result, \%old_data, \%new_data );
    }

    return $result;
}

sub delete {
    my $self = shift;

    return $self->next::method(@_) if !$enabled;

    my $stored_row = $self->get_from_storage;

    my $result = $self->next::method(@_);

	my $action = "DELETE";
	my %old_data = $stored_row->get_columns;
	$self->_store_changes( $action, $result, \%old_data, {} );

    return $result;
}

=head1 HELPER METHODS

=head2 _pg_log_schema

Returns PgLog schema from storage

	my $pl_schema = $schema->pg_log_schema;

=cut

sub _pg_log_schema {
    my $self = shift;
    return $self->result_source->schema->pg_log_schema;
}

=head2 _store_changes
 



( run in 1.486 second using v1.01-cache-2.11-cpan-39bf76dae61 )