Commit | Line | Data |
f0f14c64 |
1 | package DBIx::Class::Journal; |
2 | |
3 | use base qw/DBIx::Class/; |
f0f14c64 |
4 | |
ec16e73a |
5 | use strict; |
6 | use warnings; |
7 | |
cd810213 |
8 | our $VERSION = '0.900001_01'; |
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 |
21 | sub _journal_schema { |
22 | my $self = shift; |
23 | $self->result_source->schema->_journal_schema; |
6bfb7a1d |
24 | } |
25 | |
26 | sub 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 |
39 | sub 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 |
53 | sub delete |
54 | { |
55 | my ($self, @rest) = @_; |
56 | $self->next::method(@rest); |
6bfb7a1d |
57 | $self->journal_log_delete(@rest); |
58 | } |
1e996809 |
59 | |
6bfb7a1d |
60 | sub 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 |
72 | sub update |
73 | { |
74 | my ($self, $upd, @rest) = @_; |
6bfb7a1d |
75 | $self->journal_log_update($upd, @rest); |
76 | $self->next::method($upd, @rest); |
77 | } |
78 | |
79 | sub 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 | |
95 | DBIx::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 | |
118 | The purpose of this L<DBIx::Class> component module is to create an |
119 | audit-trail for all changes made to the data in your database (via a |
120 | DBIx::Class schema). It creates changesets and assigns each |
121 | create/update/delete operation an id. The creation and deletion date |
122 | of each row is stored, as well as the previous contents of any row |
123 | that gets changed. |
124 | |
125 | All queries which want auditing should be called using |
126 | L<DBIx::Class::Schema/txn_do>, which is used to create changesets for |
127 | each transaction. |
128 | |
129 | To track who did which changes, the user_id (an integer) of the |
130 | current user can be set, a session_id can also be set, both are |
131 | optional. |
132 | |
133 | To access the auditing schema to look at the auditdata or revert a |
134 | change, use C<< $schema->_journal_schema >>. |
135 | |
136 | =head2 TABLES |
137 | |
138 | The journal schema contains a number of tables. |
139 | |
140 | =over |
141 | |
142 | =item ChangeSet |
143 | |
144 | Each changeset row has an auto-incremented ID, optional user_id and |
145 | session_id, and a set_date which defaults to the current datetime. |
146 | |
147 | A ChangeSet has_many Changes. |
148 | |
41daf590 |
149 | =item ChangeLog |
ec16e73a |
150 | |
151 | Each change/operation done in the transaction is recorded as a row in |
41daf590 |
152 | the ChangeLog table. It contains an auto-incrementing ID, the |
ec16e73a |
153 | changeset_id and an order column for the ordering of each change in |
154 | the changeset. |
155 | |
156 | =item AuditLog |
157 | |
158 | For every table in the original database that is to be audited, an |
159 | AuditLog table is created. Each auditlog row has an id which will |
160 | contain the primary key of the table it is associated with. (NB: |
161 | currently only supports integer-based single column PKs). The |
162 | create_id and delete_id fields contain the IDs of the Changes that |
163 | created or deleted this row. |
164 | |
165 | =item AuditHistory |
166 | |
167 | For every table in the original database to be audited, an |
168 | AuditHistory table is created. Each row has a change_id field |
41daf590 |
169 | containing the ID of the ChangeLog row. The other fields correspond to |
ec16e73a |
170 | all the fields from the original table. Each time a column value in |
171 | the original table is changed, the entire row contents before the |
172 | change 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 |
182 | Set the connection information for the database to save your audit |
c1c87879 |
183 | information to. |
ec16e73a |
184 | |
c1c87879 |
185 | Leaving this blank assumes you want to store the audit data into your current |
186 | database. The storage object will be shared by the regular schema and the |
187 | journalling schema. |
ec16e73a |
188 | |
c1c87879 |
189 | =item journal_sources \@source_names |
ec16e73a |
190 | |
ec16e73a |
191 | Set a list of source names you would like to audit, if unset, all |
192 | sources are used. |
193 | |
194 | NOTE: Currently only sources with a single-column PK are supported, so |
195 | use this method if you have sources with multi-column PKs. |
196 | |
c1c87879 |
197 | =item journal_storage_type $type |
ec16e73a |
198 | |
ec16e73a |
199 | Enter the special storage type of your journal schema if needed. See |
200 | L<DBIx::Class::Storage::DBI> for more information on storage types. |
201 | |
c1c87879 |
202 | =item journal_user \@rel |
ec16e73a |
203 | |
ec16e73a |
204 | The user_id column in the L</ChangeSet> will be linked to your user id |
205 | with a belongs_to relation, if this is set with the appropriate |
206 | arguments. |
207 | |
c1c87879 |
208 | =item journal_deploy_on_connect $bool |
ec16e73a |
209 | |
c1c87879 |
210 | If set to a true value will cause C<journal_schema_deploy> to be called on |
211 | C<connect>. |
ec16e73a |
212 | |
c1c87879 |
213 | Not reccomended, but present for backwards compatibility. |
ec16e73a |
214 | |
c1c87879 |
215 | =item changeset_user $user_id |
ec16e73a |
216 | |
c1c87879 |
217 | Set 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 |
221 | Set 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 | |
225 | Overloaded L<DBIx::Class::Schema/txn_do>, this must be used to start a |
226 | new changeset to cover a group of changes. Each subsequent change to |
227 | an audited table will use the changeset_id created in the most recent |
228 | txn_do call. |
229 | |
c1c87879 |
230 | Currently nested C<txn_do> calls cause a single ChangeSet object to be created. |
231 | |
90dae731 |
232 | =back |
233 | |
ec16e73a |
234 | =head1 SEE ALSO |
235 | |
236 | L<DBIx::Class> - You'll need it to use this. |
237 | |
238 | =head1 NOTES |
239 | |
240 | Only single-column integer primary key'd tables are supported for auditing so far. |
241 | |
242 | Updates made via L<DBIx::Class::ResultSet/update> are not yet supported. |
243 | |
244 | No API for viewing or restoring changes yet. |
245 | |
246 | Patches for the above welcome ;) |
247 | |
248 | =head1 AUTHOR |
249 | |
250 | Jess Robinson <castaway@desert-island.me.uk> |
251 | |
252 | Matt S. Trout <mst@shadowcatsystems.co.uk> (ideas and prodding) |
253 | |
254 | =head1 LICENCE |
255 | |
256 | You may distribute this code under the same terms as Perl itself. |
f0f14c64 |
257 | |
ec16e73a |
258 | =cut |
f0f14c64 |
259 | |
260 | 1; |