add journal_components to allow for setting components on journal schema
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Schema / Journal.pm
CommitLineData
f0f14c64 1package DBIx::Class::Schema::Journal;
2
3use base qw/DBIx::Class/;
4
d27ed438 5use Scalar::Util 'blessed';
b5851590 6use DBIx::Class::Schema::Journal::DB;
c8f87617 7use Class::C3::Componentised ();
d27ed438 8
9__PACKAGE__->mk_classdata('journal_storage_type');
10__PACKAGE__->mk_classdata('journal_connection');
aba93491 11__PACKAGE__->mk_classdata('journal_deploy_on_connect');
f0f14c64 12__PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
13__PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
53c47638 14__PACKAGE__->mk_classdata('journal_copy_sources');
2ae5bebc 15__PACKAGE__->mk_classdata('__journal_schema_prototype');
ec16e73a 16__PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal
8dc58fe2 17__PACKAGE__->mk_classdata('journal_component');
751cfa93 18__PACKAGE__->mk_classdata('journal_components');
aba93491 19__PACKAGE__->mk_classdata('journal_nested_changesets');
f0f14c64 20
ec16e73a 21use strict;
22use warnings;
f3602465 23
d19af369 24
548cc9f7 25sub _journal_schema_prototype {
2ae5bebc 26 my $self = shift;
548cc9f7 27 if (my $proto = $self->__journal_schema_prototype) {
2ae5bebc 28 return $proto;
29 }
928b6c45 30 my $c = blessed($self)||$self;
31 my $journal_schema_class = "${c}::_JOURNAL";
32 Class::C3::Componentised->inject_base($journal_schema_class, 'DBIx::Class::Schema::Journal::DB');
751cfa93 33 $journal_schema_class->load_components($self->journal_components)
34 if $self->journal_components;
548cc9f7 35 my $proto = $self->__journal_schema_prototype (
36 $journal_schema_class->compose_namespace( $c.'::Journal')
2ae5bebc 37 );
751cfa93 38
39
53c47638 40 my $comp = $self->journal_component || "Journal";
41
42 ## Create auditlog+history per table
43 my %j_sources = map { $_ => 1 } $self->journal_sources
548cc9f7 44 ? @{$self->journal_sources}
45 : $self->sources;
53c47638 46
548cc9f7 47 foreach my $s_name ($self->sources) {
53c47638 48 next unless($j_sources{$s_name});
49 $self->create_journal_for($s_name => $proto);
50 $self->class($s_name)->load_components($comp);
53c47638 51 }
52 return $proto;
2ae5bebc 53}
54
548cc9f7 55sub connection {
f0f14c64 56 my $self = shift;
52558dc4 57 my $schema = $self->next::method(@_);
f0f14c64 58
53c47638 59 my $journal_schema = (ref $self||$self)->_journal_schema_prototype->clone;
0f91ba2b 60
548cc9f7 61 if($self->journal_connection) {
62 $journal_schema->storage_type($self->journal_storage_type)
63 if $self->journal_storage_type;
0f91ba2b 64 $journal_schema->connection(@{ $self->journal_connection });
65 } else {
66 $journal_schema->storage( $schema->storage );
d27ed438 67 }
68
0f91ba2b 69 $self->_journal_schema($journal_schema);
70
f0f14c64 71
aba93491 72 if ( $self->journal_nested_changesets ) {
73 $self->_journal_schema->nested_changesets(1);
548cc9f7 74 die 'FIXME nested changeset schema not yet supported... add parent_id to ChangeSet here';
aba93491 75 }
76
aa873584 77 $self->journal_schema_deploy()
aba93491 78 if $self->journal_deploy_on_connect;
51b16220 79
f0f14c64 80 ## Set up relationship between changeset->user_id and this schema's user
548cc9f7 81 if(!@{$self->journal_user || []}) {
64b056b4 82 #warn "No Journal User set!"; # no need to warn, user_id is useful even without a rel
51b16220 83 return $schema;
f0f14c64 84 }
85
c5fba518 86 $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
f4f0b7c9 87 $self->_journal_schema->storage->disconnect();
52558dc4 88
89 return $schema;
f0f14c64 90}
91
548cc9f7 92sub deploy {
93 my $self = shift;
aba93491 94
548cc9f7 95 $self->next::method(@_);
aba93491 96
548cc9f7 97 $self->journal_schema_deploy(@_);
aba93491 98}
99
548cc9f7 100sub journal_schema_deploy {
101 my $self = shift;
5fc8406c 102
548cc9f7 103 $self->_journal_schema->deploy(@_);
51b16220 104}
105
548cc9f7 106sub create_journal_for {
53c47638 107 my ($self, $s_name, $journal_schema) = @_;
f0f14c64 108
109 my $source = $self->source($s_name);
30a4f241 110
59c8adb5 111 foreach my $audit (qw(AuditLog AuditHistory)) {
851aad7c 112 my $audit_source = $s_name.$audit;
53c47638 113 my $class = blessed($journal_schema) . "::$audit_source";
f0f14c64 114
928b6c45 115 Class::C3::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit");
0f91ba2b 116
59c8adb5 117 $class->journal_define_table($source);
0f91ba2b 118
53c47638 119 $journal_schema->register_class($audit_source, $class);
120
548cc9f7 121 $self->register_class($audit_source, $class)
122 if $self->journal_copy_sources;
0f91ba2b 123 }
f0f14c64 124}
125
548cc9f7 126sub txn_do {
aba93491 127 my ($self, $user_code, @args) = @_;
74f04ccc 128
aba93491 129 my $jschema = $self->_journal_schema;
8092c4ed 130
4233d9a1 131 my $code = $user_code;
ec16e73a 132
0f91ba2b 133 my $current_changeset = $jschema->_current_changeset;
548cc9f7 134 if ( !$current_changeset || $self->journal_nested_changesets ) {
aba93491 135 my $current_changeset_ref = $jschema->_current_changeset_container;
136
137 unless ( $current_changeset_ref ) {
138 # this is a hash because scalar refs can't be localized
139 $current_changeset_ref = { };
140 $jschema->_current_changeset_container($current_changeset_ref);
141 }
142
143 # wrap the thunk with a new changeset creation
144 $code = sub {
794e01fa 145 my $changeset = $jschema->journal_create_changeset( parent_id => $current_changeset );
146 local $current_changeset_ref->{changeset} = $changeset->id;
147 $user_code->(@_);
148 };
4233d9a1 149
52558dc4 150 }
74f04ccc 151
794e01fa 152 if ( $jschema->storage != $self->storage ) {
153 my $inner_code = $code;
154 $code = sub { $jschema->txn_do($inner_code, @_) };
155 }
4233d9a1 156
794e01fa 157 return $self->next::method($code, @args);
74f04ccc 158}
159
548cc9f7 160sub changeset_user {
ec16e73a 161 my ($self, $userid) = @_;
162
548cc9f7 163 return $self->_journal_schema->current_user()
164 if @_ == 1;
ec16e73a 165
166 $self->_journal_schema->current_user($userid);
167}
168
548cc9f7 169sub changeset_session {
ec16e73a 170 my ($self, $sessionid) = @_;
171
548cc9f7 172 return $self->_journal_schema->current_session()
173 if @_ == 1;
ec16e73a 174
175 $self->_journal_schema->current_session($sessionid);
176}
177
178
f0f14c64 1791;