From: Jess Robinson Date: Sun, 29 Apr 2007 17:34:17 +0000 (+0000) Subject: Add settings for current_user/session/changeset, overload insert() X-Git-Tag: v0.900201~110 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=74f04cccc5938f53346a3580151080330d1573ea;p=dbsrgits%2FDBIx-Class-Journal.git Add settings for current_user/session/changeset, overload insert() --- diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index 8da929e..c040d34 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -1,12 +1,37 @@ 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; diff --git a/lib/DBIx/Class/Schema/Journal.pm b/lib/DBIx/Class/Schema/Journal.pm index 46f6963..7e611df 100644 --- a/lib/DBIx/Class/Schema/Journal.pm +++ b/lib/DBIx/Class/Schema/Journal.pm @@ -24,11 +24,13 @@ sub load_classes ## 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 @@ -38,9 +40,7 @@ sub load_classes 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 @@ -76,4 +76,19 @@ sub create_journal_for } +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; diff --git a/lib/DBIx/Class/Schema/Journal/DB.pm b/lib/DBIx/Class/Schema/Journal/DB.pm index 89e7387..5831ce4 100644 --- a/lib/DBIx/Class/Schema/Journal/DB.pm +++ b/lib/DBIx/Class/Schema/Journal/DB.pm @@ -2,6 +2,10 @@ package DBIx::Class::Schema::Journal::DB; 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; diff --git a/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm index f269227..a5c3928 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm @@ -14,7 +14,7 @@ __PACKAGE__->add_columns( }, user_id => { data_type => 'integer', - is_nullable => 0, + is_nullable => 1, is_foreign_key => 1, }, set_date => {