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 | |
fd45c476 |
10 | DBIx::Class::Schema::Journal::DB->load_classes(qw/ |
11 | ChangeSet |
12 | ChangeLog |
13 | AuditLog |
14 | AuditHistory |
15 | /); |
16 | |
17 | |
18 | sub _current_changeset { |
19 | my $self = shift; |
20 | my $ref = $self->_current_changeset_container; |
21 | $ref && $ref->{changeset}; |
22 | } |
23 | |
aba93491 |
24 | # this is for localization of the current changeset |
25 | sub current_changeset { |
26 | my ( $self, @args ) = @_; |
27 | |
fd45c476 |
28 | $self->throw_exception("setting current_changeset is not supported, use txn_do to create a new changeset") if @args; |
aba93491 |
29 | |
fd45c476 |
30 | my $id = $self->_current_changeset; |
aba93491 |
31 | |
fd45c476 |
32 | $self->throw_exception("Can't call current_changeset outside of a transaction") unless $id; |
74f04ccc |
33 | |
fd45c476 |
34 | return $id; |
35 | } |
f0f14c64 |
36 | |
aba93491 |
37 | sub journal_create_changeset { |
38 | my ( $self, @args ) = @_; |
39 | |
40 | my %changesetdata = ( @args, ID => undef ); |
41 | |
42 | delete $changesetdata{parent_id} unless $self->nested_changesets; |
43 | |
44 | if( defined( my $user = $self->current_user() ) ) |
45 | { |
46 | $changesetdata{user_id} = $user; |
47 | } |
48 | if( defined( my $session = $self->current_session() ) ) |
49 | { |
50 | $changesetdata{session_id} = $session; |
51 | } |
52 | |
53 | ## Create a new changeset, then run $code as a transaction |
54 | my $cs = $self->resultset('ChangeSet'); |
55 | |
56 | $cs->create({ %changesetdata }); |
57 | } |
58 | |
f0f14c64 |
59 | 1; |