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