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