you cannot take off a version number
[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');
5b64dcdc 20__PACKAGE__->mk_classdata('journal_prefix');
f0f14c64 21
ec16e73a 22use strict;
23use warnings;
f3602465 24
35e3517f 25our $VERSION = '0.01';
d19af369 26
548cc9f7 27sub _journal_schema_prototype {
2ae5bebc 28 my $self = shift;
548cc9f7 29 if (my $proto = $self->__journal_schema_prototype) {
2ae5bebc 30 return $proto;
31 }
928b6c45 32 my $c = blessed($self)||$self;
33 my $journal_schema_class = "${c}::_JOURNAL";
34 Class::C3::Componentised->inject_base($journal_schema_class, 'DBIx::Class::Schema::Journal::DB');
751cfa93 35 $journal_schema_class->load_components($self->journal_components)
36 if $self->journal_components;
548cc9f7 37 my $proto = $self->__journal_schema_prototype (
38 $journal_schema_class->compose_namespace( $c.'::Journal')
2ae5bebc 39 );
751cfa93 40
41
53c47638 42 my $comp = $self->journal_component || "Journal";
e0305412 43
5b64dcdc 44 my $prefix = $self->journal_prefix || '';
45 foreach my $audit (qw(ChangeSet ChangeLog)) {
46 my $class = blessed($proto) . "::$audit";
47
48 Class::C3::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit");
49
50 $class->journal_define_table(blessed($proto), $prefix);
51
52 $proto->register_class($audit, $class);
53
54 $self->register_class($audit, $class)
55 if $self->journal_copy_sources;
56 }
53c47638 57
58 ## Create auditlog+history per table
59 my %j_sources = map { $_ => 1 } $self->journal_sources
548cc9f7 60 ? @{$self->journal_sources}
61 : $self->sources;
53c47638 62
548cc9f7 63 foreach my $s_name ($self->sources) {
53c47638 64 next unless($j_sources{$s_name});
65 $self->create_journal_for($s_name => $proto);
66 $self->class($s_name)->load_components($comp);
53c47638 67 }
68 return $proto;
2ae5bebc 69}
70
548cc9f7 71sub connection {
f0f14c64 72 my $self = shift;
52558dc4 73 my $schema = $self->next::method(@_);
f0f14c64 74
53c47638 75 my $journal_schema = (ref $self||$self)->_journal_schema_prototype->clone;
0f91ba2b 76
548cc9f7 77 if($self->journal_connection) {
78 $journal_schema->storage_type($self->journal_storage_type)
79 if $self->journal_storage_type;
0f91ba2b 80 $journal_schema->connection(@{ $self->journal_connection });
81 } else {
82 $journal_schema->storage( $schema->storage );
d27ed438 83 }
84
0f91ba2b 85 $self->_journal_schema($journal_schema);
86
f0f14c64 87
aba93491 88 if ( $self->journal_nested_changesets ) {
89 $self->_journal_schema->nested_changesets(1);
548cc9f7 90 die 'FIXME nested changeset schema not yet supported... add parent_id to ChangeSet here';
aba93491 91 }
92
aa873584 93 $self->journal_schema_deploy()
aba93491 94 if $self->journal_deploy_on_connect;
51b16220 95
f0f14c64 96 ## Set up relationship between changeset->user_id and this schema's user
548cc9f7 97 if(!@{$self->journal_user || []}) {
64b056b4 98 #warn "No Journal User set!"; # no need to warn, user_id is useful even without a rel
51b16220 99 return $schema;
f0f14c64 100 }
101
c5fba518 102 $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
f4f0b7c9 103 $self->_journal_schema->storage->disconnect();
52558dc4 104
105 return $schema;
f0f14c64 106}
107
548cc9f7 108sub journal_schema_deploy {
109 my $self = shift;
5fc8406c 110
548cc9f7 111 $self->_journal_schema->deploy(@_);
51b16220 112}
113
548cc9f7 114sub create_journal_for {
53c47638 115 my ($self, $s_name, $journal_schema) = @_;
f0f14c64 116
117 my $source = $self->source($s_name);
30a4f241 118
59c8adb5 119 foreach my $audit (qw(AuditLog AuditHistory)) {
851aad7c 120 my $audit_source = $s_name.$audit;
53c47638 121 my $class = blessed($journal_schema) . "::$audit_source";
f0f14c64 122
928b6c45 123 Class::C3::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit");
0f91ba2b 124
5b64dcdc 125 $class->journal_define_table($source, blessed($journal_schema));
0f91ba2b 126
53c47638 127 $journal_schema->register_class($audit_source, $class);
128
548cc9f7 129 $self->register_class($audit_source, $class)
130 if $self->journal_copy_sources;
0f91ba2b 131 }
f0f14c64 132}
133
16ba7d5f 134# XXX FIXME deploy is not idempotent :-(
462d8e70 135sub bootstrap_journal {
1d09727d 136 my $self = shift;
462d8e70 137 $self->journal_schema_deploy;
138 $self->prepopulate_journal;
139}
1d09727d 140
462d8e70 141# copy data from original schema sources into the journal as inserts in one
142# changeset, so that later deletes will not fail to be journalled.
143sub prepopulate_journal {
144 my $self = shift;
1d09727d 145 my $schema = $self;
462d8e70 146
147 # woah, looks like prepopulate has already run?
148 return if $schema->_journal_schema->resultset('ChangeSet')->count != 0;
1d09727d 149
150 # using our own overridden txn_do (see below) will create a changeset
151 $schema->txn_do( sub {
462d8e70 152 my %j_sources = map { $_ => 1 } $self->journal_sources
153 ? @{$self->journal_sources}
154 : $self->sources;
155
156 my $j_schema = $self->_journal_schema;
157 my $changelog_rs = $j_schema->resultset('ChangeLog');
1d09727d 158 my $chs_id = $j_schema->current_changeset;
159
160 foreach my $s_name ($self->sources) {
161 next unless $j_sources{$s_name};
162
163 my $from_rs = $schema->resultset($s_name);
19de675b 164 my @pks = $from_rs->result_source->primary_columns;
1d09727d 165 $from_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
166
167 my $to_rs = $j_schema->resultset("${s_name}AuditHistory");
168 my $log_rs = $j_schema->resultset("${s_name}AuditLog");
169
170 my $page = 1;
171 while (
172 my @x = $from_rs->search(undef, {
173 rows => 1_000,
174 page => $page++,
175 })
176 ) {
177 # get some number of change log IDs to be generated for this page
19de675b 178 my @log_ids = map $_->id,
179 $changelog_rs->populate([
180 map +{ changeset_id => $chs_id }, (0 .. $#x)
181 ]);
182
183
184 my @datas;
185 for my $idx (0 .. $#x ) {
186 push @datas, {
187 create_id => $log_ids[$idx],
188 map { $_ => $x[$idx]->{$_} } @pks,
189 }
190 }
1d09727d 191 # create the audit log entries for the rows in this page
19de675b 192 $log_rs->populate([@datas]);
1d09727d 193
194 # now populate the audit history
195 $to_rs->populate([
196 map +{
197 %{$x[$_]},
198 audit_change_id => $log_ids[$_],
199 }, (0 .. $#x)
200 ]);
201 }
202 }
203 });
204}
205
548cc9f7 206sub txn_do {
aba93491 207 my ($self, $user_code, @args) = @_;
74f04ccc 208
aba93491 209 my $jschema = $self->_journal_schema;
8092c4ed 210
4233d9a1 211 my $code = $user_code;
ec16e73a 212
0f91ba2b 213 my $current_changeset = $jschema->_current_changeset;
548cc9f7 214 if ( !$current_changeset || $self->journal_nested_changesets ) {
aba93491 215 my $current_changeset_ref = $jschema->_current_changeset_container;
216
217 unless ( $current_changeset_ref ) {
218 # this is a hash because scalar refs can't be localized
219 $current_changeset_ref = { };
220 $jschema->_current_changeset_container($current_changeset_ref);
221 }
222
223 # wrap the thunk with a new changeset creation
224 $code = sub {
794e01fa 225 my $changeset = $jschema->journal_create_changeset( parent_id => $current_changeset );
226 local $current_changeset_ref->{changeset} = $changeset->id;
227 $user_code->(@_);
228 };
4233d9a1 229
52558dc4 230 }
74f04ccc 231
794e01fa 232 if ( $jschema->storage != $self->storage ) {
233 my $inner_code = $code;
234 $code = sub { $jschema->txn_do($inner_code, @_) };
235 }
4233d9a1 236
794e01fa 237 return $self->next::method($code, @args);
74f04ccc 238}
239
548cc9f7 240sub changeset_user {
ec16e73a 241 my ($self, $userid) = @_;
242
548cc9f7 243 return $self->_journal_schema->current_user()
244 if @_ == 1;
ec16e73a 245
246 $self->_journal_schema->current_user($userid);
247}
248
548cc9f7 249sub changeset_session {
ec16e73a 250 my ($self, $sessionid) = @_;
251
548cc9f7 252 return $self->_journal_schema->current_session()
253 if @_ == 1;
ec16e73a 254
255 $self->_journal_schema->current_session($sessionid);
256}
257
f0f14c64 2581;