package DBIx::Class::Journal;
use base qw/DBIx::Class/;
-__PACKAGE__->load_components(qw/AccessorGroup/);
-__PACKAGE__->mk_group_accessors('simple' => qw/
- journal_dsn
- journal_sources
- /);
+## On create/insert, add new entry to AuditLog
+
+sub new
+{
+ my ($class, $attrs, @rest) = @_;
+
+ $class->result_source->schema->_journal_schema->current_user(delete $attrs->{user_id});
+
+ $self->next::method($attrs, @rest);
+}
+
+sub insert
+{
+ my ($self) = @_;
+
+ ## create new transaction here?
+ $self->next::method();
+ if($self->in_storage)
+ {
+ my $s_name = $self->result_source_instance->name();
+ my $al = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditLog");
+ $al->create({
+ changeset => $self->result_source->schema->_journal_schema->current_changeset(),
+ });
+}
+
+## On delete, update delete_id of AuditLog
+
+## On update, copy previous row's contents to AuditHistory
+
1;
## get our own private version of the journaling sources
$self->_journal_schema($journal_schema->compose_namespace(blessed($self) . '::Journal'));
+ ## Create auditlog+history per table
my %j_sources = @{$self->journal_sources} ? map { $_ => 1 } @{$self->journal_sources} : map { $_ => 1 } $self->sources;
foreach my $s_name ($self->sources)
{
next unless($j_sources{$s_name});
$self->create_journal_for($s_name);
+ $self->source($s_name)->load_component('Journal');
}
## Set up relationship between changeset->user_id and this schema's user
return;
}
- ## get our own private version of the journaling sources
- $self->_journal_schema->compose_namespace(blessed($self) . '::Journal');
- DBIx::Class::Schema::Journal::DB::ChangeSet->belongs_to('user', @{$self->journal_user});
+ $self->_journal_schema->source('ChangeSet')->belongs_to('user', @{$self->journal_user});
}
sub get_audit_log_class_name
}
+sub create_changeset
+{
+ my ($self, $code) = @_;
+
+ ## Create a new changeset, then run $code as a transaction
+ my $cs = $self->result_source->schema->_journal_schema->resultset('ChangeSet');
+ my $changeset = $cs->create({
+ user_id => $self->result_source->schema->_journal_schema->current_user(),
+ session_id => $self->result_source->schema->_journal_schema->current_session(),
+ });
+ $self->result_source->schema->_journal_schema->current_changeset($changeset->ID);
+
+ $self->txn_do($code);
+}
+
1;
use base 'DBIx::Class::Schema';
+__PACKAGE__->mk_classdata('current_user');
+__PACKAGE__->mk_classdata('current_session');
+__PACKAGE__->mk_classdata('current_changeset');
+
DBIx::Class::Schema::Journal::DB->load_classes();
1;