Initial commit, not tested, may not work ;)
[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
5__PACKAGE__->mk_classdata('journal_dsn');
6__PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
7__PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
8__PACKAGE__->mk_classdata('_journal_schema');
9
10sub load_classes
11{
12 my $self = shift;
13 $self->next::method(@_);
14
15 $self->_journal_schema((__PACKAGE__ . '::DB')->connect($self->journal_dsn || $self->storage->connect_info));
16
17 my %j_sources = @{$self->journal_sources} ? map { $_ => 1 } @{$self->journal_sources} : map { $_ => 1 } $self->sources;
18 foreach my $s_name ($self->sources)
19 {
20 next unless($j_sources{$s_name});
21 $self->create_journal_for($s_name);
22 }
23
24 ## Set up relationship between changeset->user_id and this schema's user
25 if(!@{$self->journal_user})
26 {
27 warn "No Journal User set!";
28 return;
29 }
30
31 DBIx::Class::Schema::Journal::DB::ChangeSet->belongs_to('user', @{$self->journal_user});
32}
33
34sub get_audit_log_class_name
35{
36 my ($self, $sourcename) = @_;
37
38 return __PACKAGE__ . "::DB::${sourcename}AuditLog";
39}
40
41sub get_audit_history_class_name
42{
43 my ($self, $sourcename) = @_;
44
45 return __PACKAGE__ . "::DB::${sourcename}AuditHistory";
46}
47
48sub create_journal_for
49{
50 my ($self, $s_name) = @_;
51
52 my $source = $self->source($s_name);
53 my $newclass = $self->get_audit_log_class_name($s_name);
54 DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class');
55 $newclass->load_components('Core');
56 $newclass->table(lc($s_name) . "_audit_log");
57 $newclass->add_columns(
58 ID => {
59 data_type => 'integer',
60 is_nullable => 0,
61 },
62 create_id => {
63 data_type => 'integer',
64 is_nullable => 0,
65 },
66 delete_id => {
67 data_type => 'integer',
68 is_nullable => 1,
69 });
70 $newclass->belongs_to('created', 'DBIx::Class::Schema::Journal::DB::Change', 'create_id');
71 $newclass->belongs_to('deleted', 'DBIx::Class::Schema::Journal::DB::Change', 'delete_id');
72
73
74 my $histclass = $self->get_audit_hisory_class_name($s_name);
75 DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class');
76 $histclass->load_components('Core');
77 $histclass->table(lc($s_name) . "_audit_hisory");
78 $histclass->add_columns(
79 change_id => {
80 data_type => 'integer',
81 is_nullable => 0,
82 },
83 map { $_ => $source->column_info($_) } $source->columns
84 );
85 $histclass->belongs_to('change', 'DBIx::Class::Schema::Journal::DB::Change', 'change_id');
86
87}
88
891;