Commit | Line | Data |
f0f14c64 |
1 | package DBIx::Class::Schema::Journal::DB; |
2 | |
3 | use base 'DBIx::Class::Schema'; |
4 | |
aba93491 |
5 | __PACKAGE__->mk_classdata('nested_changesets'); |
6 | __PACKAGE__->mk_group_accessors( simple => 'current_user' ); |
7 | __PACKAGE__->mk_group_accessors( simple => 'current_session' ); |
8 | __PACKAGE__->mk_group_accessors( simple => '_current_changeset_container' ); |
9 | |
59c8adb5 |
10 | DBIx::Class::Schema::Journal::DB->load_classes(qw(ChangeSet ChangeLog)); |
fd45c476 |
11 | |
30a4f241 |
12 | require DBIx::Class::Schema::Journal::DB::AuditLog; |
59c8adb5 |
13 | require DBIx::Class::Schema::Journal::DB::AuditHistory; |
fd45c476 |
14 | |
15 | sub _current_changeset { |
16 | my $self = shift; |
17 | my $ref = $self->_current_changeset_container; |
18 | $ref && $ref->{changeset}; |
19 | } |
20 | |
aba93491 |
21 | # this is for localization of the current changeset |
22 | sub current_changeset { |
23 | my ( $self, @args ) = @_; |
24 | |
fd45c476 |
25 | $self->throw_exception("setting current_changeset is not supported, use txn_do to create a new changeset") if @args; |
aba93491 |
26 | |
fd45c476 |
27 | my $id = $self->_current_changeset; |
aba93491 |
28 | |
fd45c476 |
29 | $self->throw_exception("Can't call current_changeset outside of a transaction") unless $id; |
74f04ccc |
30 | |
fd45c476 |
31 | return $id; |
32 | } |
f0f14c64 |
33 | |
aba93491 |
34 | sub journal_create_changeset { |
35 | my ( $self, @args ) = @_; |
36 | |
37 | my %changesetdata = ( @args, ID => undef ); |
38 | |
39 | delete $changesetdata{parent_id} unless $self->nested_changesets; |
40 | |
41 | if( defined( my $user = $self->current_user() ) ) |
42 | { |
43 | $changesetdata{user_id} = $user; |
44 | } |
45 | if( defined( my $session = $self->current_session() ) ) |
46 | { |
47 | $changesetdata{session_id} = $session; |
48 | } |
49 | |
50 | ## Create a new changeset, then run $code as a transaction |
51 | my $cs = $self->resultset('ChangeSet'); |
52 | |
53 | $cs->create({ %changesetdata }); |
54 | } |
55 | |
6bfb7a1d |
56 | sub journal_create_change { |
57 | my $self = shift; |
58 | $self->resultset("ChangeLog")->create({ changeset_id => $self->current_changeset }); |
59 | } |
60 | |
7adb876c |
61 | sub journal_update_or_create_log_entry |
62 | { |
63 | my ($self, $row, @cols) = @_; |
64 | |
65 | my $s_name = $row->result_source->source_name; |
66 | |
67 | my %id = map { $_ => $row->get_column($_)} $row->primary_columns; |
68 | |
69 | $self->resultset("${s_name}AuditLog")->update_or_create({ @cols, %id }); |
70 | } |
71 | |
72 | sub journal_record_in_history |
73 | { |
74 | my ($self, $row, @cols) = @_; |
75 | |
76 | my $s_name = $row->result_source->source_name; |
77 | |
78 | $self->resultset("${s_name}AuditHistory")->create({ $row->get_columns, @cols }); |
79 | } |
80 | |
81 | |
f0f14c64 |
82 | 1; |