1 package DBIx::Class::Journal;
3 use base qw/DBIx::Class/;
8 our $VERSION = '0.900001_04';
9 $VERSION = eval $VERSION; # no errors in dev versions
11 ## On create/insert, add new entry to AuditLog and new content to AuditHistory
15 $self->result_source->schema->_journal_schema;
19 my ($self, @args) = @_;
20 return if $self->in_storage;
22 my $res = $self->next::method(@args);
23 $self->journal_log_insert;
28 sub journal_log_insert {
31 if ( $self->in_storage ) {
32 my $j = $self->_journal_schema;
33 my $change_id = $j->journal_create_change()->id;
34 $j->journal_update_or_create_log_entry( $self, create_id => $change_id );
35 $j->journal_record_in_history( $self, audit_change_id => $change_id );
39 ## On delete, update delete_id of AuditLog
43 $self->next::method(@_);
44 $self->journal_log_delete(@_);
47 sub journal_log_delete {
50 unless ($self->in_storage) {
51 my $j = $self->_journal_schema;
52 $j->journal_update_or_create_log_entry( $self, delete_id => $j->journal_create_change->id );
56 ## On update, copy row's new contents to AuditHistory
60 $self->next::method(@_);
61 $self->journal_log_update(@_);
64 sub journal_log_update {
67 if ($self->in_storage) {
68 my $j = $self->_journal_schema;
69 my $change_id = $j->journal_create_change->id;
70 $j->journal_record_in_history( $self, audit_change_id => $change_id );
76 DBIx::Class::Journal - auditing for tables managed by DBIx::Class
81 use base 'DBIx::Class::Schema';
83 __PACKAGE__->load_components(qw/Schema::Journal/);
85 __PACKAGE__->journal_connection(['dbi:SQLite:t/var/Audit.db']);
86 __PACKAGE__->journal_user(['My::Schema::User', {'foreign.userid' => 'self.user_id'}]);
91 $schema->changeset_user($user->id);
92 my $new_artist = $schema->txn_do( sub {
93 return $schema->resultset('Artist')->create({ name => 'Fred' });
99 The purpose of this L<DBIx::Class> component module is to create an
100 audit-trail for all changes made to the data in your database (via a
101 DBIx::Class schema). It creates changesets and assigns each
102 create/update/delete operation an id. The creation and deletion date
103 of each row is stored, as well as the previous contents of any row
106 All queries which need auditing must be called using
107 L<DBIx::Class::Schema/txn_do>, which is used to create changesets for
110 To track who did which changes, the user_id (an integer) of the
111 current user can be set, a session_id can also be set, both are
114 To access the auditing schema to look at the auditdata or revert a
115 change, use C<< $schema->_journal_schema >>.
119 The journal schema contains a number of tables.
125 Each changeset row has an auto-incremented ID, optional user_id and
126 session_id, and a set_date which defaults to the current datetime.
128 A ChangeSet has_many Changes.
132 Each change/operation done in the transaction is recorded as a row in
133 the ChangeLog table. It contains an auto-incrementing ID, the
134 changeset_id and an order column for the ordering of each change in
139 For every table in the original database that is to be audited, an
140 AuditLog table is created. Each auditlog row has an id which will
141 contain the primary key of the table it is associated with. (NB:
142 currently only supports integer-based single column PKs). The
143 create_id and delete_id fields contain the IDs of the Changes that
144 created or deleted this row.
148 For every table in the original database to be audited, an
149 AuditHistory table is created. Each row has a change_id field
150 containing the ID of the ChangeLog row. The other fields correspond to
151 all the fields from the original table. Each time a column value in
152 the original table is changed, the entire row contents before the
153 change are added as a new row in this table.
161 =item journal_connection \@connect_info
163 Set the connection information for the database to save your audit
166 Leaving this blank assumes you want to store the audit data into your current
167 database. The storage object will be shared by the regular schema and the
170 =item journal_components @components
172 If you want to add components to your journal
173 (L<DBIx::Class::Schema::Versioned> for example) this would be the
176 =item journal_sources \@source_names
178 Set a list of source names you would like to audit, if unset, all
181 NOTE: Currently only sources with a single-column PK are supported, so
182 use this method if you have sources with multi-column PKs.
184 =item journal_storage_type $type
186 Enter the special storage type of your journal schema if needed. See
187 L<DBIx::Class::Storage::DBI> for more information on storage types.
189 =item journal_user \@rel
191 The user_id column in the L</ChangeSet> will be linked to your user id
192 with a belongs_to relation, if this is set with the appropriate
195 =item journal_deploy_on_connect $bool
197 If set to a true value will cause C<journal_schema_deploy> to be called on
200 Not reccomended, but present for backwards compatibility.
202 =item changeset_user $user_id
204 Set the user_id for the following changeset(s). This must be an integer.
206 =item changeset_session $session_id
208 Set the session_id for the following changeset(s). This must be an integer.
210 =item txn_do $code_ref, @args
212 Overloaded L<DBIx::Class::Schema/txn_do>, this must be used to start a
213 new changeset to cover a group of changes. Each subsequent change to
214 an audited table will use the changeset_id created in the most recent
217 Currently nested C<txn_do> calls cause a single ChangeSet object to be created.
223 L<DBIx::Class> - You'll need it to use this.
227 Only single-column integer primary key'd tables are supported for auditing so far.
229 Updates made via L<DBIx::Class::ResultSet/update> are not yet supported.
231 No API for viewing or restoring changes yet.
233 Patches for the above welcome ;)
237 Jess Robinson <castaway@desert-island.me.uk>
239 Matt S. Trout <mst@shadowcatsystems.co.uk> (ideas and prodding)
243 You may distribute this code under the same terms as Perl itself.