Skip to content

Instantly share code, notes, and snippets.

@TBSliver
Last active March 27, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TBSliver/caaf87d7165c53e2e55cdffb47c2fefc to your computer and use it in GitHub Desktop.
Save TBSliver/caaf87d7165c53e2e55cdffb47c2fefc to your computer and use it in GitHub Desktop.
DBIx::Class::AuditAny with user_id column
package MyApp;
# Insert extra Mojo style bits - this uses mojo, but can be easily modified to be any other framework
has auditor => sub {
my $c = shift;
use MyApp::Audit::DataPoint;
return DBIx::Class::AuditAny->track(
schema => $c->app->schema,
track_all_sources => 1,
collector_class => 'Collector::DBIC',
collector_params => {
target_source => 'AuditChangeSet',
change_data_rel => 'audit_changes',
column_data_rel => 'change',
},
datapoint_configs => [
{
class => 'MyApp::Audit::DataPoint',
name => 'user_id',
context => 'change',
method => sub {
my $context = shift;
return $context->current_user;
}
},
],
);
};
sub setup {
my $self = shift;
# ... other setup
# Add helper and start it running
$self->helper( auditor => sub { $self->app->auditor } );
$self->auditor;
# Set the user id to 0 if no user
$self->hook(before_routes => sub {
my $c = shift;
my $c_user = $c->current_user;
$c->auditor->_datapoints->{'user_id'}->current_user( defined $c_user ? $c_user->id : 0 );
});
# more setup ...
}
1;
package MyApp::Audit::DataPoint;
use Moo;
extends 'DBIx::Class::AuditAny::DataPoint';
has current_user => (
is => 'rw',
default => 0,
);
1;
package MyApp::DB::Result::AuditChange;
use base 'DBIx::Class';
use strict;
use warnings;
__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table('audit_change');
__PACKAGE__->add_columns(
'id' => {
'is_foreign_key' => 0,
'data_type' => 'integer',
'default_value' => undef,
'is_nullable' => 0,
'name' => 'id',
'is_auto_increment' => 1,
'size' => '0'
},
'changeset_id' => {
'is_foreign_key' => 1,
'data_type' => 'integer',
'default_value' => undef,
'is_nullable' => 0,
'is_auto_increment' => 0,
'name' => 'changeset_id',
'size' => '0'
},
'source' => {
'data_type' => 'varchar',
'is_foreign_key' => 0,
'size' => '255',
'name' => 'source',
'is_auto_increment' => 0,
'is_nullable' => 0,
'default_value' => undef
},
'action' => {
'data_type' => 'char',
'is_foreign_key' => 0,
'size' => '6',
'is_auto_increment' => 0,
'name' => 'action',
'is_nullable' => 0,
'default_value' => undef
},
'pri_key_value' => {
'size' => '255',
'name' => 'pri_key_value',
'is_auto_increment' => 0,
'default_value' => undef,
'is_nullable' => 0,
'data_type' => 'varchar',
'is_foreign_key' => 0
},
'change_ts' => {
'size' => '0',
'name' => 'change_ts',
'is_auto_increment' => 0,
'default_value' => undef,
'is_nullable' => 0,
'data_type' => 'datetime',
'is_foreign_key' => 0
},
'user_id' => {
'name' => 'user_id',
'is_auto_increment' => 0,
'default_value' => 0,
'is_nullable' => 0,
'data_type' => 'integer',
'is_foreign_key' => 0
},
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->belongs_to('changeset_id', 'MyApp::DB::Result::AuditChangeSet');
__PACKAGE__->has_many('change', 'MyApp::DB::Result::AuditChangeColumn', 'change_id');
__PACKAGE__->belongs_to('user', 'MyApp::DB::Result::User', 'user_id', { is_foreign_key_constraint => 0 });
1;
package MyApp::DB::Result::AuditChangeColumn;
use base 'DBIx::Class';
use strict;
use warnings;
__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table('audit_change_column');
__PACKAGE__->add_columns(
'id' => {
'is_foreign_key' => 0,
'data_type' => 'integer',
'name' => 'id',
'is_auto_increment' => 1,
'default_value' => undef,
'is_nullable' => 0,
'size' => '0'
},
'change_id' => {
'data_type' => 'integer',
'is_foreign_key' => 1,
'size' => '0',
'is_auto_increment' => 0,
'name' => 'change_id',
'is_nullable' => 0,
'default_value' => undef
},
'column_name' => {
'is_foreign_key' => 0,
'data_type' => 'varchar',
'is_nullable' => 0,
'default_value' => undef,
'is_auto_increment' => 0,
'name' => 'column_name',
'size' => '128'
},
'old_value' => {
'data_type' => 'mediumtext',
'is_foreign_key' => 0,
'size' => '0',
'is_nullable' => 1,
'default_value' => undef,
'name' => 'old_value',
'is_auto_increment' => 0
},
'new_value' => {
'default_value' => undef,
'is_nullable' => 1,
'name' => 'new_value',
'is_auto_increment' => 0,
'size' => '0',
'is_foreign_key' => 0,
'data_type' => 'mediumtext'
},
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->belongs_to('change_id', 'MyApp::DB::Result::AuditChange');
1;
package MyApp::DB::Result::AuditChangeSet;
use base 'DBIx::Class';
use strict;
use warnings;
__PACKAGE__->load_components(qw/ Core /);
__PACKAGE__->table('audit_change_set');
__PACKAGE__->add_columns(
'id' => {
'data_type' => 'integer',
'is_foreign_key' => 0,
'size' => '0',
'is_auto_increment' => 1,
'name' => 'id',
'default_value' => undef,
'is_nullable' => 0
},
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many('audit_changes', 'MyApp::DB::Result::AuditChange', 'changeset_id');
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment