delete bogus, unused file
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Journal.pm
CommitLineData
f0f14c64 1package DBIx::Class::Journal;
2
3use base qw/DBIx::Class/;
f0f14c64 4
ec16e73a 5use strict;
6use warnings;
7
bad22379 8our $VERSION = '0.900001_02';
ec16e73a 9
74f04ccc 10## On create/insert, add new entry to AuditLog
11
c5fba518 12# sub new
13# {
14# my ($class, $attrs, @rest) = @_;
74f04ccc 15
c5fba518 16# $class->result_source->schema->_journal_schema->current_user(delete $attrs->{user_id});
74f04ccc 17
c5fba518 18# $class->next::method($attrs, @rest);
19# }
74f04ccc 20
7adb876c 21sub _journal_schema {
22 my $self = shift;
23 $self->result_source->schema->_journal_schema;
6bfb7a1d 24}
25
26sub insert
27{
28 my ($self, @args) = @_;
29
30 return if($self->in_storage);
31
32 my $res = $self->next::method(@args);
33
34 $self->journal_log_insert();
35
c5fba518 36 return $res;
74f04ccc 37}
38
6bfb7a1d 39sub journal_log_insert
40{
41 my ($self) = @_;
42
7adb876c 43 if ( $self->in_storage ) {
44 my $j = $self->_journal_schema;
45 my $change_id = $j->journal_create_change()->id;
46 $j->journal_update_or_create_log_entry( $self, create_id => $change_id );
47 $j->journal_record_in_history( $self, audit_change_id => $change_id );
48 }
6bfb7a1d 49}
50
74f04ccc 51## On delete, update delete_id of AuditLog
52
1e996809 53sub delete
54{
55 my ($self, @rest) = @_;
56 $self->next::method(@rest);
6bfb7a1d 57 $self->journal_log_delete(@rest);
58}
1e996809 59
6bfb7a1d 60sub journal_log_delete
61{
62 my ($self) = @_;
63
7adb876c 64 unless ($self->in_storage) {
65 my $j = $self->_journal_schema;
66 $j->journal_update_or_create_log_entry( $self, delete_id => $j->journal_create_change->id );
67 }
1e996809 68}
69
74f04ccc 70## On update, copy previous row's contents to AuditHistory
71
1e996809 72sub update
73{
74 my ($self, $upd, @rest) = @_;
6bfb7a1d 75 $self->journal_log_update($upd, @rest);
76 $self->next::method($upd, @rest);
77}
78
79sub journal_log_update
80{
81 my ($self, $upd, @rest) = @_;
1e996809 82
83 if($self->in_storage)
84 {
7adb876c 85 my $j = $self->_journal_schema;
86
87 my $change = $j->journal_create_change;
88 my $prev = $self->result_source->resultset->find( $self->ident_condition );
89 $j->journal_record_in_history( $prev, audit_change_id => $change );
1e996809 90 }
1e996809 91}
92
ec16e73a 93=head1 NAME
94
95DBIx::Class::Journal - auditing for tables managed by DBIx::Class
96
97=head1 SYNOPSIS
98
99 package My::Schema;
100 use base 'DBIx::Class::Schema';
101
102 __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Journal/);
103
104 __PACKAGE__->journal_connection(['dbi:SQLite:t/var/Audit.db']);
105 __PACKAGE__->journal_user(['My::Schema::User', {'foreign.userid' => 'self.user_id'}]);
106
107
108 ########
109
110 $schema->changeset_user($user->id);
111 my $new_artist = $schema->txn_do( sub {
112 return = $schema->resultset('Artist')->create({ name => 'Fred' });
113 });
114
115
116=head1 DESCRIPTION
117
118The purpose of this L<DBIx::Class> component module is to create an
119audit-trail for all changes made to the data in your database (via a
120DBIx::Class schema). It creates changesets and assigns each
121create/update/delete operation an id. The creation and deletion date
122of each row is stored, as well as the previous contents of any row
123that gets changed.
124
125All queries which want auditing should be called using
126L<DBIx::Class::Schema/txn_do>, which is used to create changesets for
127each transaction.
128
129To track who did which changes, the user_id (an integer) of the
130current user can be set, a session_id can also be set, both are
131optional.
132
133To access the auditing schema to look at the auditdata or revert a
134change, use C<< $schema->_journal_schema >>.
135
136=head2 TABLES
137
138The journal schema contains a number of tables.
139
140=over
141
142=item ChangeSet
143
144Each changeset row has an auto-incremented ID, optional user_id and
145session_id, and a set_date which defaults to the current datetime.
146
147A ChangeSet has_many Changes.
148
41daf590 149=item ChangeLog
ec16e73a 150
151Each change/operation done in the transaction is recorded as a row in
41daf590 152the ChangeLog table. It contains an auto-incrementing ID, the
ec16e73a 153changeset_id and an order column for the ordering of each change in
154the changeset.
155
156=item AuditLog
157
158For every table in the original database that is to be audited, an
159AuditLog table is created. Each auditlog row has an id which will
160contain the primary key of the table it is associated with. (NB:
161currently only supports integer-based single column PKs). The
162create_id and delete_id fields contain the IDs of the Changes that
163created or deleted this row.
164
165=item AuditHistory
166
167For every table in the original database to be audited, an
168AuditHistory table is created. Each row has a change_id field
41daf590 169containing the ID of the ChangeLog row. The other fields correspond to
ec16e73a 170all the fields from the original table. Each time a column value in
171the original table is changed, the entire row contents before the
172change are added as a new row in this table.
173
174=back
175
176=head2 METHODS
177
178=over
179
c1c87879 180=item journal_connection \@connect_info
ec16e73a 181
ec16e73a 182Set the connection information for the database to save your audit
c1c87879 183information to.
ec16e73a 184
c1c87879 185Leaving this blank assumes you want to store the audit data into your current
186database. The storage object will be shared by the regular schema and the
187journalling schema.
ec16e73a 188
c1c87879 189=item journal_sources \@source_names
ec16e73a 190
ec16e73a 191Set a list of source names you would like to audit, if unset, all
192sources are used.
193
194NOTE: Currently only sources with a single-column PK are supported, so
195use this method if you have sources with multi-column PKs.
196
c1c87879 197=item journal_storage_type $type
ec16e73a 198
ec16e73a 199Enter the special storage type of your journal schema if needed. See
200L<DBIx::Class::Storage::DBI> for more information on storage types.
201
c1c87879 202=item journal_user \@rel
ec16e73a 203
ec16e73a 204The user_id column in the L</ChangeSet> will be linked to your user id
205with a belongs_to relation, if this is set with the appropriate
206arguments.
207
c1c87879 208=item journal_deploy_on_connect $bool
ec16e73a 209
c1c87879 210If set to a true value will cause C<journal_schema_deploy> to be called on
211C<connect>.
ec16e73a 212
c1c87879 213Not reccomended, but present for backwards compatibility.
ec16e73a 214
c1c87879 215=item changeset_user $user_id
ec16e73a 216
c1c87879 217Set the user_id for the following changeset(s). This must be an integer.
ec16e73a 218
c1c87879 219=item changeset_session $session_id
ec16e73a 220
c1c87879 221Set the session_id for the following changeset(s). This must be an integer.
ec16e73a 222
c1c87879 223=item txn_do $code_ref, @args
ec16e73a 224
225Overloaded L<DBIx::Class::Schema/txn_do>, this must be used to start a
226new changeset to cover a group of changes. Each subsequent change to
227an audited table will use the changeset_id created in the most recent
228txn_do call.
229
c1c87879 230Currently nested C<txn_do> calls cause a single ChangeSet object to be created.
231
90dae731 232=back
233
ec16e73a 234=head1 SEE ALSO
235
236L<DBIx::Class> - You'll need it to use this.
237
238=head1 NOTES
239
240Only single-column integer primary key'd tables are supported for auditing so far.
241
242Updates made via L<DBIx::Class::ResultSet/update> are not yet supported.
243
244No API for viewing or restoring changes yet.
245
246Patches for the above welcome ;)
247
248=head1 AUTHOR
249
250Jess Robinson <castaway@desert-island.me.uk>
251
252Matt S. Trout <mst@shadowcatsystems.co.uk> (ideas and prodding)
253
254=head1 LICENCE
255
256You may distribute this code under the same terms as Perl itself.
f0f14c64 257
ec16e73a 258=cut
f0f14c64 259
2601;