X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FJournal.pm;h=8e0b7e7a5631263281693487b96a751efd316042;hb=9a52f4b41dc49206cb5287c3fc2a138c5bd8a575;hp=64185bd00fea05fc9762bea97a556bdf77ada8df;hpb=5fc8406c5d74ebc57e3c543673270be9783db8e7;p=dbsrgits%2FDBIx-Class-Journal.git diff --git a/lib/DBIx/Class/Schema/Journal.pm b/lib/DBIx/Class/Schema/Journal.pm index 64185bd..8e0b7e7 100644 --- a/lib/DBIx/Class/Schema/Journal.pm +++ b/lib/DBIx/Class/Schema/Journal.pm @@ -7,88 +7,82 @@ use DBIx::Class::Schema::Journal::DB; __PACKAGE__->mk_classdata('journal_storage_type'); __PACKAGE__->mk_classdata('journal_connection'); -__PACKAGE__->mk_classdata('journal_no_automatic_deploy'); +__PACKAGE__->mk_classdata('journal_deploy_on_connect'); __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ] __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ] +__PACKAGE__->mk_classdata('journal_copy_sources'); +__PACKAGE__->mk_classdata('__journal_schema_prototype'); __PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal -__PACKAGE__->mk_classdata('_journal_internal_sources'); # the sources used to journal journal_sources - -our $VERSION = '0.01'; +__PACKAGE__->mk_classdata('journal_component'); +__PACKAGE__->mk_classdata('journal_nested_changesets'); use strict; use warnings; -# sub throw_exception -# { -# } - -# sub exception_action -# { -# my $self = shift; -# # print STDERR Carp::longmess; - -# $self->next::method(@_); -# } -# sub load_classes -# { -# my $class = shift; +sub _journal_schema_prototype +{ + my $self = shift; + if (my $proto = $self->__journal_schema_prototype) + { + return $proto; + } + my $proto = $self->__journal_schema_prototype + ( + DBIx::Class::Schema::Journal::DB->compose_namespace + ( + (blessed($self)||$self) . '::Journal' + ) + ); + my $comp = $self->journal_component || "Journal"; + ## Create auditlog+history per table + my %j_sources = map { $_ => 1 } $self->journal_sources + ? @{$self->journal_sources} + : $self->sources; -# $class->next::method(@_); - -# } + foreach my $s_name ($self->sources) + { + next unless($j_sources{$s_name}); + $self->create_journal_for($s_name => $proto); + $self->class($s_name)->load_components($comp); + } + return $proto; +} sub connection { my $self = shift; my $schema = $self->next::method(@_); -# print STDERR join(":", $self->sources), "\n"; + my $journal_schema = (ref $self||$self)->_journal_schema_prototype->clone; - my $journal_schema; - if(!defined($self->journal_connection)) - { - ## If no connection, use the same schema/storage etc as the user - DBIx::Class::Componentised->inject_base(ref $self, 'DBIx::Class::Schema::Journal::DB'); - $journal_schema = $self; - } - else + if($self->journal_connection) { - $journal_schema = DBIx::Class::Schema::Journal::DB->connect(@{ $self->journal_connection }); if($self->journal_storage_type) { $journal_schema->storage_type($self->journal_storage_type); } + $journal_schema->connection(@{ $self->journal_connection }); + } else { + $journal_schema->storage( $schema->storage ); } - ## get our own private version of the journaling sources - $self->_journal_schema($journal_schema->compose_namespace(blessed($self) . '::Journal')); + $self->_journal_schema($journal_schema); - ## Create auditlog+history per table - my %j_sources = map { $_ => 1 } $self->journal_sources - ? @{$self->journal_sources} - : $self->sources; - - my @journal_sources = $journal_schema->sources; # not sources to journal, but sources used by the journal internally - foreach my $s_name ($self->sources) - { - next unless($j_sources{$s_name}); - push @journal_sources, $self->create_journal_for($s_name); - $self->class($s_name)->load_components('Journal'); -# print STDERR "$s_name :", $self->class($s_name), "\n"; + if ( $self->journal_nested_changesets ) { + $self->_journal_schema->nested_changesets(1); + die "FIXME nested changeset schema not yet supported... add parent_id to ChangeSet here"; } - $self->_journal_internal_sources(\@journal_sources); - $self->journal_schema_deploy() - unless $self->journal_no_automatic_deploy; + if $self->journal_deploy_on_connect; ## Set up relationship between changeset->user_id and this schema's user if(!@{$self->journal_user || []}) { - warn "No Journal User set!"; + #warn "No Journal User set!"; # no need to warn, user_id is useful even without a rel return $schema; } @@ -98,91 +92,79 @@ sub connection return $schema; } -sub journal_schema_deploy +sub deploy { my ( $self, $sqlt_args, @args ) = @_; - $sqlt_args ||= {}; - $sqlt_args->{sources} = $self->_journal_internal_sources - unless exists $sqlt_args->{sources}; + $self->next::method($sqlt_args, @args); - $self->_journal_schema->deploy( $sqlt_args, @args ); + $self->journal_schema_deploy($sqlt_args, @args); } -sub get_audit_log_class_name -{ - my ($self, $sourcename) = @_; - - return blessed($self->_journal_schema) . "::${sourcename}AuditLog"; -} - -sub get_audit_history_class_name +sub journal_schema_deploy { - my ($self, $sourcename) = @_; + my ( $self, $sqlt_args, @args ) = @_; - return blessed($self->_journal_schema) . "::${sourcename}AuditHistory"; + $self->_journal_schema->deploy( $sqlt_args, @args ); } sub create_journal_for { - my ($self, $s_name) = @_; + my ($self, $s_name, $journal_schema) = @_; my $source = $self->source($s_name); - my $newclass = $self->get_audit_log_class_name($s_name); - DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class::Schema::Journal::DB::AuditLog'); - $newclass->table(lc($s_name) . "_audit_log"); - my $log_source = "${s_name}AuditLog"; - $self->_journal_schema->register_class($log_source, $newclass); - - - my $histclass = $self->get_audit_history_class_name($s_name); - DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class::Schema::Journal::DB::AuditHistory'); - $histclass->table(lc($s_name) . "_audit_history"); -# $histclass->result_source_instance->name(lc($s_name) . "_audit_hisory"); - $histclass->add_columns( - map { $_ => $source->column_info($_) } $source->columns - ); - - my $hist_source = "${s_name}AuditHistory"; - $self->_journal_schema->register_class($hist_source, $histclass); - - return ( $log_source, $hist_source ); + + foreach my $audit (qw(AuditLog AuditHistory)) { + my $audit_source = join("", $s_name, $audit); + my $class = blessed($journal_schema) . "::$audit_source"; + + DBIx::Class::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit"); + + $class->journal_define_table($source); + + $journal_schema->register_class($audit_source, $class); + + if ($self->journal_copy_sources) + { + $self->register_class($audit_source, $class); + } + } } sub txn_do { - my ($self, $code) = @_; + my ($self, $user_code, @args) = @_; - ## Create a new changeset, then run $code as a transaction - my $cs = $self->_journal_schema->resultset('ChangeSet'); + my $jschema = $self->_journal_schema; - $self->txn_begin; - my %changesetdata; - if( defined $self->_journal_schema->current_user() ) - { - $changesetdata{user_id} = $self->_journal_schema->current_user(); - } - if( defined $self->_journal_schema->current_session() ) + my $code = $user_code; + + my $current_changeset = $jschema->_current_changeset; + if ( !$current_changeset || $self->journal_nested_changesets ) { - $changesetdata{session_id} = $self->_journal_schema->current_session(); + my $current_changeset_ref = $jschema->_current_changeset_container; + + unless ( $current_changeset_ref ) { + # this is a hash because scalar refs can't be localized + $current_changeset_ref = { }; + $jschema->_current_changeset_container($current_changeset_ref); + } + + # wrap the thunk with a new changeset creation + $code = sub { + my $changeset = $jschema->journal_create_changeset( parent_id => $current_changeset ); + local $current_changeset_ref->{changeset} = $changeset->id; + $user_code->(@_); + }; + } -# ( -# $self->_journal_schema->current_user() -# ? ( user_id => $self->_journal_schema->current_user()) -# : (), -# $self->_journal_schema->current_session() -# ? ( session_id => $self->_journal_schema->current_session() ) -# : () -# ); - if(!%changesetdata) - { - %changesetdata = ( ID => undef ); + if ( $jschema->storage != $self->storage ) { + my $inner_code = $code; + $code = sub { $jschema->txn_do($inner_code, @_) }; } - my $changeset = $cs->create({ %changesetdata }); - $self->_journal_schema->current_changeset($changeset->ID); - $self->next::method($code); + return $self->next::method($code, @args); } sub changeset_user