nested txn_do doesn't create nested changesets
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Schema / Journal / DB.pm
1 package DBIx::Class::Schema::Journal::DB;
2
3 use base 'DBIx::Class::Schema';
4
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
10 # this is for localization of the current changeset
11 sub current_changeset {
12     my ( $self, @args ) = @_;
13
14     $self->throw_error("setting current_changeset is not supported, use txn_do to create a new changeset") if @args;
15
16     my $ref = $self->_current_changeset_container;
17
18     return $ref && $ref->{changeset};
19 }
20
21 DBIx::Class::Schema::Journal::DB->load_classes(qw/
22                                                ChangeSet
23                                                Change
24                                                AuditLog
25                                                AuditHistory
26                                                /);
27
28 sub journal_create_changeset {
29     my ( $self, @args ) = @_;
30
31     my %changesetdata = ( @args, ID => undef );
32
33     delete $changesetdata{parent_id} unless $self->nested_changesets;
34
35     if( defined( my $user = $self->current_user() ) )
36     {
37         $changesetdata{user_id} = $user;
38     }
39     if( defined( my $session = $self->current_session() ) )
40     {
41         $changesetdata{session_id} = $session;
42     }
43
44     ## Create a new changeset, then run $code as a transaction
45     my $cs = $self->resultset('ChangeSet');
46
47     $cs->create({ %changesetdata });
48 }
49
50 1;