1 package DBIx::Class::Schema::Journal;
3 use base qw/DBIx::Class/;
5 use Scalar::Util 'blessed';
6 use DBIx::Class::Schema::Journal::DB;
8 __PACKAGE__->mk_classdata('journal_storage_type');
9 __PACKAGE__->mk_classdata('journal_connection');
10 __PACKAGE__->mk_classdata('journal_deploy_on_connect');
11 __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
12 __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
13 __PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal
14 __PACKAGE__->mk_classdata('_journal_internal_sources'); # the sources used to journal journal_sources
15 __PACKAGE__->mk_classdata('journal_nested_changesets');
17 our $VERSION = '0.01';
26 # sub exception_action
29 # # print STDERR Carp::longmess;
31 # $self->next::method(@_);
39 # $class->next::method(@_);
46 my $schema = $self->next::method(@_);
48 # print STDERR join(":", $self->sources), "\n";
51 if(!defined($self->journal_connection))
53 ## If no connection, use the same schema/storage etc as the user
54 DBIx::Class::Componentised->inject_base(ref $self, 'DBIx::Class::Schema::Journal::DB');
55 $journal_schema = $self;
59 $journal_schema = DBIx::Class::Schema::Journal::DB->connect(@{ $self->journal_connection });
60 if($self->journal_storage_type)
62 $journal_schema->storage_type($self->journal_storage_type);
66 ## get our own private version of the journaling sources
67 $self->_journal_schema($journal_schema->compose_namespace(blessed($self) . '::Journal'));
69 ## Create auditlog+history per table
70 my %j_sources = map { $_ => 1 } $self->journal_sources
71 ? @{$self->journal_sources}
74 my @journal_sources = $journal_schema->sources; # not sources to journal, but sources used by the journal internally
76 foreach my $s_name ($self->sources)
78 next unless($j_sources{$s_name});
79 push @journal_sources, $self->create_journal_for($s_name);
80 $self->class($s_name)->load_components('Journal');
81 # print STDERR "$s_name :", $self->class($s_name), "\n";
84 $self->_journal_internal_sources(\@journal_sources);
86 if ( $self->journal_nested_changesets ) {
87 $self->_journal_schema->nested_changesets(1);
88 die "FIXME nested changeset schema not yet supported... add parent_id to ChangeSet here";
91 $self->journal_schema_deploy()
92 if $self->journal_deploy_on_connect;
94 ## Set up relationship between changeset->user_id and this schema's user
95 if(!@{$self->journal_user || []})
97 #warn "No Journal User set!"; # no need to warn, user_id is useful even without a rel
101 $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
102 $self->_journal_schema->storage->disconnect();
109 my ( $self, $sqlt_args, @args ) = @_;
111 $self->next::method($sqlt_args, @args);
114 local $sqlt_args->{sources} = $self->_journal_internal_sources;
115 $self->journal_schema_deploy($sqlt_args, @args);
118 sub journal_schema_deploy
120 my ( $self, $sqlt_args, @args ) = @_;
123 $sqlt_args->{sources} = $self->_journal_internal_sources
124 unless exists $sqlt_args->{sources};
126 $self->_journal_schema->deploy( $sqlt_args, @args );
129 sub get_audit_log_class_name
131 my ($self, $sourcename) = @_;
133 return blessed($self->_journal_schema) . "::${sourcename}AuditLog";
136 sub get_audit_history_class_name
138 my ($self, $sourcename) = @_;
140 return blessed($self->_journal_schema) . "::${sourcename}AuditHistory";
143 sub create_journal_for
145 my ($self, $s_name) = @_;
147 my $source = $self->source($s_name);
148 my $newclass = $self->get_audit_log_class_name($s_name);
149 DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class::Schema::Journal::DB::AuditLog');
150 $newclass->table(lc($s_name) . "_audit_log");
151 my $log_source = "${s_name}AuditLog";
152 $self->_journal_schema->register_class($log_source, $newclass);
155 my $histclass = $self->get_audit_history_class_name($s_name);
156 DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class::Schema::Journal::DB::AuditHistory');
157 $histclass->table(lc($s_name) . "_audit_history");
158 # $histclass->result_source_instance->name(lc($s_name) . "_audit_hisory");
159 $histclass->add_columns(
160 map { $_ => $source->column_info($_) } $source->columns
163 my $hist_source = "${s_name}AuditHistory";
164 $self->_journal_schema->register_class($hist_source, $histclass);
166 return ( $log_source, $hist_source );
171 my ($self, $user_code, @args) = @_;
173 my $jschema = $self->_journal_schema;
177 my $current_changeset = $jschema->current_changeset;
178 if ( !$current_changeset || $self->journal_nested_changesets )
180 my $current_changeset_ref = $jschema->_current_changeset_container;
182 unless ( $current_changeset_ref ) {
183 # this is a hash because scalar refs can't be localized
184 $current_changeset_ref = { };
185 $jschema->_current_changeset_container($current_changeset_ref);
188 # wrap the thunk with a new changeset creation
190 my $changeset = $jschema->journal_create_changeset( parent_id => $current_changeset );
191 local $current_changeset_ref->{changeset} = $changeset->ID;
196 $self->next::method($code || $user_code);
201 my ($self, $userid) = @_;
203 return $self->_journal_schema->current_user() if(@_ == 1);
205 $self->_journal_schema->current_user($userid);
208 sub changeset_session
210 my ($self, $sessionid) = @_;
212 return $self->_journal_schema->current_session() if(@_ == 1);
214 $self->_journal_schema->current_session($sessionid);